about summary refs log tree commit diff
path: root/src/libutil
diff options
context:
space:
mode:
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/util.cc32
-rw-r--r--src/libutil/util.hh8
2 files changed, 28 insertions, 12 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index d8d3751a1fc3..428b1ff9a755 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -350,13 +350,16 @@ Path createTempDir(const Path & tmpRoot)
 }
 
 
-void createDirs(const Path & path)
+Paths createDirs(const Path & path)
 {
-    if (path == "/") return;
-    createDirs(dirOf(path));
-    if (!pathExists(path))
+    if (path == "/") return Paths();
+    Paths created = createDirs(dirOf(path));
+    if (!pathExists(path)) {
         if (mkdir(path.c_str(), 0777) == -1)
             throw SysError(format("creating directory `%1%'") % path);
+        created.push_back(path);
+    }
+    return created;
 }
 
 
@@ -509,14 +512,25 @@ string drainFD(int fd)
 //////////////////////////////////////////////////////////////////////
 
 
-AutoDelete::AutoDelete(const string & p) : path(p)
+AutoDelete::AutoDelete(const string & p, bool recursive) : path(p)
 {
     del = true;
+    this->recursive = recursive;
 }
 
 AutoDelete::~AutoDelete()
 {
-    if (del) deletePath(path);
+    try {
+        if (del)
+            if (recursive)
+                deletePath(path);
+            else {
+                if (remove(path.c_str()) == -1)
+                    throw SysError(format("cannot unlink `%1%'") % path);
+            }
+    } catch (...) {
+        ignoreException();
+    }
 }
 
 void AutoDelete::cancel()
@@ -752,10 +766,10 @@ void killUser(uid_t uid)
 		if (errno != EINTR)
 		    throw SysError(format("cannot kill processes for uid `%1%'") % uid);
 	    }
-        
+
         } catch (std::exception & e) {
-            std::cerr << format("killing processes beloging to uid `%1%': %1%\n")
-                % uid % e.what();
+            std::cerr << format("killing processes beloging to uid `%1%': %1%")
+                % uid % e.what() << std::endl;
             quickExit(1);
         }
         quickExit(0);
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index f72a6f82011b..0ed98118c53b 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -72,8 +72,9 @@ void makePathReadOnly(const Path & path);
 /* Create a temporary directory. */
 Path createTempDir(const Path & tmpRoot = "");
 
-/* Create a directory and all its parents, if necessary. */
-void createDirs(const Path & path);
+/* Create a directory and all its parents, if necessary.  Returns the
+   list of created directories, in order of creation. */
+Paths createDirs(const Path & path);
 
 /* Create a file and write the given text to it.  The file is written
    in binary mode (i.e., no end-of-line conversions).  The path should
@@ -166,8 +167,9 @@ class AutoDelete
 {
     Path path;
     bool del;
+    bool recursive;    
 public:
-    AutoDelete(const Path & p);
+    AutoDelete(const Path & p, bool recursive = true);
     ~AutoDelete();
     void cancel();
 };