1 /* makepath.c -- Ensure that a directory path exists.
2 Copyright (C) 1990, 1997, 1998, 1999 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)
75 # define S_ISUID 04000
78 # define S_ISGID 02000
81 # define S_ISVTX 01000
94 # define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
97 #define WX_USR (S_IWUSR | S_IXUSR)
99 /* Include this before libintl.h so we get our definition of PARAMS. */
100 #include "makepath.h"
107 # include <libintl.h>
108 # define _(Text) gettext (Text)
110 # define _(Text) Text
118 #include "save-cwd.h"
121 void strip_trailing_slashes ();
123 #define CLEANUP_CWD \
126 /* We're done operating on basename_dir. \
127 Restore working directory. */ \
130 int _fail = restore_cwd (&cwd, NULL, NULL); \
146 /* Attempt to create directory DIR (aka DIRPATH) with the specified MODE.
147 If CREATED_DIR_P is non-NULL, set *CREATED_DIR_P to non-zero if this
148 function creates DIR and to zero otherwise. Give a diagnostic and
149 return non-zero if DIR cannot be created or cannot be determined to
150 exist already. Use DIRPATH in any diagnostic, not DIR.
151 Note that if DIR already exists, this function will return zero
152 (indicating success) and will set *CREATED_DIR_P to zero. */
155 make_dir (const char *dir, const char *dirpath, mode_t mode, int *created_dir_p)
160 created_dir = (mkdir (dir, mode) == 0);
166 /* The mkdir and stat calls below may appear to be reversed.
167 They are not. It is important to call mkdir first and then to
168 call stat (to distinguish the three cases) only if mkdir fails.
169 The alternative to this approach is to `stat' each directory,
170 then to call mkdir if it doesn't exist. But if some other process
171 were to create the directory between the stat & mkdir, the mkdir
172 would fail with EEXIST. */
174 if (stat (dir, &stats))
176 error (0, errno, _("cannot create directory `%s'"), dirpath);
179 else if (!S_ISDIR (stats.st_mode))
181 error (0, 0, _("`%s' exists but is not a directory"), dirpath);
186 /* DIR (aka DIRPATH) already exists and is a directory. */
191 *created_dir_p = created_dir;
196 /* Ensure that the directory ARGPATH exists.
197 Remove any trailing slashes from ARGPATH before calling this function.
199 Create any leading directories that don't already exist, with
200 permissions PARENT_MODE.
201 If the last element of ARGPATH does not exist, create it as
202 a new directory with permissions MODE.
203 If OWNER and GROUP are non-negative, use them to set the UID and GID of
204 any created directories.
205 If VERBOSE_FMT_STRING is nonzero, use it as a printf format
206 string for printing a message after successfully making a directory,
207 with the name of the directory that was just made as an argument.
208 If PRESERVE_EXISTING is non-zero and ARGPATH is an existing directory,
209 then do not attempt to set its permissions and ownership.
211 Return 0 if ARGPATH exists as a directory with the proper
212 ownership and permissions when done, otherwise 1. */
215 make_path (const char *argpath,
220 int preserve_existing,
221 const char *verbose_fmt_string)
226 if (stat (argpath, &stats))
229 int tmp_mode; /* Initial perms for leading dirs. */
230 int re_protect; /* Should leading dirs be unwritable? */
234 struct ptr_list *next;
236 struct ptr_list *p, *leading_dirs = NULL;
237 int do_chdir; /* Whether to chdir before each mkdir. */
238 struct saved_cwd cwd;
242 /* Temporarily relax umask in case it's overly restrictive. */
243 mode_t oldmask = umask (0);
245 /* Make a copy of ARGPATH that we can scribble NULs on. */
246 dirpath = (char *) alloca (strlen (argpath) + 1);
247 strcpy (dirpath, argpath);
248 strip_trailing_slashes (dirpath);
250 /* If leading directories shouldn't be writable or executable,
251 or should have set[ug]id or sticky bits set and we are setting
252 their owners, we need to fix their permissions after making them. */
253 if (((parent_mode & WX_USR) != WX_USR)
254 || ((owner != (uid_t) -1 || group != (gid_t) -1)
255 && (parent_mode & (S_ISUID | S_ISGID | S_ISVTX)) != 0))
262 tmp_mode = parent_mode;
266 /* If we can record the current working directory, we may be able
267 to do the chdir optimization. */
268 do_chdir = !save_cwd (&cwd);
270 /* If we've saved the cwd and DIRPATH is an absolute pathname,
271 we must chdir to `/' in order to enable the chdir optimization.
272 So if chdir ("/") fails, turn off the optimization. */
273 if (do_chdir && *dirpath == '/' && chdir ("/") < 0)
278 /* Skip over leading slashes. */
279 while (*slash == '/')
284 int newly_created_dir;
287 /* slash points to the leftmost unprocessed component of dirpath. */
288 basename_dir = slash;
290 slash = strchr (slash, '/');
294 /* If we're *not* doing chdir before each mkdir, then we have to refer
295 to the target using the full (multi-component) directory name. */
297 basename_dir = dirpath;
300 fail = make_dir (basename_dir, dirpath, tmp_mode, &newly_created_dir);
307 if (newly_created_dir)
309 if (verbose_fmt_string)
310 fprintf (stderr, verbose_fmt_string, dirpath);
312 if ((owner != (uid_t) -1 || group != (gid_t) -1)
313 && chown (basename_dir, owner, group)
314 #if defined(AFS) && defined (EPERM)
319 error (0, errno, "%s", dirpath);
326 struct ptr_list *new = (struct ptr_list *)
327 alloca (sizeof (struct ptr_list));
328 new->dirname_end = slash;
329 new->next = leading_dirs;
334 /* If we were able to save the initial working directory,
335 then we can use chdir to change into each directory before
336 creating an entry in that directory. This avoids making
337 stat and mkdir process O(n^2) file name components. */
338 if (do_chdir && chdir (basename_dir) < 0)
340 error (0, errno, _("cannot chdir to directory, %s"), dirpath);
347 /* Avoid unnecessary calls to `stat' when given
348 pathnames containing multiple adjacent slashes. */
349 while (*slash == '/')
354 basename_dir = dirpath;
356 /* We're done making leading directories.
357 Create the final component of the path. */
359 if (make_dir (basename_dir, dirpath, mode, NULL))
365 /* Done creating directories. Restore original umask. */
368 if (verbose_fmt_string != NULL)
369 error (0, 0, verbose_fmt_string, dirpath);
371 if (owner != (uid_t) -1 && group != (gid_t) -1)
373 if (chown (basename_dir, owner, group)
379 error (0, errno, _("cannot chown %s"), dirpath);
382 /* chown may have turned off some permission bits we wanted. */
383 if ((mode & (S_ISUID | S_ISGID | S_ISVTX))
384 && chmod (basename_dir, mode))
386 error (0, errno, _("cannot chmod %s"), dirpath);
393 /* If the mode for leading directories didn't include owner "wx"
394 privileges, we have to reset their protections to the correct
396 for (p = leading_dirs; p != NULL; p = p->next)
398 *(p->dirname_end) = '\0';
399 if (chmod (dirpath, parent_mode))
401 error (0, errno, "%s", dirpath);
408 /* We get here if the entire path already exists. */
410 const char *dirpath = argpath;
412 if (!S_ISDIR (stats.st_mode))
414 error (0, 0, _("`%s' exists but is not a directory"), dirpath);
418 if (!preserve_existing)
420 /* chown must precede chmod because on some systems,
421 chown clears the set[ug]id bits for non-superusers,
422 resulting in incorrect permissions.
423 On System V, users can give away files with chown and then not
424 be able to chmod them. So don't give files away. */
426 if ((owner != (uid_t) -1 || group != (gid_t) -1)
427 && chown (dirpath, owner, group)
433 error (0, errno, "%s", dirpath);
436 if (chmod (dirpath, mode))
438 error (0, errno, "%s", dirpath);