blob: df0b4a13d49562d0f7056f69e77dc6d1362f53fc [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 *
"Robert P. J. Day"5d8843e2006-07-10 11:41:19 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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{
Manuel Novoa III cad53642003-03-19 09:13:01 +000031 mode_t mask;
32 const char *fail_msg;
33 char *s = path;
34 char c;
Eric Andersen28426592004-10-08 07:21:58 +000035 struct stat st;
Glenn L McGrath822e7fd2002-11-24 22:48:20 +000036
Manuel Novoa III cad53642003-03-19 09:13:01 +000037 mask = umask(0);
Denis Vlasenko11152e32008-08-15 19:18:35 +000038 umask(mask & ~0300); /* Ensure intermediate dirs are wx */
Glenn L McGrath822e7fd2002-11-24 22:48:20 +000039
Denis Vlasenko11152e32008-08-15 19:18:35 +000040 while (1) {
41 c = '\0';
Glenn L McGrath5b110872002-11-24 23:22:29 +000042
Manuel Novoa III cad53642003-03-19 09:13:01 +000043 if (flags & FILEUTILS_RECUR) { /* Get the parent. */
44 /* Bypass leading non-'/'s and then subsequent '/'s. */
45 while (*s) {
46 if (*s == '/') {
47 do {
48 ++s;
49 } while (*s == '/');
Denis Vlasenko11152e32008-08-15 19:18:35 +000050 c = *s; /* Save the current char */
51 *s = '\0'; /* and replace it with nul. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000052 break;
53 }
54 ++s;
55 }
Glenn L McGrath5b110872002-11-24 23:22:29 +000056 }
57
Denis Vlasenko11152e32008-08-15 19:18:35 +000058 if (!c) /* Last component uses orig umask */
59 umask(mask);
60
Manuel Novoa III cad53642003-03-19 09:13:01 +000061 if (mkdir(path, 0777) < 0) {
62 /* If we failed for any other reason than the directory
Denis Vlasenko11152e32008-08-15 19:18:35 +000063 * already exists, output a diagnostic and return -1. */
Eric Andersen28426592004-10-08 07:21:58 +000064 if (errno != EEXIST
Denis Vlasenko11152e32008-08-15 19:18:35 +000065 || !(flags & FILEUTILS_RECUR)
66 || ((stat(path, &st) < 0) || !S_ISDIR(st.st_mode))
67 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000068 fail_msg = "create";
69 umask(mask);
70 break;
71 }
72 /* Since the directory exists, don't attempt to change
73 * permissions if it was the full target. Note that
74 * this is not an error conditon. */
75 if (!c) {
76 umask(mask);
77 return 0;
78 }
Glenn L McGrath193697d2002-08-24 20:11:38 +000079 }
Glenn L McGrath822e7fd2002-11-24 22:48:20 +000080
Manuel Novoa III cad53642003-03-19 09:13:01 +000081 if (!c) {
Denis Vlasenko11152e32008-08-15 19:18:35 +000082 /* Done. If necessary, update perms on the newly
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 * created directory. Failure to update here _is_
Denis Vlasenko11152e32008-08-15 19:18:35 +000084 * an error. */
85 if ((mode != -1) && (chmod(path, mode) < 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000086 fail_msg = "set permissions of";
87 break;
88 }
89 return 0;
90 }
91
92 /* Remove any inserted nul from the path (recursive mode). */
93 *s = c;
Denis Vlasenko11152e32008-08-15 19:18:35 +000094 } /* while (1) */
Manuel Novoa III cad53642003-03-19 09:13:01 +000095
Denis Vlasenko501bfe22007-08-09 08:10:13 +000096 bb_perror_msg("cannot %s directory '%s'", fail_msg, path);
Manuel Novoa III cad53642003-03-19 09:13:01 +000097 return -1;
Matt Kraaiceeff732001-06-21 19:41:37 +000098}