about summary refs log tree commit diff
path: root/src/util.cc
diff options
context:
space:
mode:
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;