diff --git a/lix/libstore/local-store.cc b/lix/libstore/local-store.cc index cb4ca3e84..307430afb 100644 --- a/lix/libstore/local-store.cc +++ b/lix/libstore/local-store.cc @@ -911,7 +911,9 @@ try { kj::Promise>> LocalStore::queryPathFromHashPart(const std::string & hashPart) try { - if (hashPart.size() != StorePath::HashLen) throw Error("invalid hash part"); + if (hashPart.size() != StorePath::HASH_PART_LEN) { + throw Error("invalid hash part"); + } Path prefix = config_.storeDir + "/" + hashPart; diff --git a/lix/libstore/path-tree.cc b/lix/libstore/path-tree.cc index 85f0ec8f1..8a1b8edc9 100644 --- a/lix/libstore/path-tree.cc +++ b/lix/libstore/path-tree.cc @@ -76,7 +76,7 @@ try { std::string(contents, pos2, pos - pos2 + hash.size() + margin) ), pos - pos2, - StorePath::HashLen, + StorePath::HASH_PART_LEN, getColour(hash) )) ); @@ -88,9 +88,9 @@ try { for (auto & hash : hashes) { auto pos = target.find(hash); if (pos != std::string::npos) { - hits[hash].emplace_back( - fmt("%s -> %s", p2, hilite(target, pos, StorePath::HashLen, getColour(hash))) - ); + hits[hash].emplace_back(fmt( + "%s -> %s", p2, hilite(target, pos, StorePath::HASH_PART_LEN, getColour(hash)) + )); } } } diff --git a/lix/libstore/path.cc b/lix/libstore/path.cc index 838cb217d..706bd3edc 100644 --- a/lix/libstore/path.cc +++ b/lix/libstore/path.cc @@ -8,9 +8,11 @@ static void checkName(std::string_view path, std::string_view name) { if (name.empty()) throw BadStorePath("store path '%s' has an empty name", path); - if (name.size() > StorePath::MaxPathLen) - throw BadStorePath("store path '%s' has a name longer than %d characters", - path, StorePath::MaxPathLen); + if (name.size() > StorePath::MAX_PATH_LEN) { + throw BadStorePath( + "store path '%s' has a name longer than %d characters", path, StorePath::MAX_PATH_LEN + ); + } // See nameRegexStr for the definition if (name[0] == '.') { // check against "." and "..", followed by end or dash @@ -36,8 +38,9 @@ static void checkName(std::string_view path, std::string_view name) StorePath::StorePath(std::string_view _baseName) : baseName(_baseName) { - if (baseName.size() < HashLen + 1) + if (baseName.size() < HASH_PART_LEN + 1) { throw BadStorePath("'%s' is too short to be a valid store path", baseName); + } for (auto c : hashPart()) if (c == 'e' || c == 'o' || c == 'u' || c == 't' || !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z'))) @@ -48,6 +51,7 @@ StorePath::StorePath(std::string_view _baseName) StorePath::StorePath(const Hash & hash, std::string_view _name) : baseName((hash.to_string(Base::Base32, false) + "-").append(std::string(_name))) { + assert(hash.base32Len() == HASH_PART_LEN); checkName(baseName, name()); } diff --git a/lix/libstore/path.hh b/lix/libstore/path.hh index 0b855b98c..44f494bb7 100644 --- a/lix/libstore/path.hh +++ b/lix/libstore/path.hh @@ -26,9 +26,9 @@ public: /** * Size of the hash part of store paths, in base-32 characters. */ - constexpr static size_t HashLen = 32; // i.e. 160 bits + constexpr static size_t HASH_PART_LEN = 32; // i.e. 160 bits - constexpr static size_t MaxPathLen = 211; + constexpr static size_t MAX_PATH_LEN = 211; StorePath() = delete; @@ -63,12 +63,12 @@ public: std::string_view name() const { - return std::string_view(baseName).substr(HashLen + 1); + return std::string_view(baseName).substr(HASH_PART_LEN + 1); } std::string_view hashPart() const { - return std::string_view(baseName).substr(0, HashLen); + return std::string_view(baseName).substr(0, HASH_PART_LEN); } static StorePath dummy;