1 /* makepath.c -- Ensure that a directory path exists.
2 Copyright (C) 1990, 1997 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> and Jim Meyering. */
25 # define alloca __builtin_alloca
39 #include <sys/types.h>
45 #if STAT_MACROS_BROKEN
49 #if !defined(S_ISDIR) && defined(S_IFDIR)
50 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
82 #define WX_USR (S_IWUSR | S_IXUSR)
93 void strip_trailing_slashes ();
98 /* We're done operating on basename_dir. \
99 Restore working directory. */ \
102 int fail = restore_cwd (&cwd, NULL, NULL); \
118 /* Ensure that the directory ARGPATH exists.
119 Remove any trailing slashes from ARGPATH before calling this function.
121 Create any leading directories that don't already exist, with
122 permissions PARENT_MODE.
123 If the last element of ARGPATH does not exist, create it as
124 a new directory with permissions MODE.
125 If OWNER and GROUP are non-negative, use them to set the UID and GID of
126 any created directories.
127 If VERBOSE_FMT_STRING is nonzero, use it as a printf format
128 string for printing a message after successfully making a directory,
129 with the name of the directory that was just made as an argument.
130 If PRESERVE_EXISTING is non-zero and ARGPATH is an existing directory,
131 then do not attempt to set its permissions and ownership.
133 Return 0 if ARGPATH exists as a directory with the proper
134 ownership and permissions when done, otherwise 1. */
137 make_path (const char *argpath,
142 int preserve_existing,
143 const char *verbose_fmt_string)
148 if (stat (argpath, &stats))
151 int tmp_mode; /* Initial perms for leading dirs. */
152 int re_protect; /* Should leading dirs be unwritable? */
156 struct ptr_list *next;
158 struct ptr_list *p, *leading_dirs = NULL;
159 int do_chdir; /* Whether to chdir before each mkdir. */
160 struct saved_cwd cwd;
164 /* Temporarily relax umask in case it's overly restrictive. */
165 int oldmask = umask (0);
167 /* Make a copy of ARGPATH that we can scribble NULs on. */
168 dirpath = (char *) alloca (strlen (argpath) + 1);
169 strcpy (dirpath, argpath);
170 strip_trailing_slashes (dirpath);
172 /* If leading directories shouldn't be writable or executable,
173 or should have set[ug]id or sticky bits set and we are setting
174 their owners, we need to fix their permissions after making them. */
175 if (((parent_mode & WX_USR) != WX_USR)
176 || ((owner != (uid_t) -1 || group != (gid_t) -1)
177 && (parent_mode & 07000) != 0))
184 tmp_mode = parent_mode;
188 /* If we can record the current working directory, we may be able
189 to do the chdir optimization. */
190 do_chdir = !save_cwd (&cwd);
192 /* If we've saved the cwd and DIRPATH is an absolute pathname,
193 we must chdir to `/' in order to enable the chdir optimization.
194 So if chdir ("/") fails, turn off the optimization. */
195 if (do_chdir && *dirpath == '/' && chdir ("/") < 0)
200 /* Skip over leading slashes. */
201 while (*slash == '/')
206 int newly_created_dir = 1;
208 /* slash points to the leftmost unprocessed component of dirpath. */
209 basename_dir = slash;
211 slash = strchr (slash, '/');
215 /* If we're *not* doing chdir before each mkdir, then we have to refer
216 to the target using the full (multi-component) directory name. */
218 basename_dir = dirpath;
221 if (mkdir (basename_dir, tmp_mode))
223 if (stat (basename_dir, &stats))
225 error (0, errno, "cannot create directory `%s'", dirpath);
229 else if (!S_ISDIR (stats.st_mode))
231 error (0, 0, "`%s' exists but is not a directory", dirpath);
237 /* DIRPATH already exists and is a directory. */
238 newly_created_dir = 0;
242 if (newly_created_dir && verbose_fmt_string != NULL)
243 fprintf (stderr, verbose_fmt_string, dirpath);
245 if (newly_created_dir
246 && (owner != (uid_t) -1 || group != (gid_t) -1)
247 && chown (basename_dir, owner, group)
248 #if defined(AFS) && defined (EPERM)
253 error (0, errno, "%s", dirpath);
260 struct ptr_list *new = (struct ptr_list *)
261 alloca (sizeof (struct ptr_list));
262 new->dirname_end = slash;
263 new->next = leading_dirs;
267 /* If we were able to save the initial working directory,
268 then we can use chdir to change into each directory before
269 creating an entry in that directory. This avoids making
270 stat and mkdir process O(n^2) file name components. */
271 if (do_chdir && chdir (basename_dir) < 0)
273 error (0, errno, "cannot chdir to directory, %s", dirpath);
280 /* Avoid unnecessary calls to `stat' when given
281 pathnames containing multiple adjacent slashes. */
282 while (*slash == '/')
287 basename_dir = dirpath;
289 /* We're done making leading directories.
290 Create the final component of the path. */
292 /* The path could end in "/." or contain "/..", so test
293 if we really have to create the directory. */
295 if (stat (basename_dir, &stats) && mkdir (basename_dir, mode))
297 error (0, errno, "cannot create directory `%s'", dirpath);
302 /* Done creating directories. Restore original umask. */
305 if (verbose_fmt_string != NULL)
306 error (0, 0, verbose_fmt_string, dirpath);
308 if (owner != (uid_t) -1 && group != (gid_t) -1)
310 if (chown (basename_dir, owner, group)
316 error (0, errno, "cannot chown %s", dirpath);
319 /* chown may have turned off some permission bits we wanted. */
320 if ((mode & 07000) != 0 && chmod (basename_dir, mode))
322 error (0, errno, "cannot chmod %s", dirpath);
329 /* If the mode for leading directories didn't include owner "wx"
330 privileges, we have to reset their protections to the correct
332 for (p = leading_dirs; p != NULL; p = p->next)
334 *(p->dirname_end) = '\0';
335 if (chmod (dirpath, parent_mode))
337 error (0, errno, "%s", dirpath);
344 /* We get here if the entire path already exists. */
346 const char *dirpath = argpath;
348 if (!S_ISDIR (stats.st_mode))
350 error (0, 0, "`%s' exists but is not a directory", dirpath);
354 if (!preserve_existing)
356 /* chown must precede chmod because on some systems,
357 chown clears the set[ug]id bits for non-superusers,
358 resulting in incorrect permissions.
359 On System V, users can give away files with chown and then not
360 be able to chmod them. So don't give files away. */
362 if ((owner != (uid_t) -1 || group != (gid_t) -1)
363 && chown (dirpath, owner, group)
369 error (0, errno, "%s", dirpath);
372 if (chmod (dirpath, mode))
374 error (0, errno, "%s", dirpath);