authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-08-24 22:18:51 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-08-24 22:18:51 -07:00
log59ffaf7611ec0b535d172f8faacb5a769667bb02
treef7dbabc948784cf4db083e885cd8033e965aa806
parentbd1528faf2504118937f962da93c4007a229e91c

add File.stat


3 files changed, 45 insertions(+), 0 deletions(-)

File.zig+43
......@@ -3,6 +3,7 @@ const builtin = @import("builtin");
33const sys_libc = @import("sys-libc");
44const nio = @import("nio");
55const errno = @import("errno");
6const time = @import("time");
67
78const nfs = @import("./nfs.zig");
89const Dir = nfs.Dir;
......@@ -37,3 +38,45 @@ pub fn anyReadable(self: File) nio.AnyReadable {
3738 .state = @ptrFromInt(@as(usize, @bitCast(@as(isize, @intCast(@intFromEnum(self.fd)))))),
3839 };
3940}
41
42pub fn stat(self: File) !Stat {
43 const st = try sys_libc.fstat(@intFromEnum(self.fd));
44 return .{
45 .inode = st.ino,
46 .size = @bitCast(st.size),
47 .mode = st.mode,
48 .kind = {},
49 .atime = @as(i128, st.atim.sec) * time.ns_per_s + st.atim.nsec,
50 .mtime = @as(i128, st.mtim.sec) * time.ns_per_s + st.mtim.nsec,
51 .ctime = @as(i128, st.ctim.sec) * time.ns_per_s + st.ctim.nsec,
52 };
53}
54
55pub const Stat = struct {
56 inode: INode,
57 size: u64,
58 mode: Mode,
59 kind: void, //Kind,
60 /// Last access time in nanoseconds, relative to UTC 1970-01-01.
61 atime: i128,
62 /// Last modification time in nanoseconds, relative to UTC 1970-01-01.
63 mtime: i128,
64 /// Last status/metadata change time in nanoseconds, relative to UTC 1970-01-01.
65 ctime: i128,
66};
67
68pub const INode = switch (builtin.target.os.tag) {
69 .linux,
70 => sys_libc.ino_t,
71 else => |v| @compileError("TODO: " ++ @tagName(v)),
72};
73
74pub const Mode = switch (builtin.target.os.tag) {
75 .linux,
76 => sys_libc.mode_t,
77 else => |v| @compileError("TODO: " ++ @tagName(v)),
78};
79
80pub const Kind = enum {
81 //
82};
licenses.txt+1
......@@ -8,6 +8,7 @@ MIT:
88- git https://github.com/nektro/zig-errno
99- git https://github.com/nektro/zig-libc
1010- git https://github.com/nektro/zig-sys-libc
11- git https://github.com/nektro/zig-time
1112
1213Unspecified:
1314- system_lib c
zigmod.yml+1
......@@ -9,3 +9,4 @@ dependencies:
99 - src: git https://github.com/nektro/zig-sys-libc
1010 - src: git https://github.com/nektro/zig-nio
1111 - src: git https://github.com/nektro/zig-errno
12 - src: git https://github.com/nektro/zig-time