1 /* vi: set sw=4 ts=4: */
3 * parse_mode implementation for busybox
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 /* Mar 5, 2003 Manuel Novoa III
12 * This is the main work function for the 'mkdir' applet. As such, it
13 * strives to be SUSv3 compliant in it's behaviour when recursively
14 * making missing parent dirs, and in it's mode setting of the final
17 * To recursively build all missing intermediate directories, make
18 * sure that (flags & FILEUTILS_RECUR) is non-zero. Newly created
19 * intermediate directories will have at least u+wx perms.
21 * To set specific permissions on 'path', pass the appropriate 'mode'
22 * val. Otherwise, pass -1 to get default permissions.
27 /* This function is used from NOFORK applets. It must not allocate anything */
29 int FAST_FUNC
bb_make_directory(char *path
, long mode
, int flags
)
38 /* Happens on bb_make_directory(dirname("no_slashes"),...) */
39 if (LONE_CHAR(path
, '.'))
42 org_mask
= cur_mask
= (mode_t
)-1L;
47 if (flags
& FILEUTILS_RECUR
) { /* Get the parent */
48 /* Bypass leading non-'/'s and then subsequent '/'s */
54 c
= *s
; /* Save the current char */
55 *s
= '\0'; /* and replace it with nul */
63 /* Intermediate dirs: must have wx for user */
64 if (cur_mask
== (mode_t
)-1L) { /* wasn't done yet? */
68 /* Clear u=wx in umask - this ensures
69 * they won't be cleared on mkdir */
70 new_mask
= (org_mask
& ~(mode_t
)0300);
71 //bb_error_msg("org_mask:%o cur_mask:%o", org_mask, new_mask);
72 if (new_mask
!= cur_mask
) {
78 /* Last component: uses original umask */
79 //bb_error_msg("1 org_mask:%o", org_mask);
80 if (org_mask
!= cur_mask
) {
86 if (mkdir(path
, 0777) < 0) {
87 /* If we failed for any other reason than the directory
88 * already exists, output a diagnostic and return -1 */
89 if ((errno
!= EEXIST
&& errno
!= EISDIR
)
90 || !(flags
& FILEUTILS_RECUR
)
91 || ((stat(path
, &st
) < 0) || !S_ISDIR(st
.st_mode
))
96 /* Since the directory exists, don't attempt to change
97 * permissions if it was the full target. Note that
98 * this is not an error condition. */
105 /* Done. If necessary, update perms on the newly
106 * created directory. Failure to update here _is_
108 if ((mode
!= -1) && (chmod(path
, mode
) < 0)) {
109 fail_msg
= "set permissions of";
110 if (flags
& FILEUTILS_IGNORE_CHMOD_ERR
) {
119 /* Remove any inserted nul from the path (recursive mode) */
125 bb_perror_msg("can't %s directory '%s'", fail_msg
, path
);
130 //bb_error_msg("2 org_mask:%o", org_mask);
131 if (org_mask
!= cur_mask
)