about summary refs log tree commit diff
path: root/src/libstore
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstore')
-rw-r--r--src/libstore/build.cc30
-rw-r--r--src/libstore/local-store.cc17
-rw-r--r--src/libstore/local-store.hh6
3 files changed, 31 insertions, 22 deletions
diff --git a/src/libstore/build.cc b/src/libstore/build.cc
index a86fa0f947a0..14388a7b56dc 100644
--- a/src/libstore/build.cc
+++ b/src/libstore/build.cc
@@ -531,14 +531,28 @@ void getOwnership(const Path & path)
 }
 
 
-static void deletePathWrapped(const Path & path)
-{
-    /* When using build users and we're not root, we may not have
-       sufficient permission to delete the path.  So use the setuid
-       helper to change ownership to us. */
-    if (haveBuildUsers() && !amPrivileged())
-        getOwnership(path);
-    deletePath(path);
+void deletePathWrapped(const Path & path,
+    unsigned long long & bytesFreed)
+{
+    try {
+        /* First try to delete it ourselves. */
+        deletePath(path, bytesFreed);
+    } catch (SysError & e) {
+        /* If this failed due to a permission error, then try it with
+           the setuid helper. */
+        if (haveBuildUsers() && !amPrivileged()) {
+            getOwnership(path);
+            deletePath(path, bytesFreed);
+        } else
+            throw;
+    }
+}
+
+
+void deletePathWrapped(const Path & path)
+{
+    unsigned long long dummy;
+    deletePathWrapped(path, dummy);
 }
 
 
diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc
index 237800209714..4144f0831b96 100644
--- a/src/libstore/local-store.cc
+++ b/src/libstore/local-store.cc
@@ -637,7 +637,7 @@ Path LocalStore::addToStore(const Path & _srcPath, bool fixed,
 
         if (!isValidPath(dstPath)) {
 
-            if (pathExists(dstPath)) deletePath(dstPath);
+            if (pathExists(dstPath)) deletePathWrapped(dstPath);
 
             copyPath(srcPath, dstPath);
 
@@ -673,7 +673,7 @@ Path LocalStore::addTextToStore(const string & suffix, const string & s,
 
         if (!isValidPath(dstPath)) {
 
-            if (pathExists(dstPath)) deletePath(dstPath);
+            if (pathExists(dstPath)) deletePathWrapped(dstPath);
 
             writeStringToFile(dstPath, s);
 
@@ -710,18 +710,7 @@ void deleteFromStore(const Path & _path, unsigned long long & bytesFreed)
     }
     txn.commit();
 
-    try {
-        /* First try to delete it ourselves. */
-        deletePath(path, bytesFreed);
-    } catch (SysError & e) {
-        /* If this failed due to a permission error, then try it with
-           the setuid helper. */
-        if (haveBuildUsers() && !amPrivileged()) {
-            getOwnership(path);
-            deletePath(path, bytesFreed);
-        } else
-            throw;
-    }
+    deletePathWrapped(path, bytesFreed);
 }
 
 
diff --git a/src/libstore/local-store.hh b/src/libstore/local-store.hh
index 6e2350abef7e..ef508630ba22 100644
--- a/src/libstore/local-store.hh
+++ b/src/libstore/local-store.hh
@@ -148,6 +148,12 @@ bool amPrivileged();
 /* Recursively change the ownership of `path' to the current uid. */
 void getOwnership(const Path & path);
 
+/* Like deletePath(), but changes the ownership of `path' using the
+   setuid wrapper if necessary (and possible). */
+void deletePathWrapped(const Path & path,
+    unsigned long long & bytesFreed);
+
+void deletePathWrapped(const Path & path);
  
 }