find ./ -name .cvsignore | xargs svn rm
[oweals/busybox.git] / libbb / make_directory.c
index 710537dd1a47934c15910919870407dc9a30d5f6..d96acf0d9c045cbe912486bb12a6f0346a988a3d 100644 (file)
@@ -37,6 +37,7 @@
 
 #include <errno.h>
 #include <unistd.h>
+#include <sys/stat.h>
 #include "libbb.h"
 
 int bb_make_directory (char *path, long mode, int flags)
@@ -45,9 +46,17 @@ int bb_make_directory (char *path, long mode, int flags)
        const char *fail_msg;
        char *s = path;
        char c;
+       struct stat st;
 
        mask = umask(0);
-       umask(mask & ~0300);
+       if (mode == -1) {
+               umask(mask);
+               mode = (S_IXUSR | S_IXGRP | S_IXOTH |
+                               S_IWUSR | S_IWGRP | S_IWOTH |
+                               S_IRUSR | S_IRGRP | S_IROTH) & ~mask;
+       } else {
+               umask(mask & ~0300);
+       }
 
        do {
                c = 0;
@@ -70,7 +79,9 @@ int bb_make_directory (char *path, long mode, int flags)
                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) {
+                       if (errno != EEXIST
+                               || !(flags & FILEUTILS_RECUR)
+                               || (stat(path, &st) < 0 || !S_ISDIR(st.st_mode))) {
                                fail_msg = "create";
                                umask(mask);
                                break;