2023-11-04 22:25:41 +02:00
|
|
|
#include "file-content-address.hh"
|
|
|
|
#include "archive.hh"
|
2023-09-04 16:51:23 +03:00
|
|
|
#include "git.hh"
|
2023-11-04 22:25:41 +02:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2023-09-04 16:51:23 +03:00
|
|
|
static std::optional<FileSerialisationMethod> parseFileSerialisationMethodOpt(std::string_view input)
|
2024-02-13 16:54:07 +02:00
|
|
|
{
|
|
|
|
if (input == "flat") {
|
2023-09-04 16:51:23 +03:00
|
|
|
return FileSerialisationMethod::Flat;
|
2024-02-13 16:54:07 +02:00
|
|
|
} else if (input == "nar") {
|
2023-09-04 16:51:23 +03:00
|
|
|
return FileSerialisationMethod::Recursive;
|
2024-02-13 16:54:07 +02:00
|
|
|
} else {
|
2023-09-04 16:51:23 +03:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSerialisationMethod parseFileSerialisationMethod(std::string_view input)
|
|
|
|
{
|
|
|
|
auto ret = parseFileSerialisationMethodOpt(input);
|
|
|
|
if (ret)
|
|
|
|
return *ret;
|
|
|
|
else
|
|
|
|
throw UsageError("Unknown file serialiation method '%s', expect `flat` or `nar`");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FileIngestionMethod parseFileIngestionMethod(std::string_view input)
|
|
|
|
{
|
|
|
|
if (input == "git") {
|
|
|
|
return FileIngestionMethod::Git;
|
|
|
|
} else {
|
|
|
|
auto ret = parseFileSerialisationMethodOpt(input);
|
|
|
|
if (ret)
|
|
|
|
return static_cast<FileIngestionMethod>(*ret);
|
|
|
|
else
|
|
|
|
throw UsageError("Unknown file ingestion method '%s', expect `flat`, `nar`, or `git`");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string_view renderFileSerialisationMethod(FileSerialisationMethod method)
|
|
|
|
{
|
|
|
|
switch (method) {
|
|
|
|
case FileSerialisationMethod::Flat:
|
|
|
|
return "flat";
|
|
|
|
case FileSerialisationMethod::Recursive:
|
|
|
|
return "nar";
|
|
|
|
default:
|
|
|
|
assert(false);
|
2024-02-13 16:54:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string_view renderFileIngestionMethod(FileIngestionMethod method)
|
|
|
|
{
|
|
|
|
switch (method) {
|
|
|
|
case FileIngestionMethod::Flat:
|
|
|
|
case FileIngestionMethod::Recursive:
|
2023-09-04 16:51:23 +03:00
|
|
|
return renderFileSerialisationMethod(
|
|
|
|
static_cast<FileSerialisationMethod>(method));
|
|
|
|
case FileIngestionMethod::Git:
|
|
|
|
return "git";
|
2024-02-13 16:54:07 +02:00
|
|
|
default:
|
2024-02-13 19:50:10 +02:00
|
|
|
abort();
|
2024-02-13 16:54:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-04 22:25:41 +02:00
|
|
|
void dumpPath(
|
|
|
|
SourceAccessor & accessor, const CanonPath & path,
|
|
|
|
Sink & sink,
|
2023-09-04 16:51:23 +03:00
|
|
|
FileSerialisationMethod method,
|
2023-11-04 22:25:41 +02:00
|
|
|
PathFilter & filter)
|
|
|
|
{
|
|
|
|
switch (method) {
|
2023-09-04 16:51:23 +03:00
|
|
|
case FileSerialisationMethod::Flat:
|
2023-11-04 22:25:41 +02:00
|
|
|
accessor.readFile(path, sink);
|
|
|
|
break;
|
2023-09-04 16:51:23 +03:00
|
|
|
case FileSerialisationMethod::Recursive:
|
2023-11-04 22:25:41 +02:00
|
|
|
accessor.dumpPath(path, sink, filter);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void restorePath(
|
|
|
|
const Path & path,
|
|
|
|
Source & source,
|
2023-09-04 16:51:23 +03:00
|
|
|
FileSerialisationMethod method)
|
2023-11-04 22:25:41 +02:00
|
|
|
{
|
|
|
|
switch (method) {
|
2023-09-04 16:51:23 +03:00
|
|
|
case FileSerialisationMethod::Flat:
|
2023-11-04 22:25:41 +02:00
|
|
|
writeFile(path, source);
|
|
|
|
break;
|
2023-09-04 16:51:23 +03:00
|
|
|
case FileSerialisationMethod::Recursive:
|
2023-11-04 22:25:41 +02:00
|
|
|
restorePath(path, source);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HashResult hashPath(
|
|
|
|
SourceAccessor & accessor, const CanonPath & path,
|
2023-09-04 16:51:23 +03:00
|
|
|
FileSerialisationMethod method, HashAlgorithm ha,
|
2023-11-04 22:25:41 +02:00
|
|
|
PathFilter & filter)
|
|
|
|
{
|
2024-02-25 20:04:20 +02:00
|
|
|
HashSink sink { ha };
|
2023-11-04 22:25:41 +02:00
|
|
|
dumpPath(accessor, path, sink, method, filter);
|
|
|
|
return sink.finish();
|
|
|
|
}
|
|
|
|
|
2023-09-04 16:51:23 +03:00
|
|
|
|
|
|
|
Hash hashPath(
|
|
|
|
SourceAccessor & accessor, const CanonPath & path,
|
|
|
|
FileIngestionMethod method, HashAlgorithm ht,
|
|
|
|
PathFilter & filter)
|
|
|
|
{
|
|
|
|
switch (method) {
|
|
|
|
case FileIngestionMethod::Flat:
|
|
|
|
case FileIngestionMethod::Recursive:
|
|
|
|
return hashPath(accessor, path, (FileSerialisationMethod) method, ht, filter).first;
|
|
|
|
case FileIngestionMethod::Git:
|
|
|
|
return git::dumpHash(ht, accessor, path, filter).hash;
|
|
|
|
}
|
2024-02-28 23:59:20 +02:00
|
|
|
assert(false);
|
2023-09-04 16:51:23 +03:00
|
|
|
}
|
|
|
|
|
2023-11-04 22:25:41 +02:00
|
|
|
}
|