2006-09-05 00:06:23 +03:00
|
|
|
#include "references.hh"
|
|
|
|
#include "hash.hh"
|
|
|
|
#include "util.hh"
|
2009-03-28 22:51:33 +02:00
|
|
|
#include "archive.hh"
|
2006-09-05 00:06:23 +03:00
|
|
|
|
2003-07-16 00:24:05 +03:00
|
|
|
#include <map>
|
2009-03-30 22:35:55 +03:00
|
|
|
#include <cstdlib>
|
2003-07-16 00:24:05 +03:00
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
|
|
|
|
namespace nix {
|
2003-07-14 13:23:11 +03:00
|
|
|
|
|
|
|
|
2005-11-16 10:27:06 +02:00
|
|
|
static unsigned int refLength = 32; /* characters */
|
|
|
|
|
|
|
|
|
2009-03-28 22:51:33 +02:00
|
|
|
static void search(const unsigned char * s, unsigned int len,
|
|
|
|
StringSet & hashes, StringSet & seen)
|
2003-07-14 13:23:11 +03:00
|
|
|
{
|
2005-11-16 10:27:06 +02: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;
|
|
|
|
}
|
|
|
|
|
2006-09-22 14:28:23 +03:00
|
|
|
for (unsigned int i = 0; i + refLength <= len; ) {
|
2005-11-16 10:27:06 +02: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 14:28:23 +03:00
|
|
|
string ref((const char *) s + i, refLength);
|
2009-03-28 22:51:33 +02:00
|
|
|
if (hashes.find(ref) != hashes.end()) {
|
2005-11-16 10:27:06 +02:00
|
|
|
debug(format("found reference to `%1%' at offset `%2%'")
|
|
|
|
% ref % i);
|
|
|
|
seen.insert(ref);
|
2009-03-28 22:51:33 +02:00
|
|
|
hashes.erase(ref);
|
2003-07-14 13:23:11 +03:00
|
|
|
}
|
2005-11-16 10:27:06 +02:00
|
|
|
++i;
|
2003-07-14 13:23:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-28 22:51:33 +02:00
|
|
|
struct RefScanSink : Sink
|
2003-07-14 13:23:11 +03:00
|
|
|
{
|
2009-03-28 22:51:33 +02:00
|
|
|
HashSink hashSink;
|
|
|
|
StringSet hashes;
|
|
|
|
StringSet seen;
|
2005-02-11 18:03:47 +02:00
|
|
|
|
2009-03-28 22:51:33 +02:00
|
|
|
string tail;
|
2003-07-14 13:23:11 +03:00
|
|
|
|
2009-03-28 22:51:33 +02:00
|
|
|
RefScanSink() : hashSink(htSHA256) { }
|
|
|
|
|
2011-12-15 18:19:53 +02:00
|
|
|
void operator () (const unsigned char * data, size_t len);
|
2009-03-28 22:51:33 +02:00
|
|
|
};
|
2003-07-14 13:23:11 +03:00
|
|
|
|
|
|
|
|
2011-12-15 18:19:53 +02:00
|
|
|
void RefScanSink::operator () (const unsigned char * data, size_t len)
|
2009-03-28 22:51:33 +02:00
|
|
|
{
|
|
|
|
hashSink(data, len);
|
2006-09-22 14:13:35 +03:00
|
|
|
|
2009-03-28 22:51:33 +02: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 19:27:45 +02:00
|
|
|
search((const unsigned char *) s.data(), s.size(), hashes, seen);
|
2003-07-14 13:23:11 +03:00
|
|
|
|
2009-03-28 22:51:33 +02:00
|
|
|
search(data, len, hashes, seen);
|
2003-07-14 13:23:11 +03:00
|
|
|
|
2009-03-28 22:51:33 +02:00
|
|
|
unsigned int tailLen = len <= refLength ? len : refLength;
|
|
|
|
tail =
|
|
|
|
string(tail, tail.size() < refLength - tailLen ? 0 : tail.size() - (refLength - tailLen)) +
|
|
|
|
string((const char *) data + len - tailLen, tailLen);
|
2003-07-14 13:23:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-28 22:51:33 +02:00
|
|
|
PathSet scanForReferences(const string & path,
|
2010-11-16 19:11:46 +02:00
|
|
|
const PathSet & refs, HashResult & hash)
|
2003-07-14 13:23:11 +03:00
|
|
|
{
|
2009-03-28 22:51:33 +02:00
|
|
|
RefScanSink sink;
|
2006-09-05 00:06:23 +03:00
|
|
|
std::map<string, Path> backMap;
|
2003-07-14 13:23:11 +03: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'). */
|
2009-03-28 22:51:33 +02:00
|
|
|
foreach (PathSet::const_iterator, i, refs) {
|
2005-01-14 18:04:03 +02:00
|
|
|
string baseName = baseNameOf(*i);
|
2006-05-11 05:19:43 +03:00
|
|
|
string::size_type pos = baseName.find('-');
|
2005-01-14 18:04:03 +02:00
|
|
|
if (pos == string::npos)
|
|
|
|
throw Error(format("bad reference `%1%'") % *i);
|
|
|
|
string s = string(baseName, 0, pos);
|
2005-11-16 10:27:06 +02:00
|
|
|
assert(s.size() == refLength);
|
|
|
|
assert(backMap.find(s) == backMap.end());
|
2005-01-14 18:04:03 +02:00
|
|
|
// parseHash(htSHA256, s);
|
2009-03-28 22:51:33 +02:00
|
|
|
sink.hashes.insert(s);
|
2003-07-16 00:24:05 +03:00
|
|
|
backMap[s] = *i;
|
2003-07-14 13:23:11 +03:00
|
|
|
}
|
|
|
|
|
2009-03-28 22:51:33 +02:00
|
|
|
/* Look for the hashes in the NAR dump of the path. */
|
|
|
|
dumpPath(path, sink);
|
2003-07-16 00:24:05 +03:00
|
|
|
|
2009-03-28 22:51:33 +02:00
|
|
|
/* Map the hashes found back to their store paths. */
|
2005-11-16 10:27:06 +02:00
|
|
|
PathSet found;
|
2009-03-28 22:51:33 +02:00
|
|
|
foreach (StringSet::iterator, i, sink.seen) {
|
2006-09-05 00:06:23 +03:00
|
|
|
std::map<string, Path>::iterator j;
|
2003-07-16 00:24:05 +03:00
|
|
|
if ((j = backMap.find(*i)) == backMap.end()) abort();
|
2005-11-16 10:27:06 +02:00
|
|
|
found.insert(j->second);
|
2003-07-16 00:24:05 +03:00
|
|
|
}
|
2003-07-14 13:23:11 +03:00
|
|
|
|
2009-03-28 22:51:33 +02:00
|
|
|
hash = sink.hashSink.finish();
|
|
|
|
|
2003-07-16 00:24:05 +03:00
|
|
|
return found;
|
2003-07-14 13:23:11 +03:00
|
|
|
}
|
2006-09-05 00:06:23 +03:00
|
|
|
|
2009-03-28 22:51:33 +02:00
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
}
|