mirror of
https://github.com/NixOS/nix.git
synced 2025-11-17 16:02:43 +01:00
Improve error messages for invalid derivation names
This commit is contained in:
parent
5f4f789144
commit
7df9d6da65
16 changed files with 145 additions and 16 deletions
|
|
@ -2,25 +2,24 @@
|
|||
|
||||
namespace nix {
|
||||
|
||||
static void checkName(std::string_view path, std::string_view name)
|
||||
void checkName(std::string_view name)
|
||||
{
|
||||
if (name.empty())
|
||||
throw BadStorePath("store path '%s' has an empty name", path);
|
||||
throw BadStorePathName("name must not be empty");
|
||||
if (name.size() > StorePath::MaxPathLen)
|
||||
throw BadStorePath("store path '%s' has a name longer than %d characters",
|
||||
path, StorePath::MaxPathLen);
|
||||
throw BadStorePathName("name '%s' must be no longer than %d characters", name, StorePath::MaxPathLen);
|
||||
// See nameRegexStr for the definition
|
||||
if (name[0] == '.') {
|
||||
// check against "." and "..", followed by end or dash
|
||||
if (name.size() == 1)
|
||||
throw BadStorePath("store path '%s' has invalid name '%s'", path, name);
|
||||
throw BadStorePathName("name '%s' is not valid", name);
|
||||
if (name[1] == '-')
|
||||
throw BadStorePath("store path '%s' has invalid name '%s': first dash-separated component must not be '%s'", path, name, ".");
|
||||
throw BadStorePathName("name '%s' is not valid: first dash-separated component must not be '%s'", name, ".");
|
||||
if (name[1] == '.') {
|
||||
if (name.size() == 2)
|
||||
throw BadStorePath("store path '%s' has invalid name '%s'", path, name);
|
||||
throw BadStorePathName("name '%s' is not valid", name);
|
||||
if (name[2] == '-')
|
||||
throw BadStorePath("store path '%s' has invalid name '%s': first dash-separated component must not be '%s'", path, name, "..");
|
||||
throw BadStorePathName("name '%s' is not valid: first dash-separated component must not be '%s'", name, "..");
|
||||
}
|
||||
}
|
||||
for (auto c : name)
|
||||
|
|
@ -28,7 +27,16 @@ static void checkName(std::string_view path, std::string_view name)
|
|||
|| (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);
|
||||
throw BadStorePathName("name '%s' contains illegal character '%s'", name, c);
|
||||
}
|
||||
|
||||
static void checkPathName(std::string_view path, std::string_view name)
|
||||
{
|
||||
try {
|
||||
checkName(name);
|
||||
} catch (BadStorePathName & e) {
|
||||
throw BadStorePath("path '%s' is not a valid store path: %s", path, Uncolored(e.message()));
|
||||
}
|
||||
}
|
||||
|
||||
StorePath::StorePath(std::string_view _baseName)
|
||||
|
|
@ -40,13 +48,13 @@ StorePath::StorePath(std::string_view _baseName)
|
|||
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());
|
||||
checkPathName(baseName, name());
|
||||
}
|
||||
|
||||
StorePath::StorePath(const Hash & hash, std::string_view _name)
|
||||
: baseName((hash.to_string(HashFormat::Nix32, false) + "-").append(std::string(_name)))
|
||||
{
|
||||
checkName(baseName, name());
|
||||
checkPathName(baseName, name());
|
||||
}
|
||||
|
||||
bool StorePath::isDerivation() const noexcept
|
||||
|
|
|
|||
|
|
@ -9,6 +9,13 @@ namespace nix {
|
|||
|
||||
struct Hash;
|
||||
|
||||
/**
|
||||
* Check whether a name is a valid store path name.
|
||||
*
|
||||
* @throws BadStorePathName if the name is invalid. The message is of the format "name %s is not valid, for this specific reason".
|
||||
*/
|
||||
void checkName(std::string_view name);
|
||||
|
||||
/**
|
||||
* \ref StorePath "Store path" is the fundamental reference type of Nix.
|
||||
* A store paths refers to a Store object.
|
||||
|
|
@ -31,8 +38,10 @@ public:
|
|||
|
||||
StorePath() = delete;
|
||||
|
||||
/** @throws BadStorePath */
|
||||
StorePath(std::string_view baseName);
|
||||
|
||||
/** @throws BadStorePath */
|
||||
StorePath(const Hash & hash, std::string_view name);
|
||||
|
||||
std::string_view to_string() const noexcept
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ namespace nix {
|
|||
struct SourcePath;
|
||||
|
||||
MakeError(BadStorePath, Error);
|
||||
MakeError(BadStorePathName, BadStorePath);
|
||||
|
||||
struct StoreDirConfig : public Config
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue