diff options
Diffstat (limited to '')
| -rw-r--r-- | src/fs.zig | 21 |
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); |