2003-09-11 10:31:29 +02:00
|
|
|
#include <cerrno>
|
2003-07-15 23:24:05 +02:00
|
|
|
#include <map>
|
|
|
|
|
2003-07-14 12:23:11 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "references.hh"
|
|
|
|
#include "hash.hh"
|
|
|
|
|
|
|
|
|
2005-11-16 09:27:06 +01:00
|
|
|
static unsigned int refLength = 32; /* characters */
|
|
|
|
|
|
|
|
|
2003-07-14 12:23:11 +02:00
|
|
|
static void search(const string & s,
|
2005-11-16 09:27:06 +01:00
|
|
|
StringSet & ids, StringSet & seen)
|
2003-07-14 12:23:11 +02:00
|
|
|
{
|
2005-11-16 09:27:06 +01: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i + refLength <= s.size(); ) {
|
|
|
|
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;
|
|
|
|
string ref(s, i, refLength);
|
|
|
|
if (ids.find(ref) != ids.end()) {
|
|
|
|
debug(format("found reference to `%1%' at offset `%2%'")
|
|
|
|
% ref % i);
|
|
|
|
seen.insert(ref);
|
|
|
|
ids.erase(ref);
|
2003-07-14 12:23:11 +02:00
|
|
|
}
|
2005-11-16 09:27:06 +01:00
|
|
|
++i;
|
2003-07-14 12:23:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void checkPath(const string & path,
|
2005-11-16 09:27:06 +01:00
|
|
|
StringSet & ids, StringSet & seen)
|
2003-07-14 12:23:11 +02:00
|
|
|
{
|
2004-01-15 21:23:55 +01:00
|
|
|
checkInterrupt();
|
|
|
|
|
2005-02-11 17:03:47 +01:00
|
|
|
debug(format("checking `%1%'") % path);
|
|
|
|
|
2003-07-14 12:23:11 +02:00
|
|
|
struct stat st;
|
|
|
|
if (lstat(path.c_str(), &st))
|
|
|
|
throw SysError(format("getting attributes of path `%1%'") % path);
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2003-11-19 18:27:16 +01:00
|
|
|
Strings names = readDirectory(path);
|
|
|
|
for (Strings::iterator i = names.begin(); i != names.end(); i++) {
|
|
|
|
search(*i, ids, seen);
|
|
|
|
checkPath(path + "/" + *i, ids, seen);
|
2003-07-14 12:23:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (S_ISREG(st.st_mode)) {
|
|
|
|
|
2003-10-22 12:48:22 +02:00
|
|
|
AutoCloseFD fd = open(path.c_str(), O_RDONLY);
|
2003-07-14 12:23:11 +02:00
|
|
|
if (fd == -1) throw SysError(format("opening file `%1%'") % path);
|
|
|
|
|
2003-07-20 23:11:43 +02:00
|
|
|
unsigned char * buf = new unsigned char[st.st_size];
|
2003-07-14 12:23:11 +02:00
|
|
|
|
2003-07-20 23:11:43 +02:00
|
|
|
readFull(fd, buf, st.st_size);
|
2003-07-14 12:23:11 +02:00
|
|
|
|
2003-07-20 23:11:43 +02:00
|
|
|
search(string((char *) buf, st.st_size), ids, seen);
|
2003-07-14 12:23:11 +02:00
|
|
|
|
|
|
|
delete buf; /* !!! autodelete */
|
|
|
|
}
|
|
|
|
|
2004-01-05 17:26:43 +01:00
|
|
|
else if (S_ISLNK(st.st_mode))
|
|
|
|
search(readLink(path), ids, seen);
|
2003-07-14 12:23:11 +02:00
|
|
|
|
|
|
|
else throw Error(format("unknown file type: %1%") % path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-16 09:27:06 +01:00
|
|
|
PathSet scanForReferences(const string & path, const PathSet & paths)
|
2003-07-14 12:23:11 +02:00
|
|
|
{
|
2005-11-16 09:27:06 +01:00
|
|
|
map<string, Path> backMap;
|
|
|
|
StringSet ids;
|
|
|
|
StringSet seen;
|
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'). */
|
2005-11-16 09:27:06 +01:00
|
|
|
for (PathSet::const_iterator i = paths.begin(); i != paths.end(); i++) {
|
2005-01-14 17:04:03 +01:00
|
|
|
string baseName = baseNameOf(*i);
|
|
|
|
unsigned int pos = baseName.find('-');
|
|
|
|
if (pos == string::npos)
|
|
|
|
throw Error(format("bad reference `%1%'") % *i);
|
|
|
|
string s = string(baseName, 0, pos);
|
2005-11-16 09:27:06 +01:00
|
|
|
assert(s.size() == refLength);
|
|
|
|
assert(backMap.find(s) == backMap.end());
|
2005-01-14 17:04:03 +01:00
|
|
|
// parseHash(htSHA256, s);
|
2005-11-16 09:27:06 +01:00
|
|
|
ids.insert(s);
|
2003-07-15 23:24:05 +02:00
|
|
|
backMap[s] = *i;
|
2003-07-14 12:23:11 +02:00
|
|
|
}
|
|
|
|
|
2003-07-15 23:24:05 +02:00
|
|
|
checkPath(path, ids, seen);
|
|
|
|
|
2005-11-16 09:27:06 +01:00
|
|
|
PathSet found;
|
|
|
|
for (StringSet::iterator i = seen.begin(); i != seen.end(); i++) {
|
|
|
|
map<string, Path>::iterator j;
|
2003-07-15 23:24:05 +02:00
|
|
|
if ((j = backMap.find(*i)) == backMap.end()) abort();
|
2005-11-16 09:27:06 +01:00
|
|
|
found.insert(j->second);
|
2003-07-15 23:24:05 +02:00
|
|
|
}
|
2003-07-14 12:23:11 +02:00
|
|
|
|
2003-07-15 23:24:05 +02:00
|
|
|
return found;
|
2003-07-14 12:23:11 +02:00
|
|
|
}
|