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)
91 /* Ensure that the directory ARGPATH exists.
92 Remove any trailing slashes from ARGPATH before calling this function.
94 Make any leading directories that don't already exist, with
95 permissions PARENT_MODE.
96 If the last element of ARGPATH does not exist, create it as
97 a new directory with permissions MODE.
98 If OWNER and GROUP are non-negative, make them the UID and GID of
100 If VERBOSE_FMT_STRING is nonzero, use it as a printf format
101 string for printing a message after successfully making a directory,
102 with the name of the directory that was just made as an argument.
104 Return 0 if ARGPATH exists as a directory with the proper
105 ownership and permissions when done, otherwise 1. */
108 make_path (argpath, mode, parent_mode, owner, group, verbose_fmt_string)
114 char *verbose_fmt_string;
116 char *dirpath; /* A copy we can scribble NULs on. */
119 int oldmask = umask (0);
121 dirpath = (char *) alloca (strlen (argpath) + 1);
122 strcpy (dirpath, argpath);
124 if (stat (dirpath, &stats))
127 int tmp_mode; /* Initial perms for leading dirs. */
128 int re_protect; /* Should leading dirs be unwritable? */
132 struct ptr_list *next;
134 struct ptr_list *p, *leading_dirs = NULL;
136 /* If leading directories shouldn't be writable or executable,
137 or should have set[ug]id or sticky bits set and we are setting
138 their owners, we need to fix their permissions after making them. */
139 if (((parent_mode & 0300) != 0300)
140 || (owner != (uid_t) -1 && group != (gid_t) -1
141 && (parent_mode & 07000) != 0))
148 tmp_mode = parent_mode;
153 while (*slash == '/')
155 while ((slash = index (slash, '/')))
158 if (stat (dirpath, &stats))
160 if (mkdir (dirpath, tmp_mode))
162 error (0, errno, "cannot make directory `%s'", dirpath);
168 if (verbose_fmt_string != NULL)
169 error (0, 0, verbose_fmt_string, dirpath);
171 if (owner != (uid_t) -1 && group != (gid_t) -1
172 && chown (dirpath, owner, group)
173 #if defined(AFS) && defined (EPERM)
178 error (0, errno, "%s", dirpath);
183 struct ptr_list *new = (struct ptr_list *)
184 alloca (sizeof (struct ptr_list));
185 new->dirname_end = slash;
186 new->next = leading_dirs;
191 else if (!S_ISDIR (stats.st_mode))
193 error (0, 0, "`%s' exists but is not a directory", dirpath);
200 /* Avoid unnecessary calls to `stat' when given
201 pathnames containing multiple adjacent slashes. */
202 while (*slash == '/')
206 /* We're done making leading directories.
207 Make the final component of the path. */
209 if (mkdir (dirpath, mode))
211 error (0, errno, "cannot make directory `%s'", dirpath);
215 if (verbose_fmt_string != NULL)
216 error (0, 0, verbose_fmt_string, dirpath);
218 if (owner != (uid_t) -1 && group != (gid_t) -1)
220 if (chown (dirpath, owner, group)
226 error (0, errno, "%s", dirpath);
229 /* chown may have turned off some permission bits we wanted. */
230 if ((mode & 07000) != 0 && chmod (dirpath, mode))
232 error (0, errno, "%s", dirpath);
237 /* If the mode for leading directories didn't include owner "wx"
238 privileges, we have to reset their protections to the correct
240 for (p = leading_dirs; p != NULL; p = p->next)
242 *(p->dirname_end) = '\0';
243 if (chmod (dirpath, parent_mode))
245 error (0, errno, "%s", dirpath);
252 /* We get here if the entire path already exists. */
254 if (!S_ISDIR (stats.st_mode))
256 error (0, 0, "`%s' exists but is not a directory", dirpath);
261 /* chown must precede chmod because on some systems,
262 chown clears the set[ug]id bits for non-superusers,
263 resulting in incorrect permissions.
264 On System V, users can give away files with chown and then not
265 be able to chmod them. So don't give files away. */
267 if (owner != (uid_t) -1 && group != (gid_t) -1
268 && chown (dirpath, owner, group)
274 error (0, errno, "%s", dirpath);
277 if (chmod (dirpath, mode))
279 error (0, errno, "%s", dirpath);