2006-09-04 21:06:23 +00:00
|
|
|
#include "references.hh"
|
|
|
|
#include "hash.hh"
|
|
|
|
#include "util.hh"
|
2009-03-28 20:51:33 +00:00
|
|
|
#include "archive.hh"
|
2006-09-04 21:06:23 +00:00
|
|
|
|
2003-07-15 21:24:05 +00:00
|
|
|
#include <map>
|
2009-03-30 19:35:55 +00:00
|
|
|
#include <cstdlib>
|
2003-07-15 21:24:05 +00:00
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
|
|
|
|
namespace nix {
|
2003-07-14 10:23:11 +00:00
|
|
|
|
|
|
|
|
2005-11-16 08:27:06 +00:00
|
|
|
static unsigned int refLength = 32; /* characters */
|
|
|
|
|
|
|
|
|
2018-05-02 11:56:34 +00:00
|
|
|
static void search(const unsigned char * s, size_t len,
|
2009-03-28 20:51:33 +00:00
|
|
|
StringSet & hashes, StringSet & seen)
|
2003-07-14 10:23:11 +00:00
|
|
|
{
|
2005-11-16 08:27:06 +00:00
|
|
|
static bool initialised = false;
|
|
|
|
static bool isBase32[256];
|
|
|
|
if (!initialised) {
|
|
|
|
for (unsigned int i = 0; i < 256; ++i) isBase32[i] = false;
|
|
|
|
for (unsigned int i = 0; i < base32Chars.size(); ++i)
|
|
|
|
isBase32[(unsigned char) base32Chars[i]] = true;
|
|
|
|
initialised = true;
|
|
|
|
}
|
2015-07-17 17:24:28 +00:00
|
|
|
|
2018-05-02 11:56:34 +00:00
|
|
|
for (size_t i = 0; i + refLength <= len; ) {
|
2005-11-16 08:27:06 +00:00
|
|
|
int j;
|
|
|
|
bool match = true;
|
|
|
|
for (j = refLength - 1; j >= 0; --j)
|
|
|
|
if (!isBase32[(unsigned char) s[i + j]]) {
|
|
|
|
i += j + 1;
|
|
|
|
match = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!match) continue;
|
2006-09-22 11:28:23 +00:00
|
|
|
string ref((const char *) s + i, refLength);
|
2019-10-09 13:51:52 +00:00
|
|
|
if (hashes.erase(ref)) {
|
2017-07-30 11:27:57 +00:00
|
|
|
debug(format("found reference to '%1%' at offset '%2%'")
|
2005-11-16 08:27:06 +00:00
|
|
|
% ref % i);
|
|
|
|
seen.insert(ref);
|
2003-07-14 10:23:11 +00:00
|
|
|
}
|
2005-11-16 08:27:06 +00:00
|
|
|
++i;
|
2003-07-14 10:23:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-28 20:51:33 +00:00
|
|
|
struct RefScanSink : Sink
|
2003-07-14 10:23:11 +00:00
|
|
|
{
|
2009-03-28 20:51:33 +00:00
|
|
|
StringSet hashes;
|
|
|
|
StringSet seen;
|
2005-02-11 16:03:47 +00:00
|
|
|
|
2009-03-28 20:51:33 +00:00
|
|
|
string tail;
|
2003-07-14 10:23:11 +00:00
|
|
|
|
2020-07-22 21:37:54 +00:00
|
|
|
RefScanSink() { }
|
2015-07-17 17:24:28 +00:00
|
|
|
|
2011-12-15 16:19:53 +00:00
|
|
|
void operator () (const unsigned char * data, size_t len);
|
2009-03-28 20:51:33 +00:00
|
|
|
};
|
2003-07-14 10:23:11 +00:00
|
|
|
|
|
|
|
|
2011-12-15 16:19:53 +00:00
|
|
|
void RefScanSink::operator () (const unsigned char * data, size_t len)
|
2009-03-28 20:51:33 +00:00
|
|
|
{
|
|
|
|
/* 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. */
|
|
|
|
string s = tail + string((const char *) data, len > refLength ? refLength : len);
|
2012-02-09 17:27:45 +00:00
|
|
|
search((const unsigned char *) s.data(), s.size(), hashes, seen);
|
2003-07-14 10:23:11 +00:00
|
|
|
|
2009-03-28 20:51:33 +00:00
|
|
|
search(data, len, hashes, seen);
|
2003-07-14 10:23:11 +00:00
|
|
|
|
2018-05-02 11:56:34 +00:00
|
|
|
size_t tailLen = len <= refLength ? len : refLength;
|
2009-03-28 20:51:33 +00:00
|
|
|
tail =
|
|
|
|
string(tail, tail.size() < refLength - tailLen ? 0 : tail.size() - (refLength - tailLen)) +
|
|
|
|
string((const char *) data + len - tailLen, tailLen);
|
2003-07-14 10:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-19 18:41:33 +00:00
|
|
|
std::pair<PathSet, HashResult> scanForReferences(const string & path,
|
|
|
|
const PathSet & refs)
|
2003-07-14 10:23:11 +00:00
|
|
|
{
|
2020-07-22 21:37:54 +00:00
|
|
|
HashSink hashSink { htSHA256 };
|
2020-08-07 19:09:26 +00:00
|
|
|
auto found = scanForReferences(hashSink, path, refs);
|
|
|
|
auto hash = hashSink.finish();
|
|
|
|
return std::pair<PathSet, HashResult>(found, hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
PathSet scanForReferences(Sink & toTee,
|
|
|
|
const string & path, const PathSet & refs)
|
|
|
|
{
|
|
|
|
RefScanSink refsSink;
|
|
|
|
TeeSink sink { refsSink, toTee };
|
2006-09-04 21:06:23 +00:00
|
|
|
std::map<string, Path> backMap;
|
2003-07-14 10:23:11 +00:00
|
|
|
|
|
|
|
/* For efficiency (and a higher hit rate), just search for the
|
|
|
|
hash part of the file name. (This assumes that all references
|
|
|
|
have the form `HASH-bla'). */
|
2015-07-17 17:24:28 +00:00
|
|
|
for (auto & i : refs) {
|
2019-12-05 18:11:09 +00:00
|
|
|
auto baseName = std::string(baseNameOf(i));
|
2006-05-11 02:19:43 +00:00
|
|
|
string::size_type pos = baseName.find('-');
|
2005-01-14 16:04:03 +00:00
|
|
|
if (pos == string::npos)
|
2020-04-21 23:07:07 +00:00
|
|
|
throw Error("bad reference '%1%'", i);
|
2005-01-14 16:04:03 +00:00
|
|
|
string s = string(baseName, 0, pos);
|
2005-11-16 08:27:06 +00:00
|
|
|
assert(s.size() == refLength);
|
|
|
|
assert(backMap.find(s) == backMap.end());
|
2005-01-14 16:04:03 +00:00
|
|
|
// parseHash(htSHA256, s);
|
2020-07-22 21:37:54 +00:00
|
|
|
refsSink.hashes.insert(s);
|
2015-07-17 17:24:28 +00:00
|
|
|
backMap[s] = i;
|
2003-07-14 10:23:11 +00:00
|
|
|
}
|
|
|
|
|
2009-03-28 20:51:33 +00:00
|
|
|
/* Look for the hashes in the NAR dump of the path. */
|
|
|
|
dumpPath(path, sink);
|
2003-07-15 21:24:05 +00:00
|
|
|
|
2009-03-28 20:51:33 +00:00
|
|
|
/* Map the hashes found back to their store paths. */
|
2005-11-16 08:27:06 +00:00
|
|
|
PathSet found;
|
2020-07-22 21:37:54 +00:00
|
|
|
for (auto & i : refsSink.seen) {
|
2006-09-04 21:06:23 +00:00
|
|
|
std::map<string, Path>::iterator j;
|
2015-07-17 17:24:28 +00:00
|
|
|
if ((j = backMap.find(i)) == backMap.end()) abort();
|
2005-11-16 08:27:06 +00:00
|
|
|
found.insert(j->second);
|
2003-07-15 21:24:05 +00:00
|
|
|
}
|
2003-07-14 10:23:11 +00:00
|
|
|
|
2020-08-07 19:09:26 +00:00
|
|
|
return found;
|
2003-07-14 10:23:11 +00:00
|
|
|
}
|
2006-09-04 21:06:23 +00:00
|
|
|
|
2009-03-28 20:51:33 +00:00
|
|
|
|
2018-03-29 22:56:13 +00:00
|
|
|
RewritingSink::RewritingSink(const std::string & from, const std::string & to, Sink & nextSink)
|
|
|
|
: from(from), to(to), nextSink(nextSink)
|
|
|
|
{
|
|
|
|
assert(from.size() == to.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewritingSink::operator () (const unsigned char * data, size_t len)
|
|
|
|
{
|
|
|
|
std::string s(prev);
|
|
|
|
s.append((const char *) data, len);
|
|
|
|
|
|
|
|
size_t j = 0;
|
|
|
|
while ((j = s.find(from, j)) != string::npos) {
|
|
|
|
matches.push_back(pos + j);
|
|
|
|
s.replace(j, from.size(), to);
|
|
|
|
}
|
|
|
|
|
|
|
|
prev = s.size() < from.size() ? s : std::string(s, s.size() - from.size() + 1, from.size() - 1);
|
|
|
|
|
|
|
|
auto consumed = s.size() - prev.size();
|
|
|
|
|
|
|
|
pos += consumed;
|
|
|
|
|
|
|
|
if (consumed) nextSink((unsigned char *) s.data(), consumed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewritingSink::flush()
|
|
|
|
{
|
|
|
|
if (prev.empty()) return;
|
|
|
|
pos += prev.size();
|
|
|
|
nextSink((unsigned char *) prev.data(), prev.size());
|
|
|
|
prev.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
HashModuloSink::HashModuloSink(HashType ht, const std::string & modulus)
|
|
|
|
: hashSink(ht)
|
|
|
|
, rewritingSink(modulus, std::string(modulus.size(), 0), hashSink)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void HashModuloSink::operator () (const unsigned char * data, size_t len)
|
|
|
|
{
|
|
|
|
rewritingSink(data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
auto s = fmt("|%d", pos);
|
|
|
|
hashSink((unsigned char *) s.data(), s.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto h = hashSink.finish();
|
|
|
|
return {h.first, rewritingSink.pos};
|
|
|
|
}
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
}
|