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)
83 void strip_trailing_slashes ();
88 /* We're done operating on basename_dir. \
89 Restore working directory. */ \
92 int fail = restore_cwd (&cwd, NULL, NULL); \
108 /* Ensure that the directory ARGPATH exists.
109 Remove any trailing slashes from ARGPATH before calling this function.
111 Create any leading directories that don't already exist, with
112 permissions PARENT_MODE.
113 If the last element of ARGPATH does not exist, create it as
114 a new directory with permissions MODE.
115 If OWNER and GROUP are non-negative, use them to set the UID and GID of
116 any created directories.
117 If VERBOSE_FMT_STRING is nonzero, use it as a printf format
118 string for printing a message after successfully making a directory,
119 with the name of the directory that was just made as an argument.
120 If PRESERVE_EXISTING is non-zero and ARGPATH is an existing directory,
121 then do not attempt to set its permissions and ownership.
123 Return 0 if ARGPATH exists as a directory with the proper
124 ownership and permissions when done, otherwise 1. */
128 make_path (const char *argpath,
133 int preserve_existing,
134 const char *verbose_fmt_string)
137 make_path (argpath, mode, parent_mode, owner, group, preserve_existing,
144 int preserve_existing;
145 const char *verbose_fmt_string;
151 if (stat (argpath, &stats))
154 int tmp_mode; /* Initial perms for leading dirs. */
155 int re_protect; /* Should leading dirs be unwritable? */
159 struct ptr_list *next;
161 struct ptr_list *p, *leading_dirs = NULL;
163 struct saved_cwd cwd;
165 int first_subdir = 1;
168 /* Temporarily relax umask in case it's overly restrictive. */
169 int oldmask = umask (0);
171 /* Make a copy of ARGPATH that we can scribble NULs on. */
172 dirpath = (char *) alloca (strlen (argpath) + 1);
173 strcpy (dirpath, argpath);
174 strip_trailing_slashes (dirpath);
176 /* If leading directories shouldn't be writable or executable,
177 or should have set[ug]id or sticky bits set and we are setting
178 their owners, we need to fix their permissions after making them. */
179 if (((parent_mode & 0300) != 0300)
180 || (owner != (uid_t) -1 && group != (gid_t) -1
181 && (parent_mode & 07000) != 0))
188 tmp_mode = parent_mode;
194 /* Skip over leading slashes. */
195 while (*slash == '/')
200 /* slash points to the leftmost unprocessed component of dirpath. */
201 basename_dir = slash;
203 slash = strchr (slash, '/');
210 saved_cwd = !save_cwd (&cwd);
214 basename_dir = dirpath;
217 if (stat (basename_dir, &stats))
219 if (mkdir (basename_dir, tmp_mode))
221 error (0, errno, "cannot create directory `%s'", dirpath);
227 if (verbose_fmt_string != NULL)
228 error (0, 0, verbose_fmt_string, dirpath);
230 if (owner != (uid_t) -1 && group != (gid_t) -1
231 && chown (basename_dir, owner, group)
232 #if defined(AFS) && defined (EPERM)
237 error (0, errno, "%s", dirpath);
244 struct ptr_list *new = (struct ptr_list *)
245 alloca (sizeof (struct ptr_list));
246 new->dirname_end = slash;
247 new->next = leading_dirs;
252 else if (!S_ISDIR (stats.st_mode))
254 error (0, 0, "`%s' exists but is not a directory", dirpath);
259 if (saved_cwd && chdir (basename_dir) < 0)
261 error (0, errno, "cannot chdir to directory, %s", dirpath);
268 /* Avoid unnecessary calls to `stat' when given
269 pathnames containing multiple adjacent slashes. */
270 while (*slash == '/')
275 basename_dir = dirpath;
277 /* We're done making leading directories.
278 Create the final component of the path. */
280 /* The path could end in "/." or contain "/..", so test
281 if we really have to create the directory. */
283 if (stat (basename_dir, &stats) && mkdir (basename_dir, mode))
285 error (0, errno, "cannot create directory `%s'", dirpath);
290 /* Done creating directories. Restore original umask. */
293 if (verbose_fmt_string != NULL)
294 error (0, 0, verbose_fmt_string, dirpath);
296 if (owner != (uid_t) -1 && group != (gid_t) -1)
298 if (chown (basename_dir, owner, group)
304 error (0, errno, "cannot chown %s", dirpath);
307 /* chown may have turned off some permission bits we wanted. */
308 if ((mode & 07000) != 0 && chmod (basename_dir, mode))
310 error (0, errno, "cannot chmod %s", dirpath);
317 /* If the mode for leading directories didn't include owner "wx"
318 privileges, we have to reset their protections to the correct
320 for (p = leading_dirs; p != NULL; p = p->next)
322 *(p->dirname_end) = '\0';
323 if (chmod (dirpath, parent_mode))
325 error (0, errno, "%s", dirpath);
332 /* We get here if the entire path already exists. */
334 const char *dirpath = argpath;
336 if (!S_ISDIR (stats.st_mode))
338 error (0, 0, "`%s' exists but is not a directory", dirpath);
342 if (!preserve_existing)
344 /* chown must precede chmod because on some systems,
345 chown clears the set[ug]id bits for non-superusers,
346 resulting in incorrect permissions.
347 On System V, users can give away files with chown and then not
348 be able to chmod them. So don't give files away. */
350 if (owner != (uid_t) -1 && group != (gid_t) -1
351 && chown (dirpath, owner, group)
357 error (0, errno, "%s", dirpath);
360 if (chmod (dirpath, mode))
362 error (0, errno, "%s", dirpath);