aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/make-content-addressed.cc
blob: 626a2248004bb33537ad84522e2695fb796cf3db (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "make-content-addressed.hh"
#include "references.hh"

namespace nix {

std::map<StorePath, StorePath> makeContentAddressed(
    Store & srcStore,
    Store & dstStore,
    const StorePathSet & storePaths)
{
    StorePathSet closure;
    srcStore.computeFSClosure(storePaths, closure);

    auto paths = srcStore.topoSortPaths(closure);

    std::reverse(paths.begin(), paths.end());

    std::map<StorePath, StorePath> remappings;

    for (auto & path : paths) {
        auto pathS = srcStore.printStorePath(path);
        auto oldInfo = srcStore.queryPathInfo(path);
        std::string oldHashPart(path.hashPart());

        StringSink sink;
        srcStore.narFromPath(path, sink);

        StringMap rewrites;

        StoreReferences refs;
        for (auto & ref : oldInfo->references) {
            if (ref == path)
                refs.self = true;
            else {
                auto i = remappings.find(ref);
                auto replacement = i != remappings.end() ? i->second : ref;
                // FIXME: warn about unremapped paths?
                if (replacement != ref)
                    rewrites.insert_or_assign(srcStore.printStorePath(ref), srcStore.printStorePath(replacement));
                refs.others.insert(std::move(replacement));
            }
        }

        sink.s = rewriteStrings(sink.s, rewrites);

        HashModuloSink hashModuloSink(htSHA256, oldHashPart);
        hashModuloSink(sink.s);

        auto narModuloHash = hashModuloSink.finish().first;

        ValidPathInfo info {
            dstStore,
            path.name(),
            FixedOutputInfo {
                .hash = {
                    .method = FileIngestionMethod::Recursive,
                    .hash = narModuloHash,
                },
                .references = std::move(refs),
            },
            Hash::dummy,
        };

        printInfo("rewriting '%s' to '%s'", pathS, dstStore.printStorePath(info.path));

        StringSink sink2;
        RewritingSink rsink2(oldHashPart, std::string(info.path.hashPart()), sink2);
        rsink2(sink.s);
        rsink2.flush();

        info.narHash = hashString(htSHA256, sink2.s);
        info.narSize = sink.s.size();

        StringSource source(sink2.s);
        dstStore.addToStore(info, source);

        remappings.insert_or_assign(std::move(path), std::move(info.path));
    }

    return remappings;
}

StorePath makeContentAddressed(
    Store & srcStore,
    Store & dstStore,
    const StorePath & fromPath)
{
    auto remappings = makeContentAddressed(srcStore, dstStore, StorePathSet { fromPath });
    auto i = remappings.find(fromPath);
    assert(i != remappings.end());
    return i->second;
}

}