diff --git a/net.zig b/net.zig index 00fea857dbaf95baf5d3b954e2e50924130b5917..039e3a72a62681061ada43eccee58bbcdb9a8e04 100644 --- a/net.zig +++ b/net.zig @@ -1,6 +1,7 @@ const std = @import("std"); const builtin = @import("builtin"); const nio = @import("nio"); +const nfs = @import("nfs"); const sys_linux = @import("sys-linux"); const os = builtin.target.os.tag; @@ -10,6 +11,8 @@ const sys = switch (os) { else => @compileError("TODO"), }; +pub const off_t = sys.off_t; + pub const Address = extern union { any: sys.struct_sockaddr, in: Ip4Address, @@ -133,6 +136,16 @@ pub const Stream = struct { return sys.shutdown(@intCast(@intFromEnum(s.socket)), how); } + pub fn sendfile(s: Stream, file: nfs.File, offset: off_t, count: ?usize) !void { + const count_actual = count orelse (try file.stat()).size; + var total: u63 = 0; + while (total < count_actual) { + const len = try sys.sendfile(@intCast(@intFromEnum(s.socket)), @intFromEnum(file.fd), &(offset + total), count_actual - total); + total += @intCast(len); + if (len == 0) break; + } + } + pub const ReadError = switch (builtin.target.os.tag) { .linux => sys.errno.Error, else => @compileError("TODO"), diff --git a/zigmod.yml b/zigmod.yml index 73a48ddc8e411809daf2d7a5d86eb15752b3c0b9..7f06360393aba49b90fd5ecd2027156c4a592bf4 100644 --- a/zigmod.yml +++ b/zigmod.yml @@ -6,5 +6,6 @@ description: An alternative network layer dependencies: - src: git https://github.com/nektro/zig-nio - src: git https://github.com/nektro/zig-sys-linux + - src: git https://github.com/nektro/zig-nfs root_dependencies: - src: git https://github.com/nektro/zig-expect