aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2024-05-14 18:11:50 +0100
committergingerBill <bill@gingerbill.org>2024-05-14 18:11:50 +0100
commit91b7cdaad207511260c8cead669c276b255e7c78 (patch)
tree2a417f93377bfbaa8e232312a76a3cd9071d617c
parent361be301fa1aec086de457ae01784c66304df4a9 (diff)
Mock out `temp_file.odin` stuff
-rw-r--r--core/os/os2/errors.odin3
-rw-r--r--core/os/os2/file_linux.odin1
-rw-r--r--core/os/os2/file_util.odin4
-rw-r--r--core/os/os2/file_windows.odin6
-rw-r--r--core/os/os2/path.odin5
-rw-r--r--core/os/os2/path_linux.odin5
-rw-r--r--core/os/os2/path_windows.odin5
-rw-r--r--core/os/os2/temp_file.odin181
-rw-r--r--core/os/os2/temp_file_linux.odin10
-rw-r--r--core/os/os2/temp_file_windows.odin8
10 files changed, 194 insertions, 34 deletions
diff --git a/core/os/os2/errors.odin b/core/os/os2/errors.odin
index d76b2d549..2c570170d 100644
--- a/core/os/os2/errors.odin
+++ b/core/os/os2/errors.odin
@@ -23,6 +23,8 @@ General_Error :: enum u32 {
Invalid_Dir,
Invalid_Path,
+ Pattern_Has_Separator,
+
Unsupported,
}
@@ -63,6 +65,7 @@ error_string :: proc(ferr: Error) -> string {
case .Invalid_Dir: return "invalid directory"
case .Invalid_Path: return "invalid path"
case .Unsupported: return "unsupported"
+ case .Pattern_Has_Separator: return "pattern has separator"
}
case io.Error:
switch e {
diff --git a/core/os/os2/file_linux.odin b/core/os/os2/file_linux.odin
index b1a6d84f5..3f66bbd09 100644
--- a/core/os/os2/file_linux.odin
+++ b/core/os/os2/file_linux.odin
@@ -377,7 +377,6 @@ _temp_name_to_cstring :: proc(name: string) -> (cname: cstring) {
_file_stream_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, offset: i64, whence: io.Seek_From) -> (n: i64, err: io.Error) {
f := (^File)(stream_data)
ferr: Error
- i: int
switch mode {
case .Read:
n, ferr = _read(f, p)
diff --git a/core/os/os2/file_util.odin b/core/os/os2/file_util.odin
index 0708f708e..9ec75fc91 100644
--- a/core/os/os2/file_util.odin
+++ b/core/os/os2/file_util.odin
@@ -88,11 +88,11 @@ read_entire_file_from_path :: proc(name: string, allocator: runtime.Allocator) -
read_entire_file_from_file :: proc(f: ^File, allocator: runtime.Allocator) -> (data: []byte, err: Error) {
size: int
has_size := true
- if size64, err := file_size(f); err == nil {
+ if size64, serr := file_size(f); serr == nil {
if i64(int(size64)) != size64 {
size = int(size64)
}
- } else if err == .No_Size {
+ } else if serr == .No_Size {
has_size = false
} else {
return
diff --git a/core/os/os2/file_windows.odin b/core/os/os2/file_windows.odin
index 1984c9baf..1e0899992 100644
--- a/core/os/os2/file_windows.odin
+++ b/core/os/os2/file_windows.odin
@@ -282,10 +282,10 @@ _read :: proc(f: ^File, p: []byte) -> (n: i64, err: Error) {
to_read := min(win32.DWORD(length), MAX_RW)
ok: win32.BOOL
if f.impl.kind == .Console {
- n, err := read_console(handle, p[total_read:][:to_read])
+ n, cerr := read_console(handle, p[total_read:][:to_read])
total_read += n
- if err != nil {
- return i64(total_read), err
+ if cerr != nil {
+ return i64(total_read), cerr
}
} else {
ok = win32.ReadFile(handle, &p[total_read], to_read, &single_read_length, nil)
diff --git a/core/os/os2/path.odin b/core/os/os2/path.odin
index a3e7a5a96..277da56dd 100644
--- a/core/os/os2/path.odin
+++ b/core/os/os2/path.odin
@@ -2,8 +2,9 @@ package os2
import "base:runtime"
-Path_Separator :: _Path_Separator // OS-Specific
-Path_List_Separator :: _Path_List_Separator // OS-Specific
+Path_Separator :: _Path_Separator // OS-Specific
+Path_Separator_String :: _Path_Separator_String // OS-Specific
+Path_List_Separator :: _Path_List_Separator // OS-Specific
is_path_separator :: proc(c: byte) -> bool {
return _is_path_separator(c)
diff --git a/core/os/os2/path_linux.odin b/core/os/os2/path_linux.odin
index 93de749b8..fde4ba5b1 100644
--- a/core/os/os2/path_linux.odin
+++ b/core/os/os2/path_linux.odin
@@ -6,8 +6,9 @@ import "core:strconv"
import "base:runtime"
import "core:sys/unix"
-_Path_Separator :: '/'
-_Path_List_Separator :: ':'
+_Path_Separator :: '/'
+_Path_Separator_String :: "/"
+_Path_List_Separator :: ':'
_S_IFMT :: 0o170000 // Type of file mask
_S_IFIFO :: 0o010000 // Named pipe (fifo)
diff --git a/core/os/os2/path_windows.odin b/core/os/os2/path_windows.odin
index f3a45768d..064356cf1 100644
--- a/core/os/os2/path_windows.odin
+++ b/core/os/os2/path_windows.odin
@@ -5,8 +5,9 @@ import win32 "core:sys/windows"
import "base:runtime"
import "core:strings"
-_Path_Separator :: '\\'
-_Path_List_Separator :: ';'
+_Path_Separator :: '\\'
+_Path_Separator_String :: "\\"
+_Path_List_Separator :: ';'
_is_path_separator :: proc(c: byte) -> bool {
return c == '\\' || c == '/'
diff --git a/core/os/os2/temp_file.odin b/core/os/os2/temp_file.odin
index f12c2800e..d415eb7f9 100644
--- a/core/os/os2/temp_file.odin
+++ b/core/os/os2/temp_file.odin
@@ -1,17 +1,190 @@
package os2
+import "base:intrinsics"
import "base:runtime"
-create_temp_file :: proc(dir, pattern: string) -> (^File, Error) {
- return _create_temp(dir, pattern)
+@(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 0o666 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 randm string replaces the last "*".
+// If `dir` is an empty tring, `temp_directory()` will be used.
+//
+// The caller must `close` the file once finished with.
+create_temp_file :: proc(dir, pattern: string) -> (f: ^File, err: Error) {
+ 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)
+
+ rand_buf: [32]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}, File_Mode(0o666))
+ if err == .Exist {
+ close(f)
+ attempts += 1
+ if attempts < MAX_ATTEMPTS {
+ continue
+ }
+ return nil, err
+ }
+ return f, err
+ }
}
mkdir_temp :: make_directory_temp
-make_directory_temp :: proc(dir, pattern: string, allocator: runtime.Allocator) -> (string, Error) {
- return _mkdir_temp(dir, pattern, allocator)
+// 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 randm string replaces the last "*".
+// If `dir` is an empty tring, `temp_directory()` will be used.
+make_directory_temp :: proc(dir, pattern: string, allocator: runtime.Allocator) -> (temp_path: string, err: Error) {
+ 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)
+
+ rand_buf: [32]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), nil
+ }
+ 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
temp_directory :: proc(allocator: runtime.Allocator) -> (string, Error) {
return _temp_dir(allocator)
}
+
+
+// Splits pattern by the last wildcard "*", if it exists, and returns the prefix and suffix
+// parts which are split by the last "*"
+@(private)
+_prefix_and_suffix :: proc(pattern: string) -> (prefix, suffix: string, err: Error) {
+ for i in 0..<len(pattern) {
+ if is_path_separator(pattern[i]) {
+ err = .Pattern_Has_Separator
+ return
+ }
+ }
+ prefix = pattern
+ for i := len(pattern)-1; i >= 0; i -= 1 {
+ if pattern[i] == '*' {
+ prefix, suffix = pattern[:i], pattern[i+1:]
+ break
+ }
+ }
+ return
+}
+
+@(private)
+clone_string :: proc(s: string, allocator: runtime.Allocator) -> string {
+ buf := make([]byte, len(s), allocator)
+ copy(buf, s)
+ return string(buf)
+}
+
+
+@(private)
+concatenate_strings_from_buffer :: proc(buf: []byte, strings: ..string) -> string {
+ n := 0
+ for s in strings {
+ (n < len(buf)) or_break
+ n += copy(buf[n:], s)
+ }
+ n = min(len(buf), n)
+ return string(buf[:n])
+}
+
+
+
+@(private)
+temp_join_path :: proc(dir, name: string) -> string {
+ concat :: proc(strings: ..string) -> string {
+ n := 0
+ for s in strings {
+ n += len(s)
+ }
+ buf := make([]byte, n)
+ n = 0
+ for s in strings {
+ n += copy(buf[n:], s)
+ }
+ return string(buf)
+ }
+
+ if len(dir) > 0 && is_path_separator(dir[len(dir)-1]) {
+ return concat(dir, name)
+ }
+
+ return concat(dir, Path_Separator_String, name)
+}
+
+
+@(private="file")
+random_string_seed: [2]u64
+
+@(init, private="file")
+init_random_string_seed :: proc() {
+ seed := u64(intrinsics.read_cycle_counter())
+ s := &random_string_seed
+ s[0] = 0
+ s[1] = (seed << 1) | 1
+ _ = next_random(s)
+ s[1] += seed
+ _ = next_random(s)
+}
+
+@(private="file")
+next_random :: proc(r: ^[2]u64) -> u64 {
+ old_state := r[0]
+ r[0] = old_state * 6364136223846793005 + (r[1]|1)
+ xor_shifted := (((old_state >> 59) + 5) ~ old_state) * 12605985483714917081
+ rot := (old_state >> 59)
+ return (xor_shifted >> rot) | (xor_shifted << ((-rot) & 63))
+}
+
+@(private="file")
+random_string :: proc(buf: []byte) -> string {
+ @static digits := "0123456789"
+
+ u := next_random(&random_string_seed)
+
+ b :: 10
+ i := len(buf)
+ for u >= b {
+ i -= 1
+ buf[i] = digits[u % b]
+ u /= b
+ }
+ i -= 1
+ buf[i] = digits[u % b]
+ return string(buf[i:])
+}
diff --git a/core/os/os2/temp_file_linux.odin b/core/os/os2/temp_file_linux.odin
index dd7ac5c97..92afcde47 100644
--- a/core/os/os2/temp_file_linux.odin
+++ b/core/os/os2/temp_file_linux.odin
@@ -4,16 +4,6 @@ package os2
import "base:runtime"
-_create_temp :: proc(dir, pattern: string) -> (^File, Error) {
- //TODO
- return nil, nil
-}
-
-_mkdir_temp :: proc(dir, pattern: string, allocator: runtime.Allocator) -> (string, Error) {
- //TODO
- return "", nil
-}
-
_temp_dir :: proc(allocator: runtime.Allocator) -> (string, Error) {
//TODO
return "", nil
diff --git a/core/os/os2/temp_file_windows.odin b/core/os/os2/temp_file_windows.odin
index 09b5675f2..4c8ab9fb7 100644
--- a/core/os/os2/temp_file_windows.odin
+++ b/core/os/os2/temp_file_windows.odin
@@ -4,14 +4,6 @@ package os2
import "base:runtime"
import win32 "core:sys/windows"
-_create_temp :: proc(dir, pattern: string) -> (^File, Error) {
- return nil, nil
-}
-
-_mkdir_temp :: proc(dir, pattern: string, allocator: runtime.Allocator) -> (string, Error) {
- return "", nil
-}
-
_temp_dir :: proc(allocator: runtime.Allocator) -> (string, runtime.Allocator_Error) {
n := win32.GetTempPathW(0, nil)
if n == 0 {