Add function to extract hash part of a store path

This commit is contained in:
Eelco Dolstra 2016-02-15 12:49:01 +01:00
parent 74f954ee62
commit d089372565
3 changed files with 15 additions and 2 deletions

View file

@ -1064,7 +1064,7 @@ StringSet LocalStore::queryDerivationOutputNames(const Path & path)
Path LocalStore::queryPathFromHashPart(const string & hashPart) Path LocalStore::queryPathFromHashPart(const string & hashPart)
{ {
if (hashPart.size() != 32) throw Error("invalid hash part"); if (hashPart.size() != storePathHashLen) throw Error("invalid hash part");
Path prefix = settings.nixStore + "/" + hashPart; Path prefix = settings.nixStore + "/" + hashPart;

View file

@ -61,7 +61,14 @@ Path followLinksToStorePath(const Path & path)
string storePathToName(const Path & path) string storePathToName(const Path & path)
{ {
assertStorePath(path); assertStorePath(path);
return string(path, settings.nixStore.size() + 34); return string(path, settings.nixStore.size() + storePathHashLen + 2);
}
string storePathToHash(const Path & path)
{
assertStorePath(path);
return string(path, settings.nixStore.size() + 1, storePathHashLen);
} }

View file

@ -339,6 +339,9 @@ public:
}; };
const size_t storePathHashLen = 32; // base-32 characters, i.e. 160 bits
/* !!! These should be part of the store API, I guess. */ /* !!! These should be part of the store API, I guess. */
/* Throw an exception if `path' is not directly in the Nix store. */ /* Throw an exception if `path' is not directly in the Nix store. */
@ -350,6 +353,9 @@ bool isStorePath(const Path & path);
/* Extract the name part of the given store path. */ /* Extract the name part of the given store path. */
string storePathToName(const Path & path); string storePathToName(const Path & path);
/* Extract the hash part of the given store path. */
string storePathToHash(const Path & path);
void checkStoreName(const string & name); void checkStoreName(const string & name);