diff --git a/lib/std/c/darwin.zig b/lib/std/c/darwin.zig index 881e52c72..2134e0e7a 100644 --- a/lib/std/c/darwin.zig +++ b/lib/std/c/darwin.zig @@ -63,8 +63,8 @@ pub const sf_hdtr = extern struct { }; pub extern "c" fn sendfile( - out_fd: fd_t, in_fd: fd_t, + out_fd: fd_t, offset: off_t, len: *off_t, sf_hdtr: ?*sf_hdtr, diff --git a/lib/std/c/freebsd.zig b/lib/std/c/freebsd.zig index 2c4820bbe..3a0634dbb 100644 --- a/lib/std/c/freebsd.zig +++ b/lib/std/c/freebsd.zig @@ -15,9 +15,9 @@ pub const sf_hdtr = extern struct { trl_cnt: c_int, }; pub extern "c" fn sendfile( - out_fd: fd_t, in_fd: fd_t, - offset: ?*off_t, + out_fd: fd_t, + offset: off_t, nbytes: usize, sf_hdtr: ?*sf_hdtr, sbytes: ?*off_t, diff --git a/lib/std/os.zig b/lib/std/os.zig index 90ad3e03a..a6c8b32ba 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -3734,7 +3734,8 @@ pub fn sendfile( while (true) { var sbytes: off_t = undefined; - const err = errno(system.sendfile(out_fd, in_fd, in_offset, adjusted_count, hdtr, &sbytes, flags)); + const offset = @bitCast(off_t, in_offset); + const err = errno(system.sendfile(in_fd, out_fd, offset, adjusted_count, hdtr, &sbytes, flags)); const amt = @bitCast(usize, sbytes); switch (err) { 0 => return amt, @@ -3813,19 +3814,17 @@ pub fn sendfile( while (true) { var sbytes: off_t = adjusted_count; const signed_offset = @bitCast(i64, in_offset); - const err = errno(system.sendfile(out_fd, in_fd, signed_offset, &sbytes, hdtr, flags)); + const err = errno(system.sendfile(in_fd, out_fd, signed_offset, &sbytes, hdtr, flags)); const amt = @bitCast(usize, sbytes); switch (err) { 0 => return amt, + EBADF => unreachable, // Always a race condition. EFAULT => unreachable, // Segmentation fault. EINVAL => unreachable, ENOTCONN => unreachable, // `out_fd` is an unconnected socket. - // On macOS version 10.14.6, I observed Darwin return EBADF when - // using sendfile on a valid open file descriptor of a file - // system file. - ENOTSUP, ENOTSOCK, ENOSYS, EBADF => break :sf, + ENOTSUP, ENOTSOCK, ENOSYS => break :sf, EINTR => if (amt != 0) return amt else continue,