libstore: rename confusing const identifiers

Also adds an assert that store path hash part length is what we expect
because it's alarmingly easy to forget to truncate a hash before
throwing it into there. It's kind of messy code, someone could improve
it more later.

Change-Id: I5296ea3d5b854323d092f0256defb598dd5b87e8
This commit is contained in:
Jade Lovelace
2025-07-23 02:51:45 +00:00
committed by jade
parent 72cad8918b
commit 8a2d25054d
4 changed files with 19 additions and 13 deletions
+3 -1
View File
@@ -911,7 +911,9 @@ try {
kj::Promise<Result<std::optional<StorePath>>>
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;
+4 -4
View File
@@ -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))
));
}
}
}
+8 -4
View File
@@ -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());
}
+4 -4
View File
@@ -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;