From 1afcbaf7bb4ba87b9c5aefdf25e27c4cfbe5f43e Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Wed, 25 Nov 2026 02:45:32 +0000 Subject: [PATCH 1/3] docs: list GzipStep and gzipInPlaceAtStep Add both to the logs section of the API reference, and a note on reading the step to the compress-and-mark-logs how-to. --- changelog.d/+gzip-step-docs.doc.md | 1 + docs/orgmode/howto/compress-and-mark-logs.org | 15 +++++++++++++++ docs/orgmode/reference/api.org | 10 ++++++---- 3 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 changelog.d/+gzip-step-docs.doc.md diff --git a/changelog.d/+gzip-step-docs.doc.md b/changelog.d/+gzip-step-docs.doc.md new file mode 100644 index 0000000..e8347d3 --- /dev/null +++ b/changelog.d/+gzip-step-docs.doc.md @@ -0,0 +1 @@ +The API reference and the compress-and-mark-logs how-to list `logs.GzipStep` and `logs.gzipInPlaceAtStep`, and the how-to shows how to read the step an error came from. diff --git a/docs/orgmode/howto/compress-and-mark-logs.org b/docs/orgmode/howto/compress-and-mark-logs.org index 84d7672..3fb48af 100644 --- a/docs/orgmode/howto/compress-and-mark-logs.org +++ b/docs/orgmode/howto/compress-and-mark-logs.org @@ -92,9 +92,24 @@ The archives keep the logs' mode, 0600, and their modification time, 10:15. | =error.SymLink= | the name is a symbolic link | compress the file it points to, in its own directory | | =error.InvalidArgument= | the name is a directory, a named pipe or a device | pass the names of regular files only | +The errors share one set, so an error alone does not say whether the log was compressed. +[[zig:zig_common.logs.gzipInPlaceAtStep]] is =gzipInPlace= that also sets a [[zig:zig_common.logs.GzipStep]] to the step the error came from: + +#+begin_src zig +var failed_at: common.logs.GzipStep = undefined; +common.logs.gzipInPlaceAtStep(gpa, init.io, dir, name, &failed_at) catch |err| switch (failed_at) { + .archive => return err, + .directory_sync => {}, +}; +#+end_src + +With =archive=, the log is where it was, and the call tries to remove any =NAME.gz= it made. +With =directory_sync=, =NAME.gz= is in place and the log is gone, and only whether that survives a crash is in doubt: count the log as compressed and rename it in the list. + ** Related - [[zig:zig_common.logs.GzipError]]: the errors =gzipInPlace= returns. +- [[zig:zig_common.logs.GzipStep]]: the steps =gzipInPlaceAtStep= reports. - [[zig:zig_common.logs.licensed_list]]: the list's name and format. - [[https://github.com/stackhands/eb-keep-logs][eb-keep-logs]], which writes the list and rotates log trees. - [[https://www.gnu.org/software/gzip/manual/gzip.html][The gzip manual]], for the format and the checks. diff --git a/docs/orgmode/reference/api.org b/docs/orgmode/reference/api.org index 836ee1e..4f6e9b7 100644 --- a/docs/orgmode/reference/api.org +++ b/docs/orgmode/reference/api.org @@ -84,11 +84,13 @@ Error set: [[zig:zig_common.proc.Error]]. *** =zig_common.logs= Error set: [[zig:zig_common.logs.GzipError]]. +Step set: [[zig:zig_common.logs.GzipStep]], which =gzipInPlaceAtStep= sets to the step an error came from. -| Function | Error set | -|----------------------------------------+-------------------------------------| -| [[zig:zig_common.logs.gzipInPlace]] | [[zig:zig_common.logs.GzipError]] | -| [[zig:zig_common.logs.renameInList]] | =Allocator.Error= | +| Function | Error set | +|--------------------------------------------+-------------------------------------| +| [[zig:zig_common.logs.gzipInPlace]] | [[zig:zig_common.logs.GzipError]] | +| [[zig:zig_common.logs.gzipInPlaceAtStep]] | [[zig:zig_common.logs.GzipError]] | +| [[zig:zig_common.logs.renameInList]] | =Allocator.Error= | *** =zig_common.cli= From 48ca228e17e0dce6a7a08072fe2c4e1ad0f88692 Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Wed, 25 Nov 2026 02:49:23 +0000 Subject: [PATCH 2/3] docs: state what an archive error leaves in logs.GzipStep If opening the original fails, the archive step changes nothing. After an earlier run compressed it, `name` stays gone. Any `name.gz` from an earlier run stays as it was. Once the original is open, the step keeps it. It leaves `name.gz` behind only when that file existed before, or when it failed to remove the original and then failed to remove the archive. The `directory_sync` step follows one rename and one unlink, not two renames. --- changelog.d/+gzip-step-comments.doc.md | 1 + src/logs.zig | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 changelog.d/+gzip-step-comments.doc.md diff --git a/changelog.d/+gzip-step-comments.doc.md b/changelog.d/+gzip-step-comments.doc.md new file mode 100644 index 0000000..c4e7bde --- /dev/null +++ b/changelog.d/+gzip-step-comments.doc.md @@ -0,0 +1 @@ +`logs.GzipStep` says that an `archive` error from opening `name` changes nothing, so a `name` that was already absent stays absent and any `name.gz` already there is left alone, that after a later `archive` error the original is still there and `name.gz` is absent unless it existed before or both the removal of the original and the removal of the archive failed, and that `directory_sync` follows one rename and one unlink. diff --git a/src/logs.zig b/src/logs.zig index a8ce460..abebfa2 100644 --- a/src/logs.zig +++ b/src/logs.zig @@ -37,11 +37,18 @@ pub fn gzipInPlace(gpa: Allocator, io: Io, dir: fsx.fd_t, name: []const u8) Gzip /// one set, so the error alone cannot say whether the log was compressed. pub const GzipStep = enum { /// Up to renaming the archive into place and removing the original. - /// The original is where it was, and this call left no `name.gz`. + /// When opening `name` fails, the call has changed nothing: a `name` + /// that was already absent, as after an earlier run compressed it, + /// stays absent, and any `name.gz` already there is left alone. Once + /// `name` is open, the original is still there, and `name.gz` is absent + /// unless it was there before the call, or removing the original failed + /// and so did the removal of the archive that follows; then both are + /// present. archive, - /// Opening the archive's directory and syncing it, after both renames. - /// `name.gz` is in place and the original is gone; only whether that - /// survives a crash is in doubt. + /// Opening the archive's directory and syncing it, after the rename of + /// the archive and the removal of the original. `name.gz` is in place + /// and the original is gone; only whether that survives a crash is in + /// doubt. directory_sync, }; From af16d5830bda814f50cc9b3fd4263587a5fc74ba Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Wed, 25 Nov 2026 02:50:39 +0000 Subject: [PATCH 3/3] test: share the scratch directory setup in test_support test_support.ScratchDir makes .zig-cache/zig-common-TAG-PID empty, removing one a killed run left under the same pid, and fails when it cannot create it. The logs tests and the fsx moveAcross fixture use it in place of their own copies, so writeLog's O_EXCL create cannot hit a stale file. --- changelog.d/+scratch-dir-test-helper.misc.md | 1 + src/fsx.zig | 16 ++-- src/logs.zig | 89 +++++--------------- src/test_support.zig | 48 +++++++++++ 4 files changed, 75 insertions(+), 79 deletions(-) create mode 100644 changelog.d/+scratch-dir-test-helper.misc.md diff --git a/changelog.d/+scratch-dir-test-helper.misc.md b/changelog.d/+scratch-dir-test-helper.misc.md new file mode 100644 index 0000000..71affa6 --- /dev/null +++ b/changelog.d/+scratch-dir-test-helper.misc.md @@ -0,0 +1 @@ +Move the scratch directory the `logs` tests and the `fsx` `moveAcross` fixture make into `test_support.ScratchDir`, which removes a directory left by an earlier run of the same pid before it creates its own. diff --git a/src/fsx.zig b/src/fsx.zig index ba38607..9982d22 100644 --- a/src/fsx.zig +++ b/src/fsx.zig @@ -4,6 +4,7 @@ const std = @import("std"); const linux = std.os.linux; const sys = @import("sys.zig"); const atomic = @import("atomic.zig"); +const test_support = @import("test_support.zig"); const pytext = @import("pytext.zig"); const Allocator = std.mem.Allocator; @@ -1714,23 +1715,16 @@ const MoveTrace = struct { /// Scratch directory under `.zig-cache` for the moveAcross trace tests. const MoveScratch = struct { gpa: Allocator, - base: []u8, + scratch: test_support.ScratchDir, d: fd_t, fn init(gpa: Allocator, tag: []const u8) !MoveScratch { - var cwd_buf: [sys.path_max]u8 = undefined; - const here = try realpath(cwd, ".", &cwd_buf); - const base = try std.fmt.allocPrint(gpa, "{s}/.zig-cache/zig-common-{s}-{d}", .{ here, tag, linux.getpid() }); - errdefer gpa.free(base); - var zb: [sys.path_max:0]u8 = undefined; - _ = linux.mkdirat(cwd, (try sys.z(&zb, base)).ptr, 0o700); - return .{ .gpa = gpa, .base = base, .d = try openDir(cwd, base, false) }; + const scratch = try test_support.ScratchDir.init(gpa, tag); + return .{ .gpa = gpa, .scratch = scratch, .d = scratch.fd }; } fn deinit(self: MoveScratch) void { - deleteTree(self.gpa, cwd, self.base) catch {}; - sys.close(self.d); - self.gpa.free(self.base); + self.scratch.deinit(); } fn mkdir(self: MoveScratch, name: []const u8) !Stat { diff --git a/src/logs.zig b/src/logs.zig index abebfa2..2244a66 100644 --- a/src/logs.zig +++ b/src/logs.zig @@ -4,6 +4,7 @@ const std = @import("std"); const linux = std.os.linux; const sys = @import("sys.zig"); const fsx = @import("fsx.zig"); +const test_support = @import("test_support.zig"); const Allocator = std.mem.Allocator; const Io = std.Io; @@ -305,17 +306,10 @@ test "gunzip reads over trailing zero padding" { test "gzip does not publish a partial archive under the final name" { const gpa = std.testing.allocator; - var cwd_buf: [sys.path_max]u8 = undefined; - const here = try fsx.realpath(fsx.cwd, ".", &cwd_buf); - const base = try std.fmt.allocPrint(gpa, "{s}/.zig-cache/zig-common-gzip-{d}", .{ here, linux.getpid() }); - defer gpa.free(base); - var zb: [sys.path_max:0]u8 = undefined; - _ = linux.mkdirat(fsx.cwd, (try sys.z(&zb, base)).ptr, 0o700); - const dir = try fsx.openDir(fsx.cwd, base, false); - defer { - fsx.deleteTree(gpa, fsx.cwd, base) catch {}; - sys.close(dir); - } + const scratch = try test_support.ScratchDir.init(gpa, "gzip"); + defer scratch.deinit(); + const base = scratch.path; + const dir = scratch.fd; const src = try sys.openat(dir, "a.log", .{ .ACCMODE = .WRONLY, .CREAT = true }, 0o644); const ur = try sys.openat(fsx.cwd, "/dev/urandom", .{}, 0); defer sys.close(ur); @@ -373,17 +367,9 @@ test "renameInList" { test "gzipInPlace refuses missing files, symlinks, directories, hard links, and existing archives" { const gpa = std.testing.allocator; - var cwd_buf: [sys.path_max]u8 = undefined; - const here = try fsx.realpath(fsx.cwd, ".", &cwd_buf); - var pb: [sys.path_max]u8 = undefined; - const base = try std.mem.print(&pb, "{s}/.zig-cache/zig-common-logs-err-{d}", .{ here, linux.getpid() }); - var zb: [sys.path_max:0]u8 = undefined; - _ = linux.mkdirat(fsx.cwd, (try sys.z(&zb, base)).ptr, 0o700); - const dir = try fsx.openDir(fsx.cwd, base, false); - defer { - fsx.deleteTree(gpa, fsx.cwd, base) catch {}; - sys.close(dir); - } + const scratch = try test_support.ScratchDir.init(gpa, "logs-err"); + defer scratch.deinit(); + const dir = scratch.fd; const io = std.Io.Threaded.global_single_threaded.io(); try std.testing.expectError(error.NotFound, gzipInPlace(gpa, io, dir, "missing.log")); @@ -413,17 +399,10 @@ test "gzipInPlace refuses missing files, symlinks, directories, hard links, and test "gzipInPlace handles nested paths like gzip -9 sub/file" { const gpa = std.testing.allocator; - var cwd_buf: [sys.path_max]u8 = undefined; - const here = try fsx.realpath(fsx.cwd, ".", &cwd_buf); - var pb: [sys.path_max]u8 = undefined; - const base = try std.mem.print(&pb, "{s}/.zig-cache/zig-common-logs-sub-{d}", .{ here, linux.getpid() }); + const scratch = try test_support.ScratchDir.init(gpa, "logs-sub"); + defer scratch.deinit(); + const dir = scratch.fd; var zb: [sys.path_max:0]u8 = undefined; - _ = linux.mkdirat(fsx.cwd, (try sys.z(&zb, base)).ptr, 0o700); - const dir = try fsx.openDir(fsx.cwd, base, false); - defer { - fsx.deleteTree(gpa, fsx.cwd, base) catch {}; - sys.close(dir); - } _ = linux.mkdirat(dir, (try sys.z(&zb, "sub")).ptr, 0o700); const src = try sys.openat(dir, "sub/nested.log", .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, 0o644); try sys.writeAll(src, "nested\n"); @@ -437,17 +416,9 @@ test "gzipInPlace handles nested paths like gzip -9 sub/file" { test "gzipInPlace compresses a name its own temporary file alone would overflow" { const gpa = std.testing.allocator; - var cwd_buf: [sys.path_max]u8 = undefined; - const here = try fsx.realpath(fsx.cwd, ".", &cwd_buf); - var pb: [sys.path_max]u8 = undefined; - const base = try std.mem.print(&pb, "{s}/.zig-cache/zig-common-logs-long-{d}", .{ here, linux.getpid() }); - var zb: [sys.path_max:0]u8 = undefined; - _ = linux.mkdirat(fsx.cwd, (try sys.z(&zb, base)).ptr, 0o700); - const dir = try fsx.openDir(fsx.cwd, base, false); - defer { - fsx.deleteTree(gpa, fsx.cwd, base) catch {}; - sys.close(dir); - } + const scratch = try test_support.ScratchDir.init(gpa, "logs-long"); + defer scratch.deinit(); + const dir = scratch.fd; // 240 bytes: short enough that `gzip -9` on the real reference // compressor takes it (its own overhead is the 3 bytes of `.gz`), long @@ -469,16 +440,6 @@ test "gzipInPlace compresses a name its own temporary file alone would overflow" try std.testing.expect((fsx.statAt(dir, gz_name, false) catch null) != null); } -/// A fresh directory under `.zig-cache` for one test; `buf` holds its path. -fn testDir(buf: *[sys.path_max]u8, tag: []const u8) ![]const u8 { - var cwd_buf: [sys.path_max]u8 = undefined; - const here = try fsx.realpath(fsx.cwd, ".", &cwd_buf); - const base = try std.mem.print(buf, "{s}/.zig-cache/zig-common-logs-{s}-{d}", .{ here, tag, linux.getpid() }); - var zb: [sys.path_max:0]u8 = undefined; - _ = linux.mkdirat(fsx.cwd, (try sys.z(&zb, base)).ptr, 0o700); - return base; -} - fn writeLog(dir: fsx.fd_t, name: []const u8) !void { const fd = try sys.openat(dir, name, .{ .ACCMODE = .WRONLY, .CREAT = true, .EXCL = true }, 0o644); defer sys.close(fd); @@ -487,13 +448,9 @@ fn writeLog(dir: fsx.fd_t, name: []const u8) !void { test "gzipInPlaceAtStep reports archive when the log is left as it was" { const gpa = std.testing.allocator; - var pb: [sys.path_max]u8 = undefined; - const base = try testDir(&pb, "step-archive"); - const dir = try fsx.openDir(fsx.cwd, base, false); - defer { - fsx.deleteTree(gpa, fsx.cwd, base) catch {}; - sys.close(dir); - } + const scratch = try test_support.ScratchDir.init(gpa, "logs-step-archive"); + defer scratch.deinit(); + const dir = scratch.fd; const io = std.Io.Threaded.global_single_threaded.io(); try writeLog(dir, "kept.log"); try writeLog(dir, "kept.log.gz"); @@ -511,13 +468,10 @@ test "gzipInPlaceAtStep reports directory_sync when only the directory sync fail const Child = struct { fn run() u8 { const gpa = std.heap.page_allocator; - var pb: [sys.path_max]u8 = undefined; - const base = testDir(&pb, "step-sync") catch return 20; - const dir = fsx.openDir(fsx.cwd, base, false) catch return 21; - defer { - fsx.deleteTree(gpa, fsx.cwd, base) catch {}; - sys.close(dir); - } + const scratch = test_support.ScratchDir.init(gpa, "logs-step-sync") catch return 20; + defer scratch.deinit(); + const base = scratch.path; + const dir = scratch.fd; const io = std.Io.Threaded.global_single_threaded.io(); writeLog(dir, "a.log") catch return 22; @@ -555,6 +509,5 @@ test "gzipInPlaceAtStep reports directory_sync when only the directory sync fail return 0; } }; - const test_support = @import("test_support.zig"); try test_support.expectChildPasses(Child.run); } diff --git a/src/test_support.zig b/src/test_support.zig index 81cc1a7..5e12e90 100644 --- a/src/test_support.zig +++ b/src/test_support.zig @@ -6,6 +6,34 @@ const sys = @import("sys.zig"); const fsx = @import("fsx.zig"); const proc = @import("proc.zig"); +/// An empty directory `.zig-cache/zig-common-TAG-PID` for one test. A +/// directory a killed run of the same pid left behind is removed first, so +/// the test starts from nothing. `path` is absolute; `fd` is open on it. +pub const ScratchDir = struct { + gpa: std.mem.Allocator, + path: []u8, + fd: fsx.fd_t, + + pub fn init(gpa: std.mem.Allocator, tag: []const u8) !ScratchDir { + var cwd_buf: [sys.path_max]u8 = undefined; + const here = try fsx.realpath(fsx.cwd, ".", &cwd_buf); + const path = try std.fmt.allocPrint(gpa, "{s}/.zig-cache/zig-common-{s}-{d}", .{ here, tag, linux.getpid() }); + errdefer gpa.free(path); + fsx.deleteTree(gpa, fsx.cwd, path) catch {}; + var zb: [sys.path_max:0]u8 = undefined; + _ = try sys.call(linux.mkdirat, .{ fsx.cwd, (try sys.z(&zb, path)).ptr, @as(linux.mode_t, 0o700) }); + errdefer sys.unlinkat(fsx.cwd, path, linux.AT.REMOVEDIR) catch {}; + const fd = try fsx.openDir(fsx.cwd, path, false); + return .{ .gpa = gpa, .path = path, .fd = fd }; + } + + pub fn deinit(self: ScratchDir) void { + fsx.deleteTree(self.gpa, fsx.cwd, self.path) catch {}; + sys.close(self.fd); + self.gpa.free(self.path); + } +}; + /// Open descriptors of this process, not counting the directory descriptor /// used to read `/proc/self/fd`. Opening that directory or reading an entry /// returns the error, so a failed read cannot look like a smaller count. @@ -35,3 +63,23 @@ pub fn expectChildPasses(comptime body: fn () u8) !void { while (linux.errno(linux.wait4(@intCast(pid), &status, 0, null)) == .INTR) {} try std.testing.expectEqual(proc.Term{ .exited = 0 }, childTerm(@bitCast(status))); } + +test "ScratchDir starts empty when a directory of the same name was left behind" { + const gpa = std.testing.allocator; + { + const stale = try ScratchDir.init(gpa, "scratch-stale"); + errdefer stale.deinit(); + const file = try sys.openat(stale.fd, "a.log", .{ .ACCMODE = .WRONLY, .CREAT = true, .EXCL = true }, 0o644); + sys.close(file); + var zb: [sys.path_max:0]u8 = undefined; + _ = try sys.call(linux.mkdirat, .{ stale.fd, (try sys.z(&zb, "sub")).ptr, @as(linux.mode_t, 0o700) }); + sys.close(stale.fd); + gpa.free(stale.path); + } + + const fresh = try ScratchDir.init(gpa, "scratch-stale"); + defer fresh.deinit(); + const again = try sys.openat(fresh.fd, "a.log", .{ .ACCMODE = .WRONLY, .CREAT = true, .EXCL = true }, 0o644); + sys.close(again); + try std.testing.expectError(error.NotFound, fsx.statAt(fresh.fd, "sub", false)); +}