Improve content address parsing

- Ensure hash is in form <algo>-<prefix> and not SRI.

 - Better errors if something goes wrong

 - string_view for no coppying
This commit is contained in:
John Ericson 2020-06-22 18:08:27 +00:00
parent 965b80347e
commit 55d4bd6e0e
3 changed files with 50 additions and 35 deletions

View file

@ -1,3 +1,4 @@
#include "args.hh"
#include "content-address.hh" #include "content-address.hh"
namespace nix { namespace nix {
@ -40,38 +41,52 @@ std::string renderContentAddress(ContentAddress ca) {
} }
ContentAddress parseContentAddress(std::string_view rawCa) { ContentAddress parseContentAddress(std::string_view rawCa) {
auto prefixSeparator = rawCa.find(':'); auto rest = rawCa;
if (prefixSeparator != string::npos) {
auto prefix = string(rawCa, 0, prefixSeparator); // Ensure prefix
if (prefix == "text") { const auto prefixSeparator = rawCa.find(':');
auto hashTypeAndHash = rawCa.substr(prefixSeparator+1, string::npos); if (prefixSeparator == string::npos)
Hash hash = Hash(string(hashTypeAndHash)); throw UsageError("not a content address because it is not in the form \"<prefix>:<rest>\": %s", rawCa);
if (*hash.type != htSHA256) { auto prefix = rest.substr(0, prefixSeparator);
throw Error("parseContentAddress: the text hash should have type SHA256"); rest = rest.substr(prefixSeparator + 1);
}
return TextHash { hash }; auto parseHashType_ = [&](){
} else if (prefix == "fixed") { // Parse hash type
// This has to be an inverse of makeFixedOutputCA auto algoSeparator = rest.find(':');
auto methodAndHash = rawCa.substr(prefixSeparator+1, string::npos); HashType hashType;
if (methodAndHash.substr(0,2) == "r:") { if (algoSeparator == string::npos)
std::string_view hashRaw = methodAndHash.substr(2,string::npos); throw UsageError("content address hash must be in form \"<algo>:<hash>\", but found: %s", rest);
return FixedOutputHash { hashType = parseHashType(rest.substr(0, algoSeparator));
.method = FileIngestionMethod::Recursive,
.hash = Hash(string(hashRaw)), rest = rest.substr(algoSeparator + 1);
};
} else { return std::move(hashType);
std::string_view hashRaw = methodAndHash; };
return FixedOutputHash {
.method = FileIngestionMethod::Flat, // Switch on prefix
.hash = Hash(string(hashRaw)), if (prefix == "text") {
}; // No parsing of the method, "text" only support flat.
} HashType hashType = parseHashType_();
} else { if (hashType != htSHA256)
throw Error("parseContentAddress: format not recognized; has to be text or fixed"); throw Error("text content address hash should use %s, but instead uses %s",
printHashType(htSHA256), printHashType(hashType));
return TextHash {
.hash = Hash { rest, std::move(hashType) },
};
} else if (prefix == "fixed") {
// Parse method
auto method = FileIngestionMethod::Flat;
if (rest.substr(0, 2) == "r:") {
method = FileIngestionMethod::Recursive;
rest = rest.substr(2);
} }
} else { HashType hashType = parseHashType_();
throw Error("Not a content address because it lacks an appropriate prefix"); return FixedOutputHash {
} .method = method,
.hash = Hash { rest, std::move(hashType) },
};
} else
throw UsageError("content address prefix \"%s\" is unrecognized. Recogonized prefixes are \"text\" or \"fixed\"", prefix);
}; };
std::optional<ContentAddress> parseContentAddressOpt(std::string_view rawCaOpt) { std::optional<ContentAddress> parseContentAddressOpt(std::string_view rawCaOpt) {

View file

@ -342,7 +342,7 @@ Hash compressHash(const Hash & hash, unsigned int newSize)
} }
std::optional<HashType> parseHashTypeOpt(const string & s) std::optional<HashType> parseHashTypeOpt(std::string_view s)
{ {
if (s == "md5") return htMD5; if (s == "md5") return htMD5;
else if (s == "sha1") return htSHA1; else if (s == "sha1") return htSHA1;
@ -351,7 +351,7 @@ std::optional<HashType> parseHashTypeOpt(const string & s)
else return std::optional<HashType> {}; else return std::optional<HashType> {};
} }
HashType parseHashType(const string & s) HashType parseHashType(std::string_view s)
{ {
auto opt_h = parseHashTypeOpt(s); auto opt_h = parseHashTypeOpt(s);
if (opt_h) if (opt_h)

View file

@ -121,9 +121,9 @@ HashResult hashPath(HashType ht, const Path & path,
Hash compressHash(const Hash & hash, unsigned int newSize); Hash compressHash(const Hash & hash, unsigned int newSize);
/* Parse a string representing a hash type. */ /* Parse a string representing a hash type. */
HashType parseHashType(const string & s); HashType parseHashType(std::string_view s);
/* Will return nothing on parse error */ /* Will return nothing on parse error */
std::optional<HashType> parseHashTypeOpt(const string & s); std::optional<HashType> parseHashTypeOpt(std::string_view s);
/* And the reverse. */ /* And the reverse. */
string printHashType(HashType ht); string printHashType(HashType ht);