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-comments.doc.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions changelog.d/+gzip-step-docs.doc.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions changelog.d/+scratch-dir-test-helper.misc.md
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 15 additions & 0 deletions docs/orgmode/howto/compress-and-mark-logs.org
Original file line number Diff line number Diff line change
Expand Up @@ -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.
10 changes: 6 additions & 4 deletions docs/orgmode/reference/api.org
Original file line number Diff line number Diff line change
Expand Up @@ -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=

Expand Down
16 changes: 5 additions & 11 deletions src/fsx.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
104 changes: 32 additions & 72 deletions src/logs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,11 +38,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,
};

Expand Down Expand Up @@ -298,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);
Expand Down Expand Up @@ -366,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"));
Expand Down Expand Up @@ -406,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");
Expand All @@ -430,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
Expand All @@ -462,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);
Expand All @@ -480,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");
Expand All @@ -504,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;

Expand Down Expand Up @@ -548,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);
}
48 changes: 48 additions & 0 deletions src/test_support.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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));
}
Loading