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;...@@ -10,6 +10,9 @@ const os = builtin.target.os.tag;
10const sys = switch (os) {10const sys = switch (os) {
11 .linux => @import("sys-linux"),11 .linux => @import("sys-linux"),
12 .macos => @import("sys-darwin"),12 .macos => @import("sys-darwin"),
13 .freebsd => @import("sys-freebsd"),
14 .netbsd => @import("sys-netbsd"),
15 .openbsd => @import("sys-openbsd"),
13 else => unreachable,16 else => unreachable,
14};17};
1518
File.zig+21-14
...@@ -12,6 +12,9 @@ const os = builtin.target.os.tag;...@@ -12,6 +12,9 @@ const os = builtin.target.os.tag;
12const sys = switch (os) {12const sys = switch (os) {
13 .linux => @import("sys-linux"),13 .linux => @import("sys-linux"),
14 .macos => @import("sys-darwin"),14 .macos => @import("sys-darwin"),
15 .freebsd => @import("sys-freebsd"),
16 .netbsd => @import("sys-netbsd"),
17 .openbsd => @import("sys-openbsd"),
15 else => unreachable,18 else => unreachable,
16};19};
1720
...@@ -139,16 +142,6 @@ pub const Stat = struct {...@@ -139,16 +142,6 @@ pub const Stat = struct {
139 ctime: i128,142 ctime: i128,
140143
141 pub fn fromPosix(st: sys.struct_stat) Stat {144 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 }
152 if (os == .macos) {145 if (os == .macos) {
153 return .{146 return .{
154 .inode = st.ino,147 .inode = st.ino,
...@@ -159,6 +152,14 @@ pub const Stat = struct {...@@ -159,6 +152,14 @@ pub const Stat = struct {
159 .ctime = @as(i128, st.ctimespec.sec) * time.ns_per_s + st.ctimespec.nsec,152 .ctime = @as(i128, st.ctimespec.sec) * time.ns_per_s + st.ctimespec.nsec,
160 };153 };
161 }154 }
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 };
162 }163 }
163164
164 pub fn kind(self: Stat) Kind {165 pub fn kind(self: Stat) Kind {
...@@ -192,16 +193,22 @@ pub const Kind = enum {...@@ -192,16 +193,22 @@ pub const Kind = enum {
192 unknown,193 unknown,
193};194};
194195
195/// Maps file content into memory with a single syscall.196/// Maps entire file content into memory with a single syscall.
196/// If length is null it will also call stat.197/// Release with `nfs.munmap`.
197pub fn mmap(self: File) ![]const u8 {198pub 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 {
198 return sys.mmap(205 return sys.mmap(
199 null,206 null,
200 (try self.stat()).size,207 len,
201 sys.PROT.READ,208 sys.PROT.READ,
202 sys.MAP.PRIVATE,209 sys.MAP.PRIVATE,
203 @intFromEnum(self.fd),210 @intFromEnum(self.fd),
204 0,211 offset,
205 );212 );
206}213}
207214
licenses.txt+3
...@@ -7,5 +7,8 @@ MIT:...@@ -7,5 +7,8 @@ MIT:
7= https://spdx.org/licenses/MIT7= https://spdx.org/licenses/MIT
8- git https://github.com/nektro/zig-extras8- git https://github.com/nektro/zig-extras
9- git https://github.com/nektro/zig-sys-darwin9- git https://github.com/nektro/zig-sys-darwin
10- git https://github.com/nektro/zig-sys-freebsd
10- git https://github.com/nektro/zig-sys-linux11- 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
11- git https://github.com/nektro/zig-time14- git https://github.com/nektro/zig-time
nfs.zig+19-10
...@@ -11,6 +11,9 @@ const os = builtin.target.os.tag;...@@ -11,6 +11,9 @@ const os = builtin.target.os.tag;
11const sys = switch (os) {11const sys = switch (os) {
12 .linux => sys_linux,12 .linux => sys_linux,
13 .macos => @import("sys-darwin"),13 .macos => @import("sys-darwin"),
14 .freebsd => @import("sys-freebsd"),
15 .netbsd => @import("sys-netbsd"),
16 .openbsd => @import("sys-openbsd"),
14 else => unreachable,17 else => unreachable,
15};18};
1619
...@@ -54,22 +57,21 @@ pub fn munmap(region: []const u8) void {...@@ -54,22 +57,21 @@ pub fn munmap(region: []const u8) void {
54 return sys.munmap(region.ptr, region.len) catch {};57 return sys.munmap(region.ptr, region.len) catch {};
55}58}
5659
57pub fn mkdtemp() !Dir {60pub fn mktemp_buf() [19:0]u8 {
58 var template = "/tmp/tmp.XXXXXXXXXX\x00".*;61 var template: [19:0]u8 = "/tmp/tmp.XXXXXXXXXX".*;
59 const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";62 const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
60 const rand = nio.randomBytes(10);63 const rand = nio.randomBytes(10);
61 if (rand.len != 10) return error.EAGAIN;
62 for (template[9..][0..10], rand) |*a, b| a.* = letters[b % 62];64 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();
64 return cwd().makeOpenPath(path, .{});70 return cwd().makeOpenPath(path, .{});
65}71}
6672
67pub fn mktemp(flags: Dir.CreateFlags) !File {73pub fn mktemp(flags: Dir.CreateFlags) !File {
68 var template = "/tmp/tmp.XXXXXXXXXX\x00".*;74 const path = &mktemp_buf();
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];
73 var _flags = flags;75 var _flags = flags;
74 _flags.exclusive = true;76 _flags.exclusive = true;
75 return cwd().createFile(path, _flags);77 return cwd().createFile(path, _flags);
...@@ -93,11 +95,18 @@ pub fn realdpath(fd: Handle, buf: *[sys.PATH_MAX]u8) ![:0]u8 {...@@ -93,11 +95,18 @@ pub fn realdpath(fd: Handle, buf: *[sys.PATH_MAX]u8) ![:0]u8 {
93 const str = nio.fmt.bufPrintZ(&dbuf, "/proc/self/fd/{d}", .{@intFromEnum(fd)}) catch unreachable;95 const str = nio.fmt.bufPrintZ(&dbuf, "/proc/self/fd/{d}", .{@intFromEnum(fd)}) catch unreachable;
94 return sys.readlinkat(@intFromEnum(cwd().fd), str, buf);96 return sys.readlinkat(@intFromEnum(cwd().fd), str, buf);
95 }97 }
96 if (os == .macos) {98 if (os == .macos or os == .netbsd) {
97 @memset(buf, 0);99 @memset(buf, 0);
98 const rc = sys.libc.fcntl(@intFromEnum(fd), sys.F.GETPATH, buf.ptr);100 const rc = sys.libc.fcntl(@intFromEnum(fd), sys.F.GETPATH, buf.ptr);
99 if (rc == -1) return sys.errno.fromInt(sys.errno.fromLibC());101 if (rc == -1) return sys.errno.fromInt(sys.errno.fromLibC());
100 const idx = std.mem.indexOfScalar(u8, buf, 0).?;102 const idx = std.mem.indexOfScalar(u8, buf, 0).?;
101 return buf[0..idx :0];103 return buf[0..idx :0];
102 }104 }
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 }
103}112}
zigmod.yml+3
...@@ -8,3 +8,6 @@ dependencies:...@@ -8,3 +8,6 @@ dependencies:
8 - src: git https://github.com/nektro/zig-nio8 - src: git https://github.com/nektro/zig-nio
9 - src: git https://github.com/nektro/zig-time9 - src: git https://github.com/nektro/zig-time
10 - src: git https://github.com/nektro/zig-sys-darwin10 - 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