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. */
31 #include <sys/types.h>
37 #if STAT_MACROS_BROKEN
41 #if !defined S_ISDIR && defined S_IFDIR
42 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
46 # define S_IRWXUGO (S_IRWXU | S_IRWXG | S_IRWXO)
60 # define S_ISUID 04000
63 # define S_ISGID 02000
66 # define S_ISVTX 01000
79 # define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
82 #define WX_USR (S_IWUSR | S_IXUSR)
85 #define _(msgid) gettext (msgid)
95 /* We're done operating on basename_dir. \
96 Restore working directory. */ \
99 if (restore_cwd (&cwd) != 0) \
101 int _saved_errno = errno; \
103 _("failed to return to initial working directory")); \
105 errno = _saved_errno; \
121 /* Attempt to create directory DIR (aka DIRPATH) with the specified MODE.
122 If CREATED_DIR_P is non-NULL, set *CREATED_DIR_P to non-zero if this
123 function creates DIR and to zero otherwise. Give a diagnostic and
124 return non-zero if DIR cannot be created or cannot be determined to
125 exist already. Use DIRPATH in any diagnostic, not DIR.
126 Note that if DIR already exists, this function returns zero
127 (indicating success) and sets *CREATED_DIR_P to zero. */
130 make_dir (const char *dir
, const char *dirpath
, mode_t mode
, int *created_dir_p
)
135 created_dir
= (mkdir (dir
, mode
) == 0);
140 int saved_errno
= errno
;
142 /* The mkdir and stat calls below may appear to be reversed.
143 They are not. It is important to call mkdir first and then to
144 call stat (to distinguish the three cases) only if mkdir fails.
145 The alternative to this approach is to `stat' each directory,
146 then to call mkdir if it doesn't exist. But if some other process
147 were to create the directory between the stat & mkdir, the mkdir
148 would fail with EEXIST. */
150 if (stat (dir
, &stats
))
152 error (0, saved_errno
, _("cannot create directory %s"),
156 else if (!S_ISDIR (stats
.st_mode
))
158 error (0, 0, _("%s exists but is not a directory"), quote (dirpath
));
163 /* DIR (aka DIRPATH) already exists and is a directory. */
168 *created_dir_p
= created_dir
;
173 /* Ensure that the directory ARGPATH exists.
175 Create any leading directories that don't already exist, with
176 permissions PARENT_MODE.
177 If the last element of ARGPATH does not exist, create it as
178 a new directory with permissions MODE.
179 If OWNER and GROUP are non-negative, use them to set the UID and GID of
180 any created directories.
181 If VERBOSE_FMT_STRING is nonzero, use it as a printf format
182 string for printing a message after successfully making a directory,
183 with the name of the directory that was just made as an argument.
184 If PRESERVE_EXISTING is non-zero and ARGPATH is an existing directory,
185 then do not attempt to set its permissions and ownership.
187 Return 0 if ARGPATH exists as a directory with the proper
188 ownership and permissions when done, otherwise 1. */
191 make_path (const char *argpath
,
196 int preserve_existing
,
197 const char *verbose_fmt_string
)
202 if (stat (argpath
, &stats
))
205 int tmp_mode
; /* Initial perms for leading dirs. */
206 int re_protect
; /* Should leading dirs be unwritable? */
210 struct ptr_list
*next
;
212 struct ptr_list
*p
, *leading_dirs
= NULL
;
213 int do_chdir
; /* Whether to chdir before each mkdir. */
214 struct saved_cwd cwd
;
218 /* Temporarily relax umask in case it's overly restrictive. */
219 mode_t oldmask
= umask (0);
221 /* Make a copy of ARGPATH that we can scribble NULs on. */
222 dirpath
= (char *) alloca (strlen (argpath
) + 1);
223 strcpy (dirpath
, argpath
);
224 strip_trailing_slashes (dirpath
);
226 /* If leading directories shouldn't be writable or executable,
227 or should have set[ug]id or sticky bits set and we are setting
228 their owners, we need to fix their permissions after making them. */
229 if (((parent_mode
& WX_USR
) != WX_USR
)
230 || ((owner
!= (uid_t
) -1 || group
!= (gid_t
) -1)
231 && (parent_mode
& (S_ISUID
| S_ISGID
| S_ISVTX
)) != 0))
238 tmp_mode
= parent_mode
;
242 /* If we can record the current working directory, we may be able
243 to do the chdir optimization. */
244 do_chdir
= !save_cwd (&cwd
);
246 /* If we've saved the cwd and DIRPATH is an absolute pathname,
247 we must chdir to `/' in order to enable the chdir optimization.
248 So if chdir ("/") fails, turn off the optimization. */
249 if (do_chdir
&& *dirpath
== '/' && chdir ("/") < 0)
254 /* Skip over leading slashes. */
255 while (*slash
== '/')
260 int newly_created_dir
;
263 /* slash points to the leftmost unprocessed component of dirpath. */
264 basename_dir
= slash
;
266 slash
= strchr (slash
, '/');
270 /* If we're *not* doing chdir before each mkdir, then we have to refer
271 to the target using the full (multi-component) directory name. */
273 basename_dir
= dirpath
;
276 fail
= make_dir (basename_dir
, dirpath
, tmp_mode
, &newly_created_dir
);
283 if (newly_created_dir
)
285 if (verbose_fmt_string
)
286 error (0, 0, verbose_fmt_string
, quote (dirpath
));
288 if ((owner
!= (uid_t
) -1 || group
!= (gid_t
) -1)
289 && chown (basename_dir
, owner
, group
)
290 #if defined AFS && defined EPERM
295 error (0, errno
, _("cannot change owner and/or group of %s"),
303 struct ptr_list
*new = (struct ptr_list
*)
304 alloca (sizeof (struct ptr_list
));
305 new->dirname_end
= slash
;
306 new->next
= leading_dirs
;
311 /* If we were able to save the initial working directory,
312 then we can use chdir to change into each directory before
313 creating an entry in that directory. This avoids making
314 stat and mkdir process O(n^2) file name components. */
315 if (do_chdir
&& chdir (basename_dir
) < 0)
317 error (0, errno
, _("cannot chdir to directory %s"),
325 /* Avoid unnecessary calls to `stat' when given
326 pathnames containing multiple adjacent slashes. */
327 while (*slash
== '/')
332 basename_dir
= dirpath
;
334 /* Done creating leading directories. Restore original umask. */
337 /* We're done making leading directories.
338 Create the final component of the path. */
340 if (make_dir (basename_dir
, dirpath
, mode
, NULL
))
346 if (verbose_fmt_string
!= NULL
)
347 error (0, 0, verbose_fmt_string
, quote (dirpath
));
349 if (owner
!= (uid_t
) -1 || group
!= (gid_t
) -1)
351 if (chown (basename_dir
, owner
, group
)
357 error (0, errno
, _("cannot change owner and/or group of %s"),
363 /* The above chown may have turned off some permission bits in MODE.
364 Another reason we may have to use chmod here is that mkdir(2) is
365 required to honor only the file permission bits. In particular,
366 it need not honor the `special' bits, so if MODE includes any
367 special bits, set them here. */
368 if ((mode
& ~S_IRWXUGO
)
369 && chmod (basename_dir
, mode
))
371 error (0, errno
, _("cannot change permissions of %s"),
378 /* If the mode for leading directories didn't include owner "wx"
379 privileges, we have to reset their protections to the correct
381 for (p
= leading_dirs
; p
!= NULL
; p
= p
->next
)
383 *(p
->dirname_end
) = '\0';
384 if (chmod (dirpath
, parent_mode
))
386 error (0, errno
, _("cannot change permissions of %s"),
394 /* We get here if the entire path already exists. */
396 const char *dirpath
= argpath
;
398 if (!S_ISDIR (stats
.st_mode
))
400 error (0, 0, _("%s exists but is not a directory"), quote (dirpath
));
404 if (!preserve_existing
)
406 /* chown must precede chmod because on some systems,
407 chown clears the set[ug]id bits for non-superusers,
408 resulting in incorrect permissions.
409 On System V, users can give away files with chown and then not
410 be able to chmod them. So don't give files away. */
412 if ((owner
!= (uid_t
) -1 || group
!= (gid_t
) -1)
413 && chown (dirpath
, owner
, group
)
419 error (0, errno
, _("cannot change owner and/or group of %s"),
423 if (chmod (dirpath
, mode
))
425 error (0, errno
, _("cannot change permissions of %s"),