source: trunk/src/kmk/kmkbuiltin/mkdir.c

Last change on this file 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
RevLine 
[226]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 */
[370]40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: src/bin/mkdir/mkdir.c,v 1.28 2004/04/06 20:06:48 markm Exp $");
[226]42#endif
43
[3215]44#define FAKES_NO_GETOPT_H /* bird */
[2113]45#include "config.h"
[226]46#include <sys/types.h>
47#include <sys/stat.h>
48
[370]49#include "err.h"
[226]50#include <errno.h>
[3389]51#include <assert.h>
[361]52#ifndef _MSC_VER
[809]53# include <libgen.h>
[375]54#endif
[226]55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
[2546]58#ifndef __HAIKU__
59# include <sysexits.h>
60#endif
[226]61#include <unistd.h>
[2121]62#ifdef HAVE_ALLOCA_H
63# include <alloca.h>
64#endif
[3215]65#include "getopt_r.h"
[2546]66#ifdef __HAIKU__
67# include "haikufakes.h"
68#endif
[809]69#ifdef _MSC_VER
70# include <malloc.h>
71# include "mscfakes.h"
72#endif
[1183]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
[3389]83extern mode_t g_fUMask;
[1183]84
[1710]85extern void * bsd_setmode(const char *p);
86extern mode_t bsd_getmode(const void *bbox, mode_t omode);
[227]87
[3192]88static int build(PKMKBUILTINCTX pCtx, char *, mode_t, int);
89static int usage(PKMKBUILTINCTX pCtx, int fIsErr);
[226]90
91
92int
[3192]93kmk_builtin_mkdir(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
[226]94{
[3215]95 struct getopt_state_r gos;
[3192]96 int ch, exitval, success, pflag, vflag;
[226]97 mode_t omode, *set = (mode_t *)NULL;
98 char *mode;
99
[3192]100 omode = pflag = vflag = 0;
[226]101 mode = NULL;
[375]102
[3215]103 getopt_initialize_r(&gos, argc, argv, "m:pv", long_options, envp, pCtx);
104 while ((ch = getopt_long_r(&gos, NULL)) != -1)
[226]105 switch(ch) {
106 case 'm':
[3215]107 mode = gos.optarg;
[226]108 break;
109 case 'p':
110 pflag = 1;
111 break;
112 case 'v':
113 vflag = 1;
114 break;
[1183]115 case 261:
[3192]116 usage(pCtx, 0);
[1183]117 return 0;
118 case 262:
119 return kbuild_version(argv[0]);
[226]120 case '?':
[3192]121 default:
122 return usage(pCtx, 1);
[226]123 }
124
[3215]125 argc -= gos.optind;
126 argv += gos.optind;
[226]127 if (argv[0] == NULL)
[3192]128 return usage(pCtx, 1);
[226]129
130 if (mode == NULL) {
131 omode = S_IRWXU | S_IRWXG | S_IRWXO;
132 } else {
[1710]133 if ((set = bsd_setmode(mode)) == NULL)
[3192]134 return errx(pCtx, 1, "invalid file mode: %s", mode);
[1710]135 omode = bsd_getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
[226]136 free(set);
137 }
138
139 for (exitval = 0; *argv != NULL; ++argv) {
140 success = 1;
141 if (pflag) {
[3192]142 if (build(pCtx, *argv, omode, vflag))
[226]143 success = 0;
144 } else if (mkdir(*argv, omode) < 0) {
145 if (errno == ENOTDIR || errno == ENOENT)
[3192]146 warn(pCtx, "mkdir: %s", dirname(*argv));
[226]147 else
[3192]148 warn(pCtx, "mkdir: %s", *argv);
[226]149 success = 0;
150 } else if (vflag)
[3192]151 kmk_builtin_ctx_printf(pCtx, 0, "%s\n", *argv);
[809]152
[226]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) {
[3192]163 warn(pCtx, "chmod: %s", *argv);
[226]164 exitval = 1;
165 }
166 }
[227]167 return exitval;
[226]168}
169
[3192]170#ifdef KMK_BUILTIN_STANDALONE
[3389]171mode_t g_fUMask;
[3192]172int main(int argc, char **argv, char **envp)
173{
174 KMKBUILTINCTX Ctx = { "kmk_mkdir", NULL };
[3389]175 umask(g_fUMask = umask(0077));
[3192]176 return kmk_builtin_mkdir(argc, argv, envp, &Ctx);
177}
178#endif
179
[228]180static int
[3192]181build(PKMKBUILTINCTX pCtx, char *path, mode_t omode, int vflag)
[226]182{
183 struct stat sb;
184 mode_t numask, oumask;
185 int first, last, retval;
186 char *p;
187
[601]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__)
[1556]193 /* correct slashes. */
[601]194 p = strchr(path, '\\');
195 while (p) {
196 *p++ = '/';
197 p = strchr(p, '\\');
198 }
[809]199#endif
[601]200
[226]201 p = path;
202 oumask = 0;
203 retval = 0;
[363]204#if defined(_MSC_VER) || defined(__EMX__)
[601]205 if ( ( (p[0] >= 'A' && p[0] <= 'Z')
206 || (p[0] >= 'a' && p[0] <= 'z'))
207 && p[1] == ':')
[1556]208 p += 2; /* skip the drive letter */
[601]209 else if ( p[0] == '/'
210 && p[1] == '/'
211 && p[2] != '/')
212 {
[1556]213 /* UNC */
[601]214 p += 2;
[1556]215 while (*p && *p != '/') /* server */
216 p++;
217 while (*p == '/')
218 p++;
219 while (*p && *p != '/') /* share */
220 p++;
[601]221 }
[375]222#endif
[226]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';
[370]231 if (!last && p[1] == '\0')
[226]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 */
[3389]246#ifdef KMK_BUILTIN_STANDALONE
247 oumask = umask(0077);
248#else
249 oumask = g_fUMask;
250 assert(oumask == umask(g_fUMask));
251#endif
[226]252 numask = oumask & ~(S_IWUSR | S_IXUSR);
[3389]253 if (numask != oumask)
254 (void)umask(numask);
[226]255 first = 0;
256 }
[3389]257 if (last && oumask != numask)
[226]258 (void)umask(oumask);
259 if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
[2465]260 if (errno == EEXIST || errno == EISDIR
261 || errno == ENOSYS /* (solaris crap) */
262 || errno == EACCES /* (ditto) */) {
[226]263 if (stat(path, &sb) < 0) {
[3192]264 warn(pCtx, "stat: %s", path);
[226]265 retval = 1;
266 break;
[3389]267 }
268 if (!S_ISDIR(sb.st_mode)) {
[226]269 if (last)
270 errno = EEXIST;
271 else
272 errno = ENOTDIR;
[3192]273 warn(pCtx, "st_mode: %s", path);
[226]274 retval = 1;
275 break;
276 }
277 } else {
[3192]278 warn(pCtx, "mkdir: %s", path);
[226]279 retval = 1;
280 break;
281 }
282 } else if (vflag)
[3192]283 kmk_builtin_ctx_printf(pCtx, 0, "%s\n", path);
[226]284 if (!last)
285 *p = '/';
286 }
[3389]287 if (!first && !last && oumask != numask)
[226]288 (void)umask(oumask);
289 return (retval);
290}
291
[228]292static int
[3192]293usage(PKMKBUILTINCTX pCtx, int fIsErr)
[226]294{
[3192]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);
[227]300 return EX_USAGE;
[226]301}
[3192]302
Note: See TracBrowser for help on using the repository browser.