2003-09-11 08:31:29 +00:00
|
|
|
#include <cerrno>
|
2003-07-15 21:24:05 +00:00
|
|
|
#include <map>
|
|
|
|
|
2003-07-14 10:23:11 +00: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 08:27:06 +00:00
|
|
|
static unsigned int refLength = 32; /* characters */
|
|
|
|
|
|
|
|
|
2003-07-14 10:23:11 +00:00
|
|
|
static void search(const string & s,
|
2005-11-16 08:27:06 +00:00
|
|
|
StringSet & ids, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 10:23:11 +00:00
|
|
|
}
|
2005-11-16 08:27:06 +00:00
|
|
|
++i;
|
2003-07-14 10:23:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void checkPath(const string & path,
|
2005-11-16 08:27:06 +00:00
|
|
|
StringSet & ids, StringSet & seen)
|
2003-07-14 10:23:11 +00:00
|
|
|
{
|
2004-01-15 20:23:55 +00:00
|
|
|
checkInterrupt();
|
|
|
|
|
2005-02-11 16:03:47 +00:00
|
|
|
debug(format("checking `%1%'") % path);
|
|
|
|
|
2003-07-14 10:23:11 +00: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 17:27:16 +00: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 10:23:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (S_ISREG(st.st_mode)) {
|
|
|
|
|
2003-10-22 10:48:22 +00:00
|
|
|
AutoCloseFD fd = open(path.c_str(), O_RDONLY);
|
2003-07-14 10:23:11 +00:00
|
|
|
if (fd == -1) throw SysError(format("opening file `%1%'") % path);
|
|
|
|
|
2003-07-20 21:11:43 +00:00
|
|
|
unsigned char * buf = new unsigned char[st.st_size];
|
2003-07-14 10:23:11 +00:00
|
|
|
|
2003-07-20 21:11:43 +00:00
|
|
|
readFull(fd, buf, st.st_size);
|
2003-07-14 10:23:11 +00:00
|
|
|
|
2003-07-20 21:11:43 +00:00
|
|
|
search(string((char *) buf, st.st_size), ids, seen);
|
2003-07-14 10:23:11 +00:00
|
|
|
|
2006-03-01 14:39:10 +00:00
|
|
|
delete[] buf; /* !!! autodelete */
|
2003-07-14 10:23:11 +00:00
|
|
|
}
|
|
|
|
|
2004-01-05 16:26:43 +00:00
|
|
|
else if (S_ISLNK(st.st_mode))
|
|
|
|
search(readLink(path), ids, seen);
|
2003-07-14 10:23:11 +00:00
|
|
|
|
|
|
|
else throw Error(format("unknown file type: %1%") % path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-16 08:27:06 +00:00
|
|
|
PathSet scanForReferences(const string & path, const PathSet & paths)
|
2003-07-14 10:23:11 +00:00
|
|
|
{
|
2005-11-16 08:27:06 +00:00
|
|
|
map<string, Path> backMap;
|
|
|
|
StringSet ids;
|
|
|
|
StringSet seen;
|
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'). */
|
2005-11-16 08:27:06 +00:00
|
|
|
for (PathSet::const_iterator i = paths.begin(); i != paths.end(); i++) {
|
2005-01-14 16:04:03 +00: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 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);
|
2005-11-16 08:27:06 +00:00
|
|
|
ids.insert(s);
|
2003-07-15 21:24:05 +00:00
|
|
|
backMap[s] = *i;
|
2003-07-14 10:23:11 +00:00
|
|
|
}
|
|
|
|
|
2003-07-15 21:24:05 +00:00
|
|
|
checkPath(path, ids, seen);
|
|
|
|
|
2005-11-16 08:27:06 +00:00
|
|
|
PathSet found;
|
|
|
|
for (StringSet::iterator i = seen.begin(); i != seen.end(); i++) {
|
|
|
|
map<string, Path>::iterator j;
|
2003-07-15 21:24:05 +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
|
|
|
|
2003-07-15 21:24:05 +00:00
|
|
|
return found;
|
2003-07-14 10:23:11 +00:00
|
|
|
}
|