* 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.
This commit is contained in:
parent
4b7b0bd12c
commit
c602930e08
1 changed files with 8 additions and 1 deletions
|
@ -108,21 +108,28 @@ bool pathExists(const string & path)
|
||||||
|
|
||||||
void deletePath(string path)
|
void deletePath(string path)
|
||||||
{
|
{
|
||||||
|
msg(lvlVomit, format("deleting path `%1%'") % path);
|
||||||
|
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if (lstat(path.c_str(), &st))
|
if (lstat(path.c_str(), &st))
|
||||||
throw SysError(format("getting attributes of path %1%") % path);
|
throw SysError(format("getting attributes of path %1%") % path);
|
||||||
|
|
||||||
if (S_ISDIR(st.st_mode)) {
|
if (S_ISDIR(st.st_mode)) {
|
||||||
|
Strings names;
|
||||||
|
|
||||||
DIR * dir = opendir(path.c_str());
|
DIR * dir = opendir(path.c_str());
|
||||||
|
|
||||||
struct dirent * dirent;
|
struct dirent * dirent;
|
||||||
while (errno = 0, dirent = readdir(dir)) {
|
while (errno = 0, dirent = readdir(dir)) {
|
||||||
string name = dirent->d_name;
|
string name = dirent->d_name;
|
||||||
if (name == "." || name == "..") continue;
|
if (name == "." || name == "..") continue;
|
||||||
deletePath(path + "/" + name);
|
names.push_back(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
closedir(dir); /* !!! close on exception */
|
closedir(dir); /* !!! close on exception */
|
||||||
|
|
||||||
|
for (Strings::iterator i = names.begin(); i != names.end(); i++)
|
||||||
|
deletePath(path + "/" + *i);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (remove(path.c_str()) == -1)
|
if (remove(path.c_str()) == -1)
|
||||||
|
|
Loading…
Reference in a new issue