VirtualBox

source: kBuild/trunk/src/kmk/kmkbuiltin/mkdir.c@ 3432

Last change on this file since 3432 was 3389, checked in by bird, 5 years ago

kmk: Avoid setting umask just to get it, store the current value in a global variable (g_fUMask). The umask(0777) call in cp.c raced other code (kmk_append) that created files and directories, leaving us with read-only files sometimes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1/*
2 * Copyright (c) 1983, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if 0
31#ifndef lint
32static char const copyright[] =
33"@(#) Copyright (c) 1983, 1992, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
35#endif /* not lint */
36
37#ifndef lint
38static char sccsid[] = "@(#)mkdir.c 8.2 (Berkeley) 1/25/94";
39#endif /* not lint */
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: src/bin/mkdir/mkdir.c,v 1.28 2004/04/06 20:06:48 markm Exp $");
42#endif
43
44#define FAKES_NO_GETOPT_H /* bird */
45#include "config.h"
46#include <sys/types.h>
47#include <sys/stat.h>
48
49#include "err.h"
50#include <errno.h>
51#include <assert.h>
52#ifndef _MSC_VER
53# include <libgen.h>
54#endif
55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
58#ifndef __HAIKU__
59# include <sysexits.h>
60#endif
61#include <unistd.h>
62#ifdef HAVE_ALLOCA_H
63# include <alloca.h>
64#endif
65#include "getopt_r.h"
66#ifdef __HAIKU__
67# include "haikufakes.h"
68#endif
69#ifdef _MSC_VER
70# include <malloc.h>
71# include "mscfakes.h"
72#endif
73#include "kmkbuiltin.h"
74
75
76static struct option long_options[] =
77{
78 { "help", no_argument, 0, 261 },
79 { "version", no_argument, 0, 262 },
80 { 0, 0, 0, 0 },
81};
82
83extern mode_t g_fUMask;
84
85extern void * bsd_setmode(const char *p);
86extern mode_t bsd_getmode(const void *bbox, mode_t omode);
87
88static int build(PKMKBUILTINCTX pCtx, char *, mode_t, int);
89static int usage(PKMKBUILTINCTX pCtx, int fIsErr);
90
91
92int
93kmk_builtin_mkdir(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
94{
95 struct getopt_state_r gos;
96 int ch, exitval, success, pflag, vflag;
97 mode_t omode, *set = (mode_t *)NULL;
98 char *mode;
99
100 omode = pflag = vflag = 0;
101 mode = NULL;
102
103 getopt_initialize_r(&gos, argc, argv, "m:pv", long_options, envp, pCtx);
104 while ((ch = getopt_long_r(&gos, NULL)) != -1)
105 switch(ch) {
106 case 'm':
107 mode = gos.optarg;
108 break;
109 case 'p':
110 pflag = 1;
111 break;
112 case 'v':
113 vflag = 1;
114 break;
115 case 261:
116 usage(pCtx, 0);
117 return 0;
118 case 262:
119 return kbuild_version(argv[0]);
120 case '?':
121 default:
122 return usage(pCtx, 1);
123 }
124
125 argc -= gos.optind;
126 argv += gos.optind;
127 if (argv[0] == NULL)
128 return usage(pCtx, 1);
129
130 if (mode == NULL) {
131 omode = S_IRWXU | S_IRWXG | S_IRWXO;
132 } else {
133 if ((set = bsd_setmode(mode)) == NULL)
134 return errx(pCtx, 1, "invalid file mode: %s", mode);
135 omode = bsd_getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
136 free(set);
137 }
138
139 for (exitval = 0; *argv != NULL; ++argv) {
140 success = 1;
141 if (pflag) {
142 if (build(pCtx, *argv, omode, vflag))
143 success = 0;
144 } else if (mkdir(*argv, omode) < 0) {
145 if (errno == ENOTDIR || errno == ENOENT)
146 warn(pCtx, "mkdir: %s", dirname(*argv));
147 else
148 warn(pCtx, "mkdir: %s", *argv);
149 success = 0;
150 } else if (vflag)
151 kmk_builtin_ctx_printf(pCtx, 0, "%s\n", *argv);
152
153 if (!success)
154 exitval = 1;
155 /*
156 * The mkdir() and umask() calls both honor only the low
157 * nine bits, so if you try to set a mode including the
158 * sticky, setuid, setgid bits you lose them. Don't do
159 * this unless the user has specifically requested a mode,
160 * as chmod will (obviously) ignore the umask.
161 */
162 if (success && mode != NULL && chmod(*argv, omode) == -1) {
163 warn(pCtx, "chmod: %s", *argv);
164 exitval = 1;
165 }
166 }
167 return exitval;
168}
169
170#ifdef KMK_BUILTIN_STANDALONE
171mode_t g_fUMask;
172int main(int argc, char **argv, char **envp)
173{
174 KMKBUILTINCTX Ctx = { "kmk_mkdir", NULL };
175 umask(g_fUMask = umask(0077));
176 return kmk_builtin_mkdir(argc, argv, envp, &Ctx);
177}
178#endif
179
180static int
181build(PKMKBUILTINCTX pCtx, char *path, mode_t omode, int vflag)
182{
183 struct stat sb;
184 mode_t numask, oumask;
185 int first, last, retval;
186 char *p;
187
188 const size_t len = strlen(path);
189 p = alloca(len + 1);
190 path = memcpy(p, path, len + 1);
191
192#if defined(_MSC_VER) || defined(__EMX__)
193 /* correct slashes. */
194 p = strchr(path, '\\');
195 while (p) {
196 *p++ = '/';
197 p = strchr(p, '\\');
198 }
199#endif
200
201 p = path;
202 oumask = 0;
203 retval = 0;
204#if defined(_MSC_VER) || defined(__EMX__)
205 if ( ( (p[0] >= 'A' && p[0] <= 'Z')
206 || (p[0] >= 'a' && p[0] <= 'z'))
207 && p[1] == ':')
208 p += 2; /* skip the drive letter */
209 else if ( p[0] == '/'
210 && p[1] == '/'
211 && p[2] != '/')
212 {
213 /* UNC */
214 p += 2;
215 while (*p && *p != '/') /* server */
216 p++;
217 while (*p == '/')
218 p++;
219 while (*p && *p != '/') /* share */
220 p++;
221 }
222#endif
223 if (p[0] == '/') /* Skip leading '/'. */
224 ++p;
225 for (first = 1, last = 0; !last ; ++p) {
226 if (p[0] == '\0')
227 last = 1;
228 else if (p[0] != '/')
229 continue;
230 *p = '\0';
231 if (!last && p[1] == '\0')
232 last = 1;
233 if (first) {
234 /*
235 * POSIX 1003.2:
236 * For each dir operand that does not name an existing
237 * directory, effects equivalent to those cased by the
238 * following command shall occcur:
239 *
240 * mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
241 * mkdir [-m mode] dir
242 *
243 * We change the user's umask and then restore it,
244 * instead of doing chmod's.
245 */
246#ifdef KMK_BUILTIN_STANDALONE
247 oumask = umask(0077);
248#else
249 oumask = g_fUMask;
250 assert(oumask == umask(g_fUMask));
251#endif
252 numask = oumask & ~(S_IWUSR | S_IXUSR);
253 if (numask != oumask)
254 (void)umask(numask);
255 first = 0;
256 }
257 if (last && oumask != numask)
258 (void)umask(oumask);
259 if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
260 if (errno == EEXIST || errno == EISDIR
261 || errno == ENOSYS /* (solaris crap) */
262 || errno == EACCES /* (ditto) */) {
263 if (stat(path, &sb) < 0) {
264 warn(pCtx, "stat: %s", path);
265 retval = 1;
266 break;
267 }
268 if (!S_ISDIR(sb.st_mode)) {
269 if (last)
270 errno = EEXIST;
271 else
272 errno = ENOTDIR;
273 warn(pCtx, "st_mode: %s", path);
274 retval = 1;
275 break;
276 }
277 } else {
278 warn(pCtx, "mkdir: %s", path);
279 retval = 1;
280 break;
281 }
282 } else if (vflag)
283 kmk_builtin_ctx_printf(pCtx, 0, "%s\n", path);
284 if (!last)
285 *p = '/';
286 }
287 if (!first && !last && oumask != numask)
288 (void)umask(oumask);
289 return (retval);
290}
291
292static int
293usage(PKMKBUILTINCTX pCtx, int fIsErr)
294{
295 kmk_builtin_ctx_printf(pCtx, fIsErr,
296 "usage: %s [-pv] [-m mode] directory ...\n"
297 " or: %s --help\n"
298 " or: %s --version\n",
299 pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName);
300 return EX_USAGE;
301}
302
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette