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"
|
|
|
|
|
|
|
|
|
|
|
|
static void search(const string & s,
|
2003-07-15 23:24:05 +02:00
|
|
|
Strings & ids, Strings & seen)
|
2003-07-14 12:23:11 +02:00
|
|
|
{
|
2003-07-15 23:24:05 +02:00
|
|
|
for (Strings::iterator i = ids.begin();
|
|
|
|
i != ids.end(); )
|
2003-07-14 12:23:11 +02:00
|
|
|
{
|
2004-01-15 21:23:55 +01:00
|
|
|
checkInterrupt();
|
2003-07-14 12:23:11 +02:00
|
|
|
if (s.find(*i) == string::npos)
|
|
|
|
i++;
|
|
|
|
else {
|
|
|
|
debug(format("found reference to `%1%'") % *i);
|
|
|
|
seen.push_back(*i);
|
2003-07-15 23:24:05 +02:00
|
|
|
i = ids.erase(i);
|
2003-07-14 12:23:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void checkPath(const string & path,
|
2003-07-15 23:24:05 +02:00
|
|
|
Strings & ids, Strings & seen)
|
2003-07-14 12:23:11 +02:00
|
|
|
{
|
2004-01-15 21:23:55 +01:00
|
|
|
checkInterrupt();
|
|
|
|
|
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)) {
|
|
|
|
|
|
|
|
debug(format("checking `%1%'") % path);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-15 23:24:05 +02:00
|
|
|
Strings filterReferences(const string & path, const Strings & paths)
|
2003-07-14 12:23:11 +02:00
|
|
|
{
|
2003-07-15 23:24:05 +02:00
|
|
|
map<string, string> backMap;
|
|
|
|
Strings ids;
|
2003-07-14 12:23:11 +02:00
|
|
|
Strings seen;
|
|
|
|
|
|
|
|
/* 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'). */
|
2003-07-15 23:24:05 +02:00
|
|
|
for (Strings::const_iterator i = paths.begin();
|
|
|
|
i != paths.end(); i++)
|
2003-07-14 12:23:11 +02:00
|
|
|
{
|
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);
|
|
|
|
// parseHash(htSHA256, s);
|
2003-07-15 23:24:05 +02:00
|
|
|
ids.push_back(s);
|
|
|
|
backMap[s] = *i;
|
2003-07-14 12:23:11 +02:00
|
|
|
}
|
|
|
|
|
2003-07-15 23:24:05 +02:00
|
|
|
checkPath(path, ids, seen);
|
|
|
|
|
|
|
|
Strings found;
|
|
|
|
for (Strings::iterator i = seen.begin(); i != seen.end(); i++)
|
|
|
|
{
|
|
|
|
map<string, string>::iterator j;
|
|
|
|
if ((j = backMap.find(*i)) == backMap.end()) abort();
|
|
|
|
found.push_back(j->second);
|
|
|
|
}
|
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
|
|
|
}
|