2020-06-22 21:08:27 +03:00
|
|
|
#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
|
|
|
|
{
|
2020-06-23 20:03:10 +03:00
|
|
|
return makeFileIngestionPrefix(method) + printHashType(hash.type);
|
2020-06-02 00:32:27 +03:00
|
|
|
}
|
|
|
|
|
2020-10-13 02:51:23 +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:";
|
2023-01-30 16:59:55 +02:00
|
|
|
default:
|
|
|
|
throw Error("impossible, caught both cases");
|
2020-06-02 00:32:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-09 20:05:38 +03:00
|
|
|
std::string ContentAddressMethod::renderPrefix() const
|
|
|
|
{
|
2020-10-13 02:51:23 +03:00
|
|
|
return std::visit(overloaded {
|
2023-04-18 02:02:45 +03:00
|
|
|
[](TextIngestionMethod) -> std::string { return "text:"; },
|
2020-10-13 02:51:23 +03:00
|
|
|
[](FileIngestionMethod m2) {
|
|
|
|
/* Not prefixed for back compat with things that couldn't produce text before. */
|
|
|
|
return makeFileIngestionPrefix(m2);
|
|
|
|
},
|
2023-04-01 23:40:32 +03:00
|
|
|
}, raw);
|
2020-10-13 02:51:23 +03:00
|
|
|
}
|
|
|
|
|
2023-04-01 23:40:32 +03:00
|
|
|
ContentAddressMethod ContentAddressMethod::parsePrefix(std::string_view & m)
|
2020-10-13 02:51:23 +03:00
|
|
|
{
|
|
|
|
ContentAddressMethod method = FileIngestionMethod::Flat;
|
|
|
|
if (splitPrefix(m, "r:"))
|
|
|
|
method = FileIngestionMethod::Recursive;
|
|
|
|
else if (splitPrefix(m, "text:"))
|
2023-04-18 02:02:45 +03:00
|
|
|
method = TextIngestionMethod {};
|
2020-10-13 02:51:23 +03:00
|
|
|
return method;
|
|
|
|
}
|
|
|
|
|
2023-03-31 00:12:49 +03:00
|
|
|
std::string ContentAddress::render() const
|
2020-09-22 12:40:19 +03:00
|
|
|
{
|
2020-06-02 02:26:40 +03:00
|
|
|
return std::visit(overloaded {
|
2023-03-31 00:12:49 +03:00
|
|
|
[](const 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
|
|
|
},
|
2023-03-31 00:12:49 +03:00
|
|
|
[](const 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
|
|
|
}
|
2023-03-31 00:12:49 +03:00
|
|
|
}, raw);
|
2020-06-02 02:26:40 +03:00
|
|
|
}
|
|
|
|
|
2023-04-01 23:40:32 +03:00
|
|
|
std::string ContentAddressMethod::render(HashType ht) const
|
2020-09-22 12:40:19 +03:00
|
|
|
{
|
2020-09-17 18:15:05 +03:00
|
|
|
return std::visit(overloaded {
|
2023-04-18 02:02:45 +03:00
|
|
|
[&](const TextIngestionMethod & th) {
|
2020-10-13 02:51:23 +03:00
|
|
|
return std::string{"text:"} + printHashType(ht);
|
2020-09-17 18:15:05 +03:00
|
|
|
},
|
2023-04-01 23:40:32 +03:00
|
|
|
[&](const FileIngestionMethod & fim) {
|
2020-10-13 02:51:23 +03:00
|
|
|
return "fixed:" + makeFileIngestionPrefix(fim) + printHashType(ht);
|
2020-09-17 18:15:05 +03:00
|
|
|
}
|
2023-03-31 00:12:49 +03:00
|
|
|
}, raw);
|
2020-09-17 18:15:05 +03:00
|
|
|
}
|
|
|
|
|
2023-03-31 00:12:49 +03:00
|
|
|
/**
|
|
|
|
* Parses content address strings up to the hash.
|
2020-09-17 18:15:05 +03:00
|
|
|
*/
|
2020-10-13 02:51:23 +03:00
|
|
|
static std::pair<ContentAddressMethod, HashType> parseContentAddressMethodPrefix(std::string_view & rest)
|
2020-09-22 12:40:19 +03:00
|
|
|
{
|
2020-09-17 23:04:05 +03:00
|
|
|
std::string_view wholeInput { rest };
|
2020-06-22 21:08:27 +03:00
|
|
|
|
2020-06-30 18:57:46 +03:00
|
|
|
std::string_view prefix;
|
|
|
|
{
|
2020-07-03 02:16:57 +03:00
|
|
|
auto optPrefix = splitPrefixTo(rest, ':');
|
2020-06-30 18:57:46 +03:00
|
|
|
if (!optPrefix)
|
2020-09-17 18:15:05 +03:00
|
|
|
throw UsageError("not a content address because it is not in the form '<prefix>:<rest>': %s", wholeInput);
|
2020-06-30 18:57:46 +03:00
|
|
|
prefix = *optPrefix;
|
|
|
|
}
|
2020-06-22 21:08:27 +03:00
|
|
|
|
|
|
|
auto parseHashType_ = [&](){
|
2020-07-03 02:16:57 +03:00
|
|
|
auto hashTypeRaw = splitPrefixTo(rest, ':');
|
2020-06-30 18:57:46 +03:00
|
|
|
if (!hashTypeRaw)
|
2020-09-17 18:15:05 +03:00
|
|
|
throw UsageError("content address hash must be in form '<algo>:<hash>', but found: %s", wholeInput);
|
2020-06-30 18:57:46 +03:00
|
|
|
HashType hashType = parseHashType(*hashTypeRaw);
|
2020-06-22 21:08:27 +03:00
|
|
|
return std::move(hashType);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Switch on prefix
|
|
|
|
if (prefix == "text") {
|
2020-09-17 18:15:05 +03:00
|
|
|
// No parsing of the ingestion method, "text" only support flat.
|
2020-06-22 21:08:27 +03:00
|
|
|
HashType hashType = parseHashType_();
|
2020-10-13 02:51:23 +03:00
|
|
|
return {
|
2023-04-18 02:02:45 +03:00
|
|
|
TextIngestionMethod {},
|
2020-10-13 02:51:23 +03:00
|
|
|
std::move(hashType),
|
|
|
|
};
|
2020-06-22 21:08:27 +03:00
|
|
|
} else if (prefix == "fixed") {
|
|
|
|
// Parse method
|
|
|
|
auto method = FileIngestionMethod::Flat;
|
2020-07-03 02:16:57 +03:00
|
|
|
if (splitPrefix(rest, "r:"))
|
2020-06-22 21:08:27 +03:00
|
|
|
method = FileIngestionMethod::Recursive;
|
|
|
|
HashType hashType = parseHashType_();
|
2020-10-13 02:51:23 +03:00
|
|
|
return {
|
|
|
|
std::move(method),
|
|
|
|
std::move(hashType),
|
2020-06-22 21:08:27 +03:00
|
|
|
};
|
|
|
|
} else
|
2020-08-05 17:47:48 +03:00
|
|
|
throw UsageError("content address prefix '%s' is unrecognized. Recogonized prefixes are 'text' or 'fixed'", prefix);
|
2020-09-17 18:15:05 +03:00
|
|
|
}
|
|
|
|
|
2023-05-09 20:05:38 +03:00
|
|
|
ContentAddress ContentAddress::parse(std::string_view rawCa)
|
|
|
|
{
|
2020-09-17 18:15:05 +03:00
|
|
|
auto rest = rawCa;
|
|
|
|
|
2020-10-13 07:11:25 +03:00
|
|
|
auto [caMethod, hashType_] = parseContentAddressMethodPrefix(rest);
|
|
|
|
auto hashType = hashType_; // work around clang bug
|
2020-10-13 02:51:23 +03:00
|
|
|
|
|
|
|
return std::visit(overloaded {
|
2023-04-18 02:02:45 +03:00
|
|
|
[&](TextIngestionMethod &) {
|
2020-10-13 02:51:23 +03:00
|
|
|
return ContentAddress(TextHash {
|
|
|
|
.hash = Hash::parseNonSRIUnprefixed(rest, hashType)
|
|
|
|
});
|
|
|
|
},
|
2021-10-01 20:25:22 +03:00
|
|
|
[&](FileIngestionMethod & fim) {
|
2020-10-13 02:51:23 +03:00
|
|
|
return ContentAddress(FixedOutputHash {
|
|
|
|
.method = fim,
|
|
|
|
.hash = Hash::parseNonSRIUnprefixed(rest, hashType),
|
|
|
|
});
|
|
|
|
},
|
2023-04-01 23:40:32 +03:00
|
|
|
}, caMethod.raw);
|
2020-10-13 02:51:23 +03:00
|
|
|
}
|
|
|
|
|
2023-04-01 23:40:32 +03:00
|
|
|
std::pair<ContentAddressMethod, HashType> ContentAddressMethod::parse(std::string_view caMethod)
|
2020-09-22 12:40:19 +03:00
|
|
|
{
|
2021-11-23 17:15:34 +02:00
|
|
|
std::string asPrefix = std::string{caMethod} + ":";
|
|
|
|
// parseContentAddressMethodPrefix takes its argument by reference
|
|
|
|
std::string_view asPrefixView = asPrefix;
|
|
|
|
return parseContentAddressMethodPrefix(asPrefixView);
|
2020-09-17 18:15:05 +03:00
|
|
|
}
|
2020-06-02 03:37:43 +03:00
|
|
|
|
2023-03-31 00:12:49 +03:00
|
|
|
std::optional<ContentAddress> ContentAddress::parseOpt(std::string_view rawCaOpt)
|
2020-09-22 12:40:19 +03:00
|
|
|
{
|
2023-03-31 00:12:49 +03:00
|
|
|
return rawCaOpt == ""
|
|
|
|
? std::nullopt
|
|
|
|
: std::optional { ContentAddress::parse(rawCaOpt) };
|
2020-06-02 03:37:43 +03:00
|
|
|
};
|
|
|
|
|
2020-09-22 12:40:19 +03:00
|
|
|
std::string renderContentAddress(std::optional<ContentAddress> ca)
|
|
|
|
{
|
2023-03-31 00:12:49 +03:00
|
|
|
return ca ? ca->render() : "";
|
2020-06-02 02:26:40 +03:00
|
|
|
}
|
|
|
|
|
2023-04-19 21:13:30 +03:00
|
|
|
ContentAddress ContentAddress::fromParts(
|
2023-05-09 20:05:38 +03:00
|
|
|
ContentAddressMethod method, Hash hash) noexcept
|
2020-10-13 02:51:23 +03:00
|
|
|
{
|
|
|
|
return std::visit(overloaded {
|
2023-04-19 21:13:30 +03:00
|
|
|
[&](TextIngestionMethod _) -> ContentAddress {
|
|
|
|
return TextHash {
|
|
|
|
.hash = std::move(hash),
|
2020-10-13 02:51:23 +03:00
|
|
|
};
|
|
|
|
},
|
2023-04-19 21:13:30 +03:00
|
|
|
[&](FileIngestionMethod m2) -> ContentAddress {
|
|
|
|
return FixedOutputHash {
|
|
|
|
.method = std::move(m2),
|
|
|
|
.hash = std::move(hash),
|
2020-10-13 02:51:23 +03:00
|
|
|
};
|
|
|
|
},
|
2023-04-01 23:40:32 +03:00
|
|
|
}, method.raw);
|
2020-10-13 02:51:23 +03:00
|
|
|
}
|
|
|
|
|
2023-04-19 21:13:30 +03:00
|
|
|
ContentAddressMethod ContentAddress::getMethod() const
|
2020-10-13 02:51:23 +03:00
|
|
|
{
|
|
|
|
return std::visit(overloaded {
|
2023-04-19 21:13:30 +03:00
|
|
|
[](const TextHash & th) -> ContentAddressMethod {
|
2023-04-18 02:02:45 +03:00
|
|
|
return TextIngestionMethod {};
|
2020-10-13 02:51:23 +03:00
|
|
|
},
|
2023-04-19 21:13:30 +03:00
|
|
|
[](const FixedOutputHash & fsh) -> ContentAddressMethod {
|
|
|
|
return fsh.method;
|
2020-10-13 02:51:23 +03:00
|
|
|
},
|
2023-04-01 23:40:32 +03:00
|
|
|
}, raw);
|
2020-10-13 02:51:23 +03:00
|
|
|
}
|
|
|
|
|
2023-03-31 00:12:49 +03:00
|
|
|
const Hash & ContentAddress::getHash() const
|
2020-07-10 14:21:37 +03:00
|
|
|
{
|
|
|
|
return std::visit(overloaded {
|
2023-03-31 00:12:49 +03:00
|
|
|
[](const TextHash & th) -> auto & {
|
2020-07-10 14:21:37 +03:00
|
|
|
return th.hash;
|
|
|
|
},
|
2023-03-31 00:12:49 +03:00
|
|
|
[](const FixedOutputHash & fsh) -> auto & {
|
2020-07-10 14:21:37 +03:00
|
|
|
return fsh.hash;
|
2020-10-07 16:52:20 +03:00
|
|
|
},
|
2023-03-31 00:12:49 +03:00
|
|
|
}, raw);
|
2020-10-07 16:52:20 +03:00
|
|
|
}
|
|
|
|
|
2023-05-09 20:05:38 +03:00
|
|
|
std::string ContentAddress::printMethodAlgo() const
|
|
|
|
{
|
2023-04-19 21:48:53 +03:00
|
|
|
return getMethod().renderPrefix()
|
|
|
|
+ printHashType(getHash().type);
|
|
|
|
}
|
|
|
|
|
2023-01-14 23:38:43 +02:00
|
|
|
bool StoreReferences::empty() const
|
|
|
|
{
|
|
|
|
return !self && others.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t StoreReferences::size() const
|
|
|
|
{
|
|
|
|
return (self ? 1 : 0) + others.size();
|
|
|
|
}
|
|
|
|
|
2023-05-09 20:05:38 +03:00
|
|
|
ContentAddressWithReferences ContentAddressWithReferences::withoutRefs(const ContentAddress & ca) noexcept
|
|
|
|
{
|
2020-10-07 16:52:20 +03:00
|
|
|
return std::visit(overloaded {
|
2021-10-01 20:12:54 +03:00
|
|
|
[&](const TextHash & h) -> ContentAddressWithReferences {
|
2023-01-06 19:24:20 +02:00
|
|
|
return TextInfo {
|
2023-02-28 19:13:43 +02:00
|
|
|
.hash = h,
|
2023-01-06 19:24:20 +02:00
|
|
|
.references = {},
|
|
|
|
};
|
2020-10-07 16:52:20 +03:00
|
|
|
},
|
2021-10-01 20:12:54 +03:00
|
|
|
[&](const FixedOutputHash & h) -> ContentAddressWithReferences {
|
2023-01-06 19:24:20 +02:00
|
|
|
return FixedOutputInfo {
|
2023-02-28 19:13:43 +02:00
|
|
|
.hash = h,
|
2023-01-06 19:24:20 +02:00
|
|
|
.references = {},
|
|
|
|
};
|
2020-10-07 16:52:20 +03:00
|
|
|
},
|
2023-03-31 00:12:49 +03:00
|
|
|
}, ca.raw);
|
2020-07-10 14:21:37 +03:00
|
|
|
}
|
|
|
|
|
2023-05-09 21:44:08 +03:00
|
|
|
std::optional<ContentAddressWithReferences> ContentAddressWithReferences::fromPartsOpt(
|
|
|
|
ContentAddressMethod method, Hash hash, StoreReferences refs) noexcept
|
2023-04-19 21:13:30 +03:00
|
|
|
{
|
|
|
|
return std::visit(overloaded {
|
2023-05-09 21:44:08 +03:00
|
|
|
[&](TextIngestionMethod _) -> std::optional<ContentAddressWithReferences> {
|
2023-04-19 21:13:30 +03:00
|
|
|
if (refs.self)
|
2023-05-09 21:44:08 +03:00
|
|
|
return std::nullopt;
|
|
|
|
return ContentAddressWithReferences {
|
|
|
|
TextInfo {
|
|
|
|
.hash = { .hash = std::move(hash) },
|
|
|
|
.references = std::move(refs.others),
|
|
|
|
}
|
2023-04-19 21:13:30 +03:00
|
|
|
};
|
|
|
|
},
|
2023-05-09 21:44:08 +03:00
|
|
|
[&](FileIngestionMethod m2) -> std::optional<ContentAddressWithReferences> {
|
|
|
|
return ContentAddressWithReferences {
|
|
|
|
FixedOutputInfo {
|
|
|
|
.hash = {
|
|
|
|
.method = m2,
|
|
|
|
.hash = std::move(hash),
|
|
|
|
},
|
|
|
|
.references = std::move(refs),
|
|
|
|
}
|
2023-04-19 21:13:30 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
}, method.raw);
|
|
|
|
}
|
|
|
|
|
|
|
|
ContentAddressMethod ContentAddressWithReferences::getMethod() const
|
|
|
|
{
|
|
|
|
return std::visit(overloaded {
|
|
|
|
[](const TextInfo & th) -> ContentAddressMethod {
|
|
|
|
return TextIngestionMethod {};
|
|
|
|
},
|
|
|
|
[](const FixedOutputInfo & fsh) -> ContentAddressMethod {
|
|
|
|
return fsh.hash.method;
|
|
|
|
},
|
|
|
|
}, raw);
|
|
|
|
}
|
|
|
|
|
2023-04-01 23:40:32 +03:00
|
|
|
Hash ContentAddressWithReferences::getHash() const
|
2020-10-13 02:51:23 +03:00
|
|
|
{
|
|
|
|
return std::visit(overloaded {
|
2021-10-01 20:25:22 +03:00
|
|
|
[](const TextInfo & th) {
|
2023-02-28 19:46:00 +02:00
|
|
|
return th.hash.hash;
|
2020-10-13 02:51:23 +03:00
|
|
|
},
|
2021-10-01 20:25:22 +03:00
|
|
|
[](const FixedOutputInfo & fsh) {
|
2023-02-28 19:46:00 +02:00
|
|
|
return fsh.hash.hash;
|
2020-10-13 02:51:23 +03:00
|
|
|
},
|
2023-04-01 23:40:32 +03:00
|
|
|
}, raw);
|
2020-10-13 02:51:23 +03:00
|
|
|
}
|
|
|
|
|
2020-06-02 00:32:27 +03:00
|
|
|
}
|