mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 07:38:48 +00:00
+21
-5
@@ -568,15 +568,24 @@ close :: proc(fd: Handle) -> bool {
|
||||
return _unix_close(fd) == 0
|
||||
}
|
||||
|
||||
// If you read or write more than `SSIZE_MAX` bytes, most darwin implementations will return `EINVAL`
|
||||
// but it is really implementation defined. `SSIZE_MAX` is also implementation defined but usually
|
||||
// the max of an i32 on Darwin.
|
||||
// In practice a read/write call would probably never read/write these big buffers all at once,
|
||||
// which is why the number of bytes is returned and why there are procs that will call this in a
|
||||
// loop for you.
|
||||
// We set a max of 1GB to keep alignment and to be safe.
|
||||
@(private)
|
||||
MAX_RW :: 0x7fffffff // The limit on Darwin is max(i32), trying to read/write more than that fails.
|
||||
MAX_RW :: 1 << 30
|
||||
|
||||
write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
||||
if len(data) == 0 {
|
||||
return 0, ERROR_NONE
|
||||
}
|
||||
|
||||
bytes_written := _unix_write(fd, raw_data(data), c.size_t(len(data)))
|
||||
to_write := min(c.size_t(len(data)), MAX_RW)
|
||||
|
||||
bytes_written := _unix_write(fd, raw_data(data), to_write)
|
||||
if bytes_written < 0 {
|
||||
return -1, Errno(get_last_error())
|
||||
}
|
||||
@@ -588,18 +597,23 @@ read :: proc(fd: Handle, data: []u8) -> (int, Errno) {
|
||||
return 0, ERROR_NONE
|
||||
}
|
||||
|
||||
bytes_read := _unix_read(fd, raw_data(data), c.size_t(len(data)))
|
||||
to_read := min(c.size_t(len(data)), MAX_RW)
|
||||
|
||||
bytes_read := _unix_read(fd, raw_data(data), to_read)
|
||||
if bytes_read < 0 {
|
||||
return -1, Errno(get_last_error())
|
||||
}
|
||||
return bytes_read, ERROR_NONE
|
||||
}
|
||||
|
||||
read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Errno) {
|
||||
if len(data) == 0 {
|
||||
return 0, ERROR_NONE
|
||||
}
|
||||
|
||||
bytes_read := _unix_pread(fd, raw_data(data), c.size_t(len(data)), offset)
|
||||
to_read := min(c.size_t(len(data)), MAX_RW)
|
||||
|
||||
bytes_read := _unix_pread(fd, raw_data(data), to_read, offset)
|
||||
if bytes_read < 0 {
|
||||
return -1, Errno(get_last_error())
|
||||
}
|
||||
@@ -611,7 +625,9 @@ write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Errno) {
|
||||
return 0, ERROR_NONE
|
||||
}
|
||||
|
||||
bytes_written := _unix_pwrite(fd, raw_data(data), c.size_t(len(data)), offset)
|
||||
to_write := min(c.size_t(len(data)), MAX_RW)
|
||||
|
||||
bytes_written := _unix_pwrite(fd, raw_data(data), to_write, offset)
|
||||
if bytes_written < 0 {
|
||||
return -1, Errno(get_last_error())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user