cli: classify ssh cache errors in DiskCache

This commit is contained in:
Jon Parise
2026-07-31 16:41:25 -04:00
parent 8fca64957b
commit ca3dc9eeac
2 changed files with 36 additions and 20 deletions

View File

@@ -393,6 +393,16 @@ fn readEntries(
return entries;
}
/// Whether an error from a cache operation reflects a problem with the cache
/// itself. A destination we can't key on and a lock held by another process
/// both leave the cache healthy, so neither is worth reporting.
pub fn isFailure(err: anyerror) bool {
return switch (err) {
error.InvalidCacheKey, error.CacheLocked => false,
else => true,
};
}
// Supports both standalone hostnames and user@hostname format
pub fn isValidCacheKey(key: []const u8) bool {
if (key.len == 0) return false;
@@ -704,6 +714,15 @@ test "disk cache add survives allocation failure" {
}
}
test isFailure {
const testing = std.testing;
try testing.expect(!isFailure(error.InvalidCacheKey));
try testing.expect(!isFailure(error.CacheLocked));
try testing.expect(isFailure(error.AccessDenied));
try testing.expect(isFailure(error.IsDir));
}
test isValidHost {
const testing = std.testing;

View File

@@ -253,10 +253,9 @@ fn runInner(
if (cache) |c| {
const cached = c.contains(alloc, dest) catch |err| cached: {
// An invalid key is a bad destination, not a bad cache.
if (err != error.InvalidCacheKey) warnPrint(
if (DiskCache.isFailure(err)) warnPrint(
stderr,
"unable to add '{s}' to the cache: {t}",
"unable to read the cache '{s}': {t}",
.{ c.path, err },
);
break :cached false;
@@ -319,23 +318,21 @@ fn runInner(
.real,
).toSeconds())) |_| {
verbosePrint(opts, stderr, "cache: wrote {s}", .{entry.dest});
} else |err| switch (err) {
// An uncacheable destination was already reported above, and a
// lock held by a concurrent `+ssh` resolves itself.
error.InvalidCacheKey,
error.CacheLocked,
=> verbosePrint(
opts,
stderr,
"cache: skipped {s}: {t}",
.{ entry.dest, err },
),
else => warnPrint(
stderr,
"unable to add '{s}' to the cache '{s}': {t}",
.{ entry.dest, entry.cache.path, err },
),
} else |err| {
if (DiskCache.isFailure(err)) {
warnPrint(
stderr,
"unable to add '{s}' to the cache '{s}': {t}",
.{ entry.dest, entry.cache.path, err },
);
} else {
verbosePrint(
opts,
stderr,
"cache: skipped {s}: {t}",
.{ entry.dest, err },
);
}
}
};