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