A command like `mkdir -p nonexistent/.' would create the
authorJim Meyering <jim@meyering.net>
Mon, 24 Oct 2005 10:03:47 +0000 (10:03 +0000)
committerJim Meyering <jim@meyering.net>
Mon, 24 Oct 2005 10:03:47 +0000 (10:03 +0000)
directory but exit nonzero with a diagnostic.  This could also be
triggered with a non-`.' component, e.g., in a race with another
process running the same `mkdir -p nonexistent/sub' command.

(make_dir_parents): Handle the case of an existing final component.
Reported by Matthias Andree here:
http://savannah.gnu.org/bugs/?func=detailitem&item_id=14848

lib/mkdir-p.c

index 7c3965f..ac2e287 100644 (file)
@@ -264,17 +264,25 @@ make_dir_parents (char const *arg,
         Create the final component of the file name.  */
       if (retval)
        {
-         if (mkdir (basename_dir, mode) != 0)
-           {
-             error (0, errno, _("cannot create directory %s"), quote (dir));
-             retval = false;
-           }
-         else
+         bool just_created = (mkdir (basename_dir, mode) == 0);
+         if (just_created)
            {
              if (verbose_fmt_string)
                error (0, 0, verbose_fmt_string, quote (dir));
              fixup_permissions_dir = basename_dir;
            }
+         else
+           {
+             if (errno != EEXIST)
+               {
+                 error (0, errno, _("cannot create directory %s"), quote (dir));
+                 retval = false;
+               }
+             else
+               {
+                 /* The directory already exists.  Do nothing.  */
+               }
+           }
        }
     }