1 /* makepath.c -- Ensure that a directory path exists.
2 Copyright (C) 1990 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> and
19 Jim Meyering <meyering@cs.utexas.edu>. */
22 #if defined (CONFIG_BROKETS)
23 /* We use <config.h> instead of "config.h" so that a compilation
24 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
25 (which it would do because it found this file in $srcdir). */
33 #define alloca __builtin_alloca
47 #include <sys/types.h>
53 #ifdef STAT_MACROS_BROKEN
57 #endif /* STAT_MACROS_BROKEN. */
59 #if !defined(S_ISDIR) && defined(S_IFDIR)
60 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
75 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
89 #include "safe-stat.h"
92 /* Ensure that the directory ARGPATH exists.
93 Remove any trailing slashes from ARGPATH before calling this function.
95 Make any leading directories that don't already exist, with
96 permissions PARENT_MODE.
97 If the last element of ARGPATH does not exist, create it as
98 a new directory with permissions MODE.
99 If OWNER and GROUP are non-negative, make them the UID and GID of
101 If VERBOSE_FMT_STRING is nonzero, use it as a printf format
102 string for printing a message after successfully making a directory,
103 with the name of the directory that was just made as an argument.
105 Return 0 if ARGPATH exists as a directory with the proper
106 ownership and permissions when done, otherwise 1. */
109 make_path (argpath, mode, parent_mode, owner, group, verbose_fmt_string)
115 char *verbose_fmt_string;
117 char *dirpath; /* A copy we can scribble NULs on. */
120 int oldmask = umask (0);
122 dirpath = (char *) alloca (strlen (argpath) + 1);
123 strcpy (dirpath, argpath);
125 if (SAFE_STAT (dirpath, &stats))
128 int tmp_mode; /* Initial perms for leading dirs. */
129 int re_protect; /* Should leading dirs be unwritable? */
133 struct ptr_list *next;
135 struct ptr_list *p, *leading_dirs = NULL;
137 /* If leading directories shouldn't be writable or executable,
138 or should have set[ug]id or sticky bits set and we are setting
139 their owners, we need to fix their permissions after making them. */
140 if (((parent_mode & 0300) != 0300)
141 || (owner != (uid_t) -1 && group != (gid_t) -1
142 && (parent_mode & 07000) != 0))
149 tmp_mode = parent_mode;
154 while (*slash == '/')
156 while ((slash = index (slash, '/')))
159 if (SAFE_STAT (dirpath, &stats))
161 if (mkdir (dirpath, tmp_mode))
163 error (0, errno, "cannot make directory `%s'", dirpath);
169 if (verbose_fmt_string != NULL)
170 error (0, 0, verbose_fmt_string, dirpath);
172 if (owner != (uid_t) -1 && group != (gid_t) -1
173 && chown (dirpath, owner, group)
174 #if defined(AFS) && defined (EPERM)
179 error (0, errno, "%s", dirpath);
184 struct ptr_list *new = (struct ptr_list *)
185 alloca (sizeof (struct ptr_list));
186 new->dirname_end = slash;
187 new->next = leading_dirs;
192 else if (!S_ISDIR (stats.st_mode))
194 error (0, 0, "`%s' exists but is not a directory", dirpath);
201 /* Avoid unnecessary calls to `stat' when given
202 pathnames containing multiple adjacent slashes. */
203 while (*slash == '/')
207 /* We're done making leading directories.
208 Make the final component of the path. */
210 /* The path could end in "/." or contain "/..", so test
211 if we really have to create the directory. */
213 if (SAFE_STAT (dirpath, &stats) && mkdir (dirpath, mode))
215 error (0, errno, "cannot make directory `%s'", dirpath);
219 if (verbose_fmt_string != NULL)
220 error (0, 0, verbose_fmt_string, dirpath);
222 if (owner != (uid_t) -1 && group != (gid_t) -1)
224 if (chown (dirpath, owner, group)
230 error (0, errno, "%s", dirpath);
233 /* chown may have turned off some permission bits we wanted. */
234 if ((mode & 07000) != 0 && chmod (dirpath, mode))
236 error (0, errno, "%s", dirpath);
241 /* If the mode for leading directories didn't include owner "wx"
242 privileges, we have to reset their protections to the correct
244 for (p = leading_dirs; p != NULL; p = p->next)
246 *(p->dirname_end) = '\0';
247 if (chmod (dirpath, parent_mode))
249 error (0, errno, "%s", dirpath);
256 /* We get here if the entire path already exists. */
258 if (!S_ISDIR (stats.st_mode))
260 error (0, 0, "`%s' exists but is not a directory", dirpath);
265 /* chown must precede chmod because on some systems,
266 chown clears the set[ug]id bits for non-superusers,
267 resulting in incorrect permissions.
268 On System V, users can give away files with chown and then not
269 be able to chmod them. So don't give files away. */
271 if (owner != (uid_t) -1 && group != (gid_t) -1
272 && chown (dirpath, owner, group)
278 error (0, errno, "%s", dirpath);
281 if (chmod (dirpath, mode))
283 error (0, errno, "%s", dirpath);