aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2022-10-02 11:51:00 +0100
committerGitHub <noreply@github.com>2022-10-02 11:51:00 +0100
commit4c2e86b0632cf14bfb1957aef67b420a12cd797d (patch)
tree2ff0a73a192ec6baf4eff1781a23a5da8a65061f
parent775c9648f92c987308a3437d0ec4b8712307bf54 (diff)
parentd52a9b61af066deaa29398bb4195418f2817e42c (diff)
Merge pull request #2102 from yay/darwin-read-write
Darwin: allow reading/writing files larger than max(i32)
-rw-r--r--core/os/os_darwin.odin40
1 files changed, 29 insertions, 11 deletions
diff --git a/core/os/os_darwin.odin b/core/os/os_darwin.odin
index 5c2cf8ec2..ac7376752 100644
--- a/core/os/os_darwin.odin
+++ b/core/os/os_darwin.odin
@@ -369,27 +369,45 @@ close :: proc(fd: Handle) -> bool {
return _unix_close(fd) == 0
}
+@(private)
+MAX_RW :: 0x7fffffff // The limit on Darwin is max(i32), trying to read/write more than that fails.
+
write :: proc(fd: Handle, data: []u8) -> (int, Errno) {
assert(fd != -1)
- if len(data) == 0 {
- return 0, 0
- }
- bytes_written := _unix_write(fd, raw_data(data), len(data))
- if bytes_written == -1 {
- return 0, 1
+ bytes_total := len(data)
+ bytes_written_total := 0
+
+ for bytes_written_total < bytes_total {
+ bytes_to_write := min(bytes_total - bytes_written_total, MAX_RW)
+ slice := data[bytes_written_total:bytes_written_total + bytes_to_write]
+ bytes_written := _unix_write(fd, raw_data(slice), bytes_to_write)
+ if bytes_written == -1 {
+ return bytes_written_total, 1
+ }
+ bytes_written_total += bytes_written
}
- return bytes_written, 0
+
+ return bytes_written_total, 0
}
read :: proc(fd: Handle, data: []u8) -> (int, Errno) {
assert(fd != -1)
- bytes_read := _unix_read(fd, raw_data(data), len(data))
- if bytes_read == -1 {
- return 0, 1
+ bytes_total := len(data)
+ bytes_read_total := 0
+
+ for bytes_read_total < bytes_total {
+ bytes_to_read := min(bytes_total - bytes_read_total, MAX_RW)
+ slice := data[bytes_read_total:bytes_read_total + bytes_to_read]
+ bytes_read := _unix_read(fd, raw_data(slice), bytes_to_read)
+ if bytes_read == -1 {
+ return bytes_read_total, 1
+ }
+ bytes_read_total += bytes_read
}
- return bytes_read, 0
+
+ return bytes_read_total, 0
}
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {