1 /* mkdir-p.c -- Ensure that a directory and its parents exist.
3 Copyright (C) 1990, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005
4 Free 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> and Jim Meyering. */
31 #include <sys/types.h>
40 #define _(msgid) gettext (msgid)
46 #include "stat-macros.h"
49 # define ENOSYS EEXIST
52 #define WX_USR (S_IWUSR | S_IXUSR)
54 /* Ensure that the directory ARG exists.
56 Create any leading directories that don't already exist, with
57 permissions PARENT_MODE.
58 If the last element of ARG does not exist, create it as
59 a new directory with permissions MODE.
60 If OWNER and GROUP are non-negative, use them to set the UID and GID of
61 any created directories.
62 If VERBOSE_FMT_STRING is nonzero, use it as a printf format
63 string for printing a message after successfully making a directory,
64 with the name of the directory that was just made as an argument.
65 If PRESERVE_EXISTING is true and ARG is an existing directory,
66 then do not attempt to set its permissions and ownership.
68 Set *CWD_ERRNO to a (nonzero) error number if this
69 function has changed the current working directory and is unable to
70 restore it to its initial state. Do not change
73 Return true iff ARG exists as a directory with the proper ownership
74 and permissions when done. Note that this function returns true
75 even when it fails to return to the initial working directory. */
78 make_dir_parents (char const *arg,
83 bool preserve_existing,
84 char const *verbose_fmt_string,
89 bool do_chdir = false; /* Whether to chdir before each mkdir. */
91 bool cwd_problem = false;
92 char const *fixup_permissions_dir = NULL;
93 char const *full_dir = arg;
98 struct ptr_list *next;
100 struct ptr_list *leading_dirs = NULL;
102 if (stat (arg, &stats) == 0)
104 if (! S_ISDIR (stats.st_mode))
106 error (0, 0, _("%s exists but is not a directory"), quote (arg));
110 if (!preserve_existing)
111 fixup_permissions_dir = arg;
113 else if (errno != ENOENT || !*arg)
115 error (0, errno, "%s", quote (arg));
121 mode_t tmp_mode; /* Initial perms for leading dirs. */
122 bool re_protect; /* Should leading dirs be unwritable? */
126 /* Temporarily relax umask in case it's overly restrictive. */
127 mode_t oldmask = umask (0);
129 /* Make a copy of ARG that we can scribble NULs on. */
130 dir = (char *) alloca (strlen (arg) + 1);
132 strip_trailing_slashes (dir);
135 /* If leading directories shouldn't be writable or executable,
136 or should have set[ug]id or sticky bits set and we are setting
137 their owners, we need to fix their permissions after making them. */
138 if (((parent_mode & WX_USR) != WX_USR)
139 || ((owner != (uid_t) -1 || group != (gid_t) -1)
140 && (parent_mode & (S_ISUID | S_ISGID | S_ISVTX)) != 0))
147 tmp_mode = parent_mode;
151 /* If we can record the current working directory, we may be able
152 to do the chdir optimization. */
153 do_chdir = (save_cwd (&cwd) == 0);
155 /* If we've saved the cwd and DIR is an absolute file name,
156 we must chdir to `/' in order to enable the chdir optimization.
157 So if chdir ("/") fails, turn off the optimization. */
158 if (do_chdir && dir[0] == '/')
160 /* POSIX says "//" might be special, so chdir to "//" if the
161 file name starts with exactly two slashes. */
162 char const *root = "//" + (dir[1] != '/' || dir[2] == '/');
163 if (chdir (root) != 0)
172 /* Skip over leading slashes. */
173 while (*slash == '/')
178 /* slash points to the leftmost unprocessed component of dir. */
179 basename_dir = slash;
181 slash = strchr (slash, '/');
185 /* If we're *not* doing chdir before each mkdir, then we have to refer
186 to the target using the full (multi-component) directory name. */
191 if (mkdir (basename_dir, tmp_mode) == 0)
193 if (verbose_fmt_string)
194 error (0, 0, verbose_fmt_string, quote (dir));
196 if ((owner != (uid_t) -1 || group != (gid_t) -1)
197 && chown (basename_dir, owner, group)
198 #if defined AFS && defined EPERM
203 error (0, errno, _("cannot change owner and/or group of %s"),
211 struct ptr_list *new = (struct ptr_list *)
212 alloca (sizeof *new);
213 new->dirname_end = slash;
214 new->next = leading_dirs;
218 else if (errno == EEXIST || errno == ENOSYS)
220 /* A file is already there. Perhaps it is a directory.
221 If not, it will be diagnosed later.
223 The ENOSYS is for Solaris 8 NFS clients, which can
224 fail with errno == ENOSYS if mkdir is invoked on an
229 error (0, errno, _("cannot create directory %s"), quote (dir));
234 /* If we were able to save the initial working directory,
235 then we can use chdir to change into each directory before
236 creating an entry in that directory. This avoids making
237 mkdir process O(n^2) file name components. */
238 if (do_chdir && chdir (basename_dir) < 0)
240 error (0, errno, _("cannot chdir to directory %s"),
248 /* Avoid unnecessary calls to mkdir when given
249 file names containing multiple adjacent slashes. */
250 while (*slash == '/')
257 /* Done creating leading directories. Restore original umask. */
260 /* We're done making leading directories.
261 Create the final component of the file name. */
264 if (mkdir (basename_dir, mode) != 0)
266 error (0, errno, _("cannot create directory %s"), quote (dir));
271 if (verbose_fmt_string)
272 error (0, 0, verbose_fmt_string, quote (dir));
273 fixup_permissions_dir = basename_dir;
278 if (fixup_permissions_dir)
280 /* chown must precede chmod because on some systems,
281 chown clears the set[ug]id bits for non-superusers,
282 resulting in incorrect permissions.
283 On System V, users can give away files with chown and then not
284 be able to chmod them. So don't give files away. */
286 if (owner != (uid_t) -1 || group != (gid_t) -1)
288 if (chown (fixup_permissions_dir, owner, group) != 0
294 error (0, errno, _("cannot change owner and/or group of %s"),
300 /* The above chown may have turned off some permission bits in MODE.
301 Another reason we may have to use chmod here is that mkdir(2) is
302 required to honor only the file permission bits. In particular,
303 it need not honor the `special' bits, so if MODE includes any
304 special bits, set them here. */
305 if ((mode & ~S_IRWXUGO) && chmod (fixup_permissions_dir, mode) != 0)
307 error (0, errno, _("cannot change permissions of %s"),
315 if (restore_cwd (&cwd) != 0)
323 /* If the mode for leading directories didn't include owner "wx"
324 privileges, reset their protections to the correct value. */
325 for (; leading_dirs != NULL; leading_dirs = leading_dirs->next)
327 leading_dirs->dirname_end[0] = '\0';
328 if ((cwd_problem && *full_dir != '/')
329 || chmod (full_dir, parent_mode) != 0)
331 error (0, (cwd_problem ? 0 : errno),
332 _("cannot change permissions of %s"), quote (full_dir));