2020-06-22 21:08:27 +03:00
|
|
|
#include "args.hh"
|
2020-06-02 22:44:58 +03:00
|
|
|
#include "content-address.hh"
|
2020-06-30 18:57:46 +03:00
|
|
|
#include "parser.hh"
|
2020-06-02 00:32:27 +03:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2020-06-19 18:18: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
|
|
|
}
|
|
|
|
|
|
|
|
std::string makeFileIngestionPrefix(const FileIngestionMethod m) {
|
|
|
|
switch (m) {
|
|
|
|
case FileIngestionMethod::Flat:
|
|
|
|
return "";
|
|
|
|
case FileIngestionMethod::Recursive:
|
|
|
|
return "r:";
|
|
|
|
default:
|
|
|
|
throw Error("impossible, caught both cases");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-02 20:04:21 +03:00
|
|
|
// FIXME Put this somewhere?
|
2020-06-02 02:26:40 +03:00
|
|
|
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
|
|
|
|
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
|
|
|
|
|
|
|
|
std::string renderContentAddress(ContentAddress ca) {
|
|
|
|
return std::visit(overloaded {
|
|
|
|
[](TextHash th) {
|
2020-06-19 03:24:47 +03:00
|
|
|
return "text:" + th.hash.to_string(Base32, true);
|
2020-06-02 02:26:40 +03:00
|
|
|
},
|
2020-06-19 18:18:19 +03:00
|
|
|
[](FixedOutputHash fsh) {
|
2020-06-02 02:26:40 +03:00
|
|
|
return makeFixedOutputCA(fsh.method, fsh.hash);
|
|
|
|
}
|
|
|
|
}, ca);
|
|
|
|
}
|
|
|
|
|
2020-06-02 18:00:10 +03:00
|
|
|
ContentAddress parseContentAddress(std::string_view rawCa) {
|
2020-06-22 21:08:27 +03:00
|
|
|
auto rest = rawCa;
|
|
|
|
|
2020-06-30 18:57:46 +03:00
|
|
|
std::string_view prefix;
|
|
|
|
{
|
|
|
|
auto optPrefix = splitPrefix(rest, ':');
|
|
|
|
if (!optPrefix)
|
|
|
|
throw UsageError("not a content address because it is not in the form \"<prefix>:<rest>\": %s", rawCa);
|
|
|
|
prefix = *optPrefix;
|
|
|
|
}
|
2020-06-22 21:08:27 +03:00
|
|
|
|
|
|
|
auto parseHashType_ = [&](){
|
2020-06-30 18:57:46 +03:00
|
|
|
auto hashTypeRaw = splitPrefix(rest, ':');
|
|
|
|
if (!hashTypeRaw)
|
|
|
|
throw UsageError("content address hash must be in form \"<algo>:<hash>\", but found: %s", rawCa);
|
|
|
|
HashType hashType = parseHashType(*hashTypeRaw);
|
2020-06-22 21:08:27 +03:00
|
|
|
return std::move(hashType);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Switch on prefix
|
|
|
|
if (prefix == "text") {
|
|
|
|
// No parsing of the method, "text" only support flat.
|
|
|
|
HashType hashType = parseHashType_();
|
|
|
|
if (hashType != htSHA256)
|
|
|
|
throw Error("text content address hash should use %s, but instead uses %s",
|
|
|
|
printHashType(htSHA256), printHashType(hashType));
|
|
|
|
return TextHash {
|
2020-07-02 18:57:21 +03:00
|
|
|
.hash = Hash::parseNonSRIUnprefixed(rest, std::move(hashType)),
|
2020-06-22 21:08:27 +03:00
|
|
|
};
|
|
|
|
} else if (prefix == "fixed") {
|
|
|
|
// Parse method
|
|
|
|
auto method = FileIngestionMethod::Flat;
|
|
|
|
if (rest.substr(0, 2) == "r:") {
|
|
|
|
method = FileIngestionMethod::Recursive;
|
|
|
|
rest = rest.substr(2);
|
2020-06-02 21:15:38 +03:00
|
|
|
}
|
2020-06-22 21:08:27 +03:00
|
|
|
HashType hashType = parseHashType_();
|
|
|
|
return FixedOutputHash {
|
|
|
|
.method = method,
|
2020-07-02 18:57:21 +03:00
|
|
|
.hash = Hash::parseNonSRIUnprefixed(rest, std::move(hashType)),
|
2020-06-22 21:08:27 +03:00
|
|
|
};
|
|
|
|
} else
|
|
|
|
throw UsageError("content address prefix \"%s\" is unrecognized. Recogonized prefixes are \"text\" or \"fixed\"", prefix);
|
2020-06-02 03:37:43 +03:00
|
|
|
};
|
|
|
|
|
2020-06-02 18:00:10 +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
|
|
|
};
|
|
|
|
|
|
|
|
std::string renderContentAddress(std::optional<ContentAddress> ca) {
|
|
|
|
return ca ? renderContentAddress(*ca) : "";
|
2020-06-02 02:26:40 +03:00
|
|
|
}
|
|
|
|
|
2020-06-02 00:32:27 +03:00
|
|
|
}
|