aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--scripts/test/mkdir.test20
-rw-r--r--toys/posix/mkdir.c9
2 files changed, 23 insertions, 6 deletions
diff --git a/scripts/test/mkdir.test b/scripts/test/mkdir.test
index 035d6132..cbd425d4 100644
--- a/scripts/test/mkdir.test
+++ b/scripts/test/mkdir.test
@@ -32,13 +32,29 @@ testing "mkdir (default permissions)" \
"mkdir one && stat -c %a one" "654\n" "" ""
rmdir one
-umask 000
-
testing "mkdir -m 124" \
"mkdir -m 124 one && stat -c %a one" "124\n" "" ""
rmdir one
+umask 000
testing "mkdir -p -m 653" \
"mkdir -p -m 653 one/two && stat -c %a one && stat -c %a one/two" \
"777\n653\n" "" ""
rm -rf one
+
+testing "mkdir -p one/two/ (trailing slash)" \
+ "mkdir -p one/two/ && [ -d one/two ] && echo yes" "yes\n" "" ""
+rm -rf one
+
+umask 022
+testing "mkdir -p -m 777 (022 umask)" \
+ "mkdir -p -m 777 one/two && stat -c %a one && stat -c %a one/two" \
+ "755\n777\n" "" ""
+rm -rf one
+
+umask 377
+testing "mkdir -p -m 777 (377 umask)" \
+ "mkdir -p -m 777 one/two && stat -c %a one && stat -c %a one/two" \
+ "700\n777\n" "" ""
+umask 002
+rm -rf one
diff --git a/toys/posix/mkdir.c b/toys/posix/mkdir.c
index 24930f4a..202eaa79 100644
--- a/toys/posix/mkdir.c
+++ b/toys/posix/mkdir.c
@@ -4,7 +4,7 @@
*
* See http://opengroup.org/onlinepubs/9699919799/utilities/mkdir.html
-USE_MKDIR(NEWTOY(mkdir, "<1pm:", TOYFLAG_BIN))
+USE_MKDIR(NEWTOY(mkdir, "<1pm:", TOYFLAG_BIN|TOYFLAG_UMASK))
config MKDIR
bool "mkdir"
@@ -30,7 +30,6 @@ static int do_mkdir(char *dir)
{
struct stat buf;
char *s;
- mode_t mode = 0777;
// 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
@@ -44,6 +43,7 @@ static int do_mkdir(char *dir)
for (s=dir; ; s++) {
char save=0;
+ mode_t mode = 0777&~toys.old_umask;
// Skip leading / of absolute paths.
if (s!=dir && *s == '/' && (toys.optflags&FLAG_p)) {
@@ -52,9 +52,10 @@ static int do_mkdir(char *dir)
} else if (*s) continue;
// Use the mode from the -m option only for the last directory.
- if ((toys.optflags&FLAG_m) && save != '/') mode = TT.mode;
+ if (save == '/') mode |= 0300;
+ else if (toys.optflags&FLAG_m) mode = TT.mode;
- if (mkdir(dir, mode)<0 && ((toys.optflags&~FLAG_p) || errno != EEXIST))
+ if (mkdir(dir, mode)<0 && (!(toys.optflags&FLAG_p) || errno != EEXIST))
return 1;
if (!(*s = save)) break;