2016-04-22 12:16:48 +00:00
|
|
|
|
#include "command.hh"
|
|
|
|
|
#include "shared.hh"
|
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
#include "sync.hh"
|
2016-04-22 16:19:48 +00:00
|
|
|
|
#include "thread-pool.hh"
|
2016-04-22 12:16:48 +00:00
|
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
|
|
|
|
|
struct CmdCopy : StorePathsCommand
|
|
|
|
|
{
|
|
|
|
|
std::string srcUri, dstUri;
|
|
|
|
|
|
|
|
|
|
CmdCopy()
|
|
|
|
|
{
|
|
|
|
|
mkFlag(0, "from", "store-uri", "URI of the source Nix store", &srcUri);
|
|
|
|
|
mkFlag(0, "to", "store-uri", "URI of the destination Nix store", &dstUri);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string name() override
|
|
|
|
|
{
|
|
|
|
|
return "copy";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string description() override
|
|
|
|
|
{
|
|
|
|
|
return "copy paths between Nix stores";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Examples examples() override
|
|
|
|
|
{
|
|
|
|
|
return {
|
|
|
|
|
Example{
|
|
|
|
|
"To copy Firefox to the local store to a binary cache in file:///tmp/cache:",
|
|
|
|
|
"nix copy --to file:///tmp/cache -r $(type -p firefox)"
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void run(ref<Store> store, Paths storePaths) override
|
|
|
|
|
{
|
|
|
|
|
if (srcUri.empty() && dstUri.empty())
|
|
|
|
|
throw UsageError("you must pass ‘--from’ and/or ‘--to’");
|
|
|
|
|
|
2016-09-02 10:35:48 +00:00
|
|
|
|
ref<Store> srcStore = srcUri.empty() ? store : openStore(srcUri);
|
|
|
|
|
ref<Store> dstStore = dstUri.empty() ? store : openStore(dstUri);
|
2016-04-22 12:16:48 +00:00
|
|
|
|
|
2016-04-25 13:26:07 +00:00
|
|
|
|
std::string copiedLabel = "copied";
|
2016-04-22 12:16:48 +00:00
|
|
|
|
|
2016-04-25 13:26:07 +00:00
|
|
|
|
logger->setExpected(copiedLabel, storePaths.size());
|
2016-04-22 12:16:48 +00:00
|
|
|
|
|
2016-04-22 16:19:48 +00:00
|
|
|
|
ThreadPool pool;
|
|
|
|
|
|
2016-04-22 18:50:06 +00:00
|
|
|
|
processGraph<Path>(pool,
|
|
|
|
|
PathSet(storePaths.begin(), storePaths.end()),
|
2016-04-22 16:19:48 +00:00
|
|
|
|
|
2016-04-22 18:50:06 +00:00
|
|
|
|
[&](const Path & storePath) {
|
|
|
|
|
return srcStore->queryPathInfo(storePath)->references;
|
|
|
|
|
},
|
2016-04-22 12:16:48 +00:00
|
|
|
|
|
2016-04-22 18:50:06 +00:00
|
|
|
|
[&](const Path & storePath) {
|
|
|
|
|
checkInterrupt();
|
2016-04-22 12:16:48 +00:00
|
|
|
|
|
2016-04-22 18:50:06 +00:00
|
|
|
|
if (!dstStore->isValidPath(storePath)) {
|
2016-04-25 13:26:07 +00:00
|
|
|
|
Activity act(*logger, lvlInfo, format("copying ‘%s’...") % storePath);
|
2016-04-22 12:16:48 +00:00
|
|
|
|
|
2016-05-03 12:45:50 +00:00
|
|
|
|
copyStorePath(srcStore, dstStore, storePath);
|
2016-04-22 16:19:48 +00:00
|
|
|
|
|
2016-04-25 13:26:07 +00:00
|
|
|
|
logger->incProgress(copiedLabel);
|
2016-04-22 18:50:06 +00:00
|
|
|
|
} else
|
2016-04-25 13:26:07 +00:00
|
|
|
|
logger->incExpected(copiedLabel, -1);
|
2016-04-22 18:50:06 +00:00
|
|
|
|
});
|
2016-04-22 12:16:48 +00:00
|
|
|
|
|
2016-04-22 16:19:48 +00:00
|
|
|
|
pool.process();
|
2016-04-22 12:16:48 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static RegisterCommand r1(make_ref<CmdCopy>());
|