2020-05-27 22:56:34 +02:00
|
|
|
#include "libstore/references.hh"
|
2020-05-19 16:54:39 +02:00
|
|
|
|
2009-03-30 21:35:55 +02:00
|
|
|
#include <cstdlib>
|
2003-07-15 23:24:05 +02:00
|
|
|
#include <map>
|
2020-05-19 16:54:39 +02:00
|
|
|
|
2020-05-19 02:02:44 +02:00
|
|
|
#include <glog/logging.h>
|
2020-05-19 16:54:39 +02:00
|
|
|
|
2020-05-27 22:56:34 +02:00
|
|
|
#include "libutil/archive.hh"
|
|
|
|
#include "libutil/hash.hh"
|
|
|
|
#include "libutil/util.hh"
|
2006-09-04 23:06:23 +02:00
|
|
|
|
|
|
|
namespace nix {
|
2003-07-14 12:23:11 +02:00
|
|
|
|
2020-09-07 19:18:18 +02:00
|
|
|
constexpr unsigned int kRefLength = 32; /* characters */
|
2005-11-16 09:27:06 +01:00
|
|
|
|
2020-08-04 04:13:40 +02:00
|
|
|
static void search(const unsigned char* s, size_t len, StringSet& hashes,
|
2009-03-28 21:51:33 +01:00
|
|
|
StringSet& seen) {
|
2020-08-04 04:13:40 +02:00
|
|
|
static bool initialised = false;
|
|
|
|
static bool isBase32[256];
|
|
|
|
if (!initialised) {
|
|
|
|
for (bool& i : isBase32) {
|
|
|
|
i = false;
|
|
|
|
}
|
|
|
|
for (char base32Char : base32Chars) {
|
|
|
|
isBase32[static_cast<unsigned char>(base32Char)] = true;
|
|
|
|
}
|
|
|
|
initialised = true;
|
|
|
|
}
|
|
|
|
|
2020-09-07 19:18:18 +02:00
|
|
|
for (size_t i = 0; i + kRefLength <= len;) {
|
2020-08-04 04:13:40 +02:00
|
|
|
int j = 0;
|
|
|
|
bool match = true;
|
2020-09-07 19:18:18 +02:00
|
|
|
for (j = kRefLength - 1; j >= 0; --j) {
|
2020-08-04 04:13:40 +02:00
|
|
|
if (!isBase32[s[i + j]]) {
|
|
|
|
i += j + 1;
|
|
|
|
match = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-11-16 09:27:06 +01:00
|
|
|
if (!match) {
|
|
|
|
continue;
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2020-09-07 19:18:18 +02:00
|
|
|
std::string ref(reinterpret_cast<const char*>(s) + i, kRefLength);
|
2020-08-04 04:13:40 +02:00
|
|
|
if (hashes.find(ref) != hashes.end()) {
|
2020-05-19 02:02:44 +02:00
|
|
|
DLOG(INFO) << "found reference to '" << ref << "' at offset " << i;
|
2020-08-04 04:13:40 +02:00
|
|
|
seen.insert(ref);
|
|
|
|
hashes.erase(ref);
|
2003-07-14 12:23:11 +02:00
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
++i;
|
|
|
|
}
|
2003-07-14 12:23:11 +02:00
|
|
|
}
|
|
|
|
|
2009-03-28 21:51:33 +01:00
|
|
|
struct RefScanSink : Sink {
|
|
|
|
HashSink hashSink;
|
|
|
|
StringSet hashes;
|
|
|
|
StringSet seen;
|
2003-07-14 12:23:11 +02:00
|
|
|
|
2020-05-24 23:29:21 +02:00
|
|
|
std::string tail;
|
2005-02-11 17:03:47 +01:00
|
|
|
|
2009-03-28 21:51:33 +01:00
|
|
|
RefScanSink() : hashSink(htSHA256) {}
|
2003-07-14 12:23:11 +02:00
|
|
|
|
2020-05-20 05:33:07 +02:00
|
|
|
void operator()(const unsigned char* data, size_t len) override;
|
2009-03-28 21:51:33 +01:00
|
|
|
};
|
2003-07-14 12:23:11 +02:00
|
|
|
|
2011-12-15 17:19:53 +01:00
|
|
|
void RefScanSink::operator()(const unsigned char* data, size_t len) {
|
2009-03-28 21:51:33 +01:00
|
|
|
hashSink(data, len);
|
2003-07-14 12:23:11 +02:00
|
|
|
|
2009-03-28 21:51:33 +01: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. */
|
2020-08-04 04:13:40 +02:00
|
|
|
std::string s = tail + std::string(reinterpret_cast<const char*>(data),
|
2020-09-07 19:18:18 +02:00
|
|
|
len > kRefLength ? kRefLength : len);
|
2020-08-04 04:13:40 +02:00
|
|
|
search(reinterpret_cast<const unsigned char*>(s.data()), s.size(), hashes,
|
|
|
|
seen);
|
2003-07-14 12:23:11 +02:00
|
|
|
|
2020-08-04 04:13:40 +02:00
|
|
|
search(data, len, hashes, seen);
|
2003-07-14 12:23:11 +02:00
|
|
|
|
2020-09-07 19:18:18 +02:00
|
|
|
size_t tailLen = len <= kRefLength ? len : kRefLength;
|
2020-08-04 04:13:40 +02:00
|
|
|
tail =
|
2020-09-07 19:18:18 +02:00
|
|
|
std::string(tail, tail.size() < kRefLength - tailLen
|
2020-08-04 04:13:40 +02:00
|
|
|
? 0
|
2020-09-07 19:18:18 +02:00
|
|
|
: tail.size() - (kRefLength - tailLen)) +
|
2020-08-04 04:13:40 +02:00
|
|
|
std::string(reinterpret_cast<const char*>(data) + len - tailLen, tailLen);
|
2003-07-14 12:23:11 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 23:29:21 +02:00
|
|
|
PathSet scanForReferences(const std::string& path, const PathSet& refs,
|
2010-11-16 18:11:46 +01:00
|
|
|
HashResult& hash) {
|
2009-03-28 21:51:33 +01:00
|
|
|
RefScanSink sink;
|
2020-05-24 23:29:21 +02:00
|
|
|
std::map<std::string, Path> backMap;
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2003-07-14 12:23:11 +02: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 19:24:28 +02:00
|
|
|
for (auto& i : refs) {
|
2020-05-24 23:29:21 +02:00
|
|
|
std::string baseName = baseNameOf(i);
|
|
|
|
std::string::size_type pos = baseName.find('-');
|
|
|
|
if (pos == std::string::npos) {
|
2017-07-30 13:27:57 +02:00
|
|
|
throw Error(format("bad reference '%1%'") % i);
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2020-05-24 23:29:21 +02:00
|
|
|
std::string s = std::string(baseName, 0, pos);
|
2020-09-07 19:18:18 +02:00
|
|
|
assert(s.size() == kRefLength);
|
2005-11-16 09:27:06 +01:00
|
|
|
assert(backMap.find(s) == backMap.end());
|
2005-01-14 17:04:03 +01:00
|
|
|
// parseHash(htSHA256, s);
|
2009-03-28 21:51:33 +01:00
|
|
|
sink.hashes.insert(s);
|
2015-07-17 19:24:28 +02:00
|
|
|
backMap[s] = i;
|
2003-07-14 12:23:11 +02:00
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2009-03-28 21:51:33 +01:00
|
|
|
/* Look for the hashes in the NAR dump of the path. */
|
|
|
|
dumpPath(path, sink);
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2009-03-28 21:51:33 +01:00
|
|
|
/* Map the hashes found back to their store paths. */
|
2005-11-16 09:27:06 +01:00
|
|
|
PathSet found;
|
2015-07-17 19:24:28 +02:00
|
|
|
for (auto& i : sink.seen) {
|
2020-05-24 23:29:21 +02:00
|
|
|
std::map<std::string, Path>::iterator j;
|
2015-07-17 19:24:28 +02:00
|
|
|
if ((j = backMap.find(i)) == backMap.end()) {
|
|
|
|
abort();
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2005-11-16 09:27:06 +01:00
|
|
|
found.insert(j->second);
|
2003-07-15 23:24:05 +02:00
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2009-03-28 21:51:33 +01:00
|
|
|
hash = sink.hashSink.finish();
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2003-07-15 23:24:05 +02:00
|
|
|
return found;
|
2003-07-14 12:23:11 +02:00
|
|
|
}
|
2006-09-04 23:06:23 +02:00
|
|
|
|
|
|
|
} // namespace nix
|