Skip to content

Commit

Permalink
Handle more stream errors on os Darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
Feoramund committed Aug 22, 2024
1 parent 67a7797 commit 00a3b4d
Showing 1 changed file with 52 additions and 6 deletions.
58 changes: 52 additions & 6 deletions core/os/os_darwin.odin
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,12 @@ write :: proc(fd: Handle, data: []byte) -> (int, Error) {

bytes_written := _unix_write(fd, raw_data(data), to_write)
if bytes_written < 0 {
return -1, get_last_error()
errno := get_last_error()
switch errno {
case .EBADF:
return 0, .Invalid_Stream
}
return 0, errno
}
return bytes_written, nil
}
Expand All @@ -742,7 +747,12 @@ read :: proc(fd: Handle, data: []u8) -> (int, Error) {

bytes_read := _unix_read(fd, raw_data(data), to_read)
if bytes_read < 0 {
return -1, get_last_error()
errno := get_last_error()
switch errno {
case .EBADF:
return 0, .Invalid_Stream
}
return 0, errno
}
return bytes_read, nil
}
Expand All @@ -756,7 +766,12 @@ read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Error) {

bytes_read := _unix_pread(fd, raw_data(data), to_read, offset)
if bytes_read < 0 {
return -1, get_last_error()
errno := get_last_error()
switch errno {
case .EBADF:
return 0, .Invalid_Stream
}
return 0, errno
}
return bytes_read, nil
}
Expand All @@ -770,17 +785,35 @@ write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Error) {

bytes_written := _unix_pwrite(fd, raw_data(data), to_write, offset)
if bytes_written < 0 {
return -1, get_last_error()
errno := get_last_error()
switch errno {
case .EBADF:
return 0, .Invalid_Stream
}
return 0, errno
}
return bytes_written, nil
}

seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Error) {
assert(fd != -1)
switch whence {
case SEEK_SET, SEEK_CUR, SEEK_END:
break
case:
return 0, .Invalid_Whence
}

final_offset := i64(_unix_lseek(fd, int(offset), c.int(whence)))
if final_offset == -1 {
return 0, get_last_error()
errno := get_last_error()
switch errno {
case .EINVAL:
return 0, .Invalid_Offset
case .EBADF:
return 0, .Invalid_Stream
}
return 0, errno
}
return final_offset, nil
}
Expand All @@ -789,6 +822,9 @@ seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Error) {
file_size :: proc(fd: Handle) -> (i64, Error) {
prev, _ := seek(fd, 0, SEEK_CUR)
size, err := seek(fd, 0, SEEK_END)
if err == .EBADF {
return 0, .Invalid_Stream
}
seek(fd, prev, SEEK_SET)
return i64(size), err
}
Expand Down Expand Up @@ -1042,7 +1078,17 @@ access :: proc(path: string, mask: int) -> bool {
}

flush :: proc(fd: Handle) -> Error {
return cast(Platform_Error)_unix_fsync(fd)
result := _unix_fsync(fd)
if result == -1 {
errno := get_last_error()
switch errno {
case .EBADF:
return .Invalid_Stream
case:
return errno
}
}
return nil
}

@(require_results)
Expand Down

0 comments on commit 00a3b4d

Please sign in to comment.