lix/src/libstore/content-address.cc

183 lines
5.3 KiB
C++
Raw Normal View History

#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-09-22 09:40:19 +00:00
std::string FixedOutputHash::printMethodAlgo() const
{
return makeFileIngestionPrefix(method) + printHashType(hash.type);
2020-06-01 21:32:27 +00:00
}
2020-10-07 13:52:20 +00:00
2022-04-19 22:25:21 +00:00
std::string makeFileIngestionPrefix(FileIngestionMethod m)
2020-09-22 09:40:19 +00:00
{
2020-06-01 21:32:27 +00:00
switch (m) {
case FileIngestionMethod::Flat:
return "";
case FileIngestionMethod::Recursive:
return "r:";
default:
throw Error("impossible, caught both cases");
2020-06-01 21:32:27 +00:00
}
}
std::string makeFixedOutputCA(FileIngestionMethod method, const Hash & hash)
{
return "fixed:"
+ makeFileIngestionPrefix(method)
+ hash.to_string(Base32, true);
2020-06-01 21:32:27 +00:00
}
2020-09-22 09:40:19 +00:00
std::string renderContentAddress(ContentAddress ca)
{
2020-06-01 23:26:40 +00:00
return std::visit(overloaded {
[](TextHash & th) {
2020-10-07 13:52:20 +00:00
return "text:"
+ th.hash.to_string(Base32, true);
2020-06-01 23:26:40 +00:00
},
[](FixedOutputHash & fsh) {
2020-10-07 13:52:20 +00:00
return "fixed:"
+ makeFileIngestionPrefix(fsh.method)
+ fsh.hash.to_string(Base32, true);
2020-06-01 23:26:40 +00:00
}
}, ca);
}
2020-09-22 09:40:19 +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.
*/
2020-09-22 09:40:19 +00:00
static ContentAddressMethod parseContentAddressMethodPrefix(std::string_view & rest)
{
std::string_view wholeInput { rest };
std::string_view prefix;
{
auto optPrefix = splitPrefixTo(rest, ':');
if (!optPrefix)
throw UsageError("not a content address because it is not in the form '<prefix>:<rest>': %s", wholeInput);
prefix = *optPrefix;
}
auto parseHashType_ = [&](){
auto hashTypeRaw = splitPrefixTo(rest, ':');
if (!hashTypeRaw)
throw UsageError("content address hash must be in form '<algo>:<hash>', but found: %s", wholeInput);
HashType hashType = parseHashType(*hashTypeRaw);
return std::move(hashType);
};
// Switch on prefix
if (prefix == "text") {
// No parsing of the ingestion 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 TextHashMethod {};
} else if (prefix == "fixed") {
// Parse method
auto method = FileIngestionMethod::Flat;
if (splitPrefix(rest, "r:"))
method = FileIngestionMethod::Recursive;
HashType hashType = parseHashType_();
return FixedOutputHashMethod {
.fileIngestionMethod = method,
.hashType = std::move(hashType),
};
} else
2020-08-05 14:47:48 +00:00
throw UsageError("content address prefix '%s' is unrecognized. Recogonized prefixes are 'text' or 'fixed'", prefix);
}
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);
}
2020-09-22 09:40:19 +00:00
ContentAddressMethod parseContentAddressMethod(std::string_view caMethod)
{
std::string asPrefix = std::string{caMethod} + ":";
// parseContentAddressMethodPrefix takes its argument by reference
std::string_view asPrefixView = asPrefix;
return parseContentAddressMethodPrefix(asPrefixView);
}
2020-06-02 00:37:43 +00:00
2020-09-22 09:40:19 +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
};
2020-09-22 09:40:19 +00:00
std::string renderContentAddress(std::optional<ContentAddress> ca)
{
2020-06-02 00:37:43 +00:00
return ca ? renderContentAddress(*ca) : "";
2020-06-01 23:26:40 +00:00
}
Hash getContentAddressHash(const ContentAddress & ca)
{
return std::visit(overloaded {
[](const TextHash & th) {
return th.hash;
},
[](const FixedOutputHash & fsh) {
return fsh.hash;
2020-10-07 13:52:20 +00:00
},
}, ca);
}
bool StoreReferences::empty() const
{
return !self && others.empty();
}
size_t StoreReferences::size() const
{
return (self ? 1 : 0) + others.size();
}
2020-10-07 13:52:20 +00:00
ContentAddressWithReferences caWithoutRefs(const ContentAddress & ca) {
return std::visit(overloaded {
[&](const TextHash & h) -> ContentAddressWithReferences {
return TextInfo {
h,
.references = {},
};
2020-10-07 13:52:20 +00:00
},
[&](const FixedOutputHash & h) -> ContentAddressWithReferences {
return FixedOutputInfo {
h,
.references = {},
};
2020-10-07 13:52:20 +00:00
},
}, ca);
}
2020-06-01 21:32:27 +00:00
}