11#include < nlohmann/json.hpp>
22#include " nix/store/remote-fs-accessor.hh"
3- #include " nix/util/nar-accessor.hh"
4-
5- #include < sys/types.h>
6- #include < sys/stat.h>
7- #include < fcntl.h>
3+ #include " nix/store/nar-cache.hh"
84
95namespace nix {
106
11- RemoteFSAccessor::RemoteFSAccessor (
12- ref<Store> store, bool requireValidPath, std::optional<std::filesystem::path> cacheDir_)
7+ RemoteFSAccessor::RemoteFSAccessor (ref<Store> store, bool requireValidPath, std::shared_ptr<NarCache> narCache)
138 : store(store)
149 , requireValidPath(requireValidPath)
15- , cacheDir(std::move(cacheDir_))
16- {
17- if (cacheDir)
18- createDirs (*cacheDir);
19- }
20-
21- std::filesystem::path RemoteFSAccessor::makeCacheFile (const Hash & narHash, const std::string & ext)
22- {
23- assert (cacheDir);
24- return (*cacheDir / narHash.to_string (HashFormat::Nix32, false )) + " ." + ext;
25- }
26-
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)
10+ , narCache(std::move(narCache))
3211{
33- if (!cacheFile.empty ()) {
34- try {
35- /* FIXME: do this asynchronously. */
36- writeFile (cacheFile, nar);
37- } catch (...) {
38- ignoreExceptionExceptInterrupt ();
39- }
40- }
41-
42- auto narAccessor = makeNarAccessor (std::move (nar));
43- nars.emplace (hashPart, narAccessor);
44-
45- if (!listingFile.empty ()) {
46- try {
47- nlohmann::json j = listNarDeep (*narAccessor, CanonPath::root);
48- writeFile (listingFile, j.dump ());
49- } catch (...) {
50- ignoreExceptionExceptInterrupt ();
51- }
52- }
53-
54- return narAccessor;
5512}
5613
5714std::pair<ref<SourceAccessor>, CanonPath> RemoteFSAccessor::fetch (const CanonPath & path)
@@ -68,36 +25,43 @@ std::shared_ptr<SourceAccessor> RemoteFSAccessor::accessObject(const StorePath &
6825 if (i != nars.end ())
6926 return i->second ;
7027
71- std::filesystem::path cacheFile, listingFile ;
28+ Hash narHash{HashAlgorithm::SHA256} ;
7229
73- if (cacheDir ) {
30+ if (narCache ) {
7431 auto info = store->queryPathInfo (storePath);
32+ narHash = info->narHash ;
7533
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- }
34+ if (auto listingData = narCache->getNarListing (narHash)) {
35+ auto listingJson = nlohmann::json::parse (*listingData);
36+ auto narAccessor = makeLazyNarAccessor (listingJson, narCache->getNarBytes (narHash));
37+ nars.emplace (storePath.hashPart (), narAccessor);
38+ return narAccessor;
39+ }
40+
41+ if (auto nar = narCache->getNar (narHash)) {
42+ auto narAccessor = makeNarAccessor (std::move (*nar));
43+ nars.emplace (storePath.hashPart (), narAccessor);
44+ return narAccessor;
9545 }
9646 }
9747
9848 StringSink sink;
9949 store->narFromPath (storePath, sink);
100- return addToCache (storePath.hashPart (), cacheFile, listingFile, std::move (sink.s ));
50+
51+ if (narCache) {
52+ StringSource source{sink.s };
53+ narCache->upsertNar (narHash, source);
54+ }
55+
56+ auto narAccessor = makeNarAccessor (std::move (sink.s ));
57+ nars.emplace (storePath.hashPart (), narAccessor);
58+
59+ if (narCache) {
60+ nlohmann::json j = listNarDeep (*narAccessor, CanonPath::root);
61+ narCache->upsertNarListing (narHash, j.dump ());
62+ }
63+
64+ return narAccessor;
10165}
10266
10367std::optional<SourceAccessor::Stat> RemoteFSAccessor::maybeLstat (const CanonPath & path)
0 commit comments