More clean ups of ERROR_NONE and != nil usage

This commit is contained in:
gingerBill
2024-08-04 11:37:49 +01:00
parent a241168142
commit 28666414bc
15 changed files with 224 additions and 297 deletions
+29 -36
View File
@@ -401,7 +401,7 @@ fork :: proc() -> (Pid, Errno) {
if pid == -1 {
return Pid(-1), Errno(get_last_error())
}
return Pid(pid), ERROR_NONE
return Pid(pid), nil
}
open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Errno) {
@@ -411,7 +411,7 @@ open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Err
if handle == -1 {
return INVALID_HANDLE, Errno(get_last_error())
}
return handle, ERROR_NONE
return handle, nil
}
close :: proc(fd: Handle) -> Errno {
@@ -436,12 +436,12 @@ read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
if bytes_read == -1 {
return -1, Errno(get_last_error())
}
return int(bytes_read), ERROR_NONE
return int(bytes_read), nil
}
write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
if len(data) == 0 {
return 0, ERROR_NONE
return 0, nil
}
to_write := min(c.size_t(len(data)), MAX_RW)
@@ -449,7 +449,7 @@ write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
if bytes_written == -1 {
return -1, Errno(get_last_error())
}
return int(bytes_written), ERROR_NONE
return int(bytes_written), nil
}
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
@@ -457,15 +457,14 @@ seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
if res == -1 {
return -1, Errno(get_last_error())
}
return res, ERROR_NONE
return res, nil
}
file_size :: proc(fd: Handle) -> (i64, Errno) {
s, err := _fstat(fd)
if err != ERROR_NONE {
return -1, err
}
return s.size, ERROR_NONE
file_size :: proc(fd: Handle) -> (size: i64, err: Errno) {
size = -1
s := _fstat(fd) or_return
size = s.size
return
}
rename :: proc(old_path, new_path: string) -> Errno {
@@ -511,7 +510,7 @@ remove_directory :: proc(path: string) -> Errno {
is_file_handle :: proc(fd: Handle) -> bool {
s, err := _fstat(fd)
if err != ERROR_NONE {
if err != nil {
return false
}
return S_ISREG(s.mode)
@@ -525,7 +524,7 @@ is_file_path :: proc(path: string, follow_links: bool = true) -> bool {
} else {
s, err = _lstat(path)
}
if err != ERROR_NONE {
if err != nil {
return false
}
return S_ISREG(s.mode)
@@ -533,7 +532,7 @@ is_file_path :: proc(path: string, follow_links: bool = true) -> bool {
is_dir_handle :: proc(fd: Handle) -> bool {
s, err := _fstat(fd)
if err != ERROR_NONE {
if err != nil {
return false
}
return S_ISDIR(s.mode)
@@ -547,7 +546,7 @@ is_dir_path :: proc(path: string, follow_links: bool = true) -> bool {
} else {
s, err = _lstat(path)
}
if err != ERROR_NONE {
if err != nil {
return false
}
return S_ISDIR(s.mode)
@@ -566,22 +565,16 @@ 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) -> (File_Time, Errno) {
s, err := _fstat(fd)
if err != ERROR_NONE {
return 0, err
}
last_write_time :: proc(fd: Handle) -> (time: File_Time, err: Errno) {
s := _fstat(fd) or_return
modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
return File_Time(modified), ERROR_NONE
return File_Time(modified), nil
}
last_write_time_by_name :: proc(name: string) -> (File_Time, Errno) {
s, err := _stat(name)
if err != ERROR_NONE {
return 0, err
}
last_write_time_by_name :: proc(name: string) -> (time: File_Time, err: Errno) {
s := _stat(name) or_return
modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
return File_Time(modified), ERROR_NONE
return File_Time(modified), nil
}
@private
@@ -595,7 +588,7 @@ _stat :: proc(path: string) -> (OS_Stat, Errno) {
if res == -1 {
return s, Errno(get_last_error())
}
return s, ERROR_NONE
return s, nil
}
@private
@@ -609,7 +602,7 @@ _lstat :: proc(path: string) -> (OS_Stat, Errno) {
if res == -1 {
return s, Errno(get_last_error())
}
return s, ERROR_NONE
return s, nil
}
@private
@@ -620,7 +613,7 @@ _fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
if res == -1 {
return s, Errno(get_last_error())
}
return s, ERROR_NONE
return s, nil
}
@private
@@ -629,7 +622,7 @@ _fdopendir :: proc(fd: Handle) -> (Dir, Errno) {
if dirp == cast(Dir)nil {
return nil, Errno(get_last_error())
}
return dirp, ERROR_NONE
return dirp, nil
}
@private
@@ -655,7 +648,7 @@ _readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool)
err = Errno(get_last_error())
return
}
err = ERROR_NONE
err = nil
if result == nil {
end_of_stream = true
@@ -682,7 +675,7 @@ _readlink :: proc(path: string) -> (string, Errno) {
delete(buf)
buf = make([]byte, bufsz)
} else {
return strings.string_from_ptr(&buf[0], rc), ERROR_NONE
return strings.string_from_ptr(&buf[0], rc), nil
}
}
}
@@ -709,7 +702,7 @@ absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
path = strings.clone(string(cstring(path_ptr)))
return path, ERROR_NONE
return path, nil
}
access :: proc(path: string, mask: int) -> (bool, Errno) {
@@ -719,7 +712,7 @@ access :: proc(path: string, mask: int) -> (bool, Errno) {
if res == -1 {
return false, Errno(get_last_error())
}
return true, ERROR_NONE
return true, nil
}
lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) {