| author | |
| committer | |
| log | faee6e5d1569075cf148744c95bd532b69c43028 |
| tree | a9911bb4b02e601d651ad3e281ff3303feec883d |
| parent | b47a28b8a90555bf58929546fea3b043905ae808 |
6 files changed, 90 insertions(+), 0 deletions(-)
Dir.zig created+21| ... | @@ -0,0 +1,21 @@ | ||
| 1 | const std = @import("std"); | ||
| 2 | const sys_libc = @import("sys-libc"); | ||
| 3 | |||
| 4 | const nfs = @import("./nfs.zig"); | ||
| 5 | const File = nfs.File; | ||
| 6 | const Dir = @This(); | ||
| 7 | |||
| 8 | fd: nfs.Handle, | ||
| 9 | |||
| 10 | pub fn close(self: Dir) void { | ||
| 11 | sys_libc.close(self.fd) catch {}; | ||
| 12 | } | ||
| 13 | |||
| 14 | pub fn openFile(self: Dir, sub_path: [:0]const u8, flags: OpenFileFlags) !File { | ||
| 15 | _ = flags; | ||
| 16 | return .{ .fd = @enumFromInt(try sys_libc.openat(@intFromEnum(self.fd), sub_path.ptr, sys_libc.O.RDONLY)) }; | ||
| 17 | } | ||
| 18 | |||
| 19 | pub const OpenFileFlags = packed struct { | ||
| 20 | // | ||
| 21 | }; | ||
File.zig created+39| ... | @@ -0,0 +1,39 @@ | ||
| 1 | const std = @import("std"); | ||
| 2 | const builtin = @import("builtin"); | ||
| 3 | const sys_libc = @import("sys-libc"); | ||
| 4 | const nio = @import("nio"); | ||
| 5 | const errno = @import("errno"); | ||
| 6 | |||
| 7 | const nfs = @import("./nfs.zig"); | ||
| 8 | const Dir = nfs.Dir; | ||
| 9 | const File = @This(); | ||
| 10 | |||
| 11 | fd: nfs.Handle, | ||
| 12 | |||
| 13 | pub fn close(self: File) void { | ||
| 14 | sys_libc.close(@intFromEnum(self.fd)) catch {}; | ||
| 15 | } | ||
| 16 | |||
| 17 | pub const ReadError = switch (builtin.target.os.tag) { | ||
| 18 | .linux, | ||
| 19 | => errno.Error, | ||
| 20 | else => @compileError("TODO"), | ||
| 21 | }; | ||
| 22 | pub usingnamespace nio.Readable(@This()); | ||
| 23 | pub fn read(self: File, buffer: []u8) ReadError!usize { | ||
| 24 | return sys_libc.read(@intFromEnum(self.fd), buffer); | ||
| 25 | } | ||
| 26 | |||
| 27 | pub fn anyReadable(self: File) nio.AnyReadable { | ||
| 28 | const S = struct { | ||
| 29 | fn foo(s: *anyopaque, buffer: []u8) anyerror!usize { | ||
| 30 | const fd: nfs.Handle = @enumFromInt(@intFromPtr(s)); | ||
| 31 | const f: File = .{ .fd = fd }; | ||
| 32 | return f.read(buffer); | ||
| 33 | } | ||
| 34 | }; | ||
| 35 | return .{ | ||
| 36 | .readFn = S.foo, | ||
| 37 | .state = @ptrFromInt(@as(usize, @bitCast(@as(isize, @intCast(@intFromEnum(self.fd)))))), | ||
| 38 | }; | ||
| 39 | } | ||
README.md+2| ... | @@ -7,3 +7,5 @@ | ... | @@ -7,3 +7,5 @@ |
| 7 | [](https://github.com/nektro/zigmod) | 7 | [](https://github.com/nektro/zigmod) |
| 8 | 8 | ||
| 9 | Nektro's Filesystem, an alternative to `std.fs`. | 9 | Nektro's Filesystem, an alternative to `std.fs`. |
| 10 | |||
| 11 | Some code ported from Zig 0.14.1, via MIT Copyright (c) Zig contributors. New code MPL-2.0. |
licenses.txt+10| ... | @@ -1,3 +1,13 @@ | ... | @@ -1,3 +1,13 @@ |
| 1 | MPL-2.0: | 1 | MPL-2.0: |
| 2 | = https://spdx.org/licenses/MPL-2.0 | 2 | = https://spdx.org/licenses/MPL-2.0 |
| 3 | - This | 3 | - This |
| 4 | - git https://github.com/nektro/zig-nio | ||
| 5 | |||
| 6 | MIT: | ||
| 7 | = https://spdx.org/licenses/MIT | ||
| 8 | - git https://github.com/nektro/zig-errno | ||
| 9 | - git https://github.com/nektro/zig-libc | ||
| 10 | - git https://github.com/nektro/zig-sys-libc | ||
| 11 | |||
| 12 | Unspecified: | ||
| 13 | - system_lib c |
nfs.zig+15| ... | @@ -1 +1,16 @@ | ... | @@ -1 +1,16 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const builtin = @import("builtin"); | ||
| 3 | const sys_libc = @import("sys-libc"); | ||
| 4 | |||
| 5 | pub const Dir = @import("./Dir.zig"); | ||
| 6 | pub const File = @import("./File.zig"); | ||
| 7 | |||
| 8 | pub const Handle = switch (builtin.target.os.tag) { | ||
| 9 | .linux, | ||
| 10 | => enum(c_int) { _ }, | ||
| 11 | else => @compileError("TODO"), | ||
| 12 | }; | ||
| 13 | |||
| 14 | pub fn cwd() Dir { | ||
| 15 | return .{ .fd = @enumFromInt(sys_libc.AT.FDCWD) }; | ||
| 16 | } |
zigmod.yml+3| ... | @@ -6,3 +6,6 @@ description: Nektro's Filesystem, an alternative to std.fs | ... | @@ -6,3 +6,6 @@ description: Nektro's Filesystem, an alternative to std.fs |
| 6 | min_zig_version: 0.14.0 | 6 | min_zig_version: 0.14.0 |
| 7 | min_zigmod_version: r96 | 7 | min_zigmod_version: r96 |
| 8 | dependencies: | 8 | dependencies: |
| 9 | - src: git https://github.com/nektro/zig-sys-libc | ||
| 10 | - src: git https://github.com/nektro/zig-nio | ||
| 11 | - src: git https://github.com/nektro/zig-errno |