diff options
| author | Robert Günzler <r@gnzler.io> | 2025-05-14 16:09:50 +0200 |
|---|---|---|
| committer | Robert Günzler <r@gnzler.io> | 2025-05-14 16:22:32 +0200 |
| commit | b6e421ccf2408f51e56d411059cd257bb10c26b3 (patch) | |
| tree | 0c40be6ee6fbc9414e0867f89e3f46aaacb67564 /src | |
| parent | b8da9008f702a13a870b912cb923ae39c7360875 (diff) | |
| download | site-b6e421ccf2408f51e56d411059cd257bb10c26b3.tar.gz | |
refactor mtime retrieval into function
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to '')
| -rw-r--r-- | src/main.zig | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/src/main.zig b/src/main.zig index ef4e592..e3bfb44 100644 --- a/src/main.zig +++ b/src/main.zig @@ -60,6 +60,23 @@ fn joinIn(allocator: std.mem.Allocator, paths: []const []const u8, filename_part return try std.fs.path.join(allocator, paths2); } +fn getMtime(allocator: std.mem.Allocator, srcpath: []const u8) ![]u8 { + const attr = try allocator.create(std.os.linux.Stat); + defer allocator.destroy(attr); + const srcpathZ = try allocator.dupeZ(u8, srcpath); + defer allocator.free(srcpathZ); + _ = std.os.linux.stat(srcpathZ, attr); + const mtime = attr.mtime(); + const mtime_str = try allocator.alloc(u8, 512); + const n = c.strftime( + mtime_str.ptr, + mtime_str.len, + "%F", + c.localtime(&mtime.sec), + ); + return try allocator.realloc(mtime_str, n); +} + fn portal(allocator: std.mem.Allocator, w: std.io.AnyWriter, refname: []const u8, insert_heading: bool) anyerror!void { const basename = try std.mem.replaceOwned(u8, allocator, refname, " ", "_"); defer allocator.free(basename); @@ -173,22 +190,8 @@ fn inject(allocator: std.mem.Allocator, w: std.io.AnyWriter, srcpath: []const u8 } fn build(allocator: std.mem.Allocator, f: std.fs.File, page_name: []const u8, srcpath: []const u8) !void { - const mtime = blk: { - const attr = try allocator.create(std.os.linux.Stat); - defer allocator.destroy(attr); - const srcpathZ = try allocator.dupeZ(u8, srcpath); - defer allocator.free(srcpathZ); - _ = std.os.linux.stat(srcpathZ, attr); - const mtime = attr.mtime(); - var mtime_str: [256]u8 = undefined; - const n = c.strftime( - &mtime_str, - mtime_str.len, - "%F", - c.localtime(&mtime.sec), - ); - break :blk mtime_str[0..n]; - }; + const mtime = try getMtime(allocator, srcpath); + defer allocator.free(mtime); // HEAD try f.writer().print( |