Skip to content

Commit

Permalink
Add read_at/write_at to missing platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerBill committed Aug 4, 2024
1 parent c32f345 commit 7663a20
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 19 deletions.
10 changes: 9 additions & 1 deletion core/os/errors.odin
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,12 @@ _error_string :: proc "contextless" (e: Platform_Error) -> string where intrinsi
return ti.names[idx]
}
return "<unknown platform error>"
}
}

@(private, require_results)
error_to_io_error :: proc(ferr: Error) -> io.Error {
if ferr == nil {
return .None
}
return ferr.(io.Error) or_else .Unknown
}
20 changes: 20 additions & 0 deletions core/os/os_freebsd.odin
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,26 @@ write :: proc(fd: Handle, data: []byte) -> (int, Error) {
return int(bytes_written), nil
}

read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) {
curr := seek(fd, offset, SEEK_CUR) or_return
n, err = read(fd, data)
_, err1 := seek(fd, curr, SEEK_SET)
if err1 != nil && err == nil {
err = err1
}
return
}

write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) {
curr := seek(fd, offset, SEEK_CUR) or_return
n, err = write(fd, data)
_, err1 := seek(fd, curr, SEEK_SET)
if err1 != nil && err == nil {
err = err1
}
return
}

seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Error) {
res := _unix_seek(fd, offset, c.int(whence))
if res == -1 {
Expand Down
20 changes: 20 additions & 0 deletions core/os/os_haiku.odin
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,26 @@ write :: proc(fd: Handle, data: []byte) -> (int, Error) {
return int(bytes_written), nil
}

read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) {
curr := seek(fd, offset, SEEK_CUR) or_return
n, err = read(fd, data)
_, err1 := seek(fd, curr, SEEK_SET)
if err1 != nil && err == nil {
err = err1
}
return
}

write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) {
curr := seek(fd, offset, SEEK_CUR) or_return
n, err = write(fd, data)
_, err1 := seek(fd, curr, SEEK_SET)
if err1 != nil && err == nil {
err = err1
}
return
}

seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Error) {
res := _unix_seek(fd, offset, c.int(whence))
if res == -1 {
Expand Down
20 changes: 20 additions & 0 deletions core/os/os_netbsd.odin
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,26 @@ write :: proc(fd: Handle, data: []byte) -> (int, Error) {
return int(bytes_written), nil
}

read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) {
curr := seek(fd, offset, SEEK_CUR) or_return
n, err = read(fd, data)
_, err1 := seek(fd, curr, SEEK_SET)
if err1 != nil && err == nil {
err = err1
}
return
}

write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) {
curr := seek(fd, offset, SEEK_CUR) or_return
n, err = write(fd, data)
_, err1 := seek(fd, curr, SEEK_SET)
if err1 != nil && err == nil {
err = err1
}
return
}

seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Error) {
res := _unix_seek(fd, offset, c.int(whence))
if res == -1 {
Expand Down
20 changes: 20 additions & 0 deletions core/os/os_openbsd.odin
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,26 @@ write :: proc(fd: Handle, data: []byte) -> (int, Error) {
return int(bytes_written), nil
}

read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) {
curr := seek(fd, offset, SEEK_CUR) or_return
n, err = read(fd, data)
_, err1 := seek(fd, curr, SEEK_SET)
if err1 != nil && err == nil {
err = err1
}
return
}

write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) {
curr := seek(fd, offset, SEEK_CUR) or_return
n, err = write(fd, data)
_, err1 := seek(fd, curr, SEEK_SET)
if err1 != nil && err == nil {
err = err1
}
return
}

seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Error) {
res := _unix_seek(fd, offset, c.int(whence))
if res == -1 {
Expand Down
28 changes: 10 additions & 18 deletions core/os/stream.odin
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ _file_stream_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte,
}

case .Read_At:
when !(ODIN_OS == .FreeBSD || ODIN_OS == .OpenBSD || ODIN_OS == .NetBSD || ODIN_OS == .Haiku) {
n_int, os_err = read_at(fd, p, offset)
n = i64(n_int)
if n == 0 && os_err == nil {
err = .EOF
}
n_int, os_err = read_at(fd, p, offset)
n = i64(n_int)
if n == 0 && os_err == nil {
err = .EOF
}
case .Write:
n_int, os_err = write(fd, p)
Expand All @@ -46,12 +44,10 @@ _file_stream_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte,
err = .EOF
}
case .Write_At:
when !(ODIN_OS == .FreeBSD || ODIN_OS == .OpenBSD || ODIN_OS == .NetBSD || ODIN_OS == .Haiku) {
n_int, os_err = write_at(fd, p, offset)
n = i64(n_int)
if n == 0 && os_err == nil {
err = .EOF
}
n_int, os_err = write_at(fd, p, offset)
n = i64(n_int)
if n == 0 && os_err == nil {
err = .EOF
}
case .Seek:
n, os_err = seek(fd, offset, int(whence))
Expand All @@ -60,11 +56,7 @@ _file_stream_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte,
case .Destroy:
err = .Empty
case .Query:
when ODIN_OS == .FreeBSD || ODIN_OS == .OpenBSD || ODIN_OS == .NetBSD || ODIN_OS == .Haiku {
return io.query_utility({.Close, .Flush, .Read, .Write, .Seek, .Size, .Query})
} else {
return io.query_utility({.Close, .Flush, .Read, .Read_At, .Write, .Write_At, .Seek, .Size, .Query})
}
return io.query_utility({.Close, .Flush, .Read, .Read_At, .Write, .Write_At, .Seek, .Size, .Query})
}

if err == nil && os_err != nil {
Expand All @@ -73,7 +65,7 @@ _file_stream_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte,
return n, .EOF
}
}
err = .Unknown
err = error_to_io_error(os_err)
}
return
}

0 comments on commit 7663a20

Please sign in to comment.