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
+35 -34
View File
@@ -119,7 +119,7 @@ S_ISSOCK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFSOCK
foreign libc {
@(link_name="_errnop") __error :: proc() -> ^c.int ---
@(link_name="_Errorp") __error :: proc() -> ^c.int ---
@(link_name="fork") _unix_fork :: proc() -> pid_t ---
@(link_name="getthrid") _unix_getthrid :: proc() -> int ---
@@ -181,32 +181,33 @@ is_path_separator :: proc(r: rune) -> bool {
return r == '/'
}
@(require_results, no_instrumentation)
get_last_error :: proc "contextless" () -> Error {
return Platform_Error(__error()^)
}
fork :: proc() -> (Pid, Errno) {
fork :: proc() -> (Pid, Error) {
pid := _unix_fork()
if pid == -1 {
return Pid(-1), Errno(get_last_error())
return Pid(-1), Error(get_last_error())
}
return Pid(pid), nil
}
open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Errno) {
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
}
@@ -218,16 +219,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
}
@@ -235,20 +236,20 @@ 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) -> (i64, Errno) {
file_size :: proc(fd: Handle) -> (i64, Error) {
s, err := _fstat(fd)
if err != nil {
return -1, err
@@ -268,7 +269,7 @@ _alloc_command_line_arguments :: proc() -> []string {
}
@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)
@@ -276,13 +277,13 @@ _stat :: proc(path: string) -> (OS_Stat, Errno) {
s: OS_Stat = ---
res := _unix_stat(cstr, &s)
if res == -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)
@@ -290,36 +291,36 @@ _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) {
// deliberately uninitialized
s: OS_Stat = ---
res := _unix_fstat(fd, &s)
if res == -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
}
@@ -330,12 +331,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
}
@@ -348,7 +349,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)
@@ -358,7 +359,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)
@@ -369,11 +370,11 @@ _readlink :: proc(path: string) -> (string, Errno) {
}
}
absolute_path_from_handle :: proc(fd: Handle) -> (string, Errno) {
return "", Errno(ENOSYS)
absolute_path_from_handle :: proc(fd: Handle) -> (string, Error) {
return "", Error(ENOSYS)
}
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 = "."
@@ -384,7 +385,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)
@@ -394,12 +395,12 @@ 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)
res := _unix_access(cstr, c.int(mask))
if res == -1 {
return false, Errno(get_last_error())
return false, Error(get_last_error())
}
return true, nil
}