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)
104 # include <libintl.h>
105 # define _(Text) gettext (Text)
107 # define _(Text) Text
115 #include "save-cwd.h"
116 #include "makepath.h"
119 void strip_trailing_slashes ();
121 #define CLEANUP_CWD \
124 /* We're done operating on basename_dir. \
125 Restore working directory. */ \
128 int _fail = restore_cwd (&cwd, NULL, NULL); \
144 /* Attempt to create directory DIR (aka DIRPATH) with the specified MODE.
145 If CREATED_DIR_P is non-NULL, set *CREATED_DIR_P to non-zero if this
146 function creates DIR and to zero otherwise. Give a diagnostic and
147 return non-zero if DIR cannot be created or cannot be determined to
148 exist already. Use DIRPATH in any diagnostic, not DIR.
149 Note that if DIR already exists, this function will return zero
150 (indicating success) and will set *CREATED_DIR_P to zero. */
153 make_dir (const char *dir, const char *dirpath, mode_t mode, int *created_dir_p)
158 created_dir = (mkdir (dir, mode) == 0);
164 /* The mkdir and stat calls below may appear to be reversed.
165 They are not. It is important to call mkdir first and then to
166 call stat (to distinguish the three cases) only if mkdir fails.
167 The alternative to this approach is to `stat' each directory,
168 then to call mkdir if it doesn't exist. But if some other process
169 were to create the directory between the stat & mkdir, the mkdir
170 would fail with EEXIST. */
172 if (stat (dir, &stats))
174 error (0, errno, _("cannot create directory `%s'"), dirpath);
177 else if (!S_ISDIR (stats.st_mode))
179 error (0, 0, _("`%s' exists but is not a directory"), dirpath);
184 /* DIR (aka DIRPATH) already exists and is a directory. */
189 *created_dir_p = created_dir;
194 /* Ensure that the directory ARGPATH exists.
195 Remove any trailing slashes from ARGPATH before calling this function.
197 Create any leading directories that don't already exist, with
198 permissions PARENT_MODE.
199 If the last element of ARGPATH does not exist, create it as
200 a new directory with permissions MODE.
201 If OWNER and GROUP are non-negative, use them to set the UID and GID of
202 any created directories.
203 If VERBOSE_FMT_STRING is nonzero, use it as a printf format
204 string for printing a message after successfully making a directory,
205 with the name of the directory that was just made as an argument.
206 If PRESERVE_EXISTING is non-zero and ARGPATH is an existing directory,
207 then do not attempt to set its permissions and ownership.
209 Return 0 if ARGPATH exists as a directory with the proper
210 ownership and permissions when done, otherwise 1. */
213 make_path (const char *argpath,
218 int preserve_existing,
219 const char *verbose_fmt_string)
224 if (stat (argpath, &stats))
227 int tmp_mode; /* Initial perms for leading dirs. */
228 int re_protect; /* Should leading dirs be unwritable? */
232 struct ptr_list *next;
234 struct ptr_list *p, *leading_dirs = NULL;
235 int do_chdir; /* Whether to chdir before each mkdir. */
236 struct saved_cwd cwd;
240 /* Temporarily relax umask in case it's overly restrictive. */
241 mode_t oldmask = umask (0);
243 /* Make a copy of ARGPATH that we can scribble NULs on. */
244 dirpath = (char *) alloca (strlen (argpath) + 1);
245 strcpy (dirpath, argpath);
246 strip_trailing_slashes (dirpath);
248 /* If leading directories shouldn't be writable or executable,
249 or should have set[ug]id or sticky bits set and we are setting
250 their owners, we need to fix their permissions after making them. */
251 if (((parent_mode & WX_USR) != WX_USR)
252 || ((owner != (uid_t) -1 || group != (gid_t) -1)
253 && (parent_mode & (S_ISUID | S_ISGID | S_ISVTX)) != 0))
260 tmp_mode = parent_mode;
264 /* If we can record the current working directory, we may be able
265 to do the chdir optimization. */
266 do_chdir = !save_cwd (&cwd);
268 /* If we've saved the cwd and DIRPATH is an absolute pathname,
269 we must chdir to `/' in order to enable the chdir optimization.
270 So if chdir ("/") fails, turn off the optimization. */
271 if (do_chdir && *dirpath == '/' && chdir ("/") < 0)
276 /* Skip over leading slashes. */
277 while (*slash == '/')
282 int newly_created_dir;
285 /* slash points to the leftmost unprocessed component of dirpath. */
286 basename_dir = slash;
288 slash = strchr (slash, '/');
292 /* If we're *not* doing chdir before each mkdir, then we have to refer
293 to the target using the full (multi-component) directory name. */
295 basename_dir = dirpath;
298 fail = make_dir (basename_dir, dirpath, tmp_mode, &newly_created_dir);
305 if (newly_created_dir)
307 if (verbose_fmt_string)
308 fprintf (stderr, verbose_fmt_string, dirpath);
310 if ((owner != (uid_t) -1 || group != (gid_t) -1)
311 && chown (basename_dir, owner, group)
312 #if defined(AFS) && defined (EPERM)
317 error (0, errno, "%s", dirpath);
324 struct ptr_list *new = (struct ptr_list *)
325 alloca (sizeof (struct ptr_list));
326 new->dirname_end = slash;
327 new->next = leading_dirs;
332 /* If we were able to save the initial working directory,
333 then we can use chdir to change into each directory before
334 creating an entry in that directory. This avoids making
335 stat and mkdir process O(n^2) file name components. */
336 if (do_chdir && chdir (basename_dir) < 0)
338 error (0, errno, _("cannot chdir to directory, %s"), dirpath);
345 /* Avoid unnecessary calls to `stat' when given
346 pathnames containing multiple adjacent slashes. */
347 while (*slash == '/')
352 basename_dir = dirpath;
354 /* We're done making leading directories.
355 Create the final component of the path. */
357 if (make_dir (basename_dir, dirpath, mode, NULL))
363 /* Done creating directories. Restore original umask. */
366 if (verbose_fmt_string != NULL)
367 error (0, 0, verbose_fmt_string, dirpath);
369 if (owner != (uid_t) -1 && group != (gid_t) -1)
371 if (chown (basename_dir, owner, group)
377 error (0, errno, _("cannot chown %s"), dirpath);
380 /* chown may have turned off some permission bits we wanted. */
381 if ((mode & (S_ISUID | S_ISGID | S_ISVTX))
382 && chmod (basename_dir, mode))
384 error (0, errno, _("cannot chmod %s"), dirpath);
391 /* If the mode for leading directories didn't include owner "wx"
392 privileges, we have to reset their protections to the correct
394 for (p = leading_dirs; p != NULL; p = p->next)
396 *(p->dirname_end) = '\0';
397 if (chmod (dirpath, parent_mode))
399 error (0, errno, "%s", dirpath);
406 /* We get here if the entire path already exists. */
408 const char *dirpath = argpath;
410 if (!S_ISDIR (stats.st_mode))
412 error (0, 0, _("`%s' exists but is not a directory"), dirpath);
416 if (!preserve_existing)
418 /* chown must precede chmod because on some systems,
419 chown clears the set[ug]id bits for non-superusers,
420 resulting in incorrect permissions.
421 On System V, users can give away files with chown and then not
422 be able to chmod them. So don't give files away. */
424 if ((owner != (uid_t) -1 || group != (gid_t) -1)
425 && chown (dirpath, owner, group)
431 error (0, errno, "%s", dirpath);
434 if (chmod (dirpath, mode))
436 error (0, errno, "%s", dirpath);