From 59ffaf7611ec0b535d172f8faacb5a769667bb02 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sun, 24 Aug 2025 22:18:51 -0700 Subject: [PATCH] add File.stat --- File.zig | 43 +++++++++++++++++++++++++++++++++++++++++++ licenses.txt | 1 + zigmod.yml | 1 + 3 files changed, 45 insertions(+) diff --git a/File.zig b/File.zig index a1d6235508020f784bfd9556e10f543f1bd00d4c..f649db5f887c64b60a61b2ff786729b49035321b 100644 --- a/File.zig +++ b/File.zig @@ -3,6 +3,7 @@ const builtin = @import("builtin"); const sys_libc = @import("sys-libc"); const nio = @import("nio"); const errno = @import("errno"); +const time = @import("time"); const nfs = @import("./nfs.zig"); const Dir = nfs.Dir; @@ -37,3 +38,45 @@ pub fn anyReadable(self: File) nio.AnyReadable { .state = @ptrFromInt(@as(usize, @bitCast(@as(isize, @intCast(@intFromEnum(self.fd)))))), }; } + +pub fn stat(self: File) !Stat { + const st = try sys_libc.fstat(@intFromEnum(self.fd)); + return .{ + .inode = st.ino, + .size = @bitCast(st.size), + .mode = st.mode, + .kind = {}, + .atime = @as(i128, st.atim.sec) * time.ns_per_s + st.atim.nsec, + .mtime = @as(i128, st.mtim.sec) * time.ns_per_s + st.mtim.nsec, + .ctime = @as(i128, st.ctim.sec) * time.ns_per_s + st.ctim.nsec, + }; +} + +pub const Stat = struct { + inode: INode, + size: u64, + mode: Mode, + kind: void, //Kind, + /// Last access time in nanoseconds, relative to UTC 1970-01-01. + atime: i128, + /// Last modification time in nanoseconds, relative to UTC 1970-01-01. + mtime: i128, + /// Last status/metadata change time in nanoseconds, relative to UTC 1970-01-01. + ctime: i128, +}; + +pub const INode = switch (builtin.target.os.tag) { + .linux, + => sys_libc.ino_t, + else => |v| @compileError("TODO: " ++ @tagName(v)), +}; + +pub const Mode = switch (builtin.target.os.tag) { + .linux, + => sys_libc.mode_t, + else => |v| @compileError("TODO: " ++ @tagName(v)), +}; + +pub const Kind = enum { + // +}; diff --git a/licenses.txt b/licenses.txt index e1abb7bfd5f306ef3813567449b7e77d5f5cc34b..71a9eadd569cd3890927d8358ff80fd62fb9a4d9 100644 --- a/licenses.txt +++ b/licenses.txt @@ -8,6 +8,7 @@ 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-time Unspecified: - system_lib c diff --git a/zigmod.yml b/zigmod.yml index 3fc46646c3eb4d4d7d976413794f670fa4c13a42..99e36bead65cf54a2f02445ae0140d8454e0b060 100644 --- a/zigmod.yml +++ b/zigmod.yml @@ -9,3 +9,4 @@ 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 + - src: git https://github.com/nektro/zig-time -- 2.54.0