blob: 72303e7a30e8abf48be39a9f503aee42573dd162 [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 }
Glenn L McGrath193697d2002-08-24 20:11:38 +0000102 }
Glenn L McGrath822e7fd2002-11-24 22:48:20 +0000103
Manuel Novoa III cad53642003-03-19 09:13:01 +0000104 if (!c) {
Denis Vlasenko11152e32008-08-15 19:18:35 +0000105 /* Done. If necessary, update perms on the newly
Manuel Novoa III cad53642003-03-19 09:13:01 +0000106 * created directory. Failure to update here _is_
Denis Vlasenko11152e32008-08-15 19:18:35 +0000107 * an error. */
108 if ((mode != -1) && (chmod(path, mode) < 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000109 fail_msg = "set permissions of";
110 break;
111 }
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100112 goto ret0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000113 }
114
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100115 /* Remove any inserted nul from the path (recursive mode) */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000116 *s = c;
Denis Vlasenko11152e32008-08-15 19:18:35 +0000117 } /* while (1) */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000118
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100119 bb_perror_msg("can't %s directory '%s'", fail_msg, path);
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100120 flags = -1;
121 goto ret;
122 ret0:
123 flags = 0;
124 ret:
125 //bb_error_msg("2 org_mask:%o", org_mask);
126 if (org_mask != cur_mask)
127 umask(org_mask);
128 return flags;
Matt Kraaiceeff732001-06-21 19:41:37 +0000129}