From 6448ea84ab537600d3f350867063bc305b3bb910 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 17 Jan 2022 13:58:26 +0100 Subject: [PATCH] Factor out --from / --to logic --- src/libcmd/command.cc | 54 +++++++++++++++++++++++++++++++++++++------ src/libcmd/command.hh | 17 ++++++++++++++ src/nix/copy.cc | 39 ++----------------------------- 3 files changed, 66 insertions(+), 44 deletions(-) diff --git a/src/libcmd/command.cc b/src/libcmd/command.cc index 429cd32cc..5e6d4a857 100644 --- a/src/libcmd/command.cc +++ b/src/libcmd/command.cc @@ -73,13 +73,16 @@ ref EvalCommand::getEvalStore() ref EvalCommand::getEvalState() { - if (!evalState) evalState = -#if HAVE_BOEHMGC - std::allocate_shared(traceable_allocator(), -#else - std::make_shared( -#endif - searchPath, getEvalStore(), getStore()); + if (!evalState) + evalState = + #if HAVE_BOEHMGC + std::allocate_shared(traceable_allocator(), + searchPath, getEvalStore(), getStore()) + #else + std::make_shared( + searchPath, getEvalStore(), getStore()) + #endif + ; return ref(evalState); } @@ -156,6 +159,43 @@ void StorePathsCommand::run(ref store, BuiltPaths && paths) run(store, std::move(sorted)); } +CopyCommand::CopyCommand() + : BuiltPathsCommand(true) +{ + addFlag({ + .longName = "from", + .description = "URL of the source Nix store.", + .labels = {"store-uri"}, + .handler = {&srcUri}, + }); + + addFlag({ + .longName = "to", + .description = "URL of the destination Nix store.", + .labels = {"store-uri"}, + .handler = {&dstUri}, + }); +} + +ref CopyCommand::createStore() +{ + return srcUri.empty() ? StoreCommand::createStore() : openStore(srcUri); +} + +void CopyCommand::run(ref store) +{ + if (srcUri.empty() && dstUri.empty()) + throw UsageError("you must pass '--from' and/or '--to'"); + + BuiltPathsCommand::run(store); +} + +void CopyCommand::run(ref srcStore, BuiltPaths && paths) +{ + ref dstStore = dstUri.empty() ? openStore() : openStore(dstUri); + run(srcStore, dstStore, std::move(paths)); +} + void StorePathCommand::run(ref store, std::vector && storePaths) { if (storePaths.size() != 1) diff --git a/src/libcmd/command.hh b/src/libcmd/command.hh index 07f398468..0c3e29e25 100644 --- a/src/libcmd/command.hh +++ b/src/libcmd/command.hh @@ -176,6 +176,23 @@ public: bool useDefaultInstallables() override { return !all; } }; +/* A command that copies something between `--from` and `--to` + stores. */ +struct CopyCommand : virtual BuiltPathsCommand +{ + std::string srcUri, dstUri; + + CopyCommand(); + + ref createStore() override; + + void run(ref store) override; + + void run(ref srcStore, BuiltPaths && paths) override; + + virtual void run(ref srcStore, ref dstStore, BuiltPaths && paths) = 0; +}; + struct StorePathsCommand : public BuiltPathsCommand { StorePathsCommand(bool recursive = false); diff --git a/src/nix/copy.cc b/src/nix/copy.cc index 197c85316..9f7cef304 100644 --- a/src/nix/copy.cc +++ b/src/nix/copy.cc @@ -1,17 +1,11 @@ #include "command.hh" #include "shared.hh" #include "store-api.hh" -#include "sync.hh" -#include "thread-pool.hh" - -#include using namespace nix; -struct CmdCopy : BuiltPathsCommand +struct CmdCopy : CopyCommand { - std::string srcUri, dstUri; - CheckSigsFlag checkSigs = CheckSigs; SubstituteFlag substitute = NoSubstitute; @@ -21,20 +15,6 @@ struct CmdCopy : BuiltPathsCommand CmdCopy() : BuiltPathsCommand(true) { - addFlag({ - .longName = "from", - .description = "URL of the source Nix store.", - .labels = {"store-uri"}, - .handler = {&srcUri}, - }); - - addFlag({ - .longName = "to", - .description = "URL of the destination Nix store.", - .labels = {"store-uri"}, - .handler = {&dstUri}, - }); - addFlag({ .longName = "no-check-sigs", .description = "Do not require that paths are signed by trusted keys.", @@ -65,23 +45,8 @@ struct CmdCopy : BuiltPathsCommand Category category() override { return catSecondary; } - ref createStore() override + void run(ref srcStore, ref dstStore, BuiltPaths && paths) override { - return srcUri.empty() ? StoreCommand::createStore() : openStore(srcUri); - } - - void run(ref store) override - { - if (srcUri.empty() && dstUri.empty()) - throw UsageError("you must pass '--from' and/or '--to'"); - - BuiltPathsCommand::run(store); - } - - void run(ref srcStore, BuiltPaths && paths) override - { - ref dstStore = dstUri.empty() ? openStore() : openStore(dstUri); - RealisedPath::Set stuffToCopy; for (auto & builtPath : paths) {