about summary refs log tree commit diff
path: root/src/util.cc
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2003-08-22T20·12+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2003-08-22T20·12+0000
commita88144215c263e62528108dfae1e781058344ef2 (patch)
treeff44455c3a5a5d4e6b3a556c78cc1511f7630378 /src/util.cc
parent56b98c3857b89d4f81f0127c53cfce6d8e48a71f (diff)
* Remove write permission from output paths after they have been built.
* Point $HOME to a non-existing path when building to prevent certain tools (such as 
  wget) from falling back on /etc/passwd to locate the home directory (which we 
  don't want them to look at since it's not declared as an input).

Diffstat (limited to 'src/util.cc')
-rw-r--r--src/util.cc38
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 */
+    }
 }