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 | * |
"Robert P. J. Day" | 5d8843e | 2006-07-10 11:41:19 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 10 | /* 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 Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 21 | * To set specific permissions on 'path', pass the appropriate 'mode' |
| 22 | * val. Otherwise, pass -1 to get default permissions. |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 23 | */ |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 24 | |
| 25 | #include "libbb.h" |
| 26 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 27 | /* This function is used from NOFORK applets. It must not allocate anything */ |
| 28 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 29 | int FAST_FUNC bb_make_directory(char *path, long mode, int flags) |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 30 | { |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 31 | mode_t cur_mask; |
| 32 | mode_t org_mask; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 33 | const char *fail_msg; |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 34 | char *s; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 35 | char c; |
Eric Andersen | 2842659 | 2004-10-08 07:21:58 +0000 | [diff] [blame] | 36 | struct stat st; |
Glenn L McGrath | 822e7fd | 2002-11-24 22:48:20 +0000 | [diff] [blame] | 37 | |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 38 | /* Happens on bb_make_directory(dirname("no_slashes"),...) */ |
| 39 | if (LONE_CHAR(path, '.')) |
| 40 | return 0; |
Glenn L McGrath | 822e7fd | 2002-11-24 22:48:20 +0000 | [diff] [blame] | 41 | |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 42 | org_mask = cur_mask = (mode_t)-1L; |
| 43 | s = path; |
Denis Vlasenko | 11152e3 | 2008-08-15 19:18:35 +0000 | [diff] [blame] | 44 | while (1) { |
| 45 | c = '\0'; |
Glenn L McGrath | 5b11087 | 2002-11-24 23:22:29 +0000 | [diff] [blame] | 46 | |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 47 | if (flags & FILEUTILS_RECUR) { /* Get the parent */ |
| 48 | /* Bypass leading non-'/'s and then subsequent '/'s */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | while (*s) { |
| 50 | if (*s == '/') { |
| 51 | do { |
| 52 | ++s; |
| 53 | } while (*s == '/'); |
Denis Vlasenko | 11152e3 | 2008-08-15 19:18:35 +0000 | [diff] [blame] | 54 | c = *s; /* Save the current char */ |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 55 | *s = '\0'; /* and replace it with nul */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 56 | break; |
| 57 | } |
| 58 | ++s; |
| 59 | } |
Glenn L McGrath | 5b11087 | 2002-11-24 23:22:29 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 62 | 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 Vlasenko | 11152e3 | 2008-08-15 19:18:35 +0000 | [diff] [blame] | 85 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 86 | if (mkdir(path, 0777) < 0) { |
| 87 | /* If we failed for any other reason than the directory |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 88 | * already exists, output a diagnostic and return -1 */ |
Eric Andersen | 2842659 | 2004-10-08 07:21:58 +0000 | [diff] [blame] | 89 | if (errno != EEXIST |
Denis Vlasenko | 11152e3 | 2008-08-15 19:18:35 +0000 | [diff] [blame] | 90 | || !(flags & FILEUTILS_RECUR) |
| 91 | || ((stat(path, &st) < 0) || !S_ISDIR(st.st_mode)) |
| 92 | ) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 93 | fail_msg = "create"; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 94 | break; |
| 95 | } |
| 96 | /* Since the directory exists, don't attempt to change |
| 97 | * permissions if it was the full target. Note that |
Denis Vlasenko | 582dff0 | 2008-10-19 19:36:30 +0000 | [diff] [blame] | 98 | * this is not an error condition. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 99 | if (!c) { |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 100 | goto ret0; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 101 | } |
Glenn L McGrath | 193697d | 2002-08-24 20:11:38 +0000 | [diff] [blame] | 102 | } |
Glenn L McGrath | 822e7fd | 2002-11-24 22:48:20 +0000 | [diff] [blame] | 103 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 104 | if (!c) { |
Denis Vlasenko | 11152e3 | 2008-08-15 19:18:35 +0000 | [diff] [blame] | 105 | /* Done. If necessary, update perms on the newly |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 106 | * created directory. Failure to update here _is_ |
Denis Vlasenko | 11152e3 | 2008-08-15 19:18:35 +0000 | [diff] [blame] | 107 | * an error. */ |
| 108 | if ((mode != -1) && (chmod(path, mode) < 0)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 109 | fail_msg = "set permissions of"; |
| 110 | break; |
| 111 | } |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 112 | goto ret0; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 115 | /* Remove any inserted nul from the path (recursive mode) */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 116 | *s = c; |
Denis Vlasenko | 11152e3 | 2008-08-15 19:18:35 +0000 | [diff] [blame] | 117 | } /* while (1) */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 118 | |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 119 | bb_perror_msg("can't %s directory '%s'", fail_msg, path); |
Denys Vlasenko | f94c9bf | 2009-11-29 07:45:33 +0100 | [diff] [blame] | 120 | 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 Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 129 | } |