|
4 | 4 | #include "nix/util/archive.hh" |
5 | 5 | #include "nix/store/common-protocol.hh" |
6 | 6 | #include "nix/store/common-protocol-impl.hh" |
7 | | - |
8 | | -#include <algorithm> |
| 7 | +#include "nix/store/worker-protocol.hh" |
9 | 8 |
|
10 | 9 | namespace nix { |
11 | 10 |
|
12 | | -static void exportPath(Store & store, const StorePath & path, Sink & sink) |
13 | | -{ |
14 | | - auto info = store.queryPathInfo(path); |
15 | | - |
16 | | - HashSink hashSink(HashAlgorithm::SHA256); |
17 | | - TeeSink teeSink(sink, hashSink); |
18 | | - |
19 | | - store.narFromPath(path, teeSink); |
20 | | - |
21 | | - /* Refuse to export paths that have changed. This prevents |
22 | | - filesystem corruption from spreading to other machines. |
23 | | - Don't complain if the stored hash is zero (unknown). */ |
24 | | - Hash hash = hashSink.currentHash().hash; |
25 | | - if (hash != info->narHash && info->narHash != Hash(info->narHash.algo)) |
26 | | - throw Error( |
27 | | - "hash of path '%s' has changed from '%s' to '%s'!", |
28 | | - store.printStorePath(path), |
29 | | - info->narHash.to_string(HashFormat::Nix32, true), |
30 | | - hash.to_string(HashFormat::Nix32, true)); |
31 | | - |
32 | | - teeSink << exportMagic << store.printStorePath(path); |
33 | | - CommonProto::write(store, CommonProto::WriteConn{.to = teeSink}, info->references); |
34 | | - teeSink << (info->deriver ? store.printStorePath(*info->deriver) : "") << 0; |
35 | | -} |
| 11 | +static const uint32_t exportMagicV1 = 0x4558494e; |
| 12 | +static const uint64_t exportMagicV2 = 0x324f4952414e; // = 'NARIO2' |
36 | 13 |
|
37 | | -void exportPaths(Store & store, const StorePathSet & paths, Sink & sink) |
| 14 | +void exportPaths(Store & store, const StorePathSet & paths, Sink & sink, unsigned int version) |
38 | 15 | { |
39 | 16 | auto sorted = store.topoSortPaths(paths); |
40 | 17 | std::reverse(sorted.begin(), sorted.end()); |
41 | 18 |
|
42 | | - for (auto & path : sorted) { |
43 | | - sink << 1; |
44 | | - exportPath(store, path, sink); |
| 19 | + auto dumpNar = [&](const ValidPathInfo & info) { |
| 20 | + HashSink hashSink(HashAlgorithm::SHA256); |
| 21 | + TeeSink teeSink(sink, hashSink); |
| 22 | + |
| 23 | + store.narFromPath(info.path, teeSink); |
| 24 | + |
| 25 | + /* Refuse to export paths that have changed. This prevents |
| 26 | + filesystem corruption from spreading to other machines. |
| 27 | + Don't complain if the stored hash is zero (unknown). */ |
| 28 | + Hash hash = hashSink.currentHash().hash; |
| 29 | + if (hash != info.narHash && info.narHash != Hash(info.narHash.algo)) |
| 30 | + throw Error( |
| 31 | + "hash of path '%s' has changed from '%s' to '%s'!", |
| 32 | + store.printStorePath(info.path), |
| 33 | + info.narHash.to_string(HashFormat::Nix32, true), |
| 34 | + hash.to_string(HashFormat::Nix32, true)); |
| 35 | + }; |
| 36 | + |
| 37 | + switch (version) { |
| 38 | + |
| 39 | + case 1: |
| 40 | + for (auto & path : sorted) { |
| 41 | + sink << 1; |
| 42 | + auto info = store.queryPathInfo(path); |
| 43 | + dumpNar(*info); |
| 44 | + sink << exportMagicV1 << store.printStorePath(path); |
| 45 | + CommonProto::write(store, CommonProto::WriteConn{.to = sink}, info->references); |
| 46 | + sink << (info->deriver ? store.printStorePath(*info->deriver) : "") << 0; |
| 47 | + } |
| 48 | + sink << 0; |
| 49 | + break; |
| 50 | + |
| 51 | + case 2: |
| 52 | + sink << exportMagicV2; |
| 53 | + |
| 54 | + for (auto & path : sorted) { |
| 55 | + Activity act(*logger, lvlTalkative, actUnknown, fmt("exporting path '%s'", store.printStorePath(path))); |
| 56 | + sink << 1; |
| 57 | + auto info = store.queryPathInfo(path); |
| 58 | + // FIXME: move to CommonProto? |
| 59 | + WorkerProto::Serialise<ValidPathInfo>::write( |
| 60 | + store, WorkerProto::WriteConn{.to = sink, .version = 16, .shortStorePaths = true}, *info); |
| 61 | + dumpNar(*info); |
| 62 | + } |
| 63 | + |
| 64 | + sink << 0; |
| 65 | + break; |
| 66 | + |
| 67 | + default: |
| 68 | + throw Error("unsupported nario version %d", version); |
45 | 69 | } |
46 | | - |
47 | | - sink << 0; |
48 | 70 | } |
49 | 71 |
|
50 | 72 | StorePaths importPaths(Store & store, Source & source, CheckSigsFlag checkSigs) |
51 | 73 | { |
52 | 74 | StorePaths res; |
53 | | - while (true) { |
54 | | - auto n = readNum<uint64_t>(source); |
55 | | - if (n == 0) |
56 | | - break; |
57 | | - if (n != 1) |
58 | | - throw Error("input doesn't look like something created by 'nix-store --export'"); |
59 | | - |
60 | | - /* Extract the NAR from the source. */ |
61 | | - StringSink saved; |
62 | | - TeeSource tee{source, saved}; |
63 | | - NullFileSystemObjectSink ether; |
64 | | - parseDump(ether, tee); |
65 | | - |
66 | | - uint32_t magic = readInt(source); |
67 | | - if (magic != exportMagic) |
68 | | - throw Error("Nix archive cannot be imported; wrong format"); |
69 | | - |
70 | | - auto path = store.parseStorePath(readString(source)); |
71 | | - |
72 | | - // Activity act(*logger, lvlInfo, "importing path '%s'", info.path); |
73 | | - |
74 | | - auto references = CommonProto::Serialise<StorePathSet>::read(store, CommonProto::ReadConn{.from = source}); |
75 | | - auto deriver = readString(source); |
76 | | - auto narHash = hashString(HashAlgorithm::SHA256, saved.s); |
77 | | - |
78 | | - ValidPathInfo info{path, narHash}; |
79 | | - if (deriver != "") |
80 | | - info.deriver = store.parseStorePath(deriver); |
81 | | - info.references = references; |
82 | | - info.narSize = saved.s.size(); |
83 | | - |
84 | | - // Ignore optional legacy signature. |
85 | | - if (readInt(source) == 1) |
86 | | - readString(source); |
87 | | - |
88 | | - // Can't use underlying source, which would have been exhausted |
89 | | - auto source = StringSource(saved.s); |
90 | | - store.addToStore(info, source, NoRepair, checkSigs); |
91 | | - |
92 | | - res.push_back(info.path); |
| 75 | + |
| 76 | + auto version = readNum<uint64_t>(source); |
| 77 | + |
| 78 | + /* Note: nario version 1 lacks an explicit header. The first |
| 79 | + integer denotes whether a store path follows or not. So look |
| 80 | + for 0 or 1. */ |
| 81 | + switch (version) { |
| 82 | + |
| 83 | + case 0: |
| 84 | + /* Empty version 1 nario, nothing to do. */ |
| 85 | + break; |
| 86 | + |
| 87 | + case 1: |
| 88 | + /* Non-empty version 1 nario. */ |
| 89 | + while (true) { |
| 90 | + /* Extract the NAR from the source. */ |
| 91 | + StringSink saved; |
| 92 | + TeeSource tee{source, saved}; |
| 93 | + NullFileSystemObjectSink ether; |
| 94 | + parseDump(ether, tee); |
| 95 | + |
| 96 | + uint32_t magic = readInt(source); |
| 97 | + if (magic != exportMagicV1) |
| 98 | + throw Error("nario cannot be imported; wrong format"); |
| 99 | + |
| 100 | + auto path = store.parseStorePath(readString(source)); |
| 101 | + |
| 102 | + auto references = CommonProto::Serialise<StorePathSet>::read(store, CommonProto::ReadConn{.from = source}); |
| 103 | + auto deriver = readString(source); |
| 104 | + auto narHash = hashString(HashAlgorithm::SHA256, saved.s); |
| 105 | + |
| 106 | + ValidPathInfo info{path, narHash}; |
| 107 | + if (deriver != "") |
| 108 | + info.deriver = store.parseStorePath(deriver); |
| 109 | + info.references = references; |
| 110 | + info.narSize = saved.s.size(); |
| 111 | + |
| 112 | + // Ignore optional legacy signature. |
| 113 | + if (readInt(source) == 1) |
| 114 | + readString(source); |
| 115 | + |
| 116 | + // Can't use underlying source, which would have been exhausted. |
| 117 | + auto source2 = StringSource(saved.s); |
| 118 | + store.addToStore(info, source2, NoRepair, checkSigs); |
| 119 | + |
| 120 | + res.push_back(info.path); |
| 121 | + |
| 122 | + auto n = readNum<uint64_t>(source); |
| 123 | + if (n == 0) |
| 124 | + break; |
| 125 | + if (n != 1) |
| 126 | + throw Error("input doesn't look like a nario"); |
| 127 | + } |
| 128 | + break; |
| 129 | + |
| 130 | + case exportMagicV2: |
| 131 | + while (true) { |
| 132 | + auto n = readNum<uint64_t>(source); |
| 133 | + if (n == 0) |
| 134 | + break; |
| 135 | + if (n != 1) |
| 136 | + throw Error("input doesn't look like a nario"); |
| 137 | + |
| 138 | + auto info = WorkerProto::Serialise<ValidPathInfo>::read( |
| 139 | + store, WorkerProto::ReadConn{.from = source, .version = 16, .shortStorePaths = true}); |
| 140 | + |
| 141 | + Activity act( |
| 142 | + *logger, lvlTalkative, actUnknown, fmt("importing path '%s'", store.printStorePath(info.path))); |
| 143 | + |
| 144 | + store.addToStore(info, source, NoRepair, checkSigs); |
| 145 | + |
| 146 | + res.push_back(info.path); |
| 147 | + } |
| 148 | + |
| 149 | + break; |
| 150 | + |
| 151 | + default: |
| 152 | + throw Error("input doesn't look like a nario"); |
93 | 153 | } |
94 | 154 |
|
95 | 155 | return res; |
|
0 commit comments