authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-08-24 12:25:02-07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-08-24 12:25:02-07:00
loge648d8b8bc8d807d0f0d802c20eb5f2280f5e036
treefe9a452f7f63cb510623d74904241b6ddced54c5
parent6ca77ebd1d497bed27c5c74071e6c2ae9f8649a1
signature Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

add support for freebsd, netbsd, openbsd


5 files changed, 49 insertions(+), 24 deletions(-)

Dir.zig+3
......@@ -10,6 +10,9 @@ const os = builtin.target.os.tag;
1010const sys = switch (os) {
1111 .linux => @import("sys-linux"),
1212 .macos => @import("sys-darwin"),
13 .freebsd => @import("sys-freebsd"),
14 .netbsd => @import("sys-netbsd"),
15 .openbsd => @import("sys-openbsd"),
1316 else => unreachable,
1417};
1518
File.zig+21-14
......@@ -12,6 +12,9 @@ const os = builtin.target.os.tag;
1212const sys = switch (os) {
1313 .linux => @import("sys-linux"),
1414 .macos => @import("sys-darwin"),
15 .freebsd => @import("sys-freebsd"),
16 .netbsd => @import("sys-netbsd"),
17 .openbsd => @import("sys-openbsd"),
1518 else => unreachable,
1619};
1720
......@@ -139,16 +142,6 @@ pub const Stat = struct {
139142 ctime: i128,
140143
141144 pub fn fromPosix(st: sys.struct_stat) Stat {
142 if (os == .linux) {
143 return .{
144 .inode = st.ino,
145 .size = @bitCast(st.size),
146 .mode = st.mode,
147 .atime = @as(i128, st.atim.sec) * time.ns_per_s + st.atim.nsec,
148 .mtime = @as(i128, st.mtim.sec) * time.ns_per_s + st.mtim.nsec,
149 .ctime = @as(i128, st.ctim.sec) * time.ns_per_s + st.ctim.nsec,
150 };
151 }
152145 if (os == .macos) {
153146 return .{
154147 .inode = st.ino,
......@@ -159,6 +152,14 @@ pub const Stat = struct {
159152 .ctime = @as(i128, st.ctimespec.sec) * time.ns_per_s + st.ctimespec.nsec,
160153 };
161154 }
155 return .{
156 .inode = st.ino,
157 .size = @bitCast(st.size),
158 .mode = st.mode,
159 .atime = @as(i128, st.atim.sec) * time.ns_per_s + st.atim.nsec,
160 .mtime = @as(i128, st.mtim.sec) * time.ns_per_s + st.mtim.nsec,
161 .ctime = @as(i128, st.ctim.sec) * time.ns_per_s + st.ctim.nsec,
162 };
162163 }
163164
164165 pub fn kind(self: Stat) Kind {
......@@ -192,16 +193,22 @@ pub const Kind = enum {
192193 unknown,
193194};
194195
195/// Maps file content into memory with a single syscall.
196/// If length is null it will also call stat.
196/// Maps entire file content into memory with a single syscall.
197/// Release with `nfs.munmap`.
197198pub fn mmap(self: File) ![]const u8 {
199 return mmapRegion(self, 0, (try self.stat()).size);
200}
201
202/// Maps file content into memory with a single syscall.
203/// Release with `nfs.munmap`.
204pub fn mmapRegion(self: File, offset: sys.off_t, len: usize) ![]const u8 {
198205 return sys.mmap(
199206 null,
200 (try self.stat()).size,
207 len,
201208 sys.PROT.READ,
202209 sys.MAP.PRIVATE,
203210 @intFromEnum(self.fd),
204 0,
211 offset,
205212 );
206213}
207214
licenses.txt+3
......@@ -7,5 +7,8 @@ MIT:
77= https://spdx.org/licenses/MIT
88- git https://github.com/nektro/zig-extras
99- git https://github.com/nektro/zig-sys-darwin
10- git https://github.com/nektro/zig-sys-freebsd
1011- git https://github.com/nektro/zig-sys-linux
12- git https://github.com/nektro/zig-sys-netbsd
13- git https://github.com/nektro/zig-sys-openbsd
1114- git https://github.com/nektro/zig-time
nfs.zig+19-10
......@@ -11,6 +11,9 @@ const os = builtin.target.os.tag;
1111const sys = switch (os) {
1212 .linux => sys_linux,
1313 .macos => @import("sys-darwin"),
14 .freebsd => @import("sys-freebsd"),
15 .netbsd => @import("sys-netbsd"),
16 .openbsd => @import("sys-openbsd"),
1417 else => unreachable,
1518};
1619
......@@ -54,22 +57,21 @@ pub fn munmap(region: []const u8) void {
5457 return sys.munmap(region.ptr, region.len) catch {};
5558}
5659
57pub fn mkdtemp() !Dir {
58 var template = "/tmp/tmp.XXXXXXXXXX\x00".*;
60pub fn mktemp_buf() [19:0]u8 {
61 var template: [19:0]u8 = "/tmp/tmp.XXXXXXXXXX".*;
5962 const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
6063 const rand = nio.randomBytes(10);
61 if (rand.len != 10) return error.EAGAIN;
6264 for (template[9..][0..10], rand) |*a, b| a.* = letters[b % 62];
63 const path = template[0 .. template.len - 1 :0];
65 return template;
66}
67
68pub fn mkdtemp() !Dir {
69 const path = &mktemp_buf();
6470 return cwd().makeOpenPath(path, .{});
6571}
6672
6773pub fn mktemp(flags: Dir.CreateFlags) !File {
68 var template = "/tmp/tmp.XXXXXXXXXX\x00".*;
69 const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
70 const rand = nio.randomBytes(10);
71 for (template[9..][0..10], rand) |*a, b| a.* = letters[b % 62];
72 const path = template[0 .. template.len - 1 :0];
74 const path = &mktemp_buf();
7375 var _flags = flags;
7476 _flags.exclusive = true;
7577 return cwd().createFile(path, _flags);
......@@ -93,11 +95,18 @@ pub fn realdpath(fd: Handle, buf: *[sys.PATH_MAX]u8) ![:0]u8 {
9395 const str = nio.fmt.bufPrintZ(&dbuf, "/proc/self/fd/{d}", .{@intFromEnum(fd)}) catch unreachable;
9496 return sys.readlinkat(@intFromEnum(cwd().fd), str, buf);
9597 }
96 if (os == .macos) {
98 if (os == .macos or os == .netbsd) {
9799 @memset(buf, 0);
98100 const rc = sys.libc.fcntl(@intFromEnum(fd), sys.F.GETPATH, buf.ptr);
99101 if (rc == -1) return sys.errno.fromInt(sys.errno.fromLibC());
100102 const idx = std.mem.indexOfScalar(u8, buf, 0).?;
101103 return buf[0..idx :0];
102104 }
105 if (os == .freebsd) {
106 // https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=198570
107 comptime unreachable; // TODO
108 }
109 if (os == .openbsd) {
110 comptime unreachable;
111 }
103112}
zigmod.yml+3
......@@ -8,3 +8,6 @@ dependencies:
88 - src: git https://github.com/nektro/zig-nio
99 - src: git https://github.com/nektro/zig-time
1010 - src: git https://github.com/nektro/zig-sys-darwin
11 - src: git https://github.com/nektro/zig-sys-freebsd
12 - src: git https://github.com/nektro/zig-sys-netbsd
13 - src: git https://github.com/nektro/zig-sys-openbsd