From faee6e5d1569075cf148744c95bd532b69c43028 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sun, 24 Aug 2025 02:38:37 -0700 Subject: [PATCH] it's alive! --- Dir.zig | 21 +++++++++++++++++++++ File.zig | 39 +++++++++++++++++++++++++++++++++++++++ README.md | 2 ++ licenses.txt | 10 ++++++++++ nfs.zig | 15 +++++++++++++++ zigmod.yml | 3 +++ 6 files changed, 90 insertions(+) create mode 100644 Dir.zig create mode 100644 File.zig diff --git a/Dir.zig b/Dir.zig new file mode 100644 index 0000000000000000000000000000000000000000..e2d5e240b98da43d6591376154e9ad32e97ea010 --- /dev/null +++ b/Dir.zig @@ -0,0 +1,21 @@ +const std = @import("std"); +const sys_libc = @import("sys-libc"); + +const nfs = @import("./nfs.zig"); +const File = nfs.File; +const Dir = @This(); + +fd: nfs.Handle, + +pub fn close(self: Dir) void { + sys_libc.close(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)) }; +} + +pub const OpenFileFlags = packed struct { + // +}; diff --git a/File.zig b/File.zig new file mode 100644 index 0000000000000000000000000000000000000000..0707568d77aa828b4b18cc216d17efa0bfec4026 --- /dev/null +++ b/File.zig @@ -0,0 +1,39 @@ +const std = @import("std"); +const builtin = @import("builtin"); +const sys_libc = @import("sys-libc"); +const nio = @import("nio"); +const errno = @import("errno"); + +const nfs = @import("./nfs.zig"); +const Dir = nfs.Dir; +const File = @This(); + +fd: nfs.Handle, + +pub fn close(self: File) void { + sys_libc.close(@intFromEnum(self.fd)) catch {}; +} + +pub const ReadError = switch (builtin.target.os.tag) { + .linux, + => errno.Error, + else => @compileError("TODO"), +}; +pub usingnamespace nio.Readable(@This()); +pub fn read(self: File, buffer: []u8) ReadError!usize { + return sys_libc.read(@intFromEnum(self.fd), buffer); +} + +pub fn anyReadable(self: File) nio.AnyReadable { + const S = struct { + fn foo(s: *anyopaque, buffer: []u8) anyerror!usize { + const fd: nfs.Handle = @enumFromInt(@intFromPtr(s)); + const f: File = .{ .fd = fd }; + return f.read(buffer); + } + }; + return .{ + .readFn = S.foo, + .state = @ptrFromInt(@as(usize, @bitCast(@as(isize, @intCast(@intFromEnum(self.fd)))))), + }; +} diff --git a/README.md b/README.md index 2b2c8fb1913a8ec9cedeb0337e13caa089a9526a..b35c3aa19ecbf83248577343fd0b22d1081453e4 100644 --- a/README.md +++ b/README.md @@ -7,3 +7,5 @@ [![Zigmod](https://img.shields.io/badge/Zigmod-latest-f7a41d)](https://github.com/nektro/zigmod) Nektro's Filesystem, an alternative to `std.fs`. + +Some code ported from Zig 0.14.1, via MIT Copyright (c) Zig contributors. New code MPL-2.0. diff --git a/licenses.txt b/licenses.txt index 38f772be6082b61f8cf89b7e597e98d02d63f6b8..e1abb7bfd5f306ef3813567449b7e77d5f5cc34b 100644 --- a/licenses.txt +++ b/licenses.txt @@ -1,3 +1,13 @@ MPL-2.0: = https://spdx.org/licenses/MPL-2.0 - This +- git https://github.com/nektro/zig-nio + +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 + +Unspecified: +- system_lib c diff --git a/nfs.zig b/nfs.zig index 95a0b682a71942e4010a9c8ee96461fb9a3b53a3..b65cfc1e5c40422f86acba9be3ad399c857394a3 100644 --- a/nfs.zig +++ b/nfs.zig @@ -1 +1,16 @@ const std = @import("std"); +const builtin = @import("builtin"); +const sys_libc = @import("sys-libc"); + +pub const Dir = @import("./Dir.zig"); +pub const File = @import("./File.zig"); + +pub const Handle = switch (builtin.target.os.tag) { + .linux, + => enum(c_int) { _ }, + else => @compileError("TODO"), +}; + +pub fn cwd() Dir { + return .{ .fd = @enumFromInt(sys_libc.AT.FDCWD) }; +} diff --git a/zigmod.yml b/zigmod.yml index 324005534e2c45661e7446dffb67fed9f0c8e783..3fc46646c3eb4d4d7d976413794f670fa4c13a42 100644 --- a/zigmod.yml +++ b/zigmod.yml @@ -6,3 +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-nio + - src: git https://github.com/nektro/zig-errno -- 2.54.0