2019-12-05 18:11:09 +00:00
|
|
|
#include "store-api.hh"
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2020-06-16 12:16:39 +00:00
|
|
|
static void checkName(std::string_view path, std::string_view name)
|
2019-12-05 18:11:09 +00:00
|
|
|
{
|
2020-06-16 12:16:39 +00:00
|
|
|
if (name.empty())
|
|
|
|
throw BadStorePath("store path '%s' has an empty name", path);
|
|
|
|
if (name.size() > 211)
|
|
|
|
throw BadStorePath("store path '%s' has a name longer than 211 characters", path);
|
|
|
|
for (auto c : name)
|
|
|
|
if (!((c >= '0' && c <= '9')
|
|
|
|
|| (c >= 'a' && c <= 'z')
|
|
|
|
|| (c >= 'A' && c <= 'Z')
|
|
|
|
|| c == '+' || c == '-' || c == '.' || c == '_' || c == '?' || c == '='))
|
|
|
|
throw BadStorePath("store path '%s' contains illegal character '%s'", path, c);
|
2019-12-05 18:11:09 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 12:16:39 +00:00
|
|
|
StorePath::StorePath(std::string_view _baseName)
|
|
|
|
: baseName(_baseName)
|
2019-12-05 18:11:09 +00:00
|
|
|
{
|
2020-06-16 12:16:39 +00:00
|
|
|
if (baseName.size() < HashLen + 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')))
|
|
|
|
throw BadStorePath("store path '%s' contains illegal base-32 character '%s'", baseName, c);
|
|
|
|
checkName(baseName, name());
|
2019-12-05 18:11:09 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 12:16:39 +00:00
|
|
|
StorePath::StorePath(const Hash & hash, std::string_view _name)
|
|
|
|
: baseName((hash.to_string(Base32, false) + "-").append(std::string(_name)))
|
2019-12-05 18:11:09 +00:00
|
|
|
{
|
2020-06-16 12:16:39 +00:00
|
|
|
checkName(baseName, name());
|
2019-12-05 18:11:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StorePath::isDerivation() const
|
|
|
|
{
|
|
|
|
return hasSuffix(name(), drvExtension);
|
|
|
|
}
|
|
|
|
|
2020-06-16 12:16:39 +00:00
|
|
|
StorePath StorePath::dummy("ffffffffffffffffffffffffffffffff-x");
|
2020-01-21 20:14:13 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePath Store::parseStorePath(std::string_view path) const
|
|
|
|
{
|
2020-06-16 12:16:39 +00:00
|
|
|
auto p = canonPath(std::string(path));
|
|
|
|
if (dirOf(p) != storeDir)
|
|
|
|
throw BadStorePath("path '%s' is not in the Nix store", p);
|
|
|
|
return StorePath(baseNameOf(p));
|
2019-12-05 18:11:09 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 17:07:10 +00:00
|
|
|
std::optional<StorePath> Store::maybeParseStorePath(std::string_view path) const
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return parseStorePath(path);
|
|
|
|
} catch (Error &) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 12:55:15 +00:00
|
|
|
bool Store::isStorePath(std::string_view path) const
|
2020-02-28 17:07:10 +00:00
|
|
|
{
|
|
|
|
return (bool) maybeParseStorePath(path);
|
|
|
|
}
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePathSet Store::parseStorePathSet(const PathSet & paths) const
|
|
|
|
{
|
|
|
|
StorePathSet res;
|
|
|
|
for (auto & i : paths) res.insert(parseStorePath(i));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Store::printStorePath(const StorePath & path) const
|
|
|
|
{
|
2020-06-16 12:16:39 +00:00
|
|
|
return (storeDir + "/").append(path.to_string());
|
2019-12-05 18:11:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PathSet Store::printStorePathSet(const StorePathSet & paths) const
|
|
|
|
{
|
|
|
|
PathSet res;
|
|
|
|
for (auto & i : paths) res.insert(printStorePath(i));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|