Fix mkdir -p to accept paths that already exist, and detect path ending in a file.
authorRob Landley <rob@landley.net>
Thu, 8 Mar 2012 02:05:36 +0000 (20:05 -0600)
committerRob Landley <rob@landley.net>
Thu, 8 Mar 2012 02:05:36 +0000 (20:05 -0600)
toys/mkdir.c

index d875f95..9032901 100644 (file)
@@ -30,21 +30,35 @@ DEFINE_GLOBALS(
 
 static int do_mkdir(char *dir)
 {
-       unsigned int i;
-
-       if (toys.optflags && *dir) {
-               // Skip first char (it can be /)
-               for (i = 1; dir[i]; i++) {
-                       int ret;
-
-                       if (dir[i] != '/') continue;
-                       dir[i] = 0;
-                       ret = mkdir(dir, TT.mode);
-                       if (ret < 0 && errno != EEXIST) return ret;
-                       dir[i] = '/';
-               }
+       struct stat buf;
+       char *s;
+
+       // mkdir -p one/two/three is not an error if the path already exists,
+       // but is if "three" is a file.  The others we dereference and catch
+       // not-a-directory along the way, but the last one we must explicitly
+       // test for. Might as well do it up front.
+
+       if (!stat(dir, &buf) && !S_ISDIR(buf.st_mode)) {
+               errno = EEXIST;
+               return 1;
        }
-       return mkdir(dir, TT.mode);
+
+       for (s=dir; ; s++) {
+               char save=0;
+
+               // Skip leading / of absolute paths.
+               if (s!=dir && *s == '/' && toys.optflags) {
+                       save = *s;
+                       *s = 0;
+               } else if (*s) continue;
+
+               if (mkdir(dir, TT.mode)<0 && (!toys.optflags || errno != EEXIST))
+                       return 1;
+
+               if (!(*s = save)) break;
+       }
+
+       return 0;
 }
 
 void mkdir_main(void)