Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/+gzip-step-report.feature.md
Original file line number Diff line number Diff line change
@@ -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.
113 changes: 113 additions & 0 deletions src/logs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Loading