From e648d8b8bc8d807d0f0d802c20eb5f2280f5e036 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Mon, 24 Aug 2026 12:25:02 -0700 Subject: [PATCH] add support for freebsd, netbsd, openbsd --- Dir.zig | 3 +++ File.zig | 35 +++++++++++++++++++++-------------- licenses.txt | 3 +++ nfs.zig | 33 +++++++++++++++++++++------------ zigmod.yml | 3 +++ 5 files changed, 51 insertions(+), 26 deletions(-) diff --git a/Dir.zig b/Dir.zig index 16e51c886496ec04b4218933432f1e4d2dce7189..c9ca43f7958969afa22c7af9bd3aa43965bd7154 100644 --- a/Dir.zig +++ b/Dir.zig @@ -10,6 +10,9 @@ const os = builtin.target.os.tag; const sys = switch (os) { .linux => @import("sys-linux"), .macos => @import("sys-darwin"), + .freebsd => @import("sys-freebsd"), + .netbsd => @import("sys-netbsd"), + .openbsd => @import("sys-openbsd"), else => unreachable, }; diff --git a/File.zig b/File.zig index bd38bc736468627816e1a0d9e0fb430d6434cedf..6ca3eccc9e10a5f42af12215772f53f77c8f7034 100644 --- a/File.zig +++ b/File.zig @@ -12,6 +12,9 @@ const os = builtin.target.os.tag; const sys = switch (os) { .linux => @import("sys-linux"), .macos => @import("sys-darwin"), + .freebsd => @import("sys-freebsd"), + .netbsd => @import("sys-netbsd"), + .openbsd => @import("sys-openbsd"), else => unreachable, }; @@ -139,16 +142,6 @@ pub const Stat = struct { ctime: i128, pub fn fromPosix(st: sys.struct_stat) Stat { - if (os == .linux) { - return .{ - .inode = st.ino, - .size = @bitCast(st.size), - .mode = st.mode, - .atime = @as(i128, st.atim.sec) * time.ns_per_s + st.atim.nsec, - .mtime = @as(i128, st.mtim.sec) * time.ns_per_s + st.mtim.nsec, - .ctime = @as(i128, st.ctim.sec) * time.ns_per_s + st.ctim.nsec, - }; - } if (os == .macos) { return .{ .inode = st.ino, @@ -159,6 +152,14 @@ pub const Stat = struct { .ctime = @as(i128, st.ctimespec.sec) * time.ns_per_s + st.ctimespec.nsec, }; } + return .{ + .inode = st.ino, + .size = @bitCast(st.size), + .mode = st.mode, + .atime = @as(i128, st.atim.sec) * time.ns_per_s + st.atim.nsec, + .mtime = @as(i128, st.mtim.sec) * time.ns_per_s + st.mtim.nsec, + .ctime = @as(i128, st.ctim.sec) * time.ns_per_s + st.ctim.nsec, + }; } pub fn kind(self: Stat) Kind { @@ -192,16 +193,22 @@ pub const Kind = enum { unknown, }; -/// Maps file content into memory with a single syscall. -/// If length is null it will also call stat. +/// Maps entire file content into memory with a single syscall. +/// Release with `nfs.munmap`. pub fn mmap(self: File) ![]const u8 { + return mmapRegion(self, 0, (try self.stat()).size); +} + +/// Maps file content into memory with a single syscall. +/// Release with `nfs.munmap`. +pub fn mmapRegion(self: File, offset: sys.off_t, len: usize) ![]const u8 { return sys.mmap( null, - (try self.stat()).size, + len, sys.PROT.READ, sys.MAP.PRIVATE, @intFromEnum(self.fd), - 0, + offset, ); } diff --git a/licenses.txt b/licenses.txt index 06614056475a578524819f11873627d79bcc24ca..7350aee854603c92b99672e732881e9a567ceddd 100644 --- a/licenses.txt +++ b/licenses.txt @@ -7,5 +7,8 @@ MIT: = https://spdx.org/licenses/MIT - git https://github.com/nektro/zig-extras - git https://github.com/nektro/zig-sys-darwin +- git https://github.com/nektro/zig-sys-freebsd - git https://github.com/nektro/zig-sys-linux +- git https://github.com/nektro/zig-sys-netbsd +- git https://github.com/nektro/zig-sys-openbsd - git https://github.com/nektro/zig-time diff --git a/nfs.zig b/nfs.zig index 295773f8d650c6d6e5f6de7d31d8e99eb7b16819..8ae9f1f556b1507a5b28c5c6c29f6e02d27f4458 100644 --- a/nfs.zig +++ b/nfs.zig @@ -11,6 +11,9 @@ const os = builtin.target.os.tag; const sys = switch (os) { .linux => sys_linux, .macos => @import("sys-darwin"), + .freebsd => @import("sys-freebsd"), + .netbsd => @import("sys-netbsd"), + .openbsd => @import("sys-openbsd"), else => unreachable, }; @@ -54,22 +57,21 @@ pub fn munmap(region: []const u8) void { return sys.munmap(region.ptr, region.len) catch {}; } +pub fn mktemp_buf() [19:0]u8 { + var template: [19:0]u8 = "/tmp/tmp.XXXXXXXXXX".*; + const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + const rand = nio.randomBytes(10); + for (template[9..][0..10], rand) |*a, b| a.* = letters[b % 62]; + return template; +} + pub fn mkdtemp() !Dir { - var template = "/tmp/tmp.XXXXXXXXXX\x00".*; - const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - const rand = nio.randomBytes(10); - if (rand.len != 10) return error.EAGAIN; - for (template[9..][0..10], rand) |*a, b| a.* = letters[b % 62]; - const path = template[0 .. template.len - 1 :0]; + const path = &mktemp_buf(); return cwd().makeOpenPath(path, .{}); } pub fn mktemp(flags: Dir.CreateFlags) !File { - var template = "/tmp/tmp.XXXXXXXXXX\x00".*; - const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - const rand = nio.randomBytes(10); - for (template[9..][0..10], rand) |*a, b| a.* = letters[b % 62]; - const path = template[0 .. template.len - 1 :0]; + const path = &mktemp_buf(); var _flags = flags; _flags.exclusive = true; return cwd().createFile(path, _flags); @@ -93,11 +95,18 @@ pub fn realdpath(fd: Handle, buf: *[sys.PATH_MAX]u8) ![:0]u8 { const str = nio.fmt.bufPrintZ(&dbuf, "/proc/self/fd/{d}", .{@intFromEnum(fd)}) catch unreachable; return sys.readlinkat(@intFromEnum(cwd().fd), str, buf); } - if (os == .macos) { + if (os == .macos or os == .netbsd) { @memset(buf, 0); const rc = sys.libc.fcntl(@intFromEnum(fd), sys.F.GETPATH, buf.ptr); if (rc == -1) return sys.errno.fromInt(sys.errno.fromLibC()); const idx = std.mem.indexOfScalar(u8, buf, 0).?; return buf[0..idx :0]; } + if (os == .freebsd) { + // https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=198570 + comptime unreachable; // TODO + } + if (os == .openbsd) { + comptime unreachable; + } } diff --git a/zigmod.yml b/zigmod.yml index f7c9485effc71a203f1a8e72a0967b1cc02bbd33..525dcd2c619fb96d3b25e433aec1c61a2f081b56 100644 --- a/zigmod.yml +++ b/zigmod.yml @@ -8,3 +8,6 @@ dependencies: - src: git https://github.com/nektro/zig-nio - src: git https://github.com/nektro/zig-time - src: git https://github.com/nektro/zig-sys-darwin + - src: git https://github.com/nektro/zig-sys-freebsd + - src: git https://github.com/nektro/zig-sys-netbsd + - src: git https://github.com/nektro/zig-sys-openbsd -- 2.54.0