From 9a4f8159be199f9a94d96a9cb41f4a88c592b46f Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Wed, 17 Dec 2025 18:17:04 -0800 Subject: [PATCH] zig-sys rework --- Dir.zig | 14 ++++++++++---- File.zig | 20 ++++++++++++-------- licenses.txt | 4 +--- nfs.zig | 18 ++++++++++++------ zigmod.yml | 3 +-- 5 files changed, 36 insertions(+), 23 deletions(-) diff --git a/Dir.zig b/Dir.zig index 4495daa269cdd6256c4cab14bc2b7e813af1c64f..9f48194d58a871a6260fd9b7117baf7373639026 100644 --- a/Dir.zig +++ b/Dir.zig @@ -1,19 +1,24 @@ const std = @import("std"); -const sys_libc = @import("sys-libc"); +const builtin = @import("builtin"); +const sys_linux = @import("sys-linux"); const nfs = @import("./nfs.zig"); const File = nfs.File; const Dir = @This(); +const os = builtin.target.os.tag; + fd: nfs.Handle, pub fn close(self: Dir) void { - sys_libc.close(@intFromEnum(self.fd)) catch {}; + if (os == .linux) + sys_linux.close(@intFromEnum(self.fd)) catch {}; } pub fn openFile(self: Dir, sub_path: [:0]const u8, flags: OpenFileFlags) !File { _ = flags; - return .{ .fd = @enumFromInt(try sys_libc.openat(@intFromEnum(self.fd), sub_path.ptr, sys_libc.O.RDONLY)) }; + if (os == .linux) + return .{ .fd = @enumFromInt(try sys_linux.openat(@intFromEnum(self.fd), sub_path.ptr, sys_linux.O.RDONLY)) }; } pub const OpenFileFlags = packed struct { @@ -22,7 +27,8 @@ pub const OpenFileFlags = packed struct { pub fn openDir(self: Dir, sub_path: [:0]const u8, flags: OpenDirFlags) !Dir { _ = flags; - return .{ .fd = @enumFromInt(try sys_libc.openat(@intFromEnum(self.fd), sub_path.ptr, sys_libc.O.RDONLY | sys_libc.O.DIRECTORY)) }; + if (os == .linux) + return .{ .fd = @enumFromInt(try sys_linux.openat(@intFromEnum(self.fd), sub_path.ptr, sys_linux.O.RDONLY | sys_linux.O.DIRECTORY)) }; } pub const OpenDirFlags = packed struct { diff --git a/File.zig b/File.zig index ce4e52a183661efdc7724112efe8f2e0f60c92a8..2bc0a0dca7ec041e6207016e2738e861c934c87c 100644 --- a/File.zig +++ b/File.zig @@ -1,28 +1,31 @@ const std = @import("std"); const builtin = @import("builtin"); -const sys_libc = @import("sys-libc"); +const sys_linux = @import("sys-linux"); const nio = @import("nio"); -const errno = @import("errno"); const time = @import("time"); const nfs = @import("./nfs.zig"); const Dir = nfs.Dir; const File = @This(); +const os = builtin.target.os.tag; + fd: nfs.Handle, pub fn close(self: File) void { - sys_libc.close(@intFromEnum(self.fd)) catch {}; + if (os == .linux) + sys_linux.close(@intFromEnum(self.fd)) catch {}; } pub const ReadError = switch (builtin.target.os.tag) { .linux, - => errno.Error, + => sys_linux.errno.Error, else => @compileError("TODO"), }; pub usingnamespace nio.Readable(@This(), ._bare); pub fn read(self: File, buffer: []u8) ReadError!usize { - return sys_libc.read(@intFromEnum(self.fd), buffer); + if (os == .linux) + return sys_linux.read(@intFromEnum(self.fd), buffer); } pub fn anyReadable(self: File) nio.AnyReadable { @@ -42,7 +45,8 @@ pub fn anyReadable(self: File) nio.AnyReadable { } pub fn stat(self: File) !Stat { - const st = try sys_libc.fstat(@intFromEnum(self.fd)); + if (os != .linux) @compileError("TODO: File.stat"); + const st = try sys_linux.fstat(@intFromEnum(self.fd)); return .{ .inode = st.ino, .size = @bitCast(st.size), @@ -106,13 +110,13 @@ pub const Stat = struct { pub const INode = switch (builtin.target.os.tag) { .linux, - => sys_libc.ino_t, + => sys_linux.ino_t, else => |v| @compileError("TODO: " ++ @tagName(v)), }; pub const Mode = switch (builtin.target.os.tag) { .linux, - => sys_libc.mode_t, + => sys_linux.mode_t, else => |v| @compileError("TODO: " ++ @tagName(v)), }; diff --git a/licenses.txt b/licenses.txt index 71a9eadd569cd3890927d8358ff80fd62fb9a4d9..d9b0b961781cc16d353e4bd104c3a18aaa184349 100644 --- a/licenses.txt +++ b/licenses.txt @@ -5,9 +5,7 @@ MPL-2.0: MIT: = https://spdx.org/licenses/MIT -- git https://github.com/nektro/zig-errno -- git https://github.com/nektro/zig-libc -- git https://github.com/nektro/zig-sys-libc +- git https://github.com/nektro/zig-sys-linux - git https://github.com/nektro/zig-time Unspecified: diff --git a/nfs.zig b/nfs.zig index 73041c967965ffc4e8f46aa399efcd874c2f8e7d..9ad64ae7955b9c419a91b8d7657604bde6ba06c0 100644 --- a/nfs.zig +++ b/nfs.zig @@ -1,28 +1,34 @@ const std = @import("std"); const builtin = @import("builtin"); -const sys_libc = @import("sys-libc"); +const sys_linux = @import("sys-linux"); pub const Dir = @import("./Dir.zig"); pub const File = @import("./File.zig"); -pub const Handle = switch (builtin.target.os.tag) { +const os = builtin.target.os.tag; + +pub const Handle = switch (os) { .linux, => enum(c_int) { _ }, else => @compileError("TODO"), }; pub fn cwd() Dir { - return .{ .fd = @enumFromInt(sys_libc.AT.FDCWD) }; + if (os == .linux) + return .{ .fd = @enumFromInt(sys_linux.AT.FDCWD) }; } pub fn stdin() File { - return .{ .fd = @enumFromInt(0) }; + if (os == .linux) + return .{ .fd = @enumFromInt(0) }; } pub fn stdout() File { - return .{ .fd = @enumFromInt(1) }; + if (os == .linux) + return .{ .fd = @enumFromInt(1) }; } pub fn stderr() File { - return .{ .fd = @enumFromInt(2) }; + if (os == .linux) + return .{ .fd = @enumFromInt(2) }; } diff --git a/zigmod.yml b/zigmod.yml index 99e36bead65cf54a2f02445ae0140d8454e0b060..19f9158232468a6bdb3ae5623ada46f5717e9a11 100644 --- a/zigmod.yml +++ b/zigmod.yml @@ -6,7 +6,6 @@ description: Nektro's Filesystem, an alternative to std.fs min_zig_version: 0.14.0 min_zigmod_version: r96 dependencies: - - src: git https://github.com/nektro/zig-sys-libc + - src: git https://github.com/nektro/zig-sys-linux - src: git https://github.com/nektro/zig-nio - - src: git https://github.com/nektro/zig-errno - src: git https://github.com/nektro/zig-time -- 2.54.0