mirror of
https://github.com/NixOS/nix.git
synced 2025-11-16 23:42:43 +01:00
libutil: Move references.{hh,cc} to libstore
The implicit dependency on refLength (which is the StorePath::HashLen) is not good. Also the companion tests and benchmarks are already in libstore-tests.
This commit is contained in:
parent
1b4aa5c1ef
commit
2e3ebfb829
15 changed files with 48 additions and 59 deletions
|
|
@ -62,6 +62,7 @@ headers = [ config_pub_h ] + files(
|
|||
'posix-fs-canonicalise.hh',
|
||||
'profiles.hh',
|
||||
'realisation.hh',
|
||||
'references.hh',
|
||||
'remote-fs-accessor.hh',
|
||||
'remote-store-connection.hh',
|
||||
'remote-store.hh',
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#pragma once
|
||||
///@file
|
||||
|
||||
#include "nix/util/references.hh"
|
||||
#include "nix/store/references.hh"
|
||||
#include "nix/store/path.hh"
|
||||
|
||||
namespace nix {
|
||||
|
|
|
|||
60
src/libstore/include/nix/store/references.hh
Normal file
60
src/libstore/include/nix/store/references.hh
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
#pragma once
|
||||
///@file
|
||||
|
||||
#include "nix/util/hash.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
class RefScanSink : public Sink
|
||||
{
|
||||
StringSet hashes;
|
||||
StringSet seen;
|
||||
|
||||
std::string tail;
|
||||
|
||||
public:
|
||||
|
||||
RefScanSink(StringSet && hashes)
|
||||
: hashes(hashes)
|
||||
{
|
||||
}
|
||||
|
||||
StringSet & getResult()
|
||||
{
|
||||
return seen;
|
||||
}
|
||||
|
||||
void operator()(std::string_view data) override;
|
||||
};
|
||||
|
||||
struct RewritingSink : Sink
|
||||
{
|
||||
const StringMap rewrites;
|
||||
std::string::size_type maxRewriteSize;
|
||||
std::string prev;
|
||||
Sink & nextSink;
|
||||
uint64_t pos = 0;
|
||||
|
||||
std::vector<uint64_t> matches;
|
||||
|
||||
RewritingSink(const std::string & from, const std::string & to, Sink & nextSink);
|
||||
RewritingSink(const StringMap & rewrites, Sink & nextSink);
|
||||
|
||||
void operator()(std::string_view data) override;
|
||||
|
||||
void flush();
|
||||
};
|
||||
|
||||
struct HashModuloSink : AbstractHashSink
|
||||
{
|
||||
HashSink hashSink;
|
||||
RewritingSink rewritingSink;
|
||||
|
||||
HashModuloSink(HashAlgorithm ha, const std::string & modulus);
|
||||
|
||||
void operator()(std::string_view data) override;
|
||||
|
||||
HashResult finish() override;
|
||||
};
|
||||
|
||||
} // namespace nix
|
||||
|
|
@ -7,7 +7,7 @@
|
|||
#include "nix/store/derivations.hh"
|
||||
#include "nix/store/realisation.hh"
|
||||
#include "nix/store/nar-info.hh"
|
||||
#include "nix/util/references.hh"
|
||||
#include "nix/store/references.hh"
|
||||
#include "nix/util/callback.hh"
|
||||
#include "nix/util/topo-sort.hh"
|
||||
#include "nix/util/finally.hh"
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
#include "nix/store/make-content-addressed.hh"
|
||||
#include "nix/util/references.hh"
|
||||
#include "nix/store/references.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
|
|
|
|||
|
|
@ -316,6 +316,7 @@ sources = files(
|
|||
'posix-fs-canonicalise.cc',
|
||||
'profiles.cc',
|
||||
'realisation.cc',
|
||||
'references.cc',
|
||||
'remote-fs-accessor.cc',
|
||||
'remote-store.cc',
|
||||
'restricted-store.cc',
|
||||
|
|
|
|||
126
src/libstore/references.cc
Normal file
126
src/libstore/references.cc
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
#include "nix/store/references.hh"
|
||||
#include "nix/store/path.hh"
|
||||
#include "nix/util/hash.hh"
|
||||
#include "nix/util/base-nix-32.hh"
|
||||
|
||||
#include <map>
|
||||
#include <cstdlib>
|
||||
#include <mutex>
|
||||
#include <algorithm>
|
||||
|
||||
namespace nix {
|
||||
|
||||
static constexpr auto refLength = StorePath::HashLen;
|
||||
|
||||
static void search(std::string_view s, StringSet & hashes, StringSet & seen)
|
||||
{
|
||||
for (size_t i = 0; i + refLength <= s.size();) {
|
||||
int j;
|
||||
bool match = true;
|
||||
for (j = refLength - 1; j >= 0; --j)
|
||||
if (!BaseNix32::lookupReverse(s[i + j])) {
|
||||
i += j + 1;
|
||||
match = false;
|
||||
break;
|
||||
}
|
||||
if (!match)
|
||||
continue;
|
||||
std::string ref(s.substr(i, refLength));
|
||||
if (hashes.erase(ref)) {
|
||||
debug("found reference to '%1%' at offset '%2%'", ref, i);
|
||||
seen.insert(ref);
|
||||
}
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
void RefScanSink::operator()(std::string_view data)
|
||||
{
|
||||
/* It's possible that a reference spans the previous and current
|
||||
fragment, so search in the concatenation of the tail of the
|
||||
previous fragment and the start of the current fragment. */
|
||||
auto s = tail;
|
||||
auto tailLen = std::min(data.size(), refLength);
|
||||
s.append(data.data(), tailLen);
|
||||
search(s, hashes, seen);
|
||||
|
||||
search(data, hashes, seen);
|
||||
|
||||
auto rest = refLength - tailLen;
|
||||
if (rest < tail.size())
|
||||
tail = tail.substr(tail.size() - rest);
|
||||
tail.append(data.data() + data.size() - tailLen, tailLen);
|
||||
}
|
||||
|
||||
RewritingSink::RewritingSink(const std::string & from, const std::string & to, Sink & nextSink)
|
||||
: RewritingSink({{from, to}}, nextSink)
|
||||
{
|
||||
}
|
||||
|
||||
RewritingSink::RewritingSink(const StringMap & rewrites, Sink & nextSink)
|
||||
: rewrites(rewrites)
|
||||
, nextSink(nextSink)
|
||||
{
|
||||
std::string::size_type maxRewriteSize = 0;
|
||||
for (auto & [from, to] : rewrites) {
|
||||
assert(from.size() == to.size());
|
||||
maxRewriteSize = std::max(maxRewriteSize, from.size());
|
||||
}
|
||||
this->maxRewriteSize = maxRewriteSize;
|
||||
}
|
||||
|
||||
void RewritingSink::operator()(std::string_view data)
|
||||
{
|
||||
std::string s(prev);
|
||||
s.append(data);
|
||||
|
||||
s = rewriteStrings(s, rewrites);
|
||||
|
||||
prev = s.size() < maxRewriteSize ? s
|
||||
: maxRewriteSize == 0 ? ""
|
||||
: std::string(s, s.size() - maxRewriteSize + 1, maxRewriteSize - 1);
|
||||
|
||||
auto consumed = s.size() - prev.size();
|
||||
|
||||
pos += consumed;
|
||||
|
||||
if (consumed)
|
||||
nextSink(s.substr(0, consumed));
|
||||
}
|
||||
|
||||
void RewritingSink::flush()
|
||||
{
|
||||
if (prev.empty())
|
||||
return;
|
||||
pos += prev.size();
|
||||
nextSink(prev);
|
||||
prev.clear();
|
||||
}
|
||||
|
||||
HashModuloSink::HashModuloSink(HashAlgorithm ha, const std::string & modulus)
|
||||
: hashSink(ha)
|
||||
, rewritingSink(modulus, std::string(modulus.size(), 0), hashSink)
|
||||
{
|
||||
}
|
||||
|
||||
void HashModuloSink::operator()(std::string_view data)
|
||||
{
|
||||
rewritingSink(data);
|
||||
}
|
||||
|
||||
HashResult HashModuloSink::finish()
|
||||
{
|
||||
rewritingSink.flush();
|
||||
|
||||
/* Hash the positions of the self-references. This ensures that a
|
||||
NAR with self-references and a NAR with some of the
|
||||
self-references already zeroed out do not produce a hash
|
||||
collision. FIXME: proof. */
|
||||
for (auto & pos : rewritingSink.matches)
|
||||
hashSink(fmt("|%d", pos));
|
||||
|
||||
auto h = hashSink.finish();
|
||||
return {.hash = h.hash, .numBytesDigested = rewritingSink.pos};
|
||||
}
|
||||
|
||||
} // namespace nix
|
||||
|
|
@ -9,7 +9,6 @@
|
|||
#include "nix/util/util.hh"
|
||||
#include "nix/store/nar-info-disk-cache.hh"
|
||||
#include "nix/util/thread-pool.hh"
|
||||
#include "nix/util/references.hh"
|
||||
#include "nix/util/archive.hh"
|
||||
#include "nix/util/callback.hh"
|
||||
#include "nix/util/git.hh"
|
||||
|
|
@ -18,7 +17,6 @@
|
|||
// `addMultipleToStore`.
|
||||
#include "nix/store/worker-protocol.hh"
|
||||
#include "nix/util/signals.hh"
|
||||
#include "nix/util/users.hh"
|
||||
|
||||
#include <filesystem>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue