aboutsummaryrefslogtreecommitdiff
path: root/core/os/temp_file.odin
blob: be10eb22ee2fc5a0c59b4b5edf5d4a41a45bfe2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package os

import "base:runtime"

@(private="file")
MAX_ATTEMPTS :: 1<<13 // Should be enough for everyone, right?

// Creates a new temperatory file in the directory `dir`.
//
// Opens the file for reading and writing, with `Permissions_Read_Write_All` permissions, and returns the new `^File`.
// The filename is generated by taking a pattern, and adding a randomized string to the end.
// If the pattern includes an "*", the random string replaces the last "*".
// If `dir` is an empty string, `temp_directory()` will be used.
//
// The caller must `close` the file once finished with.
@(require_results)
create_temp_file :: proc(dir, pattern: string, additional_flags: File_Flags = {}) -> (f: ^File, err: Error) {
	temp_allocator := TEMP_ALLOCATOR_GUARD({})
	dir := dir if dir != "" else temp_directory(temp_allocator) or_return
	prefix, suffix := _prefix_and_suffix(pattern) or_return
	prefix = temp_join_path(dir, prefix, temp_allocator) or_return

	rand_buf: [10]byte
	name_buf := make([]byte, len(prefix)+len(rand_buf)+len(suffix), temp_allocator)

	attempts := 0
	for {
		name := concatenate_strings_from_buffer(name_buf[:], prefix, random_string(rand_buf[:]), suffix)
		f, err = open(name, {.Read, .Write, .Create, .Excl} + additional_flags, Permissions_Read_Write_All)
		if err == .Exist {
			close(f)
			attempts += 1
			if attempts < MAX_ATTEMPTS {
				continue
			}
			return nil, err
		}
		return f, err
	}
}

mkdir_temp :: make_directory_temp
// Creates a new temporary directory in the directory `dir`, and returns the path of the new directory.
//
// The directory name is generated by taking a pattern, and adding a randomized string to the end.
// If the pattern includes an "*", the random string replaces the last "*".
// If `dir` is an empty tring, `temp_directory()` will be used.
@(require_results)
make_directory_temp :: proc(dir, pattern: string, allocator: runtime.Allocator) -> (temp_path: string, err: Error) {
	temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
	dir := dir if dir != "" else temp_directory(temp_allocator) or_return
	prefix, suffix := _prefix_and_suffix(pattern) or_return
	prefix = temp_join_path(dir, prefix, temp_allocator) or_return

	rand_buf: [10]byte
	name_buf := make([]byte, len(prefix)+len(rand_buf)+len(suffix), temp_allocator)

	attempts := 0
	for {
		name := concatenate_strings_from_buffer(name_buf[:], prefix, random_string(rand_buf[:]), suffix)
		err = make_directory(name, 0o700)
		if err == nil {
			return clone_string(name, allocator)
		}
		if err == .Exist {
			attempts += 1
			if attempts < MAX_ATTEMPTS {
				continue
			}
			return "", err
		}
		if err == .Not_Exist {
			if _, serr := stat(dir, temp_allocator); serr == .Not_Exist {
				return "", serr
			}
		}
		return "", err
	}

}

temp_dir :: temp_directory

/*
	Returns the default directory to use for temporary files.

	On Unix systems, it typically returns $TMPDIR if non-empty, otherwlse `/tmp`.
	On Windows, it uses `GetTempPathW`, returning the first non-empty value from one of the following:
	    * `%TMP%`
	    * `%TEMP%`
	    * `%USERPROFILE %`
	    * or the Windows directory
	  See https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppathw for more information.
	On wasi, it returns `/tmp`.
*/
@(require_results)
temp_directory :: proc(allocator: runtime.Allocator) -> (string, Error) {
	return _temp_dir(allocator)
}



@(private="file")
temp_join_path :: proc(dir, name: string, allocator: runtime.Allocator) -> (string, runtime.Allocator_Error) {
	if len(dir) > 0 && is_path_separator(dir[len(dir)-1]) {
		return concatenate({dir, name}, allocator)
	}

	return concatenate({dir, Path_Separator_String, name}, allocator)
}