nix-super/src/nix/add-to-store.cc

133 lines
3.5 KiB
C++
Raw Normal View History

2017-09-14 14:22:32 +03:00
#include "command.hh"
#include "common-args.hh"
#include "store-api.hh"
#include "archive.hh"
using namespace nix;
static FileIngestionMethod parseIngestionMethod(std::string_view input)
{
if (input == "flat") {
return FileIngestionMethod::Flat;
} else if (input == "nar") {
return FileIngestionMethod::Recursive;
} else {
throw UsageError("Unknown hash mode '%s', expect `flat` or `nar`");
}
}
2017-09-14 14:22:32 +03:00
struct CmdAddToStore : MixDryRun, StoreCommand
{
Path path;
2019-02-12 14:43:32 +02:00
std::optional<std::string> namePart;
FileIngestionMethod ingestionMethod = FileIngestionMethod::Recursive;
2017-09-14 14:22:32 +03:00
CmdAddToStore()
{
// FIXME: completion
2017-09-14 14:22:32 +03:00
expectArg("path", &path);
2020-05-04 23:40:19 +03:00
addFlag({
.longName = "name",
.shortName = 'n',
.description = "Override the name component of the store path. It defaults to the base name of *path*.",
2020-05-04 23:40:19 +03:00
.labels = {"name"},
.handler = {&namePart},
});
addFlag({
.longName = "mode",
.shortName = 'n',
.description = R"(
How to compute the hash of the input.
One of:
- `nar` (the default): Serialises the input as an archive (following the [_Nix Archive Format_](https://edolstra.github.io/pubs/phd-thesis.pdf#page=101)) and passes that to the hash function.
- `flat`: Assumes that the input is a single file and directly passes it to the hash function;
)",
.labels = {"hash-mode"},
.handler = {[this](std::string s) {
this->ingestionMethod = parseIngestionMethod(s);
}},
});
2017-09-14 14:22:32 +03:00
}
void run(ref<Store> store) override
{
if (!namePart) namePart = baseNameOf(path);
StringSink sink;
dumpPath(path, sink);
auto narHash = hashString(htSHA256, sink.s);
Hash hash = narHash;
if (ingestionMethod == FileIngestionMethod::Flat) {
HashSink hsink(htSHA256);
readFile(path, hsink);
hash = hsink.finish().first;
}
2020-08-06 21:31:48 +03:00
ValidPathInfo info {
2020-10-07 16:52:20 +03:00
*store,
std::move(*namePart),
FixedOutputInfo {
.method = std::move(ingestionMethod),
.hash = std::move(hash),
.references = {},
2020-10-07 16:52:20 +03:00
},
2020-08-06 21:31:48 +03:00
narHash,
};
info.narSize = sink.s.size();
2017-09-14 14:22:32 +03:00
if (!dryRun) {
auto source = StringSource(sink.s);
store->addToStore(info, source);
}
2017-09-14 14:22:32 +03:00
logger->cout("%s", store->printStorePath(info.path));
2017-09-14 14:22:32 +03:00
}
};
struct CmdAdd : CmdAddToStore
{
std::string description() override
{
return "Add a file or directory to the Nix store";
}
std::string doc() override
{
return
#include "add.md"
;
}
};
struct CmdAddFile : CmdAddToStore
{
CmdAddFile()
{
ingestionMethod = FileIngestionMethod::Flat;
}
std::string description() override
{
return "Deprecated. Use [`nix store add --mode flat`](@docroot@/command-ref/new-cli/nix3-store-add.md) instead.";
}
};
struct CmdAddPath : CmdAddToStore
{
std::string description() override
{
return "Deprecated alias to [`nix store add`](@docroot@/command-ref/new-cli/nix3-store-add.md).";
2017-09-14 14:22:32 +03:00
}
};
static auto rCmdAddFile = registerCommand2<CmdAddFile>({"store", "add-file"});
static auto rCmdAddPath = registerCommand2<CmdAddPath>({"store", "add-path"});
static auto rCmdAdd = registerCommand2<CmdAdd>({"store", "add"});