Clean up err != nil usage

This commit is contained in:
gingerBill
2024-08-04 11:26:35 +01:00
parent 29b6eebcd5
commit a241168142
5 changed files with 21 additions and 33 deletions
+2 -7
View File
@@ -531,14 +531,9 @@ ftruncate :: proc(fd: Handle, length: i64) -> (err: Errno) {
} }
truncate :: proc(path: string, length: i64) -> (err: Errno) { truncate :: proc(path: string, length: i64) -> (err: Errno) {
fd: Handle fd := open(path, O_WRONLY|O_CREATE, 0o666) or_return
fd, err = open(path, O_WRONLY|O_CREATE, 0o666)
if err != nil {
return
}
defer close(fd) defer close(fd)
err = ftruncate(fd, length) return ftruncate(fd, length)
return
} }
+2 -5
View File
@@ -173,11 +173,8 @@ write_entire_file :: proc(name: string, data: []byte, perm: int, truncate := tru
if truncate { if truncate {
flags |= O_TRUNC flags |= O_TRUNC
} }
f, err := open(name, flags, perm) f := open(name, flags, perm) or_return
if err != nil { _, err := write(f, data)
return err
}
_, err = write(f, data)
if cerr := close(f); cerr != nil && err == nil { if cerr := close(f); cerr != nil && err == nil {
err = cerr err = cerr
} }
+5 -8
View File
@@ -6,25 +6,22 @@ import "core:time"
import "core:strings" import "core:strings"
import win32 "core:sys/windows" import win32 "core:sys/windows"
_fstat :: proc(f: ^File, allocator: runtime.Allocator) -> (File_Info, Error) { _fstat :: proc(f: ^File, allocator: runtime.Allocator) -> (fi: File_Info, err: Error) {
if f == nil || (^File_Impl)(f.impl).fd == nil { if f == nil || (^File_Impl)(f.impl).fd == nil {
return {}, nil return
} }
path, err := _cleanpath_from_handle(f, allocator) path := _cleanpath_from_handle(f, allocator) or_return
if err != nil {
return {}, err
}
h := _handle(f) h := _handle(f)
switch win32.GetFileType(h) { switch win32.GetFileType(h) {
case win32.FILE_TYPE_PIPE, win32.FILE_TYPE_CHAR: case win32.FILE_TYPE_PIPE, win32.FILE_TYPE_CHAR:
fi := File_Info { fi = File_Info {
fullpath = path, fullpath = path,
name = basename(path), name = basename(path),
type = file_type(h), type = file_type(h),
} }
return fi, nil return
} }
return _file_info_from_get_file_information_by_handle(path, h, allocator) return _file_info_from_get_file_information_by_handle(path, h, allocator)
+8 -7
View File
@@ -644,7 +644,7 @@ get_last_error_string :: proc() -> string {
} }
open :: proc(path: string, flags: int = O_RDWR, mode: int = 0) -> (Handle, Errno) { open :: proc(path: string, flags: int = O_RDWR, mode: int = 0) -> (handle: Handle, err: Errno) {
isDir := is_dir_path(path) isDir := is_dir_path(path)
flags := flags flags := flags
if isDir { if isDir {
@@ -657,9 +657,10 @@ open :: proc(path: string, flags: int = O_RDWR, mode: int = 0) -> (Handle, Errno
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
cstr := strings.clone_to_cstring(path, context.temp_allocator) cstr := strings.clone_to_cstring(path, context.temp_allocator)
handle := _unix_open(cstr, i32(flags), u16(mode)) handle = _unix_open(cstr, i32(flags), u16(mode))
if handle == -1 { if handle == INVALID_HANDLE {
return INVALID_HANDLE, get_last_error() err = get_last_error()
return
} }
/* /*
@@ -667,14 +668,14 @@ open :: proc(path: string, flags: int = O_RDWR, mode: int = 0) -> (Handle, Errno
should not happen if the handle is a directory should not happen if the handle is a directory
*/ */
if mode != 0 && !isDir { if mode != 0 && !isDir {
err := fchmod(handle, cast(u16)mode) err = fchmod(handle, cast(u16)mode)
if err != nil { if err != nil {
_unix_close(handle) _unix_close(handle)
return INVALID_HANDLE, err handle = INVALID_HANDLE
} }
} }
return handle, nil return
} }
fchmod :: proc(fd: Handle, mode: u16) -> Errno { fchmod :: proc(fd: Handle, mode: u16) -> Errno {
+4 -6
View File
@@ -219,12 +219,10 @@ _processor_core_count :: proc() -> int {
return 1 return 1
} }
file_size :: proc(fd: Handle) -> (i64, Errno) { file_size :: proc(fd: Handle) -> (size: i64, err: Errno) {
stat, err := wasi.fd_filestat_get(wasi.fd_t(fd)) stat := wasi.fd_filestat_get(wasi.fd_t(fd)) or_return
if err != nil { size = i64(stat.size)
return 0, Platform_Error(err) return
}
return i64(stat.size), nil
} }