chiark / gitweb /
move delete_path() to utils
[elogind.git] / udev_utils_file.c
index a3dab58b36ae5c6d7b08b850ce50edf1f8ebc749..cd9c244f0f3d1b799f58063032ba2da959c6e3b5 100644 (file)
@@ -63,6 +63,37 @@ int create_path(const char *path)
        return mkdir(p, 0755);
 }
 
+int delete_path(const char *path)
+{
+       char p[PATH_SIZE];
+       char *pos;
+       int retval;
+
+       strcpy (p, path);
+       pos = strrchr(p, '/');
+       while (1) {
+               *pos = '\0';
+               pos = strrchr(p, '/');
+
+               /* don't remove the last one */
+               if ((pos == p) || (pos == NULL))
+                       break;
+
+               /* remove if empty */
+               retval = rmdir(p);
+               if (errno == ENOENT)
+                       retval = 0;
+               if (retval) {
+                       if (errno == ENOTEMPTY)
+                               return 0;
+                       err("rmdir(%s) failed: %s", p, strerror(errno));
+                       break;
+               }
+               dbg("removed '%s'", p);
+       }
+       return 0;
+}
+
 /* Reset permissions on the device node, before unlinking it to make sure,
  * that permisions of possible hard links will be removed too.
  */