From 727beb798a701ff546adc65030f1562b87283947 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 16 Jun 2003 16:16:09 +0000 Subject: [PATCH] * Canonicalization: when hashing directories, sort the directory entries by name. --- src/hash.cc | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/hash.cc b/src/hash.cc index 9558d36705..37f6104fb0 100644 --- a/src/hash.cc +++ b/src/hash.cc @@ -154,24 +154,30 @@ static void dumpEntries(const string & path, DumpSink & sink) { DIR * dir = opendir(path.c_str()); if (!dir) throw SysError("opening directory " + path); - + + Strings names; + struct dirent * dirent; - - /* !!! sort entries */ - while (errno = 0, dirent = readdir(dir)) { string name = dirent->d_name; if (name == "." || name == "..") continue; + names.push_back(name); + } + if (errno) throw SysError("reading directory " + path); + + sort(names.begin(), names.end()); + + for (Strings::iterator it = names.begin(); + it != names.end(); it++) + { writeString("entry", sink); writeString("(", sink); writeString("name", sink); - writeString(name, sink); + writeString(*it, sink); writeString("file", sink); - dumpPath(path + "/" + name, sink); + dumpPath(path + "/" + *it, sink); writeString(")", sink); } - - if (errno) throw SysError("reading directory " + path); closedir(dir); /* !!! close on exception */ }