about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/exe.zig71
1 files changed, 43 insertions, 28 deletions
diff --git a/src/exe.zig b/src/exe.zig
index 5776538..f0d55d4 100644
--- a/src/exe.zig
+++ b/src/exe.zig
@@ -35,39 +35,54 @@ fn pandocWorker(ctx: *Context, page: []const u8) !void {
     const target_path = try fs.replaceExtension(ctx.allocator(), source_path, "htm");
     defer ctx.allocator().free(target_path);
 
-    const f = try fs.FileSwapper.createAt(ctx.allocator(), target_path);
-    defer f.close();
-
-    try f.writer().print(
-        \\<!-- Generated file, you may edit and discard {s} :) -->
-        \\
-    ,
-        .{page},
-    );
-
     // get mtime of the source document
     const mtime = try fs.getMtime(source_path);
     const mtime_str = try fs.formatMtime(ctx.allocator(), mtime, "%s");
     defer ctx.allocator().free(mtime_str);
 
-    // make pandoc runs reproducable
-    var env_map = std.process.EnvMap.init(ctx.allocator());
-    defer env_map.deinit();
-    try env_map.put("SOURCE_DATE_EPOCH", mtime_str);
-
-    try captureStdout(ctx, &.{
-        Context.params.PANDOC,
-        "--from",
-        "markdown+wikilinks_title_after_pipe+autolink_bare_uris",
-        "--to",
-        "html5",
-        "--lua-filter",
-        // TODO: embed the preproc filter
-        Context.params.PANDOC_PREPROC,
-        "--strip-comments",
-        "--output=-",
-        source_path,
-    }, &env_map, f.writer().any());
+    const f = try fs.FileSwapper.createAt(ctx.allocator(), target_path);
+
+    // open new scope to close the file swapper when we're done writing
+    {
+        defer f.close();
+
+        try f.writer().print(
+            \\<!-- Generated file, you may edit and discard {s} :) -->
+            \\
+        ,
+            .{page},
+        );
+
+        // make pandoc runs reproducable
+        var env_map = std.process.EnvMap.init(ctx.allocator());
+        defer env_map.deinit();
+        try env_map.put("SOURCE_DATE_EPOCH", mtime_str);
+
+        try captureStdout(ctx, &.{
+            Context.params.PANDOC,
+            "--from",
+            "markdown+wikilinks_title_after_pipe+autolink_bare_uris",
+            "--to",
+            "html5",
+            "--lua-filter",
+            // TODO: embed the preproc filter
+            Context.params.PANDOC_PREPROC,
+            "--strip-comments",
+            "--output=-",
+            source_path,
+        }, &env_map, f.writer().any());
+    }
+
+    // fixup mtime of the resulting file, this has to happen after the file
+    // swapper has been closed, because it works on a tmp file which is going
+    // to be renamed (meaning mtime is changed again)
+    const fd = try std.fs.cwd().openFile(target_path, .{ .mode = .write_only });
+    defer fd.close();
+
+    try std.posix.futimens(
+        fd.handle,
+        &[2]std.os.linux.timespec{ mtime, mtime },
+    );
 }
 
 pub fn captureStdout(ctx: *Context, argv: []const []const u8, env_map: ?*std.process.EnvMap, buf: std.io.AnyWriter) !void {