Skip to content

Commit

Permalink
handle linux returning EINVAL for large writes
Browse files Browse the repository at this point in the history
See #743
  • Loading branch information
andrewrk committed Feb 5, 2018
1 parent 40e4e42 commit 917e6fe
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions std/os/index.zig
Expand Up @@ -225,9 +225,15 @@ error BrokenPipe;

/// Calls POSIX write, and keeps trying if it gets interrupted.
pub fn posixWrite(fd: i32, bytes: []const u8) %void {
while (true) {
const write_ret = posix.write(fd, bytes.ptr, bytes.len);
const write_err = posix.getErrno(write_ret);
// Linux can return EINVAL when write amount is > 0x7ffff000
// See https://github.com/zig-lang/zig/pull/743#issuecomment-363165856
const max_bytes_len = 0x7ffff000;

var index: usize = 0;
while (index < bytes.len) {
const amt_to_write = math.min(bytes.len - index, usize(max_bytes_len));
const rc = posix.write(fd, &bytes[index], amt_to_write);
const write_err = posix.getErrno(rc);
if (write_err > 0) {
return switch (write_err) {
posix.EINTR => continue,
Expand All @@ -244,7 +250,7 @@ pub fn posixWrite(fd: i32, bytes: []const u8) %void {
else => unexpectedErrorPosix(write_err),
};
}
return;
index += rc;
}
}

Expand Down

0 comments on commit 917e6fe

Please sign in to comment.