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
5 changes: 5 additions & 0 deletions changelog.d/+rotate-empty-unreadable-run.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
`rotate` removes an empty run directory it cannot read once the
run is past `--delete-after`. The removal used to stop when opening
that directory failed, so the run stayed. An empty directory inside
the run is removed too. A run that still contains entries is reported
and kept.
53 changes: 52 additions & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,57 @@ fn readLinkAt(dir: fsx.fd_t, name: []const u8, out: *[sys.path_max]u8) sys.Error
return out[0..n];
}

/// Remove `parent/name` as `rm -rf` does when a directory cannot be read.
/// `fsx.deleteTree` stops at the first directory it cannot open. An empty
/// directory can still be removed: `unlinkat` with `AT.REMOVEDIR` needs a
/// writable parent, not read permission on the directory. A directory that
/// still holds entries is left, and this returns `error.AccessDenied`.
fn deleteRun(gpa: Allocator, parent: fsx.fd_t, name: []const u8) (fsx.Error || Allocator.Error)!void {
while (true) {
if (fsx.deleteTree(gpa, parent, name)) {
return;
} else |err| switch (err) {
error.AccessDenied => {},
else => return err,
}
if (sys.unlinkat(parent, name, linux.AT.REMOVEDIR)) {
return;
} else |_| {}
const fd = fsx.openDir(parent, name, false) catch return error.AccessDenied;
defer sys.close(fd);
if (!try removeUnreadableEmpty(gpa, fd)) return error.AccessDenied;
}
}

/// Remove empty directories under `dir` that cannot be opened, and empty
/// ones nested the same way under directories that can. A symbolic link is
/// not a directory and is left for `fsx.deleteTree`. Returns whether any
/// directory was removed.
fn removeUnreadableEmpty(gpa: Allocator, dir: fsx.fd_t) (fsx.Error || Allocator.Error)!bool {
var removed = false;
var it = try fsx.DirIter.init(gpa, dir);
defer it.deinit(gpa);
while (try it.next()) |e| {
const kind = fsx.entryType(dir, e) catch |err| switch (err) {
error.NotFound => continue,
else => return err,
};
if (kind != fsx.DT.DIR) continue;
if (fsx.openDir(dir, e.name, false)) |child| {
defer sys.close(child);
if (try removeUnreadableEmpty(gpa, child)) removed = true;
} else |err| switch (err) {
error.AccessDenied => {
if (sys.unlinkat(dir, e.name, linux.AT.REMOVEDIR)) |_| {
removed = true;
} else |_| {}
},
else => return err,
}
}
return removed;
}

fn cmdRotate(c: Ctx, args: []const [:0]const u8) u8 {
var root_s: ?[]const u8 = null;
var compress_v: ?[]const u8 = null;
Expand Down Expand Up @@ -1605,7 +1656,7 @@ fn cmdRotate(c: Ctx, args: []const [:0]const u8) u8 {
deleted += 1;
continue;
}
fsx.deleteTree(c.gpa, root, run) catch |err| {
deleteRun(c.gpa, root, run) catch |err| {
warn("cannot delete {s}/{s}: {t}", .{ joinable(root_p), run, err });
continue;
};
Expand Down
21 changes: 21 additions & 0 deletions tests/test_eb_keep_logs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,27 @@ check "one run deleted, singular" "$("${tool}" rotate "${root}" --compress-after
check "run past --delete-after deleted" "$(ls -d "${root}/20260801_090000" 2>/dev/null | wc -l | tr -d ' ')" "0"
check "run inside --delete-after kept" "$(ls -d "${root}/20261001_120000" 2>/dev/null | wc -l | tr -d ' ')" "1"

# rotate: an empty directory with no read permission is still removed past
# --delete-after. Opening it fails, and an empty directory can be removed
# without reading it. A symlink is not a run and is not followed.
unread="${work}/unread"
mkdir -p "${unread}/20200101_000000" "${unread}/20200102_000000/private" "${unread}/20261112_000000" "${unread}/outside"
echo kept > "${unread}/20200102_000000/n.log"
ln -s ../outside/secret.log "${unread}/20200102_000000/secret.log"
echo secret > "${unread}/outside/secret.log"
ln -s outside "${unread}/20200103_000000"
chmod 000 "${unread}/20200101_000000" "${unread}/20200102_000000/private" "${unread}/20261112_000000"
check "empty unreadable run past --delete-after" \
"$("${tool}" rotate "${unread}" --compress-after 30 --delete-after 90 2>"${work}/unread.err")" \
"eb-keep-logs rotate: 0 files compressed, 2 runs deleted under ${unread}"
check "empty unreadable run: removed" "$(ls -d "${unread}/20200101_000000" 2>/dev/null | wc -l | tr -d ' ')" "0"
check "old run with an empty unreadable directory: removed" "$(ls -d "${unread}/20200102_000000" 2>/dev/null | wc -l | tr -d ' ')" "0"
check "empty unreadable run: no warning" "$(cat "${work}/unread.err")" ""
check "young unreadable run: kept" "$(ls -d "${unread}/20261112_000000" 2>/dev/null | wc -l | tr -d ' ')" "1"
check "symlink run: not followed" "$(cat "${unread}/outside/secret.log")" "secret"
check "symlink run: left in place" "$(test -L "${unread}/20200103_000000" && echo link)" "link"
chmod -R u+rwx "${unread}" 2>/dev/null || true

mkdir -p "${work}/one/20261001_120000"
echo one > "${work}/one/20261001_120000/F.stdout.log"
check "one file compressed, singular" "$("${tool}" rotate "${work}/one" --compress-after 30)" "eb-keep-logs rotate: 1 file compressed, 0 runs deleted under ${work}/one"
Expand Down
Loading