about summary refs log tree commit diff
path: root/src/nix/copy.cc
blob: e8317dc393fdb51dba898ef32672f1d6709d3345 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "command.hh"
#include "shared.hh"
#include "store-api.hh"
#include "sync.hh"
#include "thread-pool.hh"

#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’");

        ref<Store> srcStore = srcUri.empty() ? store : openStore(srcUri);
        ref<Store> dstStore = dstUri.empty() ? store : openStore(dstUri);

        std::string copiedLabel = "copied";

        logger->setExpected(copiedLabel, storePaths.size());

        ThreadPool pool;

        processGraph<Path>(pool,
            PathSet(storePaths.begin(), storePaths.end()),

            [&](const Path & storePath) {
                return srcStore->queryPathInfo(storePath)->references;
            },

            [&](const Path & storePath) {
                checkInterrupt();

                if (!dstStore->isValidPath(storePath)) {
                    Activity act(*logger, lvlInfo, format("copying ‘%s’...") % storePath);

                    copyStorePath(srcStore, dstStore, storePath);

                    logger->incProgress(copiedLabel);
                } else
                    logger->incExpected(copiedLabel, -1);
            });

        pool.process();
    }
};

static RegisterCommand r1(make_ref<CmdCopy>());