[BusyBox] mkdir (bb_make_directory) fail silently if the target exists but isn't a directory

Michael Tokarev mjt at tls.msk.ru
Mon Sep 27 18:14:27 UTC 2004


Scenario:

   touch x -- creates plain file name `x'
   mkdir x -- exits successefully

libbb/make_directory.c, bb_make_directory(), contains
the following code:

         if (mkdir(path, 0777) < 0) {
             /* If we failed for any other reason than the directory
              * already exists, output a diagnostic and return -1.*/
             if (errno != EEXIST) {
                 fail_msg = "create";
                 umask(mask);
                 break;
             }
             /* Since the directory exists, don't attempt to change
              * permissions if it was the full target.  Note that
              * this is not an error conditon. */
             if (!c) {
                 umask(mask);
                 return 0;
             }
         }

The assumption that EEXIST error is due to that the *directory*
already exists is wrong: any file type with that name will cause
this error to be returned.  Proper way IMHO will be is to stat()
the path and check whenever this is really a directory.  Below
(attached) is a patch to fix this issue.

But I'm not sure whenever this is proper fix, because real mkdir,
without -p option, will fail with "EEXIST" error even if the
target is already existing directory, while busybox mkdir will
silently succed.  Probably another flag is needed, or re-use of
FILEUTILS_RECUR, in wich case the condition will look like:

             if (errno != EEXIST
                 || !(flags & FILEUTILS_RECUR)
                 || (stat(path, &st) < 0 || !S_ISDIR(st.st_mode))) {

/mjt
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: make_directory.patch
Url: http://lists.busybox.net/pipermail/busybox/attachments/20040927/30a3d245/attachment.diff 


More information about the busybox mailing list