| ... | @@ -1,6 +1,7 @@ | ... | @@ -1,6 +1,7 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const builtin = @import("builtin"); | 2 | const builtin = @import("builtin"); |
| 3 | const nio = @import("nio"); | 3 | const nio = @import("nio"); |
| | 4 | const nfs = @import("nfs"); |
| 4 | const sys_linux = @import("sys-linux"); | 5 | const sys_linux = @import("sys-linux"); |
| 5 | | 6 | |
| 6 | const os = builtin.target.os.tag; | 7 | const os = builtin.target.os.tag; |
| ... | @@ -10,6 +11,8 @@ const sys = switch (os) { | ... | @@ -10,6 +11,8 @@ const sys = switch (os) { |
| 10 | else => @compileError("TODO"), | 11 | else => @compileError("TODO"), |
| 11 | }; | 12 | }; |
| 12 | | 13 | |
| | 14 | pub const off_t = sys.off_t; |
| | 15 | |
| 13 | pub const Address = extern union { | 16 | pub const Address = extern union { |
| 14 | any: sys.struct_sockaddr, | 17 | any: sys.struct_sockaddr, |
| 15 | in: Ip4Address, | 18 | in: Ip4Address, |
| ... | @@ -133,6 +136,16 @@ pub const Stream = struct { | ... | @@ -133,6 +136,16 @@ pub const Stream = struct { |
| 133 | return sys.shutdown(@intCast(@intFromEnum(s.socket)), how); | 136 | return sys.shutdown(@intCast(@intFromEnum(s.socket)), how); |
| 134 | } | 137 | } |
| 135 | | 138 | |
| | 139 | pub fn sendfile(s: Stream, file: nfs.File, offset: off_t, count: ?usize) !void { |
| | 140 | const count_actual = count orelse (try file.stat()).size; |
| | 141 | var total: u63 = 0; |
| | 142 | while (total < count_actual) { |
| | 143 | const len = try sys.sendfile(@intCast(@intFromEnum(s.socket)), @intFromEnum(file.fd), &(offset + total), count_actual - total); |
| | 144 | total += @intCast(len); |
| | 145 | if (len == 0) break; |
| | 146 | } |
| | 147 | } |
| | 148 | |
| 136 | pub const ReadError = switch (builtin.target.os.tag) { | 149 | pub const ReadError = switch (builtin.target.os.tag) { |
| 137 | .linux => sys.errno.Error, | 150 | .linux => sys.errno.Error, |
| 138 | else => @compileError("TODO"), | 151 | else => @compileError("TODO"), |