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-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-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-07-06 01:53:44 +03:00
|
|
|
std::string ContentAddress::render() const
|
|
|
|
{
|
|
|
|
return std::visit(overloaded {
|
|
|
|
[](const TextIngestionMethod &) -> std::string {
|
|
|
|
return "text:";
|
|
|
|
},
|
|
|
|
[](const FileIngestionMethod & method) {
|
|
|
|
return "fixed:"
|
|
|
|
+ makeFileIngestionPrefix(method);
|
|
|
|
},
|
|
|
|
}, method.raw)
|
|
|
|
+ this->hash.to_string(Base32, true);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2023-07-06 01:53:44 +03:00
|
|
|
auto [caMethod, hashType] = parseContentAddressMethodPrefix(rest);
|
2020-10-13 02:51:23 +03:00
|
|
|
|
2023-07-06 01:53:44 +03:00
|
|
|
return ContentAddress {
|
2023-08-16 19:29:23 +03:00
|
|
|
.method = std::move(caMethod),
|
2023-07-06 01:53:44 +03:00
|
|
|
.hash = Hash::parseNonSRIUnprefixed(rest, hashType),
|
|
|
|
};
|
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-05-09 20:05:38 +03:00
|
|
|
std::string ContentAddress::printMethodAlgo() const
|
|
|
|
{
|
2023-07-06 01:53:44 +03:00
|
|
|
return method.renderPrefix()
|
|
|
|
+ printHashType(hash.type);
|
2023-04-19 21:48:53 +03:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-07-06 01:53:44 +03:00
|
|
|
[&](const TextIngestionMethod &) -> ContentAddressWithReferences {
|
2023-01-06 19:24:20 +02:00
|
|
|
return TextInfo {
|
2023-07-06 01:53:44 +03:00
|
|
|
.hash = ca.hash,
|
2023-01-06 19:24:20 +02:00
|
|
|
.references = {},
|
|
|
|
};
|
2020-10-07 16:52:20 +03:00
|
|
|
},
|
2023-07-06 01:53:44 +03:00
|
|
|
[&](const FileIngestionMethod & method) -> ContentAddressWithReferences {
|
2023-01-06 19:24:20 +02:00
|
|
|
return FixedOutputInfo {
|
2023-07-06 01:53:44 +03:00
|
|
|
.method = method,
|
|
|
|
.hash = ca.hash,
|
2023-01-06 19:24:20 +02:00
|
|
|
.references = {},
|
|
|
|
};
|
2020-10-07 16:52:20 +03:00
|
|
|
},
|
2023-07-06 01:53:44 +03:00
|
|
|
}, ca.method.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 {
|
2023-07-06 01:53:44 +03:00
|
|
|
.hash = std::move(hash),
|
2023-05-09 21:44:08 +03:00
|
|
|
.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 {
|
2023-07-06 01:53:44 +03:00
|
|
|
.method = m2,
|
|
|
|
.hash = std::move(hash),
|
2023-05-09 21:44:08 +03:00
|
|
|
.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 {
|
2023-07-06 01:53:44 +03:00
|
|
|
return fsh.method;
|
2023-04-19 21:13:30 +03:00
|
|
|
},
|
|
|
|
}, 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-07-06 01:53:44 +03:00
|
|
|
return th.hash;
|
2020-10-13 02:51:23 +03:00
|
|
|
},
|
2021-10-01 20:25:22 +03:00
|
|
|
[](const FixedOutputInfo & fsh) {
|
2023-07-06 01:53:44 +03:00
|
|
|
return fsh.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
|
|
|
}
|