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
+52 -48
View File
@@ -388,36 +388,40 @@ foreign libc {
@(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---
}
@(require_results)
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) {
@(require_results)
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) {
@(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
}
@@ -430,16 +434,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
}
@@ -447,63 +451,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
}
@@ -518,7 +522,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 {
@@ -540,7 +544,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 {
@@ -565,20 +569,20 @@ stderr: Handle = 2
last_write_time :: proc(fd: Handle) -> File_Time {}
last_write_time_by_name :: proc(name: string) -> File_Time {}
*/
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)
@@ -586,13 +590,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)
@@ -600,36 +604,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
}
@@ -640,12 +644,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
@@ -659,7 +663,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)
@@ -669,7 +673,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)
@@ -681,11 +685,11 @@ _readlink :: proc(path: string) -> (string, Errno) {
}
// XXX OpenBSD
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 = "."
@@ -696,7 +700,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)
@@ -705,12 +709,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
}
@@ -737,7 +741,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 ""
}
@@ -746,12 +750,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
}