about summary refs log tree commit diff
path: root/src/util.cc
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2003-06-23T14·40+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2003-06-23T14·40+0000
commit692b562342ac7ead43ef06497f6a8b4b6e724ae5 (patch)
tree4cd87673fff6af4c6c5501b274bfc1023246aaba /src/util.cc
parentc0cbaef4bece0c2447828739dd9622c329948064 (diff)
* `nix --delete' command.
Diffstat (limited to 'src/util.cc')
-rw-r--r--src/util.cc29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/util.cc b/src/util.cc
index 8c397aace8c8..4dada48ba67e 100644
--- a/src/util.cc
+++ b/src/util.cc
@@ -1,5 +1,10 @@
 #include <iostream>
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <dirent.h>
+
 #include "util.hh"
 
 
@@ -49,6 +54,30 @@ string baseNameOf(string path)
 }
 
 
+void deletePath(string path)
+{
+    struct stat st;
+    if (lstat(path.c_str(), &st))
+        throw SysError("getting attributes of path " + path);
+
+    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;
+            deletePath(path + "/" + name);
+        }
+
+        closedir(dir); /* !!! close on exception */
+    }
+
+    if (remove(path.c_str()) == -1)
+        throw SysError("cannot unlink " + path);
+}
+
+
 void debug(string s)
 {
     cerr << "debug: " << s << endl;