1 /* makepath.c -- Ensure that a directory path exists.
2 Copyright (C) 1990, 1997, 1998, 1999, 2000 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)
54 # define S_IRWXUGO (S_IRWXU | S_IRWXG | S_IRWXO)
79 # define S_ISUID 04000
82 # define S_ISGID 02000
85 # define S_ISVTX 01000
98 # define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
101 #define WX_USR (S_IWUSR | S_IXUSR)
103 /* Include this before libintl.h so we get our definition of PARAMS. */
104 #include "makepath.h"
111 # include <libintl.h>
112 # define _(Text) gettext (Text)
114 # define _(Text) Text
122 #include "save-cwd.h"
125 void strip_trailing_slashes ();
127 #define CLEANUP_CWD \
130 /* We're done operating on basename_dir. \
131 Restore working directory. */ \
134 int _fail = restore_cwd (&cwd, NULL, NULL); \
150 /* Attempt to create directory DIR (aka DIRPATH) with the specified MODE.
151 If CREATED_DIR_P is non-NULL, set *CREATED_DIR_P to non-zero if this
152 function creates DIR and to zero otherwise. Give a diagnostic and
153 return non-zero if DIR cannot be created or cannot be determined to
154 exist already. Use DIRPATH in any diagnostic, not DIR.
155 Note that if DIR already exists, this function will return zero
156 (indicating success) and will set *CREATED_DIR_P to zero. */
159 make_dir (const char *dir, const char *dirpath, mode_t mode, int *created_dir_p)
164 created_dir = (mkdir (dir, mode) == 0);
169 int saved_errno = errno;
171 /* The mkdir and stat calls below may appear to be reversed.
172 They are not. It is important to call mkdir first and then to
173 call stat (to distinguish the three cases) only if mkdir fails.
174 The alternative to this approach is to `stat' each directory,
175 then to call mkdir if it doesn't exist. But if some other process
176 were to create the directory between the stat & mkdir, the mkdir
177 would fail with EEXIST. */
179 if (stat (dir, &stats))
181 error (0, saved_errno, _("cannot create directory `%s'"), dirpath);
184 else if (!S_ISDIR (stats.st_mode))
186 error (0, 0, _("`%s' exists but is not a directory"), dirpath);
191 /* DIR (aka DIRPATH) already exists and is a directory. */
196 *created_dir_p = created_dir;
201 /* Ensure that the directory ARGPATH exists.
202 Remove any trailing slashes from ARGPATH before calling this function.
204 Create any leading directories that don't already exist, with
205 permissions PARENT_MODE.
206 If the last element of ARGPATH does not exist, create it as
207 a new directory with permissions MODE.
208 If OWNER and GROUP are non-negative, use them to set the UID and GID of
209 any created directories.
210 If VERBOSE_FMT_STRING is nonzero, use it as a printf format
211 string for printing a message after successfully making a directory,
212 with the name of the directory that was just made as an argument.
213 If PRESERVE_EXISTING is non-zero and ARGPATH is an existing directory,
214 then do not attempt to set its permissions and ownership.
216 Return 0 if ARGPATH exists as a directory with the proper
217 ownership and permissions when done, otherwise 1. */
220 make_path (const char *argpath,
225 int preserve_existing,
226 const char *verbose_fmt_string)
231 if (stat (argpath, &stats))
234 int tmp_mode; /* Initial perms for leading dirs. */
235 int re_protect; /* Should leading dirs be unwritable? */
239 struct ptr_list *next;
241 struct ptr_list *p, *leading_dirs = NULL;
242 int do_chdir; /* Whether to chdir before each mkdir. */
243 struct saved_cwd cwd;
247 /* Temporarily relax umask in case it's overly restrictive. */
248 mode_t oldmask = umask (0);
250 /* Make a copy of ARGPATH that we can scribble NULs on. */
251 dirpath = (char *) alloca (strlen (argpath) + 1);
252 strcpy (dirpath, argpath);
253 strip_trailing_slashes (dirpath);
255 /* If leading directories shouldn't be writable or executable,
256 or should have set[ug]id or sticky bits set and we are setting
257 their owners, we need to fix their permissions after making them. */
258 if (((parent_mode & WX_USR) != WX_USR)
259 || ((owner != (uid_t) -1 || group != (gid_t) -1)
260 && (parent_mode & (S_ISUID | S_ISGID | S_ISVTX)) != 0))
267 tmp_mode = parent_mode;
271 /* If we can record the current working directory, we may be able
272 to do the chdir optimization. */
273 do_chdir = !save_cwd (&cwd);
275 /* If we've saved the cwd and DIRPATH is an absolute pathname,
276 we must chdir to `/' in order to enable the chdir optimization.
277 So if chdir ("/") fails, turn off the optimization. */
278 if (do_chdir && *dirpath == '/' && chdir ("/") < 0)
283 /* Skip over leading slashes. */
284 while (*slash == '/')
289 int newly_created_dir;
292 /* slash points to the leftmost unprocessed component of dirpath. */
293 basename_dir = slash;
295 slash = strchr (slash, '/');
299 /* If we're *not* doing chdir before each mkdir, then we have to refer
300 to the target using the full (multi-component) directory name. */
302 basename_dir = dirpath;
305 fail = make_dir (basename_dir, dirpath, tmp_mode, &newly_created_dir);
312 if (newly_created_dir)
314 if (verbose_fmt_string)
315 error (0, 0, verbose_fmt_string, dirpath);
317 if ((owner != (uid_t) -1 || group != (gid_t) -1)
318 && chown (basename_dir, owner, group)
319 #if defined(AFS) && defined (EPERM)
324 error (0, errno, "%s", dirpath);
331 struct ptr_list *new = (struct ptr_list *)
332 alloca (sizeof (struct ptr_list));
333 new->dirname_end = slash;
334 new->next = leading_dirs;
339 /* If we were able to save the initial working directory,
340 then we can use chdir to change into each directory before
341 creating an entry in that directory. This avoids making
342 stat and mkdir process O(n^2) file name components. */
343 if (do_chdir && chdir (basename_dir) < 0)
345 error (0, errno, _("cannot chdir to directory, %s"), dirpath);
352 /* Avoid unnecessary calls to `stat' when given
353 pathnames containing multiple adjacent slashes. */
354 while (*slash == '/')
359 basename_dir = dirpath;
361 /* We're done making leading directories.
362 Create the final component of the path. */
364 if (make_dir (basename_dir, dirpath, mode, NULL))
370 /* Done creating directories. Restore original umask. */
373 if (verbose_fmt_string != NULL)
374 error (0, 0, verbose_fmt_string, dirpath);
376 if (owner != (uid_t) -1 || group != (gid_t) -1)
378 if (chown (basename_dir, owner, group)
384 error (0, errno, _("cannot chown %s"), dirpath);
389 /* The above chown may have turned off some permission bits in MODE.
390 Another reason we may have to use chmod here is that mkdir(2) is
391 required to honor only the file permission bits. In particular,
392 it need not honor the `special' bits, so if MODE includes any
393 special bits, set them here. */
394 if ((mode & ~S_IRWXUGO)
395 && chmod (basename_dir, mode))
397 error (0, errno, _("cannot chmod %s"), dirpath);
403 /* If the mode for leading directories didn't include owner "wx"
404 privileges, we have to reset their protections to the correct
406 for (p = leading_dirs; p != NULL; p = p->next)
408 *(p->dirname_end) = '\0';
409 if (chmod (dirpath, parent_mode))
411 error (0, errno, "%s", dirpath);
418 /* We get here if the entire path already exists. */
420 const char *dirpath = argpath;
422 if (!S_ISDIR (stats.st_mode))
424 error (0, 0, _("`%s' exists but is not a directory"), dirpath);
428 if (!preserve_existing)
430 /* chown must precede chmod because on some systems,
431 chown clears the set[ug]id bits for non-superusers,
432 resulting in incorrect permissions.
433 On System V, users can give away files with chown and then not
434 be able to chmod them. So don't give files away. */
436 if ((owner != (uid_t) -1 || group != (gid_t) -1)
437 && chown (dirpath, owner, group)
443 error (0, errno, "%s", dirpath);
446 if (chmod (dirpath, mode))
448 error (0, errno, "%s", dirpath);