chiark / gitweb /
udevadm: remove old man page links and compat links for debugging tools
[elogind.git] / udev_utils_file.c
index a3dab58b36ae5c6d7b08b850ce50edf1f8ebc749..0ceefe17200c68bab8e08a20b0d3be2dc60834ee 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * udev_utils_file.c - files operations
- *
  * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
  *
  *     This program is free software; you can redistribute it and/or modify it
@@ -14,7 +12,7 @@
  * 
  *     You should have received a copy of the GNU General Public License along
  *     with this program; if not, write to the Free Software Foundation, Inc.,
- *     675 Mass Ave, Cambridge, MA 02139, USA.
+ *     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  *
  */
 
 #include <sys/stat.h>
 #include <sys/mman.h>
 
-#include "udev_libc_wrapper.h"
 #include "udev.h"
-#include "logging.h"
-#include "udev_utils.h"
-#include "list.h"
 
 int create_path(const char *path)
 {
@@ -42,25 +36,63 @@ int create_path(const char *path)
        char *pos;
        struct stat stats;
 
-       strcpy (p, path);
+       strlcpy(p, path, sizeof(p));
        pos = strrchr(p, '/');
        if (pos == p || pos == NULL)
                return 0;
 
        while (pos[-1] == '/')
                pos--;
-
        pos[0] = '\0';
 
-       dbg("stat '%s'\n", p);
-       if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
+       dbg("stat '%s'", p);
+       if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
                return 0;
 
-       if (create_path (p) != 0)
+       if (create_path(p) != 0)
                return -1;
 
-       dbg("mkdir '%s'\n", p);
-       return mkdir(p, 0755);
+       dbg("mkdir '%s'", p);
+       if (mkdir(p, 0755) == 0)
+               return 0;
+       if (errno == EEXIST)
+               if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
+                       return 0;
+       return -1;
+}
+
+int delete_path(const char *path)
+{
+       char p[PATH_SIZE];
+       char *pos;
+       int retval;
+
+       strcpy (p, path);
+       pos = strrchr(p, '/');
+       if (pos == p || pos == NULL)
+               return 0;
+
+       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,