From c602930e08a508fce76b16f6f7f1fdfaed3b91ab Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 8 Aug 2003 14:55:56 +0000 Subject: [PATCH] * deletePath(): some operating systems (e.g., Mac OS X) don't like it when we delete entries from a directory while we are reading it. So read the directory into memory, then delete its contents. --- src/util.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/util.cc b/src/util.cc index 3c9a31acc1..1e1c799ece 100644 --- a/src/util.cc +++ b/src/util.cc @@ -108,21 +108,28 @@ bool pathExists(const string & path) void deletePath(string path) { + msg(lvlVomit, format("deleting path `%1%'") % path); + struct stat st; if (lstat(path.c_str(), &st)) throw SysError(format("getting attributes of path %1%") % path); if (S_ISDIR(st.st_mode)) { + Strings names; + DIR * dir = opendir(path.c_str()); struct dirent * dirent; while (errno = 0, dirent = readdir(dir)) { string name = dirent->d_name; if (name == "." || name == "..") continue; - deletePath(path + "/" + name); + names.push_back(name); } closedir(dir); /* !!! close on exception */ + + for (Strings::iterator i = names.begin(); i != names.end(); i++) + deletePath(path + "/" + *i); } if (remove(path.c_str()) == -1)