Hi, attached are two patches, one adds the -m option to mkdir (set permissions of created directory), the other adds a test script for it. The test script uses the non-standard command stat since it seems to be the right tool for determining permissions of directories.
Felix
# HG changeset patch # User Felix Janda <[email protected]> # Date 1356276331 -3600 # Node ID 89bee646c1c50346aae576ab9140bc05b1eea879 # Parent 5da97f834a06da1368b20c5dc67ad17ed8339841 Add -m option to mkdir diff -r 5da97f834a06 -r 89bee646c1c5 toys/posix/mkdir.c --- a/toys/posix/mkdir.c Wed Dec 19 09:16:45 2012 -0600 +++ b/toys/posix/mkdir.c Sun Dec 23 16:25:31 2012 +0100 @@ -4,31 +4,33 @@ * * See http://opengroup.org/onlinepubs/9699919799/utilities/mkdir.html * - * TODO: Add -m -USE_MKDIR(NEWTOY(mkdir, "<1p", TOYFLAG_BIN)) +USE_MKDIR(NEWTOY(mkdir, "<1pm:", TOYFLAG_BIN)) config MKDIR bool "mkdir" default y help - usage: mkdir [-p] [dirname...] + usage: mkdir [-p] [-m mode] [dirname...] Create one or more directories. -p make parent directories as needed. + -m set permissions of directory to mode. */ #define FOR_mkdir #include "toys.h" GLOBALS( - long mode; + char *arg_mode; + mode_t mode; ) 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,12 +46,16 @@ char save=0; // Skip leading / of absolute paths. - if (s!=dir && *s == '/' && toys.optflags) { + if (s!=dir && *s == '/' && (toys.optflags&FLAG_p)) { save = *s; *s = 0; } else if (*s) continue; - if (mkdir(dir, TT.mode)<0 && (!toys.optflags || errno != EEXIST)) + // Use the mode from the -m option only for the last directory. + if ((toys.optflags&FLAG_m) && save != '/') + mode = TT.mode; + + if (mkdir(dir, mode)<0 && ((toys.optflags&~FLAG_p) || errno != EEXIST)) return 1; if (!(*s = save)) break; @@ -63,6 +69,8 @@ char **s; TT.mode = 0777; + if(toys.optflags&FLAG_m) + TT.mode = string_to_mode(TT.arg_mode, TT.mode); for (s=toys.optargs; *s; s++) { if (do_mkdir(*s)) {
# HG changeset patch # User Felix Janda <[email protected]> # Date 1356281075 -3600 # Node ID 0ea9c498ac859d0d271b538a6cfb56e714704e33 # Parent 89bee646c1c50346aae576ab9140bc05b1eea879 Add some tests for mkdir diff -r 89bee646c1c5 -r 0ea9c498ac85 scripts/test/mkdir.test --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/test/mkdir.test Sun Dec 23 17:44:35 2012 +0100 @@ -0,0 +1,44 @@ +#!/bin/bash + +[ -f testing.sh ] && . testing.sh + +#testing "name" "command" "result" "infile" "stdin" + +testing "mkdir" "mkdir one && [ -d one ] && echo yes" "yes\n" "" "" +rmdir one + +touch existing +testing "mkdir existing" \ + "mkdir existing 2> /dev/null || [ -f existing ] && echo yes" "yes\n" "" "" +rm existing + +testing "mkdir one two" \ + "mkdir one two && [ -d one ] && [ -d two ] && echo yes" "yes\n" "" "" +rmdir one two + +testing "mkdir missing/one" \ + "mkdir missing/one 2> /dev/null || [ ! -d missing ] && echo yes" "yes\n" "" "" + +testing "mkdir -p" \ + "mkdir -p one/two/three && [ -d one/two/three ] && echo yes" "yes\n" "" "" +rm -rf one + +mkdir existing +testing "mkdir -p existing" "mkdir -p existing && echo yes" "yes\n" "" "" +rmdir existing + +umask 123 +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 + +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
_______________________________________________ Toybox mailing list [email protected] http://lists.landley.net/listinfo.cgi/toybox-landley.net
