1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-20 23:28:26 -04:00
nix/src/libstore/file-hash.cc

76 lines
2.5 KiB
C++
Raw Normal View History

2020-06-01 17:32:27 -04:00
#include "file-hash.hh"
namespace nix {
std::string FileSystemHash::printMethodAlgo() const {
return makeFileIngestionPrefix(method) + printHashType(hash.type);
}
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)
+ hash.to_string();
}
// FIXME Put this somewhere?
2020-06-01 19:26:40 -04: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) {
return "text:" + th.hash.to_string();
},
[](FileSystemHash fsh) {
return makeFixedOutputCA(fsh.method, fsh.hash);
}
}, ca);
}
ContentAddress parseContentAddress(std::string_view rawCa) {
2020-06-02 14:15:38 -04: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 15:18:05 -04:00
return TextHash { Hash(string(hashTypeAndHash)) };
2020-06-02 14:15:38 -04:00
} else if (prefix == "fixed") {
2020-06-02 15:18:05 -04:00
// This has to be an inverse to makeFixedOutputCA
2020-06-02 14:15:38 -04: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 15:18:05 -04:00
return FileSystemHash { FileIngestionMethod::Recursive, Hash(string(hashRaw)) };
} else {
std::string_view hashRaw = methodAndHash;
return FileSystemHash { FileIngestionMethod::Flat, Hash(string(hashRaw)) };
2020-06-02 14:15:38 -04:00
}
2020-06-02 15:18:05 -04:00
} else {
throw "parseContentAddress: format not recognized; has to be text or fixed";
2020-06-02 14:15:38 -04:00
}
} else {
throw "Not a content address because it lacks an appropriate prefix";
}
2020-06-01 20:37:43 -04:00
};
std::optional<ContentAddress> parseContentAddressOpt(std::string_view rawCaOpt) {
return rawCaOpt == "" ? std::optional<ContentAddress> {} : parseContentAddress(rawCaOpt);
2020-06-01 20:37:43 -04:00
};
std::string renderContentAddress(std::optional<ContentAddress> ca) {
return ca ? renderContentAddress(*ca) : "";
2020-06-01 19:26:40 -04:00
}
2020-06-01 17:32:27 -04:00
}