blob: 89352ca1f180d782dd08465f7923e23d169a35ee [file] [log] [blame]
Matt Kraaiceeff732001-06-21 19:41:37 +00001/* vi: set sw=4 ts=4: */
2/*
Manuel Novoa III cad53642003-03-19 09:13:01 +00003 * parse_mode implementation for busybox
Matt Kraaiceeff732001-06-21 19:41:37 +00004 *
Manuel Novoa III cad53642003-03-19 09:13:01 +00005 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
Matt Kraaiceeff732001-06-21 19:41:37 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Matt Kraaiceeff732001-06-21 19:41:37 +00008 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* Mar 5, 2003 Manuel Novoa III
11 *
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
15 * directory 'path'.
16 *
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.
20 *
Eric Andersenaff114c2004-04-14 17:51:38 +000021 * To set specific permissions on 'path', pass the appropriate 'mode'
22 * val. Otherwise, pass -1 to get default permissions.
Manuel Novoa III cad53642003-03-19 09:13:01 +000023 */
Matt Kraaiceeff732001-06-21 19:41:37 +000024
25#include "libbb.h"
26
Denis Vlasenko99912ca2007-04-10 15:43:37 +000027/* This function is used from NOFORK applets. It must not allocate anything */
28
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000029int FAST_FUNC bb_make_directory(char *path, long mode, int flags)
Matt Kraaiceeff732001-06-21 19:41:37 +000030{
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +010031 mode_t cur_mask;
32 mode_t org_mask;
Manuel Novoa III cad53642003-03-19 09:13:01 +000033 const char *fail_msg;
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +010034 char *s;
Manuel Novoa III cad53642003-03-19 09:13:01 +000035 char c;
Eric Andersen28426592004-10-08 07:21:58 +000036 struct stat st;
Glenn L McGrath822e7fd2002-11-24 22:48:20 +000037
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +010038 /* Happens on bb_make_directory(dirname("no_slashes"),...) */
39 if (LONE_CHAR(path, '.'))
40 return 0;
Glenn L McGrath822e7fd2002-11-24 22:48:20 +000041
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +010042 org_mask = cur_mask = (mode_t)-1L;
43 s = path;
Denis Vlasenko11152e32008-08-15 19:18:35 +000044 while (1) {
45 c = '\0';
Glenn L McGrath5b110872002-11-24 23:22:29 +000046
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020047 if (flags & FILEUTILS_RECUR) { /* Get the parent */
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +010048 /* Bypass leading non-'/'s and then subsequent '/'s */
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 while (*s) {
50 if (*s == '/') {
51 do {
52 ++s;
53 } while (*s == '/');
Denis Vlasenko11152e32008-08-15 19:18:35 +000054 c = *s; /* Save the current char */
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +010055 *s = '\0'; /* and replace it with nul */
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 break;
57 }
58 ++s;
59 }
Glenn L McGrath5b110872002-11-24 23:22:29 +000060 }
61
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +010062 if (c != '\0') {
63 /* Intermediate dirs: must have wx for user */
64 if (cur_mask == (mode_t)-1L) { /* wasn't done yet? */
65 mode_t new_mask;
66 org_mask = umask(0);
67 cur_mask = 0;
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) {
73 cur_mask = new_mask;
74 umask(new_mask);
75 }
76 }
77 } else {
78 /* Last component: uses original umask */
79 //bb_error_msg("1 org_mask:%o", org_mask);
80 if (org_mask != cur_mask) {
81 cur_mask = org_mask;
82 umask(org_mask);
83 }
84 }
Denis Vlasenko11152e32008-08-15 19:18:35 +000085
Manuel Novoa III cad53642003-03-19 09:13:01 +000086 if (mkdir(path, 0777) < 0) {
87 /* If we failed for any other reason than the directory
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +010088 * already exists, output a diagnostic and return -1 */
Jeremie Koenig84b01d52010-05-27 15:46:33 +020089 if ((errno != EEXIST && errno != EISDIR)
Denis Vlasenko11152e32008-08-15 19:18:35 +000090 || !(flags & FILEUTILS_RECUR)
91 || ((stat(path, &st) < 0) || !S_ISDIR(st.st_mode))
92 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000093 fail_msg = "create";
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 break;
95 }
96 /* Since the directory exists, don't attempt to change
97 * permissions if it was the full target. Note that
Denis Vlasenko582dff02008-10-19 19:36:30 +000098 * this is not an error condition. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000099 if (!c) {
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100100 goto ret0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101 }
Denys Vlasenko17f84182014-05-19 16:23:50 +0200102 } else {
103 if (flags & FILEUTILS_VERBOSE) {
104 printf("created directory: '%s'\n", path);
105 }
Glenn L McGrath193697d2002-08-24 20:11:38 +0000106 }
Glenn L McGrath822e7fd2002-11-24 22:48:20 +0000107
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108 if (!c) {
Denis Vlasenko11152e32008-08-15 19:18:35 +0000109 /* Done. If necessary, update perms on the newly
Manuel Novoa III cad53642003-03-19 09:13:01 +0000110 * created directory. Failure to update here _is_
Denis Vlasenko11152e32008-08-15 19:18:35 +0000111 * an error. */
112 if ((mode != -1) && (chmod(path, mode) < 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000113 fail_msg = "set permissions of";
Natanael Copa02112d82012-05-22 17:11:46 +0200114 if (flags & FILEUTILS_IGNORE_CHMOD_ERR) {
115 flags = 0;
116 goto print_err;
117 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000118 break;
119 }
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100120 goto ret0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121 }
122
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100123 /* Remove any inserted nul from the path (recursive mode) */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000124 *s = c;
Denis Vlasenko11152e32008-08-15 19:18:35 +0000125 } /* while (1) */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000126
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100127 flags = -1;
Natanael Copa02112d82012-05-22 17:11:46 +0200128 print_err:
129 bb_perror_msg("can't %s directory '%s'", fail_msg, path);
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100130 goto ret;
131 ret0:
132 flags = 0;
133 ret:
134 //bb_error_msg("2 org_mask:%o", org_mask);
135 if (org_mask != cur_mask)
136 umask(org_mask);
137 return flags;
Matt Kraaiceeff732001-06-21 19:41:37 +0000138}