2020-05-29 20:19:48 +00:00
|
|
|
#include "serialise.hh"
|
2016-05-04 11:36:54 +00:00
|
|
|
#include "store-api.hh"
|
|
|
|
#include "archive.hh"
|
|
|
|
#include "worker-protocol.hh"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
struct HashAndWriteSink : Sink
|
|
|
|
{
|
|
|
|
Sink & writeSink;
|
|
|
|
HashSink hashSink;
|
|
|
|
HashAndWriteSink(Sink & writeSink) : writeSink(writeSink), hashSink(htSHA256)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual void operator () (const unsigned char * data, size_t len)
|
|
|
|
{
|
|
|
|
writeSink(data, len);
|
|
|
|
hashSink(data, len);
|
|
|
|
}
|
|
|
|
Hash currentHash()
|
|
|
|
{
|
|
|
|
return hashSink.currentHash().first;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
void Store::exportPaths(const StorePathSet & paths, Sink & sink)
|
2016-05-04 11:36:54 +00:00
|
|
|
{
|
2019-12-05 18:11:09 +00:00
|
|
|
auto sorted = topoSortPaths(paths);
|
2016-05-04 11:36:54 +00:00
|
|
|
std::reverse(sorted.begin(), sorted.end());
|
|
|
|
|
|
|
|
std::string doneLabel("paths exported");
|
2017-05-16 14:09:57 +00:00
|
|
|
//logger->incExpected(doneLabel, sorted.size());
|
2016-05-04 11:36:54 +00:00
|
|
|
|
|
|
|
for (auto & path : sorted) {
|
2017-07-30 11:27:57 +00:00
|
|
|
//Activity act(*logger, lvlInfo, format("exporting path '%s'") % path);
|
2016-05-04 11:36:54 +00:00
|
|
|
sink << 1;
|
|
|
|
exportPath(path, sink);
|
2017-05-16 14:09:57 +00:00
|
|
|
//logger->incProgress(doneLabel);
|
2016-05-04 11:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sink << 0;
|
|
|
|
}
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
void Store::exportPath(const StorePath & path, Sink & sink)
|
2016-05-04 11:36:54 +00:00
|
|
|
{
|
|
|
|
auto info = queryPathInfo(path);
|
|
|
|
|
|
|
|
HashAndWriteSink hashAndWriteSink(sink);
|
|
|
|
|
|
|
|
narFromPath(path, hashAndWriteSink);
|
|
|
|
|
|
|
|
/* Refuse to export paths that have changed. This prevents
|
|
|
|
filesystem corruption from spreading to other machines.
|
|
|
|
Don't complain if the stored hash is zero (unknown). */
|
|
|
|
Hash hash = hashAndWriteSink.currentHash();
|
|
|
|
if (hash != info->narHash && info->narHash != Hash(info->narHash.type))
|
2019-12-05 18:11:09 +00:00
|
|
|
throw Error("hash of path '%s' has changed from '%s' to '%s'!",
|
|
|
|
printStorePath(path), info->narHash.to_string(), hash.to_string());
|
|
|
|
|
|
|
|
hashAndWriteSink
|
|
|
|
<< exportMagic
|
|
|
|
<< printStorePath(path);
|
|
|
|
writeStorePaths(*this, hashAndWriteSink, info->references);
|
|
|
|
hashAndWriteSink
|
|
|
|
<< (info->deriver ? printStorePath(*info->deriver) : "")
|
|
|
|
<< 0;
|
2016-05-04 11:36:54 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePaths Store::importPaths(Source & source, std::shared_ptr<FSAccessor> accessor, CheckSigsFlag checkSigs)
|
2016-05-04 11:36:54 +00:00
|
|
|
{
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePaths res;
|
2016-05-04 11:36:54 +00:00
|
|
|
while (true) {
|
2017-03-01 12:52:54 +00:00
|
|
|
auto n = readNum<uint64_t>(source);
|
2016-05-04 11:36:54 +00:00
|
|
|
if (n == 0) break;
|
2017-07-30 11:27:57 +00:00
|
|
|
if (n != 1) throw Error("input doesn't look like something created by 'nix-store --export'");
|
2016-05-04 11:36:54 +00:00
|
|
|
|
|
|
|
/* Extract the NAR from the source. */
|
2017-03-01 15:16:04 +00:00
|
|
|
TeeSink tee(source);
|
|
|
|
parseDump(tee, tee.source);
|
2016-05-04 11:36:54 +00:00
|
|
|
|
|
|
|
uint32_t magic = readInt(source);
|
|
|
|
if (magic != exportMagic)
|
|
|
|
throw Error("Nix archive cannot be imported; wrong format");
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
ValidPathInfo info(parseStorePath(readString(source)));
|
2016-05-04 11:36:54 +00:00
|
|
|
|
2017-07-30 11:27:57 +00:00
|
|
|
//Activity act(*logger, lvlInfo, format("importing path '%s'") % info.path);
|
2016-05-04 11:36:54 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
info.references = readStorePaths<StorePathSet>(*this, source);
|
2016-05-04 11:36:54 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
auto deriver = readString(source);
|
|
|
|
if (deriver != "")
|
|
|
|
info.deriver = parseStorePath(deriver);
|
2016-05-04 11:36:54 +00:00
|
|
|
|
2017-03-01 15:16:04 +00:00
|
|
|
info.narHash = hashString(htSHA256, *tee.source.data);
|
|
|
|
info.narSize = tee.source.data->size();
|
2016-05-04 11:36:54 +00:00
|
|
|
|
|
|
|
// Ignore optional legacy signature.
|
|
|
|
if (readInt(source) == 1)
|
|
|
|
readString(source);
|
|
|
|
|
2020-05-29 20:19:48 +00:00
|
|
|
// Can't use underlying source, which would have been exhausted
|
|
|
|
auto source = StringSource { *tee.source.data };
|
|
|
|
addToStore(info, source, NoRepair, checkSigs, accessor);
|
2016-05-04 11:36:54 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
res.push_back(info.path.clone());
|
2016-05-04 11:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|