Fix posix

This commit is contained in:
Lucas Perlind
2025-05-08 17:41:03 +10:00
parent 1bea59ee68
commit 5292a7f4f3
5 changed files with 35 additions and 32 deletions
+24 -23
View File
@@ -70,7 +70,7 @@ _open :: proc(name: string, flags: File_Flags, perm: int) -> (f: ^File, err: Err
if .Inheritable in flags { sys_flags -= {.CLOEXEC} } if .Inheritable in flags { sys_flags -= {.CLOEXEC} }
temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({})) temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({}))
cname := clone_to_cstring(name, temp_allocator) cname := clone_to_cstring(name, temp_allocator) or_return
fd := posix.open(cname, sys_flags, transmute(posix.mode_t)posix._mode_t(perm)) fd := posix.open(cname, sys_flags, transmute(posix.mode_t)posix._mode_t(perm))
if fd < 0 { if fd < 0 {
@@ -183,39 +183,39 @@ _truncate :: proc(f: ^File, size: i64) -> Error {
return nil return nil
} }
_remove :: proc(name: string) -> Error { _remove :: proc(name: string) -> (err: Error) {
temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({})) temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({}))
cname := clone_to_cstring(name, temp_allocator) cname := clone_to_cstring(name, temp_allocator) or_return
if posix.remove(cname) != 0 { if posix.remove(cname) != 0 {
return _get_platform_error() return _get_platform_error()
} }
return nil return nil
} }
_rename :: proc(old_path, new_path: string) -> Error { _rename :: proc(old_path, new_path: string) -> (err: Error) {
temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({})) temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({}))
cold := clone_to_cstring(old_path, temp_allocator) cold := clone_to_cstring(old_path, temp_allocator) or_return
cnew := clone_to_cstring(new_path, temp_allocator) cnew := clone_to_cstring(new_path, temp_allocator) or_return
if posix.rename(cold, cnew) != 0 { if posix.rename(cold, cnew) != 0 {
return _get_platform_error() return _get_platform_error()
} }
return nil return nil
} }
_link :: proc(old_name, new_name: string) -> Error { _link :: proc(old_name, new_name: string) -> (err: Error) {
temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({})) temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({}))
cold := clone_to_cstring(old_name, temp_allocator) cold := clone_to_cstring(old_name, temp_allocator) or_return
cnew := clone_to_cstring(new_name, temp_allocator) cnew := clone_to_cstring(new_name, temp_allocator) or_return
if posix.link(cold, cnew) != .OK { if posix.link(cold, cnew) != .OK {
return _get_platform_error() return _get_platform_error()
} }
return nil return nil
} }
_symlink :: proc(old_name, new_name: string) -> Error { _symlink :: proc(old_name, new_name: string) -> (err: Error) {
temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({})) temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({}))
cold := clone_to_cstring(old_name, temp_allocator) cold := clone_to_cstring(old_name, temp_allocator) or_return
cnew := clone_to_cstring(new_name, temp_allocator) cnew := clone_to_cstring(new_name, temp_allocator) or_return
if posix.symlink(cold, cnew) != .OK { if posix.symlink(cold, cnew) != .OK {
return _get_platform_error() return _get_platform_error()
} }
@@ -224,7 +224,7 @@ _symlink :: proc(old_name, new_name: string) -> Error {
_read_link :: proc(name: string, allocator: runtime.Allocator) -> (s: string, err: Error) { _read_link :: proc(name: string, allocator: runtime.Allocator) -> (s: string, err: Error) {
temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({ allocator })) temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({ allocator }))
cname := clone_to_cstring(name, temp_allocator) cname := clone_to_cstring(name, temp_allocator) or_return
buf: [dynamic]byte buf: [dynamic]byte
buf.allocator = allocator buf.allocator = allocator
@@ -268,9 +268,9 @@ _read_link :: proc(name: string, allocator: runtime.Allocator) -> (s: string, er
} }
} }
_chdir :: proc(name: string) -> Error { _chdir :: proc(name: string) -> (err: Error) {
temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({})) temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({}))
cname := clone_to_cstring(name, temp_allocator) cname := clone_to_cstring(name, temp_allocator) or_return
if posix.chdir(cname) != .OK { if posix.chdir(cname) != .OK {
return _get_platform_error() return _get_platform_error()
} }
@@ -291,9 +291,9 @@ _fchmod :: proc(f: ^File, mode: int) -> Error {
return nil return nil
} }
_chmod :: proc(name: string, mode: int) -> Error { _chmod :: proc(name: string, mode: int) -> (err: Error) {
temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({})) temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({}))
cname := clone_to_cstring(name, temp_allocator) cname := clone_to_cstring(name, temp_allocator) or_return
if posix.chmod(cname, transmute(posix.mode_t)posix._mode_t(mode)) != .OK { if posix.chmod(cname, transmute(posix.mode_t)posix._mode_t(mode)) != .OK {
return _get_platform_error() return _get_platform_error()
} }
@@ -307,9 +307,9 @@ _fchown :: proc(f: ^File, uid, gid: int) -> Error {
return nil return nil
} }
_chown :: proc(name: string, uid, gid: int) -> Error { _chown :: proc(name: string, uid, gid: int) -> (err: Error) {
temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({})) temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({}))
cname := clone_to_cstring(name, temp_allocator) cname := clone_to_cstring(name, temp_allocator) or_return
if posix.chown(cname, posix.uid_t(uid), posix.gid_t(gid)) != .OK { if posix.chown(cname, posix.uid_t(uid), posix.gid_t(gid)) != .OK {
return _get_platform_error() return _get_platform_error()
} }
@@ -318,14 +318,14 @@ _chown :: proc(name: string, uid, gid: int) -> Error {
_lchown :: proc(name: string, uid, gid: int) -> Error { _lchown :: proc(name: string, uid, gid: int) -> Error {
temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({})) temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({}))
cname := clone_to_cstring(name, temp_allocator) cname := clone_to_cstring(name, temp_allocator) or_return
if posix.lchown(cname, posix.uid_t(uid), posix.gid_t(gid)) != .OK { if posix.lchown(cname, posix.uid_t(uid), posix.gid_t(gid)) != .OK {
return _get_platform_error() return _get_platform_error()
} }
return nil return nil
} }
_chtimes :: proc(name: string, atime, mtime: time.Time) -> Error { _chtimes :: proc(name: string, atime, mtime: time.Time) -> (err: Error) {
times := [2]posix.timeval{ times := [2]posix.timeval{
{ {
tv_sec = posix.time_t(atime._nsec/1e9), /* seconds */ tv_sec = posix.time_t(atime._nsec/1e9), /* seconds */
@@ -338,7 +338,7 @@ _chtimes :: proc(name: string, atime, mtime: time.Time) -> Error {
} }
temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({})) temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({}))
cname := clone_to_cstring(name, temp_allocator) cname := clone_to_cstring(name, temp_allocator) or_return
if posix.utimes(cname, &times) != .OK { if posix.utimes(cname, &times) != .OK {
return _get_platform_error() return _get_platform_error()
@@ -366,7 +366,8 @@ _fchtimes :: proc(f: ^File, atime, mtime: time.Time) -> Error {
_exists :: proc(path: string) -> bool { _exists :: proc(path: string) -> bool {
temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({})) temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({}))
cpath := clone_to_cstring(path, temp_allocator) cpath, err := clone_to_cstring(path, temp_allocator)
if err != nil { return false }
return posix.access(cpath) == .OK return posix.access(cpath) == .OK
} }
+5 -5
View File
@@ -14,9 +14,9 @@ _is_path_separator :: proc(c: byte) -> bool {
return c == _Path_Separator return c == _Path_Separator
} }
_mkdir :: proc(name: string, perm: int) -> Error { _mkdir :: proc(name: string, perm: int) -> (err: Error) {
temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({})) temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({}))
cname := clone_to_cstring(name, temp_allocator) cname := clone_to_cstring(name, temp_allocator) or_return
if posix.mkdir(cname, transmute(posix.mode_t)posix._mode_t(perm)) != .OK { if posix.mkdir(cname, transmute(posix.mode_t)posix._mode_t(perm)) != .OK {
return _get_platform_error() return _get_platform_error()
} }
@@ -52,9 +52,9 @@ _mkdir_all :: proc(path: string, perm: int) -> Error {
} }
} }
_remove_all :: proc(path: string) -> Error { _remove_all :: proc(path: string) -> (err: Error) {
temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({})) temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({}))
cpath := clone_to_cstring(path, temp_allocator) cpath := clone_to_cstring(path, temp_allocator) or_return
dir := posix.opendir(cpath) dir := posix.opendir(cpath)
if dir == nil { if dir == nil {
@@ -117,7 +117,7 @@ _get_working_directory :: proc(allocator: runtime.Allocator) -> (dir: string, er
_set_working_directory :: proc(dir: string) -> (err: Error) { _set_working_directory :: proc(dir: string) -> (err: Error) {
temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({})) temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({}))
cdir := clone_to_cstring(dir, temp_allocator) cdir := clone_to_cstring(dir, temp_allocator) or_return
if posix.chdir(cdir) != .OK { if posix.chdir(cdir) != .OK {
err = _get_platform_error() err = _get_platform_error()
} }
+3 -3
View File
@@ -108,12 +108,12 @@ _process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
} }
cwd: cstring; if desc.working_dir != "" { cwd: cstring; if desc.working_dir != "" {
cwd = clone_to_cstring(desc.working_dir, temp_allocator) cwd = clone_to_cstring(desc.working_dir, temp_allocator) or_return
} }
cmd := make([]cstring, len(desc.command) + 1, temp_allocator) cmd := make([]cstring, len(desc.command) + 1, temp_allocator)
for part, i in desc.command { for part, i in desc.command {
cmd[i] = clone_to_cstring(part, temp_allocator) cmd[i] = clone_to_cstring(part, temp_allocator) or_return
} }
env: [^]cstring env: [^]cstring
@@ -123,7 +123,7 @@ _process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
} else { } else {
cenv := make([]cstring, len(desc.env) + 1, temp_allocator) cenv := make([]cstring, len(desc.env) + 1, temp_allocator)
for env, i in desc.env { for env, i in desc.env {
cenv[i] = clone_to_cstring(env, temp_allocator) cenv[i] = clone_to_cstring(env, temp_allocator) or_return
} }
env = raw_data(cenv) env = raw_data(cenv)
} }
+1
View File
@@ -50,6 +50,7 @@ _process_info_by_pid :: proc(pid: int, selection: Process_Info_Fields, allocator
} }
temp_allocator := get_temp_allocator(TEMP_ALLOCATOR_GUARD({ allocator }))
info.pid = pid info.pid = pid
// Thought on errors is: allocation failures return immediately (also why the non-allocation stuff is done first), // Thought on errors is: allocation failures return immediately (also why the non-allocation stuff is done first),
+2 -1
View File
@@ -122,7 +122,8 @@ _lstat :: proc(name: string, allocator: runtime.Allocator) -> (fi: File_Info, er
} }
stat: posix.stat_t stat: posix.stat_t
if posix.lstat(clone_to_cstring(fullpath), &stat, temp_allocator) != .OK { c_fullpath := clone_to_cstring(fullpath, temp_allocator) or_return
if posix.lstat(c_fullpath, &stat) != .OK {
err = _get_platform_error() err = _get_platform_error()
return return
} }