nix/ls: fall back to downloading the nar if the .ls is bad

fixes #959

Change-Id: I1715f6fcc938dac7287d9cf4a85e93c9d66768a0
This commit is contained in:
eldritch horrors
2025-08-15 18:05:06 +02:00
parent 612d553a4f
commit 935901cd46
2 changed files with 22 additions and 4 deletions
+10 -2
View File
@@ -137,13 +137,21 @@ struct CmdLsStore : StoreCommand, MixLs
);
JSON j = json::parse(std::move(file), "a nar content listing");
if (j["version"] == 1) {
path = restPath;
accessor = makeLazyNarAccessor(j["root"].dump(), [](uint64_t, uint64_t) -> std::string {
throw Error("attempted to read NAR content during listing");
});
path = restPath;
}
}
} catch (NoSuchBinaryCacheFile &) { }
} catch (NoSuchBinaryCacheFile &) {
} catch (Error & e) {
warn(
"nar listing for %s on %s is bad (falling back to full nar download): %s",
path,
store->getUri(),
e.what()
);
}
list(accessor);
}
+12 -2
View File
@@ -82,11 +82,21 @@ diff -u \
<(echo '{"narOffset": 368,"type":"regular","size":0}' | jq -S)
# Confirm that we are reading from ".ls" file by deleting the nar
rm -rf $cacheDir/nar
# Confirm that we are reading from ".ls" file by moving the nar
mv $cacheDir/nar $cacheDir/nar.gone
diff -u \
<(nix store ls --json -R $storePath/foo/bar --store "file://$cacheDir" | jq -S) \
<(echo '{"narOffset": 368,"type":"regular","size":0}' | jq -S)
mv $cacheDir/nar.gone $cacheDir/nar
# confirm that we read the nar if the listing is missing offsets
narls=$(echo "$cacheDir/"*.ls)
cp "$narls" "$narls.old"
jq 'walk(if type == "object" then del(.narOffset) else . end)' < "$narls.old" >"$narls"
diff -u \
<(nix store ls --json $storePath/foo/bar --store "file://$cacheDir" | jq -S) \
<(echo '{"narOffset": 368,"type":"regular","size":0}' | jq -S)
mv "$narls.old" "$narls"
if canWriteNonUtf8Inodes; then
# Confirm that there's no more than one `.ls` in the `$cacheDir` because non-UTF8 inodes cannot have `.ls` generated for them.