blob: a6b7c28df06c7366afc51d3a2ce528f3b8858be5 [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 Vlasenko4bd0c2a2016-12-04 10:42:07 +010038 /* "path" can be a result of dirname().
39 * dirname("no_slashes") returns ".", possibly read-only.
40 * musl dirname() can return read-only "/" too.
41 * We need writable string. And for "/", "." (and ".."?)
42 * nothing needs to be created anyway.
43 */
44 if (LONE_CHAR(path, '/'))
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +010045 return 0;
Denys Vlasenko4bd0c2a2016-12-04 10:42:07 +010046 if (path[0] == '.') {
47 if (path[1] == '\0')
48 return 0; /* "." */
49// if (path[1] == '.' && path[2] == '\0')
50// return 0; /* ".." */
51 }
Glenn L McGrath822e7fd2002-11-24 22:48:20 +000052
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +010053 org_mask = cur_mask = (mode_t)-1L;
54 s = path;
Denis Vlasenko11152e32008-08-15 19:18:35 +000055 while (1) {
56 c = '\0';
Glenn L McGrath5b110872002-11-24 23:22:29 +000057
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020058 if (flags & FILEUTILS_RECUR) { /* Get the parent */
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +010059 /* Bypass leading non-'/'s and then subsequent '/'s */
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 while (*s) {
61 if (*s == '/') {
62 do {
63 ++s;
64 } while (*s == '/');
Denis Vlasenko11152e32008-08-15 19:18:35 +000065 c = *s; /* Save the current char */
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +010066 *s = '\0'; /* and replace it with nul */
Manuel Novoa III cad53642003-03-19 09:13:01 +000067 break;
68 }
69 ++s;
70 }
Glenn L McGrath5b110872002-11-24 23:22:29 +000071 }
72
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +010073 if (c != '\0') {
74 /* Intermediate dirs: must have wx for user */
75 if (cur_mask == (mode_t)-1L) { /* wasn't done yet? */
76 mode_t new_mask;
77 org_mask = umask(0);
78 cur_mask = 0;
79 /* Clear u=wx in umask - this ensures
80 * they won't be cleared on mkdir */
81 new_mask = (org_mask & ~(mode_t)0300);
82 //bb_error_msg("org_mask:%o cur_mask:%o", org_mask, new_mask);
83 if (new_mask != cur_mask) {
84 cur_mask = new_mask;
85 umask(new_mask);
86 }
87 }
88 } else {
89 /* Last component: uses original umask */
90 //bb_error_msg("1 org_mask:%o", org_mask);
91 if (org_mask != cur_mask) {
92 cur_mask = org_mask;
93 umask(org_mask);
94 }
95 }
Denis Vlasenko11152e32008-08-15 19:18:35 +000096
Manuel Novoa III cad53642003-03-19 09:13:01 +000097 if (mkdir(path, 0777) < 0) {
98 /* If we failed for any other reason than the directory
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +010099 * already exists, output a diagnostic and return -1 */
Jeremie Koenig84b01d52010-05-27 15:46:33 +0200100 if ((errno != EEXIST && errno != EISDIR)
Denis Vlasenko11152e32008-08-15 19:18:35 +0000101 || !(flags & FILEUTILS_RECUR)
102 || ((stat(path, &st) < 0) || !S_ISDIR(st.st_mode))
103 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000104 fail_msg = "create";
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105 break;
106 }
107 /* Since the directory exists, don't attempt to change
108 * permissions if it was the full target. Note that
Denis Vlasenko582dff02008-10-19 19:36:30 +0000109 * this is not an error condition. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000110 if (!c) {
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100111 goto ret0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000112 }
Denys Vlasenko17f84182014-05-19 16:23:50 +0200113 } else {
114 if (flags & FILEUTILS_VERBOSE) {
115 printf("created directory: '%s'\n", path);
116 }
Glenn L McGrath193697d2002-08-24 20:11:38 +0000117 }
Glenn L McGrath822e7fd2002-11-24 22:48:20 +0000118
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 if (!c) {
Denis Vlasenko11152e32008-08-15 19:18:35 +0000120 /* Done. If necessary, update perms on the newly
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121 * created directory. Failure to update here _is_
Denis Vlasenko11152e32008-08-15 19:18:35 +0000122 * an error. */
123 if ((mode != -1) && (chmod(path, mode) < 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000124 fail_msg = "set permissions of";
Natanael Copa02112d82012-05-22 17:11:46 +0200125 if (flags & FILEUTILS_IGNORE_CHMOD_ERR) {
126 flags = 0;
127 goto print_err;
128 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 break;
130 }
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100131 goto ret0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000132 }
133
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100134 /* Remove any inserted nul from the path (recursive mode) */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000135 *s = c;
Denis Vlasenko11152e32008-08-15 19:18:35 +0000136 } /* while (1) */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000137
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100138 flags = -1;
Natanael Copa02112d82012-05-22 17:11:46 +0200139 print_err:
140 bb_perror_msg("can't %s directory '%s'", fail_msg, path);
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100141 goto ret;
142 ret0:
143 flags = 0;
144 ret:
145 //bb_error_msg("2 org_mask:%o", org_mask);
146 if (org_mask != cur_mask)
147 umask(org_mask);
148 return flags;
Matt Kraaiceeff732001-06-21 19:41:37 +0000149}