authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2020-03-05 19:53:58-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-05 20:46:28-05:00
logd31b65e762fbf59d29780d4cbf7be1ab824a67de
treef264cc180948c1880b0173d782ec9d0546f818ba
parent428677ea361447949f274587faa41656ce8199ab

std: fix sendfile on macOS and FreeBSD

- fix std.os.sendfile/FreeBSD use correct in/out fd_t - fix std.os.sendfile/macOS use correct in/out fd_t - undo 1141bfb21b82f8d3fc353e968a591f2ad9aaa571 (no longer needed) - fix c.freebsd.sendfile use off_t value - fix c.freebsd.sendfile decl correct in/out fd_t - fix c.darwin.sendfile decl correct in/out fd_t fix signature param names

3 files changed, 8 insertions(+), 9 deletions(-)

lib/std/c/darwin.zig+1-1
...@@ -63,8 +63,8 @@ pub const sf_hdtr = extern struct {...@@ -63,8 +63,8 @@ pub const sf_hdtr = extern struct {
63};63};
6464
65pub extern "c" fn sendfile(65pub extern "c" fn sendfile(
66 out_fd: fd_t,
67 in_fd: fd_t,66 in_fd: fd_t,
67 out_fd: fd_t,
68 offset: off_t,68 offset: off_t,
69 len: *off_t,69 len: *off_t,
70 sf_hdtr: ?*sf_hdtr,70 sf_hdtr: ?*sf_hdtr,
lib/std/c/freebsd.zig+2-2
...@@ -15,9 +15,9 @@ pub const sf_hdtr = extern struct {...@@ -15,9 +15,9 @@ pub const sf_hdtr = extern struct {
15 trl_cnt: c_int,15 trl_cnt: c_int,
16};16};
17pub extern "c" fn sendfile(17pub extern "c" fn sendfile(
18 out_fd: fd_t,
19 in_fd: fd_t,18 in_fd: fd_t,
20 offset: ?*off_t,19 out_fd: fd_t,
20 offset: off_t,
21 nbytes: usize,21 nbytes: usize,
22 sf_hdtr: ?*sf_hdtr,22 sf_hdtr: ?*sf_hdtr,
23 sbytes: ?*off_t,23 sbytes: ?*off_t,
lib/std/os.zig+5-6
...@@ -3734,7 +3734,8 @@ pub fn sendfile(...@@ -3734,7 +3734,8 @@ pub fn sendfile(
37343734
3735 while (true) {3735 while (true) {
3736 var sbytes: off_t = undefined;3736 var sbytes: off_t = undefined;
3737 const err = errno(system.sendfile(out_fd, in_fd, in_offset, adjusted_count, hdtr, &sbytes, flags));3737 const offset = @bitCast(off_t, in_offset);
3738 const err = errno(system.sendfile(in_fd, out_fd, offset, adjusted_count, hdtr, &sbytes, flags));
3738 const amt = @bitCast(usize, sbytes);3739 const amt = @bitCast(usize, sbytes);
3739 switch (err) {3740 switch (err) {
3740 0 => return amt,3741 0 => return amt,
...@@ -3813,19 +3814,17 @@ pub fn sendfile(...@@ -3813,19 +3814,17 @@ pub fn sendfile(
3813 while (true) {3814 while (true) {
3814 var sbytes: off_t = adjusted_count;3815 var sbytes: off_t = adjusted_count;
3815 const signed_offset = @bitCast(i64, in_offset);3816 const signed_offset = @bitCast(i64, in_offset);
3816 const err = errno(system.sendfile(out_fd, in_fd, signed_offset, &sbytes, hdtr, flags));3817 const err = errno(system.sendfile(in_fd, out_fd, signed_offset, &sbytes, hdtr, flags));
3817 const amt = @bitCast(usize, sbytes);3818 const amt = @bitCast(usize, sbytes);
3818 switch (err) {3819 switch (err) {
3819 0 => return amt,3820 0 => return amt,
38203821
3822 EBADF => unreachable, // Always a race condition.
3821 EFAULT => unreachable, // Segmentation fault.3823 EFAULT => unreachable, // Segmentation fault.
3822 EINVAL => unreachable,3824 EINVAL => unreachable,
3823 ENOTCONN => unreachable, // `out_fd` is an unconnected socket.3825 ENOTCONN => unreachable, // `out_fd` is an unconnected socket.
38243826
3825 // On macOS version 10.14.6, I observed Darwin return EBADF when3827 ENOTSUP, ENOTSOCK, ENOSYS => break :sf,
3826 // using sendfile on a valid open file descriptor of a file
3827 // system file.
3828 ENOTSUP, ENOTSOCK, ENOSYS, EBADF => break :sf,
38293828
3830 EINTR => if (amt != 0) return amt else continue,3829 EINTR => if (amt != 0) return amt else continue,
38313830