blob: d07ccb93c0f524cac8d5373d4b21bcd06557c4f3 [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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
Manuel Novoa III cad53642003-03-19 09:13:01 +000023/* Mar 5, 2003 Manuel Novoa III
24 *
25 * This is the main work function for the 'mkdir' applet. As such, it
26 * strives to be SUSv3 compliant in it's behaviour when recursively
27 * making missing parent dirs, and in it's mode setting of the final
28 * directory 'path'.
29 *
30 * To recursively build all missing intermediate directories, make
31 * sure that (flags & FILEUTILS_RECUR) is non-zero. Newly created
32 * intermediate directories will have at least u+wx perms.
33 *
Eric Andersenaff114c2004-04-14 17:51:38 +000034 * To set specific permissions on 'path', pass the appropriate 'mode'
35 * val. Otherwise, pass -1 to get default permissions.
Manuel Novoa III cad53642003-03-19 09:13:01 +000036 */
Matt Kraaiceeff732001-06-21 19:41:37 +000037
Manuel Novoa III cad53642003-03-19 09:13:01 +000038#include <errno.h>
39#include <unistd.h>
Eric Andersen28426592004-10-08 07:21:58 +000040#include <sys/stat.h>
Matt Kraaiceeff732001-06-21 19:41:37 +000041#include "libbb.h"
42
Manuel Novoa III cad53642003-03-19 09:13:01 +000043int bb_make_directory (char *path, long mode, int flags)
Matt Kraaiceeff732001-06-21 19:41:37 +000044{
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 mode_t mask;
46 const char *fail_msg;
47 char *s = path;
48 char c;
Eric Andersen28426592004-10-08 07:21:58 +000049 struct stat st;
Glenn L McGrath822e7fd2002-11-24 22:48:20 +000050
Manuel Novoa III cad53642003-03-19 09:13:01 +000051 mask = umask(0);
52 umask(mask & ~0300);
Glenn L McGrath822e7fd2002-11-24 22:48:20 +000053
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 do {
55 c = 0;
Glenn L McGrath5b110872002-11-24 23:22:29 +000056
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 if (flags & FILEUTILS_RECUR) { /* Get the parent. */
58 /* Bypass leading non-'/'s and then subsequent '/'s. */
59 while (*s) {
60 if (*s == '/') {
61 do {
62 ++s;
63 } while (*s == '/');
64 c = *s; /* Save the current char */
65 *s = 0; /* and replace it with nul. */
66 break;
67 }
68 ++s;
69 }
Glenn L McGrath5b110872002-11-24 23:22:29 +000070 }
71
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 if (mkdir(path, 0777) < 0) {
73 /* If we failed for any other reason than the directory
74 * already exists, output a diagnostic and return -1.*/
Eric Andersen28426592004-10-08 07:21:58 +000075 if (errno != EEXIST
76 || !(flags & FILEUTILS_RECUR)
77 || (stat(path, &st) < 0 || !S_ISDIR(st.st_mode))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000078 fail_msg = "create";
79 umask(mask);
80 break;
81 }
82 /* Since the directory exists, don't attempt to change
83 * permissions if it was the full target. Note that
84 * this is not an error conditon. */
85 if (!c) {
86 umask(mask);
87 return 0;
88 }
Glenn L McGrath193697d2002-08-24 20:11:38 +000089 }
Glenn L McGrath822e7fd2002-11-24 22:48:20 +000090
Manuel Novoa III cad53642003-03-19 09:13:01 +000091 if (!c) {
92 /* Done. If necessary, updated perms on the newly
93 * created directory. Failure to update here _is_
94 * an error.*/
95 umask(mask);
96 if ((mode != -1) && (chmod(path, mode) < 0)){
97 fail_msg = "set permissions of";
98 break;
99 }
100 return 0;
101 }
102
103 /* Remove any inserted nul from the path (recursive mode). */
104 *s = c;
105
106 } while (1);
107
108 bb_perror_msg ("Cannot %s directory `%s'", fail_msg, path);
109 return -1;
Matt Kraaiceeff732001-06-21 19:41:37 +0000110}