Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 3 | * parse_mode implementation for busybox |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 4 | * |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 5 | * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org> |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 8 | */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 9 | /* Mar 5, 2003 Manuel Novoa III |
| 10 | * |
| 11 | * This is the main work function for the 'mkdir' applet. As such, it |
| 12 | * strives to be SUSv3 compliant in it's behaviour when recursively |
| 13 | * making missing parent dirs, and in it's mode setting of the final |
| 14 | * directory 'path'. |
| 15 | * |
| 16 | * To recursively build all missing intermediate directories, make |
| 17 | * sure that (flags & FILEUTILS_RECUR) is non-zero. Newly created |
| 18 | * intermediate directories will have at least u+wx perms. |
| 19 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 20 | * To set specific permissions on 'path', pass the appropriate 'mode' |
| 21 | * val. Otherwise, pass -1 to get default permissions. |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 22 | */ |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 23 | #include "libbb.h" |
| 24 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 25 | /* This function is used from NOFORK applets. It must not allocate anything */ |
| 26 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 27 | int FAST_FUNC bb_make_directory(char *path, long mode, int flags) |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 28 | { |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 29 | mode_t cur_mask; |
| 30 | mode_t org_mask; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 31 | const char *fail_msg; |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 32 | char *s; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 33 | char c; |
Eric Andersen | 2842659 | 2004-10-08 07:21:58 +0000 | [diff] [blame] | 34 | struct stat st; |
Glenn L McGrath | 822e7fd | 2002-11-24 22:48:20 +0000 | [diff] [blame] | 35 | |
Denys Vlasenko | 4bd0c2a | 2016-12-04 10:42:07 +0100 | [diff] [blame] | 36 | /* "path" can be a result of dirname(). |
| 37 | * dirname("no_slashes") returns ".", possibly read-only. |
| 38 | * musl dirname() can return read-only "/" too. |
| 39 | * We need writable string. And for "/", "." (and ".."?) |
| 40 | * nothing needs to be created anyway. |
| 41 | */ |
| 42 | if (LONE_CHAR(path, '/')) |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 43 | return 0; |
Denys Vlasenko | 4bd0c2a | 2016-12-04 10:42:07 +0100 | [diff] [blame] | 44 | if (path[0] == '.') { |
| 45 | if (path[1] == '\0') |
| 46 | return 0; /* "." */ |
| 47 | // if (path[1] == '.' && path[2] == '\0') |
| 48 | // return 0; /* ".." */ |
| 49 | } |
Glenn L McGrath | 822e7fd | 2002-11-24 22:48:20 +0000 | [diff] [blame] | 50 | |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 51 | org_mask = cur_mask = (mode_t)-1L; |
| 52 | s = path; |
Denis Vlasenko | 11152e3 | 2008-08-15 19:18:35 +0000 | [diff] [blame] | 53 | while (1) { |
| 54 | c = '\0'; |
Glenn L McGrath | 5b11087 | 2002-11-24 23:22:29 +0000 | [diff] [blame] | 55 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 56 | if (flags & FILEUTILS_RECUR) { /* Get the parent */ |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 57 | /* Bypass leading non-'/'s and then subsequent '/'s */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 58 | while (*s) { |
| 59 | if (*s == '/') { |
| 60 | do { |
| 61 | ++s; |
| 62 | } while (*s == '/'); |
Denis Vlasenko | 11152e3 | 2008-08-15 19:18:35 +0000 | [diff] [blame] | 63 | c = *s; /* Save the current char */ |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 64 | *s = '\0'; /* and replace it with nul */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 65 | break; |
| 66 | } |
| 67 | ++s; |
| 68 | } |
Glenn L McGrath | 5b11087 | 2002-11-24 23:22:29 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 71 | if (c != '\0') { |
| 72 | /* Intermediate dirs: must have wx for user */ |
| 73 | if (cur_mask == (mode_t)-1L) { /* wasn't done yet? */ |
| 74 | mode_t new_mask; |
| 75 | org_mask = umask(0); |
| 76 | cur_mask = 0; |
| 77 | /* Clear u=wx in umask - this ensures |
| 78 | * they won't be cleared on mkdir */ |
| 79 | new_mask = (org_mask & ~(mode_t)0300); |
| 80 | //bb_error_msg("org_mask:%o cur_mask:%o", org_mask, new_mask); |
| 81 | if (new_mask != cur_mask) { |
| 82 | cur_mask = new_mask; |
| 83 | umask(new_mask); |
| 84 | } |
| 85 | } |
| 86 | } else { |
| 87 | /* Last component: uses original umask */ |
| 88 | //bb_error_msg("1 org_mask:%o", org_mask); |
| 89 | if (org_mask != cur_mask) { |
| 90 | cur_mask = org_mask; |
| 91 | umask(org_mask); |
| 92 | } |
| 93 | } |
Denis Vlasenko | 11152e3 | 2008-08-15 19:18:35 +0000 | [diff] [blame] | 94 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 95 | if (mkdir(path, 0777) < 0) { |
| 96 | /* If we failed for any other reason than the directory |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 97 | * already exists, output a diagnostic and return -1 */ |
Jeremie Koenig | 84b01d5 | 2010-05-27 15:46:33 +0200 | [diff] [blame] | 98 | if ((errno != EEXIST && errno != EISDIR) |
Denis Vlasenko | 11152e3 | 2008-08-15 19:18:35 +0000 | [diff] [blame] | 99 | || !(flags & FILEUTILS_RECUR) |
| 100 | || ((stat(path, &st) < 0) || !S_ISDIR(st.st_mode)) |
| 101 | ) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 102 | fail_msg = "create"; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 103 | break; |
| 104 | } |
| 105 | /* Since the directory exists, don't attempt to change |
| 106 | * permissions if it was the full target. Note that |
Denis Vlasenko | 582dff0 | 2008-10-19 19:36:30 +0000 | [diff] [blame] | 107 | * this is not an error condition. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 108 | if (!c) { |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 109 | goto ret0; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 110 | } |
Denys Vlasenko | 17f8418 | 2014-05-19 16:23:50 +0200 | [diff] [blame] | 111 | } else { |
| 112 | if (flags & FILEUTILS_VERBOSE) { |
| 113 | printf("created directory: '%s'\n", path); |
| 114 | } |
Glenn L McGrath | 193697d | 2002-08-24 20:11:38 +0000 | [diff] [blame] | 115 | } |
Glenn L McGrath | 822e7fd | 2002-11-24 22:48:20 +0000 | [diff] [blame] | 116 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 117 | if (!c) { |
Denis Vlasenko | 11152e3 | 2008-08-15 19:18:35 +0000 | [diff] [blame] | 118 | /* Done. If necessary, update perms on the newly |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 119 | * created directory. Failure to update here _is_ |
Denis Vlasenko | 11152e3 | 2008-08-15 19:18:35 +0000 | [diff] [blame] | 120 | * an error. */ |
| 121 | if ((mode != -1) && (chmod(path, mode) < 0)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 122 | fail_msg = "set permissions of"; |
Natanael Copa | 02112d8 | 2012-05-22 17:11:46 +0200 | [diff] [blame] | 123 | if (flags & FILEUTILS_IGNORE_CHMOD_ERR) { |
| 124 | flags = 0; |
| 125 | goto print_err; |
| 126 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 127 | break; |
| 128 | } |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 129 | goto ret0; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 132 | /* Remove any inserted nul from the path (recursive mode) */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 133 | *s = c; |
Denis Vlasenko | 11152e3 | 2008-08-15 19:18:35 +0000 | [diff] [blame] | 134 | } /* while (1) */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 135 | |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 136 | flags = -1; |
Natanael Copa | 02112d8 | 2012-05-22 17:11:46 +0200 | [diff] [blame] | 137 | print_err: |
| 138 | bb_perror_msg("can't %s directory '%s'", fail_msg, path); |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 139 | goto ret; |
| 140 | ret0: |
| 141 | flags = 0; |
| 142 | ret: |
| 143 | //bb_error_msg("2 org_mask:%o", org_mask); |
| 144 | if (org_mask != cur_mask) |
| 145 | umask(org_mask); |
| 146 | return flags; |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 147 | } |