diff --git a/changelog.d/+gzip-step-report.feature.md b/changelog.d/+gzip-step-report.feature.md new file mode 100644 index 0000000..a98ef09 --- /dev/null +++ b/changelog.d/+gzip-step-report.feature.md @@ -0,0 +1 @@ +`logs.gzipInPlaceAtStep` compresses a log as `logs.gzipInPlace` does, and on an error sets a `logs.GzipStep` that says whether the error came before the archive was in place (`archive`) or from the sync of its directory afterwards (`directory_sync`). `gzipInPlace` keeps its signature and calls it. diff --git a/src/logs.zig b/src/logs.zig index 4183275..a8ce460 100644 --- a/src/logs.zig +++ b/src/logs.zig @@ -29,6 +29,28 @@ pub const GzipError = sys.Error || Allocator.Error || error{WriteFailed}; /// A file with other hard links, a symbolic link, a non-regular file and an /// existing `name.gz` are refused, as gzip refuses them. pub fn gzipInPlace(gpa: Allocator, io: Io, dir: fsx.fd_t, name: []const u8) GzipError!void { + var failed_at: GzipStep = undefined; + return gzipInPlaceAtStep(gpa, io, dir, name, &failed_at); +} + +/// The step of `gzipInPlaceAtStep` an error came from. Every error shares +/// 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`. + 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. + directory_sync, +}; + +/// `gzipInPlace`, which also sets `failed_at` to the step an error came +/// from. It reads `.archive` until the archive is in place and the original +/// removed, and `.directory_sync` from right before the final sync of the +/// archive's directory. +pub fn gzipInPlaceAtStep(gpa: Allocator, io: Io, dir: fsx.fd_t, name: []const u8, failed_at: *GzipStep) GzipError!void { + failed_at.* = .archive; const src = try fsx.openRegular(dir, name, false); defer sys.close(src.fd); if (src.stat.nlink > 1) return error.TooManyLinks; @@ -127,6 +149,7 @@ pub fn gzipInPlace(gpa: Allocator, io: Io, dir: fsx.fd_t, name: []const u8) Gzip return err; }; done = true; + failed_at.* = .directory_sync; const parent_fd = try fsx.openDir(dir, gz_parent, true); defer sys.close(parent_fd); try sys.fsync(parent_fd); @@ -438,3 +461,93 @@ test "gzipInPlace compresses a name its own temporary file alone would overflow" const gz_name = try std.fmt.bufPrint(&gz_buf, "{s}.gz", .{name}); 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); + try sys.writeAll(fd, "a log\n"); +} + +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 io = std.Io.Threaded.global_single_threaded.io(); + try writeLog(dir, "kept.log"); + try writeLog(dir, "kept.log.gz"); + var failed_at: GzipStep = .directory_sync; + try std.testing.expectError(error.Exists, gzipInPlaceAtStep(gpa, io, dir, "kept.log", &failed_at)); + try std.testing.expectEqual(GzipStep.archive, failed_at); + try std.testing.expect((try fsx.statAt(dir, "kept.log", false)).isReg()); + + failed_at = .directory_sync; + try std.testing.expectError(error.NotFound, gzipInPlaceAtStep(gpa, io, dir, "missing.log", &failed_at)); + try std.testing.expectEqual(GzipStep.archive, failed_at); +} + +test "gzipInPlaceAtStep reports directory_sync when only the directory sync fails" { + 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 io = std.Io.Threaded.global_single_threaded.io(); + writeLog(dir, "a.log") catch return 22; + + // The source and the temporary archive take the two lowest free + // descriptors. With the limit right above them, every step up to + // removing the original has the descriptors it needs, while the + // open for the final directory sync is the first call refused. + const first = sys.openat(fsx.cwd, "/dev/null", .{}, 0) catch return 23; + const second = sys.openat(fsx.cwd, "/dev/null", .{}, 0) catch { + sys.close(first); + return 24; + }; + sys.close(first); + sys.close(second); + var saved: linux.rlimit = undefined; + if (linux.errno(linux.prlimit(0, .NOFILE, null, &saved)) != .SUCCESS) return 25; + const tight: linux.rlimit = .{ .cur = @intCast(second + 1), .max = saved.max }; + if (linux.errno(linux.prlimit(0, .NOFILE, &tight, null)) != .SUCCESS) return 26; + + var failed_at: GzipStep = .archive; + const got = gzipInPlaceAtStep(gpa, io, dir, "a.log", &failed_at); + const got_errno = sys.last_errno; + if (linux.errno(linux.prlimit(0, .NOFILE, &saved, null)) != .SUCCESS) return 27; + if (got) |_| return 28 else |err| if (err != error.Unexpected) return 29; + if (got_errno != .MFILE) return 30; + if (failed_at != .directory_sync) return 31; + var gz_buf: [sys.path_max]u8 = undefined; + var gz_z: [sys.path_max:0]u8 = undefined; + const gz_path = std.mem.print(&gz_buf, "{s}/a.log.gz", .{base}) catch return 32; + const gz_path_z = sys.z(&gz_z, gz_path) catch return 33; + if (!archiveIntact(gz_path_z.ptr)) return 34; + if (fsx.statAt(dir, "a.log", false)) |_| return 35 else |err| { + if (err != error.NotFound) return 36; + } + return 0; + } + }; + const test_support = @import("test_support.zig"); + try test_support.expectChildPasses(Child.run); +}