1 /* makepath.c -- Ensure that a directory path exists.
3 Copyright (C) 1990, 1997, 1998, 1999, 2000, 2002, 2003 Free
4 Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> and Jim Meyering. */
29 #include <sys/types.h>
35 #if STAT_MACROS_BROKEN
39 #if !defined S_ISDIR && defined S_IFDIR
40 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
44 # define S_IRWXUGO (S_IRWXU | S_IRWXG | S_IRWXO)
67 # define S_ISUID 04000
70 # define S_ISGID 02000
73 # define S_ISVTX 01000
86 # define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
89 #define WX_USR (S_IWUSR | S_IXUSR)
94 #define _(msgid) gettext (msgid)
101 #define CLEANUP_CWD \
104 /* We're done operating on basename_dir. \
105 Restore working directory. */ \
108 if (restore_cwd (&cwd) != 0) \
110 int _saved_errno = errno; \
112 _("failed to return to initial working directory")); \
114 errno = _saved_errno; \
130 /* Attempt to create directory DIR (aka DIRPATH) with the specified MODE.
131 If CREATED_DIR_P is non-NULL, set *CREATED_DIR_P to non-zero if this
132 function creates DIR and to zero otherwise. Give a diagnostic and
133 return non-zero if DIR cannot be created or cannot be determined to
134 exist already. Use DIRPATH in any diagnostic, not DIR.
135 Note that if DIR already exists, this function returns zero
136 (indicating success) and sets *CREATED_DIR_P to zero. */
139 make_dir (const char *dir, const char *dirpath, mode_t mode, int *created_dir_p)
144 created_dir = (mkdir (dir, mode) == 0);
149 int saved_errno = errno;
151 /* The mkdir and stat calls below may appear to be reversed.
152 They are not. It is important to call mkdir first and then to
153 call stat (to distinguish the three cases) only if mkdir fails.
154 The alternative to this approach is to `stat' each directory,
155 then to call mkdir if it doesn't exist. But if some other process
156 were to create the directory between the stat & mkdir, the mkdir
157 would fail with EEXIST. */
159 if (stat (dir, &stats))
161 error (0, saved_errno, _("cannot create directory %s"),
165 else if (!S_ISDIR (stats.st_mode))
167 error (0, 0, _("%s exists but is not a directory"), quote (dirpath));
172 /* DIR (aka DIRPATH) already exists and is a directory. */
177 *created_dir_p = created_dir;
182 /* Ensure that the directory ARGPATH exists.
184 Create any leading directories that don't already exist, with
185 permissions PARENT_MODE.
186 If the last element of ARGPATH does not exist, create it as
187 a new directory with permissions MODE.
188 If OWNER and GROUP are non-negative, use them to set the UID and GID of
189 any created directories.
190 If VERBOSE_FMT_STRING is nonzero, use it as a printf format
191 string for printing a message after successfully making a directory,
192 with the name of the directory that was just made as an argument.
193 If PRESERVE_EXISTING is non-zero and ARGPATH is an existing directory,
194 then do not attempt to set its permissions and ownership.
196 Return 0 if ARGPATH exists as a directory with the proper
197 ownership and permissions when done, otherwise 1. */
200 make_path (const char *argpath,
205 int preserve_existing,
206 const char *verbose_fmt_string)
211 if (stat (argpath, &stats))
214 int tmp_mode; /* Initial perms for leading dirs. */
215 int re_protect; /* Should leading dirs be unwritable? */
219 struct ptr_list *next;
221 struct ptr_list *p, *leading_dirs = NULL;
222 int do_chdir; /* Whether to chdir before each mkdir. */
223 struct saved_cwd cwd;
227 /* Temporarily relax umask in case it's overly restrictive. */
228 mode_t oldmask = umask (0);
230 /* Make a copy of ARGPATH that we can scribble NULs on. */
231 dirpath = (char *) alloca (strlen (argpath) + 1);
232 strcpy (dirpath, argpath);
233 strip_trailing_slashes (dirpath);
235 /* If leading directories shouldn't be writable or executable,
236 or should have set[ug]id or sticky bits set and we are setting
237 their owners, we need to fix their permissions after making them. */
238 if (((parent_mode & WX_USR) != WX_USR)
239 || ((owner != (uid_t) -1 || group != (gid_t) -1)
240 && (parent_mode & (S_ISUID | S_ISGID | S_ISVTX)) != 0))
247 tmp_mode = parent_mode;
251 /* If we can record the current working directory, we may be able
252 to do the chdir optimization. */
253 do_chdir = !save_cwd (&cwd);
255 /* If we've saved the cwd and DIRPATH is an absolute pathname,
256 we must chdir to `/' in order to enable the chdir optimization.
257 So if chdir ("/") fails, turn off the optimization. */
258 if (do_chdir && *dirpath == '/' && chdir ("/") < 0)
263 /* Skip over leading slashes. */
264 while (*slash == '/')
269 int newly_created_dir;
272 /* slash points to the leftmost unprocessed component of dirpath. */
273 basename_dir = slash;
275 slash = strchr (slash, '/');
279 /* If we're *not* doing chdir before each mkdir, then we have to refer
280 to the target using the full (multi-component) directory name. */
282 basename_dir = dirpath;
285 fail = make_dir (basename_dir, dirpath, tmp_mode, &newly_created_dir);
292 if (newly_created_dir)
294 if (verbose_fmt_string)
295 error (0, 0, verbose_fmt_string, quote (dirpath));
297 if ((owner != (uid_t) -1 || group != (gid_t) -1)
298 && chown (basename_dir, owner, group)
299 #if defined AFS && defined EPERM
304 error (0, errno, _("cannot change owner and/or group of %s"),
312 struct ptr_list *new = (struct ptr_list *)
313 alloca (sizeof (struct ptr_list));
314 new->dirname_end = slash;
315 new->next = leading_dirs;
320 /* If we were able to save the initial working directory,
321 then we can use chdir to change into each directory before
322 creating an entry in that directory. This avoids making
323 stat and mkdir process O(n^2) file name components. */
324 if (do_chdir && chdir (basename_dir) < 0)
326 error (0, errno, _("cannot chdir to directory %s"),
334 /* Avoid unnecessary calls to `stat' when given
335 pathnames containing multiple adjacent slashes. */
336 while (*slash == '/')
341 basename_dir = dirpath;
343 /* Done creating leading directories. Restore original umask. */
346 /* We're done making leading directories.
347 Create the final component of the path. */
349 if (make_dir (basename_dir, dirpath, mode, NULL))
355 if (verbose_fmt_string != NULL)
356 error (0, 0, verbose_fmt_string, quote (dirpath));
358 if (owner != (uid_t) -1 || group != (gid_t) -1)
360 if (chown (basename_dir, owner, group)
366 error (0, errno, _("cannot change owner and/or group of %s"),
372 /* The above chown may have turned off some permission bits in MODE.
373 Another reason we may have to use chmod here is that mkdir(2) is
374 required to honor only the file permission bits. In particular,
375 it need not honor the `special' bits, so if MODE includes any
376 special bits, set them here. */
377 if ((mode & ~S_IRWXUGO)
378 && chmod (basename_dir, mode))
380 error (0, errno, _("cannot change permissions of %s"),
387 /* If the mode for leading directories didn't include owner "wx"
388 privileges, we have to reset their protections to the correct
390 for (p = leading_dirs; p != NULL; p = p->next)
392 *(p->dirname_end) = '\0';
393 if (chmod (dirpath, parent_mode))
395 error (0, errno, _("cannot change permissions of %s"),
403 /* We get here if the entire path already exists. */
405 const char *dirpath = argpath;
407 if (!S_ISDIR (stats.st_mode))
409 error (0, 0, _("%s exists but is not a directory"), quote (dirpath));
413 if (!preserve_existing)
415 /* chown must precede chmod because on some systems,
416 chown clears the set[ug]id bits for non-superusers,
417 resulting in incorrect permissions.
418 On System V, users can give away files with chown and then not
419 be able to chmod them. So don't give files away. */
421 if ((owner != (uid_t) -1 || group != (gid_t) -1)
422 && chown (dirpath, owner, group)
428 error (0, errno, _("cannot change owner and/or group of %s"),
432 if (chmod (dirpath, mode))
434 error (0, errno, _("cannot change permissions of %s"),