2020-06-22 21:08:27 +03:00
|
|
|
#include "args.hh"
|
2020-06-02 22:44:58 +03:00
|
|
|
#include "content-address.hh"
|
2020-07-20 20:42:34 +03:00
|
|
|
#include "split.hh"
|
2020-06-02 00:32:27 +03:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2020-09-22 12:40:19 +03:00
|
|
|
std::string FixedOutputHash::printMethodAlgo() const
|
|
|
|
{
|
2020-06-23 20:03:10 +03:00
|
|
|
return makeFileIngestionPrefix(method) + printHashType(hash.type);
|
2020-06-02 00:32:27 +03:00
|
|
|
}
|
|
|
|
|
2020-10-07 16:52:20 +03:00
|
|
|
|
2020-09-22 12:40:19 +03:00
|
|
|
std::string makeFileIngestionPrefix(const FileIngestionMethod m)
|
|
|
|
{
|
2020-06-02 00:32:27 +03:00
|
|
|
switch (m) {
|
|
|
|
case FileIngestionMethod::Flat:
|
|
|
|
return "";
|
|
|
|
case FileIngestionMethod::Recursive:
|
|
|
|
return "r:";
|
|
|
|
}
|
2020-10-07 16:52:20 +03:00
|
|
|
assert(false);
|
2020-06-02 00:32:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string makeFixedOutputCA(FileIngestionMethod method, const Hash & hash)
|
|
|
|
{
|
|
|
|
return "fixed:"
|
|
|
|
+ makeFileIngestionPrefix(method)
|
2020-06-19 02:01:58 +03:00
|
|
|
+ hash.to_string(Base32, true);
|
2020-06-02 00:32:27 +03:00
|
|
|
}
|
|
|
|
|
2020-09-22 12:40:19 +03:00
|
|
|
std::string renderContentAddress(ContentAddress ca)
|
|
|
|
{
|
2020-06-02 02:26:40 +03:00
|
|
|
return std::visit(overloaded {
|
2021-10-01 00:31:21 +03:00
|
|
|
[](TextHash & th) {
|
2020-10-07 16:52:20 +03:00
|
|
|
return "text:"
|
|
|
|
+ th.hash.to_string(Base32, true);
|
2020-06-02 02:26:40 +03:00
|
|
|
},
|
2021-10-01 00:31:21 +03:00
|
|
|
[](FixedOutputHash & fsh) {
|
2020-10-07 16:52:20 +03:00
|
|
|
return "fixed:"
|
|
|
|
+ makeFileIngestionPrefix(fsh.method)
|
|
|
|
+ fsh.hash.to_string(Base32, true);
|
2020-06-02 02:26:40 +03:00
|
|
|
}
|
|
|
|
}, ca);
|
|
|
|
}
|
|
|
|
|
2020-09-22 12:40:19 +03:00
|
|
|
std::string renderContentAddressMethod(ContentAddressMethod cam)
|
|
|
|
{
|
2020-09-17 18:15:05 +03:00
|
|
|
return std::visit(overloaded {
|
2021-10-01 00:31:21 +03:00
|
|
|
[](TextHashMethod & th) {
|
2020-09-17 18:15:05 +03:00
|
|
|
return std::string{"text:"} + printHashType(htSHA256);
|
|
|
|
},
|
2021-10-01 00:31:21 +03:00
|
|
|
[](FixedOutputHashMethod & fshm) {
|
2020-09-17 18:15:05 +03:00
|
|
|
return "fixed:" + makeFileIngestionPrefix(fshm.fileIngestionMethod) + printHashType(fshm.hashType);
|
|
|
|
}
|
|
|
|
}, cam);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Parses content address strings up to the hash.
|
|
|
|
*/
|
2020-09-22 12:40:19 +03:00
|
|
|
static ContentAddressMethod parseContentAddressMethodPrefix(std::string_view & rest)
|
|
|
|
{
|
2020-09-17 23:04:05 +03:00
|
|
|
std::string_view wholeInput { rest };
|
2020-06-22 21:08:27 +03:00
|
|
|
|
2020-06-30 18:57:46 +03:00
|
|
|
std::string_view prefix;
|
|
|
|
{
|
2020-07-03 02:16:57 +03:00
|
|
|
auto optPrefix = splitPrefixTo(rest, ':');
|
2020-06-30 18:57:46 +03:00
|
|
|
if (!optPrefix)
|
2020-09-17 18:15:05 +03:00
|
|
|
throw UsageError("not a content address because it is not in the form '<prefix>:<rest>': %s", wholeInput);
|
2020-06-30 18:57:46 +03:00
|
|
|
prefix = *optPrefix;
|
|
|
|
}
|
2020-06-22 21:08:27 +03:00
|
|
|
|
|
|
|
auto parseHashType_ = [&](){
|
2020-07-03 02:16:57 +03:00
|
|
|
auto hashTypeRaw = splitPrefixTo(rest, ':');
|
2020-06-30 18:57:46 +03:00
|
|
|
if (!hashTypeRaw)
|
2020-09-17 18:15:05 +03:00
|
|
|
throw UsageError("content address hash must be in form '<algo>:<hash>', but found: %s", wholeInput);
|
2020-06-30 18:57:46 +03:00
|
|
|
HashType hashType = parseHashType(*hashTypeRaw);
|
2020-06-22 21:08:27 +03:00
|
|
|
return std::move(hashType);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Switch on prefix
|
|
|
|
if (prefix == "text") {
|
2020-09-17 18:15:05 +03:00
|
|
|
// No parsing of the ingestion method, "text" only support flat.
|
2020-06-22 21:08:27 +03:00
|
|
|
HashType hashType = parseHashType_();
|
|
|
|
if (hashType != htSHA256)
|
|
|
|
throw Error("text content address hash should use %s, but instead uses %s",
|
|
|
|
printHashType(htSHA256), printHashType(hashType));
|
2020-09-17 18:15:05 +03:00
|
|
|
return TextHashMethod {};
|
2020-06-22 21:08:27 +03:00
|
|
|
} else if (prefix == "fixed") {
|
|
|
|
// Parse method
|
|
|
|
auto method = FileIngestionMethod::Flat;
|
2020-07-03 02:16:57 +03:00
|
|
|
if (splitPrefix(rest, "r:"))
|
2020-06-22 21:08:27 +03:00
|
|
|
method = FileIngestionMethod::Recursive;
|
|
|
|
HashType hashType = parseHashType_();
|
2020-09-17 18:15:05 +03:00
|
|
|
return FixedOutputHashMethod {
|
|
|
|
.fileIngestionMethod = method,
|
|
|
|
.hashType = std::move(hashType),
|
2020-06-22 21:08:27 +03:00
|
|
|
};
|
|
|
|
} else
|
2020-08-05 17:47:48 +03:00
|
|
|
throw UsageError("content address prefix '%s' is unrecognized. Recogonized prefixes are 'text' or 'fixed'", prefix);
|
2020-09-17 18:15:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ContentAddress parseContentAddress(std::string_view rawCa) {
|
|
|
|
auto rest = rawCa;
|
|
|
|
|
|
|
|
ContentAddressMethod caMethod = parseContentAddressMethodPrefix(rest);
|
|
|
|
|
|
|
|
return std::visit(
|
|
|
|
overloaded {
|
2021-10-01 00:31:21 +03:00
|
|
|
[&](TextHashMethod & thm) {
|
2020-09-17 18:15:05 +03:00
|
|
|
return ContentAddress(TextHash {
|
|
|
|
.hash = Hash::parseNonSRIUnprefixed(rest, htSHA256)
|
|
|
|
});
|
|
|
|
},
|
2021-10-01 00:31:21 +03:00
|
|
|
[&](FixedOutputHashMethod & fohMethod) {
|
2020-09-17 18:15:05 +03:00
|
|
|
return ContentAddress(FixedOutputHash {
|
|
|
|
.method = fohMethod.fileIngestionMethod,
|
|
|
|
.hash = Hash::parseNonSRIUnprefixed(rest, std::move(fohMethod.hashType)),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
}, caMethod);
|
|
|
|
}
|
|
|
|
|
2020-09-22 12:40:19 +03:00
|
|
|
ContentAddressMethod parseContentAddressMethod(std::string_view caMethod)
|
|
|
|
{
|
2021-11-23 17:15:34 +02:00
|
|
|
std::string asPrefix = std::string{caMethod} + ":";
|
|
|
|
// parseContentAddressMethodPrefix takes its argument by reference
|
|
|
|
std::string_view asPrefixView = asPrefix;
|
|
|
|
return parseContentAddressMethodPrefix(asPrefixView);
|
2020-09-17 18:15:05 +03:00
|
|
|
}
|
2020-06-02 03:37:43 +03:00
|
|
|
|
2020-09-22 12:40:19 +03:00
|
|
|
std::optional<ContentAddress> parseContentAddressOpt(std::string_view rawCaOpt)
|
|
|
|
{
|
|
|
|
return rawCaOpt == "" ? std::optional<ContentAddress>() : parseContentAddress(rawCaOpt);
|
2020-06-02 03:37:43 +03:00
|
|
|
};
|
|
|
|
|
2020-09-22 12:40:19 +03:00
|
|
|
std::string renderContentAddress(std::optional<ContentAddress> ca)
|
|
|
|
{
|
2020-06-02 03:37:43 +03:00
|
|
|
return ca ? renderContentAddress(*ca) : "";
|
2020-06-02 02:26:40 +03:00
|
|
|
}
|
|
|
|
|
2020-07-10 14:21:37 +03:00
|
|
|
Hash getContentAddressHash(const ContentAddress & ca)
|
|
|
|
{
|
|
|
|
return std::visit(overloaded {
|
2021-10-01 00:31:21 +03:00
|
|
|
[](const TextHash & th) {
|
2020-07-10 14:21:37 +03:00
|
|
|
return th.hash;
|
|
|
|
},
|
2021-10-01 00:31:21 +03:00
|
|
|
[](const FixedOutputHash & fsh) {
|
2020-07-10 14:21:37 +03:00
|
|
|
return fsh.hash;
|
2020-10-07 16:52:20 +03:00
|
|
|
},
|
|
|
|
}, ca);
|
|
|
|
}
|
|
|
|
|
|
|
|
ContentAddressWithReferences caWithoutRefs(const ContentAddress & ca) {
|
|
|
|
return std::visit(overloaded {
|
2021-10-01 20:12:54 +03:00
|
|
|
[&](const TextHash & h) -> ContentAddressWithReferences {
|
2020-10-07 16:52:20 +03:00
|
|
|
return TextInfo { h, {}};
|
|
|
|
},
|
2021-10-01 20:12:54 +03:00
|
|
|
[&](const FixedOutputHash & h) -> ContentAddressWithReferences {
|
2020-10-07 16:52:20 +03:00
|
|
|
return FixedOutputInfo { h, {}};
|
|
|
|
},
|
2020-07-10 14:21:37 +03:00
|
|
|
}, ca);
|
|
|
|
}
|
|
|
|
|
2020-06-02 00:32:27 +03:00
|
|
|
}
|