Begin mapping os.Error in the rest of the codebase

This commit is contained in:
gingerBill
2024-08-04 11:58:04 +01:00
parent 1d75a612d5
commit 97c499dbb4
18 changed files with 231 additions and 222 deletions
+56 -53
View File
@@ -150,7 +150,7 @@ _Platform_Error :: enum i32 {
EOWNERDEAD = 97, /* Previous owner died */
ENOTRECOVERABLE = 98, /* State not recoverable */
ELAST = 98, /* Must equal largest errno */
ELAST = 98, /* Must equal largest Error */
}
EPERM :: Platform_Error.EPERM /* Operation not permitted */
@@ -289,9 +289,9 @@ EPROTO :: Platform_Error.EPROTO /* Protocol error */
EOWNERDEAD :: Platform_Error.EOWNERDEAD /* Previous owner died */
ENOTRECOVERABLE :: Platform_Error.ENOTRECOVERABLE /* State not recoverable */
ELAST :: Platform_Error.ELAST /* Must equal largest errno */
ELAST :: Platform_Error.ELAST /* Must equal largest Error */
/* end of errno */
/* end of Error */
O_RDONLY :: 0x000000000
O_WRONLY :: 0x000000001
@@ -421,7 +421,7 @@ W_OK :: 2 // Test for write permission
R_OK :: 4 // Test for read permission
foreign libc {
@(link_name="__errno") __errno_location :: proc() -> ^c.int ---
@(link_name="__Error") __Error_location :: proc() -> ^c.int ---
@(link_name="open") _unix_open :: proc(path: cstring, flags: c.int, mode: c.int) -> Handle ---
@(link_name="close") _unix_close :: proc(fd: Handle) -> c.int ---
@@ -473,28 +473,31 @@ foreign libc {
// NOTE(phix): Perhaps share the following functions with FreeBSD if they turn out to be the same in the end.
@(require_results)
is_path_separator :: proc(r: rune) -> bool {
return r == '/'
}
@(require_results, no_instrumentation)
get_last_error :: proc "contextless" () -> Error {
return Platform_Error(__errno_location()^)
return Platform_Error(__Error_location()^)
}
open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Errno) {
@(require_results)
open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Error) {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
cstr := strings.clone_to_cstring(path, context.temp_allocator)
handle := _unix_open(cstr, c.int(flags), c.int(mode))
if handle == -1 {
return INVALID_HANDLE, Errno(get_last_error())
return INVALID_HANDLE, Error(get_last_error())
}
return handle, nil
}
close :: proc(fd: Handle) -> Errno {
close :: proc(fd: Handle) -> Error {
result := _unix_close(fd)
if result == -1 {
return Errno(get_last_error())
return Error(get_last_error())
}
return nil
}
@@ -503,16 +506,16 @@ close :: proc(fd: Handle) -> Errno {
@(private)
MAX_RW :: 1 << 30
read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
read :: proc(fd: Handle, data: []byte) -> (int, Error) {
to_read := min(c.size_t(len(data)), MAX_RW)
bytes_read := _unix_read(fd, &data[0], to_read)
if bytes_read == -1 {
return -1, Errno(get_last_error())
return -1, Error(get_last_error())
}
return int(bytes_read), nil
}
write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
write :: proc(fd: Handle, data: []byte) -> (int, Error) {
if len(data) == 0 {
return 0, nil
}
@@ -520,63 +523,63 @@ write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
to_write := min(c.size_t(len(data)), MAX_RW)
bytes_written := _unix_write(fd, &data[0], to_write)
if bytes_written == -1 {
return -1, Errno(get_last_error())
return -1, Error(get_last_error())
}
return int(bytes_written), nil
}
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Error) {
res := _unix_seek(fd, offset, c.int(whence))
if res == -1 {
return -1, Errno(get_last_error())
return -1, Error(get_last_error())
}
return res, nil
}
file_size :: proc(fd: Handle) -> (size: i64, err: Errno) {
file_size :: proc(fd: Handle) -> (size: i64, err: Error) {
size = -1
s := _fstat(fd) or_return
size = s.size
return
}
rename :: proc(old_path, new_path: string) -> Errno {
rename :: proc(old_path, new_path: string) -> Error {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
old_path_cstr := strings.clone_to_cstring(old_path, context.temp_allocator)
new_path_cstr := strings.clone_to_cstring(new_path, context.temp_allocator)
res := _unix_rename(old_path_cstr, new_path_cstr)
if res == -1 {
return Errno(get_last_error())
return Error(get_last_error())
}
return nil
}
remove :: proc(path: string) -> Errno {
remove :: proc(path: string) -> Error {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
res := _unix_unlink(path_cstr)
if res == -1 {
return Errno(get_last_error())
return Error(get_last_error())
}
return nil
}
make_directory :: proc(path: string, mode: mode_t = 0o775) -> Errno {
make_directory :: proc(path: string, mode: mode_t = 0o775) -> Error {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
res := _unix_mkdir(path_cstr, mode)
if res == -1 {
return Errno(get_last_error())
return Error(get_last_error())
}
return nil
}
remove_directory :: proc(path: string) -> Errno {
remove_directory :: proc(path: string) -> Error {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
res := _unix_rmdir(path_cstr)
if res == -1 {
return Errno(get_last_error())
return Error(get_last_error())
}
return nil
}
@@ -591,7 +594,7 @@ is_file_handle :: proc(fd: Handle) -> bool {
is_file_path :: proc(path: string, follow_links: bool = true) -> bool {
s: OS_Stat
err: Errno
err: Error
if follow_links {
s, err = _stat(path)
} else {
@@ -613,7 +616,7 @@ is_dir_handle :: proc(fd: Handle) -> bool {
is_dir_path :: proc(path: string, follow_links: bool = true) -> bool {
s: OS_Stat
err: Errno
err: Error
if follow_links {
s, err = _stat(path)
} else {
@@ -635,10 +638,10 @@ exists :: proc(path: string) -> bool {
return res == 0
}
fcntl :: proc(fd: int, cmd: int, arg: int) -> (int, Errno) {
fcntl :: proc(fd: int, cmd: int, arg: int) -> (int, Error) {
result := _unix_fcntl(Handle(fd), c.int(cmd), uintptr(arg))
if result < 0 {
return 0, Errno(get_last_error())
return 0, Error(get_last_error())
}
return int(result), nil
}
@@ -649,32 +652,32 @@ stdin: Handle = 0
stdout: Handle = 1
stderr: Handle = 2
last_write_time :: proc(fd: Handle) -> (time: File_Time, err: Errno) {
last_write_time :: proc(fd: Handle) -> (time: File_Time, err: Error) {
s := _fstat(fd) or_return
modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
return File_Time(modified), nil
}
last_write_time_by_name :: proc(name: string) -> (time: File_Time, err: Errno) {
last_write_time_by_name :: proc(name: string) -> (time: File_Time, err: Error) {
s := _stat(name) or_return
modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
return File_Time(modified), nil
}
@private
_stat :: proc(path: string) -> (OS_Stat, Errno) {
_stat :: proc(path: string) -> (OS_Stat, Error) {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
cstr := strings.clone_to_cstring(path, context.temp_allocator)
s: OS_Stat = ---
result := _unix_lstat(cstr, &s)
if result == -1 {
return s, Errno(get_last_error())
return s, Error(get_last_error())
}
return s, nil
}
@private
_lstat :: proc(path: string) -> (OS_Stat, Errno) {
_lstat :: proc(path: string) -> (OS_Stat, Error) {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
cstr := strings.clone_to_cstring(path, context.temp_allocator)
@@ -682,35 +685,35 @@ _lstat :: proc(path: string) -> (OS_Stat, Errno) {
s: OS_Stat = ---
res := _unix_lstat(cstr, &s)
if res == -1 {
return s, Errno(get_last_error())
return s, Error(get_last_error())
}
return s, nil
}
@private
_fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
_fstat :: proc(fd: Handle) -> (OS_Stat, Error) {
s: OS_Stat = ---
result := _unix_fstat(fd, &s)
if result == -1 {
return s, Errno(get_last_error())
return s, Error(get_last_error())
}
return s, nil
}
@private
_fdopendir :: proc(fd: Handle) -> (Dir, Errno) {
_fdopendir :: proc(fd: Handle) -> (Dir, Error) {
dirp := _unix_fdopendir(fd)
if dirp == cast(Dir)nil {
return nil, Errno(get_last_error())
return nil, Error(get_last_error())
}
return dirp, nil
}
@private
_closedir :: proc(dirp: Dir) -> Errno {
_closedir :: proc(dirp: Dir) -> Error {
rc := _unix_closedir(dirp)
if rc != 0 {
return Errno(get_last_error())
return Error(get_last_error())
}
return nil
}
@@ -721,12 +724,12 @@ _rewinddir :: proc(dirp: Dir) {
}
@private
_readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool) {
_readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Error, end_of_stream: bool) {
result: ^Dirent
rc := _unix_readdir_r(dirp, &entry, &result)
if rc != 0 {
err = Errno(get_last_error())
err = Error(get_last_error())
return
}
err = nil
@@ -740,7 +743,7 @@ _readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool)
}
@private
_readlink :: proc(path: string) -> (string, Errno) {
_readlink :: proc(path: string) -> (string, Error) {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == context.allocator)
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
@@ -751,7 +754,7 @@ _readlink :: proc(path: string) -> (string, Errno) {
rc := _unix_readlink(path_cstr, &(buf[0]), bufsz)
if rc == -1 {
delete(buf)
return "", Errno(get_last_error())
return "", Error(get_last_error())
} else if rc == int(bufsz) {
bufsz += MAX_PATH
delete(buf)
@@ -761,16 +764,16 @@ _readlink :: proc(path: string) -> (string, Errno) {
}
}
return "", Errno{}
return "", Error{}
}
absolute_path_from_handle :: proc(fd: Handle) -> (path: string, err: Errno) {
absolute_path_from_handle :: proc(fd: Handle) -> (path: string, err: Error) {
buf: [MAX_PATH]byte
_ = fcntl(int(fd), F_GETPATH, int(uintptr(&buf[0]))) or_return
return strings.clone_from_cstring(cstring(&buf[0]))
}
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Error) {
rel := rel
if rel == "" {
rel = "."
@@ -781,7 +784,7 @@ absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
path_ptr := _unix_realpath(rel_cstr, nil)
if path_ptr == nil {
return "", Errno(get_last_error())
return "", Error(get_last_error())
}
defer _unix_free(path_ptr)
@@ -790,13 +793,13 @@ absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
return path, nil
}
access :: proc(path: string, mask: int) -> (bool, Errno) {
access :: proc(path: string, mask: int) -> (bool, Error) {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
cstr := strings.clone_to_cstring(path, context.temp_allocator)
result := _unix_access(cstr, c.int(mask))
if result == -1 {
return false, Errno(get_last_error())
return false, Error(get_last_error())
}
return true, nil
}
@@ -828,7 +831,7 @@ get_current_directory :: proc() -> string {
if cwd != nil {
return string(cwd)
}
if Errno(get_last_error()) != ERANGE {
if Error(get_last_error()) != ERANGE {
delete(buf)
return ""
}
@@ -837,12 +840,12 @@ get_current_directory :: proc() -> string {
unreachable()
}
set_current_directory :: proc(path: string) -> (err: Errno) {
set_current_directory :: proc(path: string) -> (err: Error) {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
cstr := strings.clone_to_cstring(path, context.temp_allocator)
res := _unix_chdir(cstr)
if res == -1 {
return Errno(get_last_error())
return Error(get_last_error())
}
return nil
}