about summary refs log tree commit diff
path: root/src/fs.zig
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2025-10-03 19:01:17 +0200
committerRobert Günzler <r@gnzler.io>2025-10-04 18:41:56 +0200
commit35edf2a5a15795c0ea109cae6bca69eef796bff6 (patch)
tree77e20b5f12b72580d16bca8ef3c280d07c8a7603 /src/fs.zig
parent17cec3c7f6cb46d8d80c2364eba8296f6dd01ef3 (diff)
downloadsite-35edf2a5a15795c0ea109cae6bca69eef796bff6.tar.gz
use higher level stdlib APIs
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to '')
-rw-r--r--src/fs.zig21
1 files changed, 10 insertions, 11 deletions
diff --git a/src/fs.zig b/src/fs.zig
index d29ee05..2a141d1 100644
--- a/src/fs.zig
+++ b/src/fs.zig
@@ -100,22 +100,21 @@ pub fn joinPathAndFilename(alloc: std.mem.Allocator, paths: []const []const u8,
     return try std.fs.path.join(alloc, paths2);
 }
 
-pub fn getMtime(alloc: std.mem.Allocator, path: []const u8) !std.os.linux.timespec {
-    const attr = try alloc.create(std.os.linux.Stat);
-    defer alloc.destroy(attr);
+pub fn getMtime(path: []const u8) !std.posix.timespec {
+    const fd = try std.fs.cwd().openFile(path, .{ .mode = .read_only });
+    defer fd.close();
 
-    const pathZ = try alloc.dupeZ(u8, path);
-    defer alloc.free(pathZ);
+    const meta = try fd.metadata();
+    const mtime = meta.modified();
 
-    // use stat to get mtime
-    const rc = std.os.linux.stat(pathZ, attr);
-    if (rc < 0) return error.UnknownStatError;
-
-    return attr.mtime();
+    return .{
+        .sec = std.math.cast(isize, @divFloor(mtime, std.time.ns_per_s)) orelse std.math.maxInt(isize),
+        .nsec = std.math.cast(isize, @mod(mtime, std.time.ns_per_s)) orelse std.math.maxInt(isize),
+    };
 }
 
 // use libc strftime to format time string
-pub fn formatMtime(alloc: std.mem.Allocator, mtime: std.os.linux.timespec, fmt: [*c]const u8) ![]const u8 {
+pub fn formatMtime(alloc: std.mem.Allocator, mtime: std.posix.timespec, fmt: [*c]const u8) ![]const u8 {
     const mtime_str = try alloc.alloc(u8, std.os.linux.PATH_MAX);
     var tm: c.struct_tm = undefined;
     _ = c.localtime_r(&mtime.sec, &tm);