chiark / gitweb /
install: make systemd-install useful for installation of template instances
[elogind.git] / src / util.c
index 2363ea27b2b87766a459aa31522f7a71a968874d..78d8d5d9c5d99fd1a91d72858fda6d17a09407fe 100644 (file)
@@ -893,6 +893,53 @@ int mkdir_p(const char *path, mode_t mode) {
         return 0;
 }
 
+int rmdir_parents(const char *path, const char *stop) {
+        size_t l;
+        int r = 0;
+
+        assert(path);
+        assert(stop);
+
+        l = strlen(path);
+
+        /* Skip trailing slashes */
+        while (l > 0 && path[l-1] == '/')
+                l--;
+
+        while (l > 0) {
+                char *t;
+
+                /* Skip last component */
+                while (l > 0 && path[l-1] != '/')
+                        l--;
+
+                /* Skip trailing slashes */
+                while (l > 0 && path[l-1] == '/')
+                        l--;
+
+                if (l <= 0)
+                        break;
+
+                if (!(t = strndup(path, l)))
+                        return -ENOMEM;
+
+                if (path_startswith(stop, t)) {
+                        free(t);
+                        return 0;
+                }
+
+                r = rmdir(t);
+                free(t);
+
+                if (r < 0)
+                        if (errno != ENOENT)
+                                return -errno;
+        }
+
+        return 0;
+}
+
+
 char hexchar(int x) {
         static const char table[16] = "0123456789abcdef";