diff options
| author | Robert Günzler <r@gnzler.io> | 2025-10-03 19:01:17 +0200 |
|---|---|---|
| committer | Robert Günzler <r@gnzler.io> | 2025-10-04 18:41:56 +0200 |
| commit | 35edf2a5a15795c0ea109cae6bca69eef796bff6 (patch) | |
| tree | 77e20b5f12b72580d16bca8ef3c280d07c8a7603 /src | |
| parent | 17cec3c7f6cb46d8d80c2364eba8296f6dd01ef3 (diff) | |
| download | site-35edf2a5a15795c0ea109cae6bca69eef796bff6.tar.gz | |
use higher level stdlib APIs
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to '')
| -rw-r--r-- | src/exe.zig | 2 | ||||
| -rw-r--r-- | src/fs.zig | 21 | ||||
| -rw-r--r-- | src/gen.zig | 2 |
3 files changed, 12 insertions, 13 deletions
diff --git a/src/exe.zig b/src/exe.zig index 06e7a4d..5776538 100644 --- a/src/exe.zig +++ b/src/exe.zig @@ -46,7 +46,7 @@ fn pandocWorker(ctx: *Context, page: []const u8) !void { ); // get mtime of the source document - const mtime = try fs.getMtime(ctx.allocator(), source_path); + const mtime = try fs.getMtime(source_path); const mtime_str = try fs.formatMtime(ctx.allocator(), mtime, "%s"); defer ctx.allocator().free(mtime_str); 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); diff --git a/src/gen.zig b/src/gen.zig index 0504d39..a02503b 100644 --- a/src/gen.zig +++ b/src/gen.zig @@ -109,7 +109,7 @@ fn genSite(ctx: *Context) !void { // build a single html file fn build(allocator: std.mem.Allocator, w: std.io.AnyWriter, page_name: []const u8, srcpath: []const u8) !void { - const mtime = try fs.getMtime(allocator, srcpath); + const mtime = try fs.getMtime(srcpath); const mtime_str = try fs.formatMtime(allocator, mtime, "%F"); defer allocator.free(mtime_str); |