1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-05 16:31:47 +02:00

Merge pull request #10757 from obsidiansystems/fix-4977

Require `drvPath` attribute to end with `.drv`
This commit is contained in:
John Ericson 2024-05-24 12:14:59 -04:00 committed by GitHub
commit e0c94b91ee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 71 additions and 34 deletions

View file

@ -930,10 +930,9 @@ DerivationOutputsAndOptPaths BasicDerivation::outputsAndOptPaths(const StoreDirC
std::string_view BasicDerivation::nameFromPath(const StorePath & drvPath)
{
drvPath.requireDerivation();
auto nameWithSuffix = drvPath.name();
constexpr std::string_view extension = ".drv";
assert(hasSuffix(nameWithSuffix, extension));
nameWithSuffix.remove_suffix(extension.size());
nameWithSuffix.remove_suffix(drvExtension.size());
return nameWithSuffix;
}

View file

@ -49,11 +49,17 @@ StorePath::StorePath(const Hash & hash, std::string_view _name)
checkName(baseName, name());
}
bool StorePath::isDerivation() const
bool StorePath::isDerivation() const noexcept
{
return hasSuffix(name(), drvExtension);
}
void StorePath::requireDerivation() const
{
if (!isDerivation())
throw FormatError("store path '%s' is not a valid derivation path", to_string());
}
StorePath StorePath::dummy("ffffffffffffffffffffffffffffffff-x");
StorePath StorePath::random(std::string_view name)

View file

@ -35,30 +35,23 @@ public:
StorePath(const Hash & hash, std::string_view name);
std::string_view to_string() const
std::string_view to_string() const noexcept
{
return baseName;
}
bool operator < (const StorePath & other) const
{
return baseName < other.baseName;
}
bool operator == (const StorePath & other) const
{
return baseName == other.baseName;
}
bool operator != (const StorePath & other) const
{
return baseName != other.baseName;
}
bool operator == (const StorePath & other) const noexcept = default;
auto operator <=> (const StorePath & other) const noexcept = default;
/**
* Check whether a file name ends with the extension for derivations.
*/
bool isDerivation() const;
bool isDerivation() const noexcept;
/**
* Throw an exception if `isDerivation` is false.
*/
void requireDerivation() const;
std::string_view name() const
{
@ -82,7 +75,7 @@ typedef std::vector<StorePath> StorePaths;
* The file extension of \ref Derivation derivations when serialized
* into store objects.
*/
const std::string drvExtension = ".drv";
constexpr std::string_view drvExtension = ".drv";
}