mirror of
https://github.com/NixOS/nix.git
synced 2025-12-24 18:00:55 +01:00
The short answer for why we need to do this is so we can consistently do
`#include "nix/..."`. Without this change, there are ways to still make
that work, but they are hacky, and they have downsides such as making it
harder to make sure headers from the wrong Nix library (e..g.
`libnixexpr` headers in `libnixutil`) aren't being used.
The C API alraedy used `nix_api_*`, so its headers are *not* put in
subdirectories accordingly.
Progress on #7876
We resisted doing this for a while because it would be annoying to not
have the header source file pairs close by / easy to change file
path/name from one to the other. But I am ameliorating that with
symlinks in the next commit.
(cherry picked from commit f3e1c47f47)
72 lines
1.7 KiB
C++
72 lines
1.7 KiB
C++
#include "nix/path-references.hh"
|
|
#include "nix/hash.hh"
|
|
#include "nix/archive.hh"
|
|
|
|
#include <map>
|
|
#include <cstdlib>
|
|
#include <mutex>
|
|
#include <algorithm>
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
PathRefScanSink::PathRefScanSink(StringSet && hashes, std::map<std::string, StorePath> && backMap)
|
|
: RefScanSink(std::move(hashes))
|
|
, backMap(std::move(backMap))
|
|
{ }
|
|
|
|
PathRefScanSink PathRefScanSink::fromPaths(const StorePathSet & refs)
|
|
{
|
|
StringSet hashes;
|
|
std::map<std::string, StorePath> backMap;
|
|
|
|
for (auto & i : refs) {
|
|
std::string hashPart(i.hashPart());
|
|
auto inserted = backMap.emplace(hashPart, i).second;
|
|
assert(inserted);
|
|
hashes.insert(hashPart);
|
|
}
|
|
|
|
return PathRefScanSink(std::move(hashes), std::move(backMap));
|
|
}
|
|
|
|
StorePathSet PathRefScanSink::getResultPaths()
|
|
{
|
|
/* Map the hashes found back to their store paths. */
|
|
StorePathSet found;
|
|
for (auto & i : getResult()) {
|
|
auto j = backMap.find(i);
|
|
assert(j != backMap.end());
|
|
found.insert(j->second);
|
|
}
|
|
|
|
return found;
|
|
}
|
|
|
|
|
|
std::pair<StorePathSet, HashResult> scanForReferences(
|
|
const std::string & path,
|
|
const StorePathSet & refs)
|
|
{
|
|
HashSink hashSink { HashAlgorithm::SHA256 };
|
|
auto found = scanForReferences(hashSink, path, refs);
|
|
auto hash = hashSink.finish();
|
|
return std::pair<StorePathSet, HashResult>(found, hash);
|
|
}
|
|
|
|
StorePathSet scanForReferences(
|
|
Sink & toTee,
|
|
const Path & path,
|
|
const StorePathSet & refs)
|
|
{
|
|
PathRefScanSink refsSink = PathRefScanSink::fromPaths(refs);
|
|
TeeSink sink { refsSink, toTee };
|
|
|
|
/* Look for the hashes in the NAR dump of the path. */
|
|
dumpPath(path, sink);
|
|
|
|
return refsSink.getResultPaths();
|
|
}
|
|
|
|
}
|