From 5b427ce7c37a517695b30a415c14dace633da6b4 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Mon, 24 Oct 2005 10:03:47 +0000 Subject: A command like `mkdir -p nonexistent/.' would create the 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 | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/mkdir-p.c b/lib/mkdir-p.c index 7c3965f3e..ac2e28749 100644 --- a/lib/mkdir-p.c +++ b/lib/mkdir-p.c @@ -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. */ + } + } } } -- cgit v1.2.3-70-g09d2