authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-12-17 18:17:04 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-12-17 18:17:04 -08:00
log9a4f8159be199f9a94d96a9cb41f4a88c592b46f
treec093c2570990a1497f0653e162ddfdd2a3301754
parent874ae5c71c029969d4aef1aaea77b6dcbf913289

zig-sys rework


5 files changed, 36 insertions(+), 23 deletions(-)

Dir.zig+10-4
...@@ -1,19 +1,24 @@...@@ -1,19 +1,24 @@
1const std = @import("std");1const std = @import("std");
2const sys_libc = @import("sys-libc");2const builtin = @import("builtin");
3const sys_linux = @import("sys-linux");
34
4const nfs = @import("./nfs.zig");5const nfs = @import("./nfs.zig");
5const File = nfs.File;6const File = nfs.File;
6const Dir = @This();7const Dir = @This();
78
9const os = builtin.target.os.tag;
10
8fd: nfs.Handle,11fd: nfs.Handle,
912
10pub fn close(self: Dir) void {13pub fn close(self: Dir) void {
11 sys_libc.close(@intFromEnum(self.fd)) catch {};14 if (os == .linux)
15 sys_linux.close(@intFromEnum(self.fd)) catch {};
12}16}
1317
14pub fn openFile(self: Dir, sub_path: [:0]const u8, flags: OpenFileFlags) !File {18pub fn openFile(self: Dir, sub_path: [:0]const u8, flags: OpenFileFlags) !File {
15 _ = flags;19 _ = flags;
16 return .{ .fd = @enumFromInt(try sys_libc.openat(@intFromEnum(self.fd), sub_path.ptr, sys_libc.O.RDONLY)) };20 if (os == .linux)
21 return .{ .fd = @enumFromInt(try sys_linux.openat(@intFromEnum(self.fd), sub_path.ptr, sys_linux.O.RDONLY)) };
17}22}
1823
19pub const OpenFileFlags = packed struct {24pub const OpenFileFlags = packed struct {
...@@ -22,7 +27,8 @@ pub const OpenFileFlags = packed struct {...@@ -22,7 +27,8 @@ pub const OpenFileFlags = packed struct {
2227
23pub fn openDir(self: Dir, sub_path: [:0]const u8, flags: OpenDirFlags) !Dir {28pub fn openDir(self: Dir, sub_path: [:0]const u8, flags: OpenDirFlags) !Dir {
24 _ = flags;29 _ = flags;
25 return .{ .fd = @enumFromInt(try sys_libc.openat(@intFromEnum(self.fd), sub_path.ptr, sys_libc.O.RDONLY | sys_libc.O.DIRECTORY)) };30 if (os == .linux)
31 return .{ .fd = @enumFromInt(try sys_linux.openat(@intFromEnum(self.fd), sub_path.ptr, sys_linux.O.RDONLY | sys_linux.O.DIRECTORY)) };
26}32}
2733
28pub const OpenDirFlags = packed struct {34pub const OpenDirFlags = packed struct {
File.zig+12-8
...@@ -1,28 +1,31 @@...@@ -1,28 +1,31 @@
1const std = @import("std");1const std = @import("std");
2const builtin = @import("builtin");2const builtin = @import("builtin");
3const sys_libc = @import("sys-libc");3const sys_linux = @import("sys-linux");
4const nio = @import("nio");4const nio = @import("nio");
5const errno = @import("errno");
6const time = @import("time");5const time = @import("time");
76
8const nfs = @import("./nfs.zig");7const nfs = @import("./nfs.zig");
9const Dir = nfs.Dir;8const Dir = nfs.Dir;
10const File = @This();9const File = @This();
1110
11const os = builtin.target.os.tag;
12
12fd: nfs.Handle,13fd: nfs.Handle,
1314
14pub fn close(self: File) void {15pub fn close(self: File) void {
15 sys_libc.close(@intFromEnum(self.fd)) catch {};16 if (os == .linux)
17 sys_linux.close(@intFromEnum(self.fd)) catch {};
16}18}
1719
18pub const ReadError = switch (builtin.target.os.tag) {20pub const ReadError = switch (builtin.target.os.tag) {
19 .linux,21 .linux,
20 => errno.Error,22 => sys_linux.errno.Error,
21 else => @compileError("TODO"),23 else => @compileError("TODO"),
22};24};
23pub usingnamespace nio.Readable(@This(), ._bare);25pub usingnamespace nio.Readable(@This(), ._bare);
24pub fn read(self: File, buffer: []u8) ReadError!usize {26pub fn read(self: File, buffer: []u8) ReadError!usize {
25 return sys_libc.read(@intFromEnum(self.fd), buffer);27 if (os == .linux)
28 return sys_linux.read(@intFromEnum(self.fd), buffer);
26}29}
2730
28pub fn anyReadable(self: File) nio.AnyReadable {31pub fn anyReadable(self: File) nio.AnyReadable {
...@@ -42,7 +45,8 @@ pub fn anyReadable(self: File) nio.AnyReadable {...@@ -42,7 +45,8 @@ pub fn anyReadable(self: File) nio.AnyReadable {
42}45}
4346
44pub fn stat(self: File) !Stat {47pub fn stat(self: File) !Stat {
45 const st = try sys_libc.fstat(@intFromEnum(self.fd));48 if (os != .linux) @compileError("TODO: File.stat");
49 const st = try sys_linux.fstat(@intFromEnum(self.fd));
46 return .{50 return .{
47 .inode = st.ino,51 .inode = st.ino,
48 .size = @bitCast(st.size),52 .size = @bitCast(st.size),
...@@ -106,13 +110,13 @@ pub const Stat = struct {...@@ -106,13 +110,13 @@ pub const Stat = struct {
106110
107pub const INode = switch (builtin.target.os.tag) {111pub const INode = switch (builtin.target.os.tag) {
108 .linux,112 .linux,
109 => sys_libc.ino_t,113 => sys_linux.ino_t,
110 else => |v| @compileError("TODO: " ++ @tagName(v)),114 else => |v| @compileError("TODO: " ++ @tagName(v)),
111};115};
112116
113pub const Mode = switch (builtin.target.os.tag) {117pub const Mode = switch (builtin.target.os.tag) {
114 .linux,118 .linux,
115 => sys_libc.mode_t,119 => sys_linux.mode_t,
116 else => |v| @compileError("TODO: " ++ @tagName(v)),120 else => |v| @compileError("TODO: " ++ @tagName(v)),
117};121};
118122
licenses.txt+1-3
...@@ -5,9 +5,7 @@ MPL-2.0:...@@ -5,9 +5,7 @@ MPL-2.0:
55
6MIT:6MIT:
7= https://spdx.org/licenses/MIT7= https://spdx.org/licenses/MIT
8- git https://github.com/nektro/zig-errno8- git https://github.com/nektro/zig-sys-linux
9- git https://github.com/nektro/zig-libc
10- git https://github.com/nektro/zig-sys-libc
11- git https://github.com/nektro/zig-time9- git https://github.com/nektro/zig-time
1210
13Unspecified:11Unspecified:
nfs.zig+12-6
...@@ -1,28 +1,34 @@...@@ -1,28 +1,34 @@
1const std = @import("std");1const std = @import("std");
2const builtin = @import("builtin");2const builtin = @import("builtin");
3const sys_libc = @import("sys-libc");3const sys_linux = @import("sys-linux");
44
5pub const Dir = @import("./Dir.zig");5pub const Dir = @import("./Dir.zig");
6pub const File = @import("./File.zig");6pub const File = @import("./File.zig");
77
8pub const Handle = switch (builtin.target.os.tag) {8const os = builtin.target.os.tag;
9
10pub const Handle = switch (os) {
9 .linux,11 .linux,
10 => enum(c_int) { _ },12 => enum(c_int) { _ },
11 else => @compileError("TODO"),13 else => @compileError("TODO"),
12};14};
1315
14pub fn cwd() Dir {16pub fn cwd() Dir {
15 return .{ .fd = @enumFromInt(sys_libc.AT.FDCWD) };17 if (os == .linux)
18 return .{ .fd = @enumFromInt(sys_linux.AT.FDCWD) };
16}19}
1720
18pub fn stdin() File {21pub fn stdin() File {
19 return .{ .fd = @enumFromInt(0) };22 if (os == .linux)
23 return .{ .fd = @enumFromInt(0) };
20}24}
2125
22pub fn stdout() File {26pub fn stdout() File {
23 return .{ .fd = @enumFromInt(1) };27 if (os == .linux)
28 return .{ .fd = @enumFromInt(1) };
24}29}
2530
26pub fn stderr() File {31pub fn stderr() File {
27 return .{ .fd = @enumFromInt(2) };32 if (os == .linux)
33 return .{ .fd = @enumFromInt(2) };
28}34}
zigmod.yml+1-2
...@@ -6,7 +6,6 @@ description: Nektro's Filesystem, an alternative to std.fs...@@ -6,7 +6,6 @@ description: Nektro's Filesystem, an alternative to std.fs
6min_zig_version: 0.14.06min_zig_version: 0.14.0
7min_zigmod_version: r967min_zigmod_version: r96
8dependencies:8dependencies:
9 - src: git https://github.com/nektro/zig-sys-libc9 - src: git https://github.com/nektro/zig-sys-linux
10 - src: git https://github.com/nektro/zig-nio10 - src: git https://github.com/nektro/zig-nio
11 - src: git https://github.com/nektro/zig-errno
12 - src: git https://github.com/nektro/zig-time11 - src: git https://github.com/nektro/zig-time