From daf3f2c11ff467b600473a2fda7bd513aacc1efa Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 1 Aug 2014 16:37:47 +0200 Subject: Make readDirectory() return inode / file type --- src/libutil/archive.cc | 15 +++++++-------- src/libutil/util.cc | 15 +++++++-------- src/libutil/util.hh | 11 ++++++++++- 3 files changed, 24 insertions(+), 17 deletions(-) (limited to 'src/libutil') diff --git a/src/libutil/archive.cc b/src/libutil/archive.cc index 5450fd2f7182..6856ea0f2837 100644 --- a/src/libutil/archive.cc +++ b/src/libutil/archive.cc @@ -83,22 +83,21 @@ static void dump(const Path & path, Sink & sink, PathFilter & filter) /* If we're on a case-insensitive system like Mac OS X, undo the case hack applied by restorePath(). */ - Strings names = readDirectory(path); std::map unhacked; - for (auto & i : names) + for (auto & i : readDirectory(path)) if (useCaseHack) { - string name(i); - size_t pos = i.find(caseHackSuffix); + string name(i.name); + size_t pos = i.name.find(caseHackSuffix); if (pos != string::npos) { - printMsg(lvlDebug, format("removing case hack suffix from `%1%'") % (path + "/" + i)); + printMsg(lvlDebug, format("removing case hack suffix from `%1%'") % (path + "/" + i.name)); name.erase(pos); } if (unhacked.find(name) != unhacked.end()) throw Error(format("file name collision in between `%1%' and `%2%'") - % (path + "/" + unhacked[name]) % (path + "/" + i)); - unhacked[name] = i; + % (path + "/" + unhacked[name]) % (path + "/" + i.name)); + unhacked[name] = i.name; } else - unhacked[i] = i; + unhacked[i.name] = i.name; for (auto & i : unhacked) if (filter(path + "/" + i.first)) { diff --git a/src/libutil/util.cc b/src/libutil/util.cc index f762b79c24da..7dc6fe72a41d 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -203,9 +203,10 @@ bool isLink(const Path & path) } -Strings readDirectory(const Path & path) +DirEntries readDirectory(const Path & path) { - Strings names; + DirEntries entries; + entries.reserve(64); AutoCloseDir dir = opendir(path.c_str()); if (!dir) throw SysError(format("opening directory `%1%'") % path); @@ -215,11 +216,11 @@ Strings readDirectory(const Path & path) checkInterrupt(); string name = dirent->d_name; if (name == "." || name == "..") continue; - names.push_back(name); + entries.emplace_back(DirEntry({ name, dirent->d_ino, dirent->d_type })); } if (errno) throw SysError(format("reading directory `%1%'") % path); - return names; + return entries; } @@ -294,16 +295,14 @@ static void _deletePath(const Path & path, unsigned long long & bytesFreed) bytesFreed += st.st_blocks * 512; if (S_ISDIR(st.st_mode)) { - Strings names = readDirectory(path); - /* Make the directory writable. */ if (!(st.st_mode & S_IWUSR)) { if (chmod(path.c_str(), st.st_mode | S_IWUSR) == -1) throw SysError(format("making `%1%' writable") % path); } - for (Strings::iterator i = names.begin(); i != names.end(); ++i) - _deletePath(path + "/" + *i, bytesFreed); + for (auto & i : readDirectory(path)) + _deletePath(path + "/" + i.name, bytesFreed); } if (remove(path.c_str()) == -1) diff --git a/src/libutil/util.hh b/src/libutil/util.hh index 64250c522a52..42215eb1d365 100644 --- a/src/libutil/util.hh +++ b/src/libutil/util.hh @@ -64,7 +64,16 @@ bool isLink(const Path & path); /* Read the contents of a directory. The entries `.' and `..' are removed. */ -Strings readDirectory(const Path & path); +struct DirEntry +{ + string name; + ino_t ino; + unsigned char type; // one of DT_* +}; + +typedef vector DirEntries; + +DirEntries readDirectory(const Path & path); /* Read the contents of a file into a string. */ string readFile(int fd); -- cgit 1.4.1