diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2003-06-16T15·59+0000 |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2003-06-16T15·59+0000 |
commit | 2f04e7102eaad3159073019af96e6e5c4f2c9bcf (patch) | |
tree | 324814e02be135c03df6e456c1eec1227b3b85dc /src/hash.cc | |
parent | a09e66da5af348dc25e3b372ec9f518d3532f863 (diff) |
* Path hashing.
Diffstat (limited to 'src/hash.cc')
-rw-r--r-- | src/hash.cc | 141 |
1 files changed, 140 insertions, 1 deletions
diff --git a/src/hash.cc b/src/hash.cc index 9451ac8d80e4..9558d36705f3 100644 --- a/src/hash.cc +++ b/src/hash.cc @@ -1,9 +1,16 @@ +#include <iostream> + +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <dirent.h> +#include <fcntl.h> + extern "C" { #include "md5.h" } #include "hash.hh" -#include <iostream> Hash::Hash() @@ -88,3 +95,135 @@ Hash hashFile(const string & fileName) if (err) throw SysError("cannot hash file " + fileName); return hash; } + + +struct HashSink : DumpSink +{ + struct md5_ctx ctx; + virtual void operator () + (const unsigned char * data, unsigned int len) + { + md5_process_bytes(data, len, &ctx); + } +}; + + +Hash hashPath(const string & path) +{ + Hash hash; + HashSink sink; + md5_init_ctx(&sink.ctx); + dumpPath(path, sink); + md5_finish_ctx(&sink.ctx, hash.hash); + return hash; +} + + +static void pad(unsigned int len, DumpSink & sink) +{ + if (len % 8) { + unsigned char zero[8]; + memset(zero, 0, sizeof(zero)); + sink(zero, 8 - (len % 8)); + } +} + + +static void writeInt(unsigned int n, DumpSink & sink) +{ + unsigned char buf[8]; + memset(buf, 0, sizeof(buf)); + buf[0] = n & 0xff; + buf[1] = (n >> 8) & 0xff; + buf[2] = (n >> 16) & 0xff; + buf[3] = (n >> 24) & 0xff; + sink(buf, sizeof(buf)); +} + + +static void writeString(const string & s, DumpSink & sink) +{ + unsigned int len = s.length(); + writeInt(len, sink); + sink((const unsigned char *) s.c_str(), len); + pad(len, sink); +} + + +static void dumpEntries(const string & path, DumpSink & sink) +{ + DIR * dir = opendir(path.c_str()); + if (!dir) throw SysError("opening directory " + path); + + struct dirent * dirent; + + /* !!! sort entries */ + + while (errno = 0, dirent = readdir(dir)) { + string name = dirent->d_name; + if (name == "." || name == "..") continue; + writeString("entry", sink); + writeString("(", sink); + writeString("name", sink); + writeString(name, sink); + writeString("file", sink); + dumpPath(path + "/" + name, sink); + writeString(")", sink); + } + + if (errno) throw SysError("reading directory " + path); + + closedir(dir); /* !!! close on exception */ +} + + +static void dumpContents(const string & path, unsigned int size, + DumpSink & sink) +{ + writeString("contents", sink); + writeInt(size, sink); + + int fd = open(path.c_str(), O_RDONLY); + if (!fd) throw SysError("opening file " + path); + + unsigned char buf[16384]; + + unsigned int total = 0; + ssize_t n; + while ((n = read(fd, buf, sizeof(buf)))) { + if (n == -1) throw SysError("reading file " + path); + total += n; + sink(buf, n); + } + + if (total != size) + throw SysError("file changed while reading it: " + path); + + pad(size, sink); + + close(fd); /* !!! close on exception */ +} + + +void dumpPath(const string & path, DumpSink & sink) +{ + cerr << path << endl; + + struct stat st; + if (lstat(path.c_str(), &st)) + throw SysError("getting attributes of path " + path); + + writeString("(", sink); + + if (S_ISREG(st.st_mode)) { + writeString("type", sink); + writeString("regular", sink); + dumpContents(path, st.st_size, sink); + } else if (S_ISDIR(st.st_mode)) { + writeString("type", sink); + writeString("directory", sink); + dumpEntries(path, sink); + } else throw Error("unknown file type: " + path); + + writeString(")", sink); +} |