mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-27 00:06:16 +02:00
bbe97dff8b
Most functions now take a StorePath argument rather than a Path (which is just an alias for std::string). The StorePath constructor ensures that the path is syntactically correct (i.e. it looks like <store-dir>/<base32-hash>-<name>). Similarly, functions like buildPaths() now take a StorePathWithOutputs, rather than abusing Path by adding a '!<outputs>' suffix. Note that the StorePath type is implemented in Rust. This involves some hackery to allow Rust values to be used directly in C++, via a helper type whose destructor calls the Rust type's drop() function. The main issue is the dynamic nature of C++ move semantics: after we have moved a Rust value, we should not call the drop function on the original value. So when we move a value, we set the original value to bitwise zero, and the destructor only calls drop() if the value is not bitwise zero. This should be sufficient for most types. Also lots of minor cleanups to the C++ API to make it more modern (e.g. using std::optional and std::string_view in some places).
111 lines
3.1 KiB
C++
111 lines
3.1 KiB
C++
#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;
|
|
}
|
|
};
|
|
|
|
void Store::exportPaths(const StorePathSet & paths, Sink & sink)
|
|
{
|
|
auto sorted = topoSortPaths(paths);
|
|
std::reverse(sorted.begin(), sorted.end());
|
|
|
|
std::string doneLabel("paths exported");
|
|
//logger->incExpected(doneLabel, sorted.size());
|
|
|
|
for (auto & path : sorted) {
|
|
//Activity act(*logger, lvlInfo, format("exporting path '%s'") % path);
|
|
sink << 1;
|
|
exportPath(path, sink);
|
|
//logger->incProgress(doneLabel);
|
|
}
|
|
|
|
sink << 0;
|
|
}
|
|
|
|
void Store::exportPath(const StorePath & path, Sink & sink)
|
|
{
|
|
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))
|
|
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;
|
|
}
|
|
|
|
StorePaths Store::importPaths(Source & source, std::shared_ptr<FSAccessor> accessor, CheckSigsFlag checkSigs)
|
|
{
|
|
StorePaths res;
|
|
while (true) {
|
|
auto n = readNum<uint64_t>(source);
|
|
if (n == 0) break;
|
|
if (n != 1) throw Error("input doesn't look like something created by 'nix-store --export'");
|
|
|
|
/* Extract the NAR from the source. */
|
|
TeeSink tee(source);
|
|
parseDump(tee, tee.source);
|
|
|
|
uint32_t magic = readInt(source);
|
|
if (magic != exportMagic)
|
|
throw Error("Nix archive cannot be imported; wrong format");
|
|
|
|
ValidPathInfo info(parseStorePath(readString(source)));
|
|
|
|
//Activity act(*logger, lvlInfo, format("importing path '%s'") % info.path);
|
|
|
|
info.references = readStorePaths<StorePathSet>(*this, source);
|
|
|
|
auto deriver = readString(source);
|
|
if (deriver != "")
|
|
info.deriver = parseStorePath(deriver);
|
|
|
|
info.narHash = hashString(htSHA256, *tee.source.data);
|
|
info.narSize = tee.source.data->size();
|
|
|
|
// Ignore optional legacy signature.
|
|
if (readInt(source) == 1)
|
|
readString(source);
|
|
|
|
addToStore(info, tee.source.data, NoRepair, checkSigs, accessor);
|
|
|
|
res.push_back(info.path.clone());
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
}
|