2020-06-02 19:44:58 +00:00
|
|
|
#include "content-address.hh"
|
2020-06-01 21:32:27 +00:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
std::string FileSystemHash::printMethodAlgo() const {
|
2020-06-02 23:30:38 +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-02 17:04:21 +00:00
|
|
|
// FIXME Put this somewhere?
|
2020-06-01 23:26:40 +00: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 00:24:47 +00:00
|
|
|
return "text:" + th.hash.to_string(Base32, true);
|
2020-06-01 23:26:40 +00:00
|
|
|
},
|
|
|
|
[](FileSystemHash fsh) {
|
|
|
|
return makeFixedOutputCA(fsh.method, fsh.hash);
|
|
|
|
}
|
|
|
|
}, ca);
|
|
|
|
}
|
|
|
|
|
2020-06-02 15:00:10 +00:00
|
|
|
ContentAddress parseContentAddress(std::string_view rawCa) {
|
2020-06-02 18:15:38 +00:00
|
|
|
auto prefixSeparator = rawCa.find(':');
|
|
|
|
if (prefixSeparator != string::npos) {
|
|
|
|
auto prefix = string(rawCa, 0, prefixSeparator);
|
|
|
|
if (prefix == "text") {
|
|
|
|
auto hashTypeAndHash = rawCa.substr(prefixSeparator+1, string::npos);
|
2020-06-02 20:20:22 +00:00
|
|
|
Hash hash = Hash(string(hashTypeAndHash));
|
2020-06-18 23:01:58 +00:00
|
|
|
if (*hash.type != htSHA256) {
|
2020-06-02 20:20:22 +00:00
|
|
|
throw Error("parseContentAddress: the text hash should have type SHA256");
|
|
|
|
}
|
|
|
|
return TextHash { hash };
|
2020-06-02 18:15:38 +00:00
|
|
|
} else if (prefix == "fixed") {
|
2020-06-02 20:21:18 +00:00
|
|
|
// This has to be an inverse of makeFixedOutputCA
|
2020-06-02 18:15:38 +00:00
|
|
|
auto methodAndHash = rawCa.substr(prefixSeparator+1, string::npos);
|
|
|
|
if (methodAndHash.substr(0,2) == "r:") {
|
|
|
|
std::string_view hashRaw = methodAndHash.substr(2,string::npos);
|
2020-06-02 20:20:22 +00:00
|
|
|
return FileSystemHash { FileIngestionMethod::Recursive, Hash(string(hashRaw)) };
|
2020-06-02 19:18:05 +00:00
|
|
|
} else {
|
|
|
|
std::string_view hashRaw = methodAndHash;
|
2020-06-02 20:20:22 +00:00
|
|
|
return FileSystemHash { FileIngestionMethod::Flat, Hash(string(hashRaw)) };
|
2020-06-02 18:15:38 +00:00
|
|
|
}
|
2020-06-02 19:18:05 +00:00
|
|
|
} else {
|
2020-06-02 20:20:22 +00:00
|
|
|
throw Error("parseContentAddress: format not recognized; has to be text or fixed");
|
2020-06-02 18:15:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-06-02 20:20:22 +00:00
|
|
|
throw Error("Not a content address because it lacks an appropriate prefix");
|
2020-06-02 18:15:38 +00:00
|
|
|
}
|
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-06-01 21:32:27 +00:00
|
|
|
}
|