nix-super/src/libstore/content-address.cc

256 lines
7.3 KiB
C++
Raw Normal View History

#include "args.hh"
2020-06-02 22:44:58 +03:00
#include "content-address.hh"
2020-07-20 20:42:34 +03:00
#include "split.hh"
2020-06-02 00:32:27 +03:00
namespace nix {
2020-09-22 12:40:19 +03:00
std::string FixedOutputHash::printMethodAlgo() const
{
return makeFileIngestionPrefix(method) + printHashType(hash.type);
2020-06-02 00:32:27 +03:00
}
2020-10-07 16:52:20 +03:00
std::string makeFileIngestionPrefix(FileIngestionMethod m)
2020-09-22 12:40:19 +03:00
{
2020-06-02 00:32:27 +03:00
switch (m) {
case FileIngestionMethod::Flat:
return "";
case FileIngestionMethod::Recursive:
return "r:";
}
2020-10-07 16:52:20 +03:00
assert(false);
2020-06-02 00:32:27 +03:00
}
std::string makeContentAddressingPrefix(ContentAddressMethod m) {
return std::visit(overloaded {
[](TextHashMethod) -> std::string { return "text:"; },
[](FileIngestionMethod m2) {
/* Not prefixed for back compat with things that couldn't produce text before. */
return makeFileIngestionPrefix(m2);
},
}, m);
}
ContentAddressMethod parseContentAddressingPrefix(std::string_view & m)
{
ContentAddressMethod method = FileIngestionMethod::Flat;
if (splitPrefix(m, "r:"))
method = FileIngestionMethod::Recursive;
else if (splitPrefix(m, "text:"))
method = TextHashMethod {};
return method;
}
2020-06-02 00:32:27 +03:00
std::string makeFixedOutputCA(FileIngestionMethod method, const Hash & hash)
{
return "fixed:"
+ makeFileIngestionPrefix(method)
+ hash.to_string(Base32, true);
2020-06-02 00:32:27 +03:00
}
2020-09-22 12:40:19 +03:00
std::string renderContentAddress(ContentAddress ca)
{
2020-06-02 02:26:40 +03:00
return std::visit(overloaded {
[](TextHash & th) {
2020-10-07 16:52:20 +03:00
return "text:"
+ th.hash.to_string(Base32, true);
2020-06-02 02:26:40 +03:00
},
[](FixedOutputHash & fsh) {
2020-10-07 16:52:20 +03:00
return "fixed:"
+ makeFileIngestionPrefix(fsh.method)
+ fsh.hash.to_string(Base32, true);
2020-06-02 02:26:40 +03:00
}
}, ca);
}
std::string renderContentAddressMethodAndHash(ContentAddressMethod cam, HashType ht)
2020-09-22 12:40:19 +03:00
{
return std::visit(overloaded {
[&](TextHashMethod & th) {
return std::string{"text:"} + printHashType(ht);
},
[&](FileIngestionMethod & fim) {
return "fixed:" + makeFileIngestionPrefix(fim) + printHashType(ht);
}
}, cam);
}
/*
Parses content address strings up to the hash.
*/
static std::pair<ContentAddressMethod, HashType> parseContentAddressMethodPrefix(std::string_view & rest)
2020-09-22 12:40:19 +03:00
{
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_();
return {
TextHashMethod {},
std::move(hashType),
};
} else if (prefix == "fixed") {
// Parse method
auto method = FileIngestionMethod::Flat;
if (splitPrefix(rest, "r:"))
method = FileIngestionMethod::Recursive;
HashType hashType = parseHashType_();
return {
std::move(method),
std::move(hashType),
};
} else
2020-08-05 17:47:48 +03: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;
auto [caMethod, hashType_] = parseContentAddressMethodPrefix(rest);
auto hashType = hashType_; // work around clang bug
return std::visit(overloaded {
[&](TextHashMethod &) {
return ContentAddress(TextHash {
.hash = Hash::parseNonSRIUnprefixed(rest, hashType)
});
},
[&](FileIngestionMethod & fim) {
return ContentAddress(FixedOutputHash {
.method = fim,
.hash = Hash::parseNonSRIUnprefixed(rest, hashType),
});
},
}, caMethod);
}
std::pair<ContentAddressMethod, HashType> parseContentAddressMethod(std::string_view caMethod)
2020-09-22 12:40:19 +03:00
{
std::string asPrefix = std::string{caMethod} + ":";
// parseContentAddressMethodPrefix takes its argument by reference
std::string_view asPrefixView = asPrefix;
return parseContentAddressMethodPrefix(asPrefixView);
}
2020-06-02 03:37:43 +03:00
2020-09-22 12:40:19 +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
};
2020-09-22 12:40:19 +03:00
std::string renderContentAddress(std::optional<ContentAddress> ca)
{
2020-06-02 03:37:43 +03:00
return ca ? renderContentAddress(*ca) : "";
2020-06-02 02:26:40 +03:00
}
ContentAddressWithReferences contentAddressFromMethodHashAndRefs(
ContentAddressMethod method, Hash && hash, StoreReferences && refs)
{
return std::visit(overloaded {
[&](TextHashMethod _) -> ContentAddressWithReferences {
if (refs.self)
throw UsageError("Cannot have a self reference with text hashing scheme");
return TextInfo {
{ .hash = std::move(hash) },
.references = std::move(refs.others),
};
},
[&](FileIngestionMethod m2) -> ContentAddressWithReferences {
return FixedOutputInfo {
{
.method = m2,
.hash = std::move(hash),
},
.references = std::move(refs),
};
},
}, method);
}
ContentAddressMethod getContentAddressMethod(const ContentAddressWithReferences & ca)
{
return std::visit(overloaded {
[](const TextInfo & th) -> ContentAddressMethod {
return TextHashMethod {};
},
[](const FixedOutputInfo & fsh) -> ContentAddressMethod {
return fsh.method;
},
}, ca);
}
Hash getContentAddressHash(const ContentAddress & ca)
{
return std::visit(overloaded {
[](const TextHash & th) {
return th.hash;
},
[](const FixedOutputHash & fsh) {
return fsh.hash;
2020-10-07 16:52:20 +03:00
},
}, ca);
}
bool StoreReferences::empty() const
{
return !self && others.empty();
}
size_t StoreReferences::size() const
{
return (self ? 1 : 0) + others.size();
}
2020-10-07 16:52:20 +03:00
ContentAddressWithReferences caWithoutRefs(const ContentAddress & ca) {
return std::visit(overloaded {
[&](const TextHash & h) -> ContentAddressWithReferences {
return TextInfo {
h,
.references = {},
};
2020-10-07 16:52:20 +03:00
},
[&](const FixedOutputHash & h) -> ContentAddressWithReferences {
return FixedOutputInfo {
h,
.references = {},
};
2020-10-07 16:52:20 +03:00
},
}, ca);
}
Hash getContentAddressHash(const ContentAddressWithReferences & ca)
{
return std::visit(overloaded {
[](const TextInfo & th) {
return th.hash;
},
[](const FixedOutputInfo & fsh) {
return fsh.hash;
},
}, ca);
}
std::string printMethodAlgo(const ContentAddressWithReferences & ca) {
return makeContentAddressingPrefix(getContentAddressMethod(ca))
+ printHashType(getContentAddressHash(ca).type);
}
2020-06-02 00:32:27 +03:00
}