From fdbbcc44924cb4d9028fa68b2f7d423fb5d8670f Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 21 Oct 2016 18:09:30 +0200 Subject: Remove addPathToAccessor --- src/libstore/binary-cache-store.cc | 152 +++++++++++++++++++------------------ 1 file changed, 77 insertions(+), 75 deletions(-) (limited to 'src/libstore/binary-cache-store.cc') diff --git a/src/libstore/binary-cache-store.cc b/src/libstore/binary-cache-store.cc index 7692da742f..4125af118c 100644 --- a/src/libstore/binary-cache-store.cc +++ b/src/libstore/binary-cache-store.cc @@ -17,6 +17,66 @@ namespace nix { +/* Given requests for a path /nix/store//, this accessor will + first download the NAR for /nix/store/ from the binary cache, + build a NAR accessor for that NAR, and use that to access . */ +struct BinaryCacheStoreAccessor : public FSAccessor +{ + ref store; + + std::map> nars; + + BinaryCacheStoreAccessor(ref store) + : store(store) + { + } + + std::pair, Path> fetch(const Path & path_) + { + auto path = canonPath(path_); + + auto storePath = store->toStorePath(path); + std::string restPath = std::string(path, storePath.size()); + + if (!store->isValidPath(storePath)) + throw InvalidPath(format("path ‘%1%’ is not a valid store path") % storePath); + + auto i = nars.find(storePath); + if (i != nars.end()) return {i->second, restPath}; + + StringSink sink; + store->narFromPath(storePath, sink); + + auto accessor = makeNarAccessor(sink.s); + nars.emplace(storePath, accessor); + return {accessor, restPath}; + } + + Stat stat(const Path & path) override + { + auto res = fetch(path); + return res.first->stat(res.second); + } + + StringSet readDirectory(const Path & path) override + { + auto res = fetch(path); + return res.first->readDirectory(res.second); + } + + std::string readFile(const Path & path) override + { + auto res = fetch(path); + return res.first->readFile(res.second); + } + + std::string readLink(const Path & path) override + { + auto res = fetch(path); + return res.first->readLink(res.second); + } +}; + BinaryCacheStore::BinaryCacheStore(const Params & params) : Store(params) , compression(get(params, "compression", "xz")) @@ -82,7 +142,7 @@ Path BinaryCacheStore::narInfoFileFor(const Path & storePath) } void BinaryCacheStore::addToStore(const ValidPathInfo & info, const ref & nar, - bool repair, bool dontCheckSigs) + bool repair, bool dontCheckSigs, std::shared_ptr accessor) { if (!repair && isValidPath(info.path)) return; @@ -109,6 +169,8 @@ void BinaryCacheStore::addToStore(const ValidPathInfo & info, const refnarHash) throw Error(format("refusing to copy corrupted path ‘%1%’ to binary cache") % info.path); + auto accessor_ = std::dynamic_pointer_cast(accessor); + /* Optionally write a JSON file containing a listing of the contents of the NAR. */ if (writeNARListing) { @@ -118,12 +180,15 @@ void BinaryCacheStore::addToStore(const ValidPathInfo & info, const refnars.emplace(info.path, narAccessor); std::function recurse; recurse = [&](const Path & path, JSONPlaceholder & res) { - auto st = accessor->stat(path); + auto st = narAccessor->stat(path); auto obj = res.object(); @@ -138,7 +203,7 @@ void BinaryCacheStore::addToStore(const ValidPathInfo & info, const refreadDirectory(path)) { + for (auto & name : narAccessor->readDirectory(path)) { auto res3 = res2.placeholder(name); recurse(path + "/" + name, res3); } @@ -146,7 +211,7 @@ void BinaryCacheStore::addToStore(const ValidPathInfo & info, const refreadLink(path)); + obj.attr("target", narAccessor->readLink(path)); break; default: abort(); @@ -162,6 +227,11 @@ void BinaryCacheStore::addToStore(const ValidPathInfo & info, const refnars.emplace(info.path, makeNarAccessor(nar)); + } + /* Compress the NAR. */ narInfo->compression = compression; auto now1 = std::chrono::steady_clock::now(); @@ -286,7 +356,7 @@ Path BinaryCacheStore::addToStore(const string & name, const Path & srcPath, ValidPathInfo info; info.path = makeFixedOutputPath(recursive, h, name); - addToStore(info, sink.s, repair); + addToStore(info, sink.s, repair, false, 0); return info.path; } @@ -301,84 +371,16 @@ Path BinaryCacheStore::addTextToStore(const string & name, const string & s, if (repair || !isValidPath(info.path)) { StringSink sink; dumpString(s, sink); - addToStore(info, sink.s, repair); + addToStore(info, sink.s, repair, false, 0); } return info.path; } -/* Given requests for a path /nix/store//, this accessor will - first download the NAR for /nix/store/ from the binary cache, - build a NAR accessor for that NAR, and use that to access . */ -struct BinaryCacheStoreAccessor : public FSAccessor -{ - ref store; - - std::map> nars; - - BinaryCacheStoreAccessor(ref store) - : store(store) - { - } - - std::pair, Path> fetch(const Path & path_) - { - auto path = canonPath(path_); - - auto storePath = store->toStorePath(path); - std::string restPath = std::string(path, storePath.size()); - - if (!store->isValidPath(storePath)) - throw InvalidPath(format("path ‘%1%’ is not a valid store path") % storePath); - - auto i = nars.find(storePath); - if (i != nars.end()) return {i->second, restPath}; - - StringSink sink; - store->narFromPath(storePath, sink); - - auto accessor = makeNarAccessor(sink.s); - nars.emplace(storePath, accessor); - return {accessor, restPath}; - } - - Stat stat(const Path & path) override - { - auto res = fetch(path); - return res.first->stat(res.second); - } - - StringSet readDirectory(const Path & path) override - { - auto res = fetch(path); - return res.first->readDirectory(res.second); - } - - std::string readFile(const Path & path) override - { - auto res = fetch(path); - return res.first->readFile(res.second); - } - - std::string readLink(const Path & path) override - { - auto res = fetch(path); - return res.first->readLink(res.second); - } -}; - ref BinaryCacheStore::getFSAccessor() { return make_ref(ref( std::dynamic_pointer_cast(shared_from_this()))); } -void BinaryCacheStore::addPathToAccessor(ref accessor, - const Path & storePath, const ref & data) -{ - auto accessor_ = accessor.dynamic_pointer_cast(); - if (accessor_) - accessor_->nars.emplace(storePath, makeNarAccessor(data)); -} - } -- cgit 1.4.1