diff options
Diffstat (limited to 'src/util.cc')
-rw-r--r-- | src/util.cc | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/src/util.cc b/src/util.cc index 1e1c799ece6d..c7ae711bbac7 100644 --- a/src/util.cc +++ b/src/util.cc @@ -106,13 +106,13 @@ bool pathExists(const string & path) } -void deletePath(string path) +void deletePath(const 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); + throw SysError(format("getting attributes of path `%1%'") % path); if (S_ISDIR(st.st_mode)) { Strings names; @@ -128,12 +128,44 @@ void deletePath(string path) closedir(dir); /* !!! close on exception */ + /* 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")); + } + for (Strings::iterator i = names.begin(); i != names.end(); i++) deletePath(path + "/" + *i); } if (remove(path.c_str()) == -1) - throw SysError(format("cannot unlink %1%") % path); + throw SysError(format("cannot unlink `%1%'") % path); +} + + +void makePathReadOnly(const string & path) +{ + struct stat st; + if (lstat(path.c_str(), &st)) + throw SysError(format("getting attributes of path `%1%'") % path); + + if (st.st_mode & S_IWUSR) { + if (chmod(path.c_str(), st.st_mode & ~S_IWUSR) == -1) + throw SysError(format("making `%1%' read-only")); + } + + if (S_ISDIR(st.st_mode)) { + DIR * dir = opendir(path.c_str()); + + struct dirent * dirent; + while (errno = 0, dirent = readdir(dir)) { + string name = dirent->d_name; + if (name == "." || name == "..") continue; + makePathReadOnly(path + "/" + name); + } + + closedir(dir); /* !!! close on exception */ + } } |