From b24b8ef77c63727b9d6494fe25cfce45d57581be Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 17 Oct 2017 21:39:48 +0200 Subject: [PATCH] BinaryCacheStore::addToStore(): Add NARs to the local cache --- src/libstore/binary-cache-store.cc | 8 ++++++-- src/libstore/remote-fs-accessor.cc | 24 ++++++++++++++++-------- src/libstore/remote-fs-accessor.hh | 4 ++++ 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/libstore/binary-cache-store.cc b/src/libstore/binary-cache-store.cc index f1179f189..67607ab3d 100644 --- a/src/libstore/binary-cache-store.cc +++ b/src/libstore/binary-cache-store.cc @@ -114,8 +114,10 @@ void BinaryCacheStore::addToStore(const ValidPathInfo & info, const refnars.emplace(info.path, narAccessor); + accessor_->addToCache(info.path, *nar); + } std::function recurse; @@ -160,8 +162,10 @@ void BinaryCacheStore::addToStore(const ValidPathInfo & info, const refnars.emplace(info.path, makeNarAccessor(nar)); + accessor_->addToCache(info.path, *nar); + } } /* Compress the NAR. */ diff --git a/src/libstore/remote-fs-accessor.cc b/src/libstore/remote-fs-accessor.cc index da4e30b22..939691204 100644 --- a/src/libstore/remote-fs-accessor.cc +++ b/src/libstore/remote-fs-accessor.cc @@ -11,6 +11,19 @@ RemoteFSAccessor::RemoteFSAccessor(ref store, const Path & cacheDir) createDirs(cacheDir); } +Path RemoteFSAccessor::makeCacheFile(const Path & storePath) +{ + assert(cacheDir != ""); + return fmt("%s/%s.nar", cacheDir, storePathToHash(storePath)); +} + +void RemoteFSAccessor::addToCache(const Path & storePath, const std::string & nar) +{ + if (cacheDir != "") + /* FIXME: do this asynchronously. */ + writeFile(makeCacheFile(storePath), nar); +} + std::pair, Path> RemoteFSAccessor::fetch(const Path & path_) { auto path = canonPath(path_); @@ -26,19 +39,14 @@ std::pair, Path> RemoteFSAccessor::fetch(const Path & path_) StringSink sink; - Path cacheFile = cacheDir != "" ? fmt("%s/%s.nar", cacheDir, storePathToHash(storePath)) : ""; - try { - if (cacheFile != "") - *sink.s = nix::readFile(cacheFile); + if (cacheDir != "") + *sink.s = nix::readFile(makeCacheFile(storePath)); } catch (SysError &) { } if (sink.s->empty()) { store->narFromPath(storePath, sink); - - if (cacheFile != "") - /* FIXME: do this asynchronously. */ - writeFile(cacheFile, *sink.s); + addToCache(storePath, *sink.s); } auto accessor = makeNarAccessor(sink.s); diff --git a/src/libstore/remote-fs-accessor.hh b/src/libstore/remote-fs-accessor.hh index d359ecc9c..2a3fc01ef 100644 --- a/src/libstore/remote-fs-accessor.hh +++ b/src/libstore/remote-fs-accessor.hh @@ -18,6 +18,10 @@ class RemoteFSAccessor : public FSAccessor friend class BinaryCacheStore; + Path makeCacheFile(const Path & storePath); + + void addToCache(const Path & storePath, const std::string & nar); + public: RemoteFSAccessor(ref store,