diff options
author | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2016-06-01T12·49+0200 |
---|---|---|
committer | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2016-06-01T14·24+0200 |
commit | 7850d3d27910c30232dd09dd86ee8afdaad26b90 (patch) | |
tree | da539f14d98d815e89b6ad60ed8e1e1ab9981cbf /src/libstore/store-api.cc | |
parent | 1b5b654fe25cf7f2219ebe96a943397d683bfa0e (diff) |
Make the store directory a member variable of Store
Diffstat (limited to 'src/libstore/store-api.cc')
-rw-r--r-- | src/libstore/store-api.cc | 71 |
1 files changed, 38 insertions, 33 deletions
diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index f39d6b54787c..cea4b4b6ebcb 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -8,32 +8,32 @@ namespace nix { -bool isInStore(const Path & path) +bool Store::isInStore(const Path & path) const { - return isInDir(path, settings.nixStore); + return isInDir(path, storeDir); } -bool isStorePath(const Path & path) +bool Store::isStorePath(const Path & path) const { return isInStore(path) - && path.size() >= settings.nixStore.size() + 1 + storePathHashLen - && path.find('/', settings.nixStore.size() + 1) == Path::npos; + && path.size() >= storeDir.size() + 1 + storePathHashLen + && path.find('/', storeDir.size() + 1) == Path::npos; } -void assertStorePath(const Path & path) +void Store::assertStorePath(const Path & path) const { if (!isStorePath(path)) throw Error(format("path ‘%1%’ is not in the Nix store") % path); } -Path toStorePath(const Path & path) +Path Store::toStorePath(const Path & path) const { if (!isInStore(path)) throw Error(format("path ‘%1%’ is not in the Nix store") % path); - Path::size_type slash = path.find('/', settings.nixStore.size() + 1); + Path::size_type slash = path.find('/', storeDir.size() + 1); if (slash == Path::npos) return path; else @@ -41,7 +41,7 @@ Path toStorePath(const Path & path) } -Path followLinksToStore(const Path & _path) +Path Store::followLinksToStore(const Path & _path) const { Path path = absPath(_path); while (!isInStore(path)) { @@ -55,7 +55,7 @@ Path followLinksToStore(const Path & _path) } -Path followLinksToStorePath(const Path & path) +Path Store::followLinksToStorePath(const Path & path) const { return toStorePath(followLinksToStore(path)); } @@ -63,18 +63,17 @@ Path followLinksToStorePath(const Path & path) string storePathToName(const Path & path) { - assertStorePath(path); - auto l = settings.nixStore.size() + 1 + storePathHashLen; - assert(path.size() >= l); - return path.size() == l ? "" : string(path, l + 1); + auto base = baseNameOf(path); + assert(base.size() == storePathHashLen || (base.size() > storePathHashLen && base[storePathHashLen] == '-')); + return base.size() == storePathHashLen ? "" : string(base, storePathHashLen + 1); } string storePathToHash(const Path & path) { - assertStorePath(path); - assert(path.size() >= settings.nixStore.size() + 1 + storePathHashLen); - return string(path, settings.nixStore.size() + 1, storePathHashLen); + auto base = baseNameOf(path); + assert(base.size() >= storePathHashLen); + return string(base, 0, storePathHashLen); } @@ -168,31 +167,31 @@ void checkStoreName(const string & name) */ -Path makeStorePath(const string & type, - const Hash & hash, const string & name) +Path Store::makeStorePath(const string & type, + const Hash & hash, const string & name) const { /* e.g., "source:sha256:1abc...:/nix/store:foo.tar.gz" */ string s = type + ":sha256:" + printHash(hash) + ":" - + settings.nixStore + ":" + name; + + storeDir + ":" + name; checkStoreName(name); - return settings.nixStore + "/" + return storeDir + "/" + printHash32(compressHash(hashString(htSHA256, s), 20)) + "-" + name; } -Path makeOutputPath(const string & id, - const Hash & hash, const string & name) +Path Store::makeOutputPath(const string & id, + const Hash & hash, const string & name) const { return makeStorePath("output:" + id, hash, name + (id == "out" ? "" : "-" + id)); } -Path makeFixedOutputPath(bool recursive, - HashType hashAlgo, Hash hash, string name) +Path Store::makeFixedOutputPath(bool recursive, + HashType hashAlgo, Hash hash, string name) const { return hashAlgo == htSHA256 && recursive ? makeStorePath("source", hash, name) @@ -203,8 +202,8 @@ Path makeFixedOutputPath(bool recursive, } -std::pair<Path, Hash> computeStorePathForPath(const Path & srcPath, - bool recursive, HashType hashAlgo, PathFilter & filter) +std::pair<Path, Hash> Store::computeStorePathForPath(const Path & srcPath, + bool recursive, HashType hashAlgo, PathFilter & filter) const { HashType ht(hashAlgo); Hash h = recursive ? hashPath(ht, srcPath, filter).first : hashFile(ht, srcPath); @@ -214,8 +213,8 @@ std::pair<Path, Hash> computeStorePathForPath(const Path & srcPath, } -Path computeStorePathForText(const string & name, const string & s, - const PathSet & references) +Path Store::computeStorePathForText(const string & name, const string & s, + const PathSet & references) const { Hash hash = hashString(htSHA256, s); /* Stuff the references (if any) into the type. This is a bit @@ -230,6 +229,12 @@ Path computeStorePathForText(const string & name, const string & s, } +Store::Store(const Params & params) + : storeDir(settings.nixStore) +{ +} + + std::string Store::getUri() { return ""; @@ -465,7 +470,7 @@ RegisterStoreImplementation::Implementations * RegisterStoreImplementation::impl ref<Store> openStoreAt(const std::string & uri_) { auto uri(uri_); - StoreParams params; + Store::Params params; auto q = uri.find('?'); if (q != std::string::npos) { for (auto s : tokenizeString<Strings>(uri.substr(q + 1), "&")) { @@ -492,7 +497,7 @@ ref<Store> openStore() static RegisterStoreImplementation regStore([]( - const std::string & uri, const StoreParams & params) + const std::string & uri, const Store::Params & params) -> std::shared_ptr<Store> { enum { mDaemon, mLocal, mAuto } mode; @@ -512,8 +517,8 @@ static RegisterStoreImplementation regStore([]( } return mode == mDaemon - ? std::shared_ptr<Store>(std::make_shared<RemoteStore>()) - : std::shared_ptr<Store>(std::make_shared<LocalStore>()); + ? std::shared_ptr<Store>(std::make_shared<RemoteStore>(params)) + : std::shared_ptr<Store>(std::make_shared<LocalStore>(params)); }); |