|
@@ -9,19 +9,20 @@ import "core:sys/unix"
|
|
|
|
|
|
INVALID_HANDLE :: -1
|
|
|
|
|
|
-_O_RDONLY :: 0o0
|
|
|
-_O_WRONLY :: 0o1
|
|
|
-_O_RDWR :: 0o2
|
|
|
-_O_CREAT :: 0o100
|
|
|
-_O_EXCL :: 0o200
|
|
|
-_O_TRUNC :: 0o1000
|
|
|
-_O_APPEND :: 0o2000
|
|
|
-_O_NONBLOCK :: 0o4000
|
|
|
-_O_LARGEFILE :: 0o100000
|
|
|
-_O_DIRECTORY :: 0o200000
|
|
|
-_O_NOFOLLOW :: 0o400000
|
|
|
-_O_SYNC :: 0o4010000
|
|
|
-_O_CLOEXEC :: 0o2000000
|
|
|
+_O_RDONLY :: 0o00000000
|
|
|
+_O_WRONLY :: 0o00000001
|
|
|
+_O_RDWR :: 0o00000002
|
|
|
+_O_CREAT :: 0o00000100
|
|
|
+_O_EXCL :: 0o00000200
|
|
|
+_O_NOCTTY :: 0o00000400
|
|
|
+_O_TRUNC :: 0o00001000
|
|
|
+_O_APPEND :: 0o00002000
|
|
|
+_O_NONBLOCK :: 0o00004000
|
|
|
+_O_LARGEFILE :: 0o00100000
|
|
|
+_O_DIRECTORY :: 0o00200000
|
|
|
+_O_NOFOLLOW :: 0o00400000
|
|
|
+_O_SYNC :: 0o04010000
|
|
|
+_O_CLOEXEC :: 0o02000000
|
|
|
_O_PATH :: 0o10000000
|
|
|
|
|
|
_AT_FDCWD :: -100
|
|
@@ -40,9 +41,12 @@ _file_allocator :: proc() -> runtime.Allocator {
|
|
|
|
|
|
_open :: proc(name: string, flags: File_Flags, perm: File_Mode) -> (^File, Error) {
|
|
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
|
|
- name_cstr := _name_to_cstring(name)
|
|
|
+ name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
|
|
|
|
|
|
- flags_i: int
|
|
|
+ // Just default to using O_NOCTTY because needing to open a controlling
|
|
|
+ // terminal would be incredibly rare. This has no effect on files while
|
|
|
+ // allowing us to open serial devices.
|
|
|
+ flags_i: int = _O_NOCTTY
|
|
|
switch flags & O_RDONLY|O_WRONLY|O_RDWR {
|
|
|
case O_RDONLY: flags_i = _O_RDONLY
|
|
|
case O_WRONLY: flags_i = _O_WRONLY
|
|
@@ -56,7 +60,7 @@ _open :: proc(name: string, flags: File_Flags, perm: File_Mode) -> (^File, Error
|
|
|
flags_i |= (_O_TRUNC * int(.Trunc in flags))
|
|
|
flags_i |= (_O_CLOEXEC * int(.Close_On_Exec in flags))
|
|
|
|
|
|
- fd := unix.sys_open(name_cstr, flags_i, int(perm))
|
|
|
+ fd := unix.sys_open(name_cstr, flags_i, uint(perm))
|
|
|
if fd < 0 {
|
|
|
return nil, _get_platform_error(fd)
|
|
|
}
|
|
@@ -196,10 +200,7 @@ _truncate :: proc(f: ^File, size: i64) -> Error {
|
|
|
}
|
|
|
|
|
|
_remove :: proc(name: string) -> Error {
|
|
|
- name_cstr, allocated := _name_to_cstring(name)
|
|
|
- defer if allocated {
|
|
|
- delete(name_cstr)
|
|
|
- }
|
|
|
+ name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
|
|
|
|
|
|
fd := unix.sys_open(name_cstr, int(File_Flags.Read))
|
|
|
if fd < 0 {
|
|
@@ -214,40 +215,22 @@ _remove :: proc(name: string) -> Error {
|
|
|
}
|
|
|
|
|
|
_rename :: proc(old_name, new_name: string) -> Error {
|
|
|
- old_name_cstr, old_allocated := _name_to_cstring(old_name)
|
|
|
- new_name_cstr, new_allocated := _name_to_cstring(new_name)
|
|
|
- defer if old_allocated {
|
|
|
- delete(old_name_cstr)
|
|
|
- }
|
|
|
- defer if new_allocated {
|
|
|
- delete(new_name_cstr)
|
|
|
- }
|
|
|
+ old_name_cstr := strings.clone_to_cstring(old_name, context.temp_allocator)
|
|
|
+ new_name_cstr := strings.clone_to_cstring(new_name, context.temp_allocator)
|
|
|
|
|
|
return _ok_or_error(unix.sys_rename(old_name_cstr, new_name_cstr))
|
|
|
}
|
|
|
|
|
|
_link :: proc(old_name, new_name: string) -> Error {
|
|
|
- old_name_cstr, old_allocated := _name_to_cstring(old_name)
|
|
|
- new_name_cstr, new_allocated := _name_to_cstring(new_name)
|
|
|
- defer if old_allocated {
|
|
|
- delete(old_name_cstr)
|
|
|
- }
|
|
|
- defer if new_allocated {
|
|
|
- delete(new_name_cstr)
|
|
|
- }
|
|
|
+ old_name_cstr := strings.clone_to_cstring(old_name, context.temp_allocator)
|
|
|
+ new_name_cstr := strings.clone_to_cstring(new_name, context.temp_allocator)
|
|
|
|
|
|
return _ok_or_error(unix.sys_link(old_name_cstr, new_name_cstr))
|
|
|
}
|
|
|
|
|
|
_symlink :: proc(old_name, new_name: string) -> Error {
|
|
|
- old_name_cstr, old_allocated := _name_to_cstring(old_name)
|
|
|
- new_name_cstr, new_allocated := _name_to_cstring(new_name)
|
|
|
- defer if old_allocated {
|
|
|
- delete(old_name_cstr)
|
|
|
- }
|
|
|
- defer if new_allocated {
|
|
|
- delete(new_name_cstr)
|
|
|
- }
|
|
|
+ old_name_cstr := strings.clone_to_cstring(old_name, context.temp_allocator)
|
|
|
+ new_name_cstr := strings.clone_to_cstring(new_name, context.temp_allocator)
|
|
|
|
|
|
return _ok_or_error(unix.sys_symlink(old_name_cstr, new_name_cstr))
|
|
|
}
|
|
@@ -271,26 +254,17 @@ _read_link_cstr :: proc(name_cstr: cstring, allocator: runtime.Allocator) -> (st
|
|
|
}
|
|
|
|
|
|
_read_link :: proc(name: string, allocator: runtime.Allocator) -> (string, Error) {
|
|
|
- name_cstr, allocated := _name_to_cstring(name)
|
|
|
- defer if allocated {
|
|
|
- delete(name_cstr)
|
|
|
- }
|
|
|
+ name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
|
|
|
return _read_link_cstr(name_cstr, allocator)
|
|
|
}
|
|
|
|
|
|
_unlink :: proc(name: string) -> Error {
|
|
|
- name_cstr, allocated := _name_to_cstring(name)
|
|
|
- defer if allocated {
|
|
|
- delete(name_cstr)
|
|
|
- }
|
|
|
+ name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
|
|
|
return _ok_or_error(unix.sys_unlink(name_cstr))
|
|
|
}
|
|
|
|
|
|
_chdir :: proc(name: string) -> Error {
|
|
|
- name_cstr, allocated := _name_to_cstring(name)
|
|
|
- defer if allocated {
|
|
|
- delete(name_cstr)
|
|
|
- }
|
|
|
+ name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
|
|
|
return _ok_or_error(unix.sys_chdir(name_cstr))
|
|
|
}
|
|
|
|
|
@@ -299,32 +273,23 @@ _fchdir :: proc(f: ^File) -> Error {
|
|
|
}
|
|
|
|
|
|
_chmod :: proc(name: string, mode: File_Mode) -> Error {
|
|
|
- name_cstr, allocated := _name_to_cstring(name)
|
|
|
- defer if allocated {
|
|
|
- delete(name_cstr)
|
|
|
- }
|
|
|
- return _ok_or_error(unix.sys_chmod(name_cstr, int(mode)))
|
|
|
+ name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
|
|
|
+ return _ok_or_error(unix.sys_chmod(name_cstr, uint(mode)))
|
|
|
}
|
|
|
|
|
|
_fchmod :: proc(f: ^File, mode: File_Mode) -> Error {
|
|
|
- return _ok_or_error(unix.sys_fchmod(f.impl.fd, int(mode)))
|
|
|
+ return _ok_or_error(unix.sys_fchmod(f.impl.fd, uint(mode)))
|
|
|
}
|
|
|
|
|
|
// NOTE: will throw error without super user priviledges
|
|
|
_chown :: proc(name: string, uid, gid: int) -> Error {
|
|
|
- name_cstr, allocated := _name_to_cstring(name)
|
|
|
- defer if allocated {
|
|
|
- delete(name_cstr)
|
|
|
- }
|
|
|
+ name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
|
|
|
return _ok_or_error(unix.sys_chown(name_cstr, uid, gid))
|
|
|
}
|
|
|
|
|
|
// NOTE: will throw error without super user priviledges
|
|
|
_lchown :: proc(name: string, uid, gid: int) -> Error {
|
|
|
- name_cstr, allocated := _name_to_cstring(name)
|
|
|
- defer if allocated {
|
|
|
- delete(name_cstr)
|
|
|
- }
|
|
|
+ name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
|
|
|
return _ok_or_error(unix.sys_lchown(name_cstr, uid, gid))
|
|
|
}
|
|
|
|
|
@@ -334,10 +299,7 @@ _fchown :: proc(f: ^File, uid, gid: int) -> Error {
|
|
|
}
|
|
|
|
|
|
_chtimes :: proc(name: string, atime, mtime: time.Time) -> Error {
|
|
|
- name_cstr, allocated := _name_to_cstring(name)
|
|
|
- defer if allocated {
|
|
|
- delete(name_cstr)
|
|
|
- }
|
|
|
+ name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
|
|
|
times := [2]Unix_File_Time {
|
|
|
{ atime._nsec, 0 },
|
|
|
{ mtime._nsec, 0 },
|
|
@@ -354,18 +316,12 @@ _fchtimes :: proc(f: ^File, atime, mtime: time.Time) -> Error {
|
|
|
}
|
|
|
|
|
|
_exists :: proc(name: string) -> bool {
|
|
|
- name_cstr, allocated := _name_to_cstring(name)
|
|
|
- defer if allocated {
|
|
|
- delete(name_cstr)
|
|
|
- }
|
|
|
+ name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
|
|
|
return unix.sys_access(name_cstr, F_OK) == 0
|
|
|
}
|
|
|
|
|
|
_is_file :: proc(name: string) -> bool {
|
|
|
- name_cstr, allocated := _name_to_cstring(name)
|
|
|
- defer if allocated {
|
|
|
- delete(name_cstr)
|
|
|
- }
|
|
|
+ name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
|
|
|
s: _Stat
|
|
|
res := unix.sys_stat(name_cstr, &s)
|
|
|
if res < 0 {
|
|
@@ -384,10 +340,7 @@ _is_file_fd :: proc(fd: int) -> bool {
|
|
|
}
|
|
|
|
|
|
_is_dir :: proc(name: string) -> bool {
|
|
|
- name_cstr, allocated := _name_to_cstring(name)
|
|
|
- defer if allocated {
|
|
|
- delete(name_cstr)
|
|
|
- }
|
|
|
+ name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
|
|
|
s: _Stat
|
|
|
res := unix.sys_stat(name_cstr, &s)
|
|
|
if res < 0 {
|