Skip to content

Commit 6f8b214

Browse files
committed
RemoteFSAccessor: Make the local NAR cache content-addressed
1 parent 517f53f commit 6f8b214

2 files changed

Lines changed: 41 additions & 30 deletions

File tree

src/libstore/include/nix/store/remote-fs-accessor.hh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ class RemoteFSAccessor : public SourceAccessor
2121

2222
friend struct BinaryCacheStore;
2323

24-
std::filesystem::path makeCacheFile(std::string_view hashPart, const std::string & ext);
24+
std::filesystem::path makeCacheFile(const Hash & narHash, const std::string & ext);
2525

26-
ref<SourceAccessor> addToCache(std::string_view hashPart, std::string && nar);
26+
ref<SourceAccessor> addToCache(
27+
std::string_view hashPart,
28+
const std::filesystem::path & cacheFile,
29+
const std::filesystem::path & listingFile,
30+
std::string && nar);
2731

2832
public:
2933

src/libstore/remote-fs-accessor.cc

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,22 @@ RemoteFSAccessor::RemoteFSAccessor(
1818
createDirs(*cacheDir);
1919
}
2020

21-
std::filesystem::path RemoteFSAccessor::makeCacheFile(std::string_view hashPart, const std::string & ext)
21+
std::filesystem::path RemoteFSAccessor::makeCacheFile(const Hash & narHash, const std::string & ext)
2222
{
2323
assert(cacheDir);
24-
return (*cacheDir / hashPart) + "." + ext;
24+
return (*cacheDir / narHash.to_string(HashFormat::Nix32, false)) + "." + ext;
2525
}
2626

27-
ref<SourceAccessor> RemoteFSAccessor::addToCache(std::string_view hashPart, std::string && nar)
27+
ref<SourceAccessor> RemoteFSAccessor::addToCache(
28+
std::string_view hashPart,
29+
const std::filesystem::path & cacheFile,
30+
const std::filesystem::path & listingFile,
31+
std::string && nar)
2832
{
29-
if (cacheDir) {
33+
if (!cacheFile.empty()) {
3034
try {
3135
/* FIXME: do this asynchronously. */
32-
writeFile(makeCacheFile(hashPart, "nar"), nar);
36+
writeFile(cacheFile, nar);
3337
} catch (...) {
3438
ignoreExceptionExceptInterrupt();
3539
}
@@ -38,10 +42,10 @@ ref<SourceAccessor> RemoteFSAccessor::addToCache(std::string_view hashPart, std:
3842
auto narAccessor = makeNarAccessor(std::move(nar));
3943
nars.emplace(hashPart, narAccessor);
4044

41-
if (cacheDir) {
45+
if (!listingFile.empty()) {
4246
try {
4347
nlohmann::json j = listNarDeep(*narAccessor, CanonPath::root);
44-
writeFile(makeCacheFile(hashPart, "ls"), j.dump());
48+
writeFile(listingFile, j.dump());
4549
} catch (...) {
4650
ignoreExceptionExceptInterrupt();
4751
}
@@ -64,33 +68,36 @@ std::shared_ptr<SourceAccessor> RemoteFSAccessor::accessObject(const StorePath &
6468
if (i != nars.end())
6569
return i->second;
6670

67-
std::string listing;
68-
std::filesystem::path cacheFile;
69-
70-
if (cacheDir && nix::pathExists(cacheFile = makeCacheFile(storePath.hashPart(), "nar"))) {
71-
72-
try {
73-
listing = nix::readFile(makeCacheFile(storePath.hashPart(), "ls"));
74-
auto listingJson = nlohmann::json::parse(listing);
75-
auto narAccessor = makeLazyNarAccessor(listingJson, seekableGetNarBytes(cacheFile));
76-
77-
nars.emplace(storePath.hashPart(), narAccessor);
78-
return narAccessor;
79-
80-
} catch (SystemError &) {
81-
}
71+
std::filesystem::path cacheFile, listingFile;
8272

83-
try {
84-
auto narAccessor = makeNarAccessor(nix::readFile(cacheFile));
85-
nars.emplace(storePath.hashPart(), narAccessor);
86-
return narAccessor;
87-
} catch (SystemError &) {
73+
if (cacheDir) {
74+
auto info = store->queryPathInfo(storePath);
75+
76+
cacheFile = makeCacheFile(info->narHash, "nar");
77+
listingFile = makeCacheFile(info->narHash, "ls");
78+
79+
if (nix::pathExists(cacheFile)) {
80+
try {
81+
auto listing = nix::readFile(listingFile);
82+
auto listingJson = nlohmann::json::parse(listing);
83+
auto narAccessor = makeLazyNarAccessor(listingJson, seekableGetNarBytes(cacheFile));
84+
nars.emplace(storePath.hashPart(), narAccessor);
85+
return narAccessor;
86+
} catch (SystemError &) {
87+
}
88+
89+
try {
90+
auto narAccessor = makeNarAccessor(nix::readFile(cacheFile));
91+
nars.emplace(storePath.hashPart(), narAccessor);
92+
return narAccessor;
93+
} catch (SystemError &) {
94+
}
8895
}
8996
}
9097

9198
StringSink sink;
9299
store->narFromPath(storePath, sink);
93-
return addToCache(storePath.hashPart(), std::move(sink.s));
100+
return addToCache(storePath.hashPart(), cacheFile, listingFile, std::move(sink.s));
94101
}
95102

96103
std::optional<SourceAccessor::Stat> RemoteFSAccessor::maybeLstat(const CanonPath & path)

0 commit comments

Comments
 (0)