1 /* makepath.c -- Ensure that a directory path exists.
2 Copyright (C) 1990 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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> and
19 Jim Meyering <meyering@cs.utexas.edu>. */
26 #define alloca __builtin_alloca
40 #include <sys/types.h>
46 #ifdef STAT_MACROS_BROKEN
48 #endif /* STAT_MACROS_BROKEN. */
50 #if !defined(S_ISDIR) && defined(S_IFDIR)
51 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
84 /* Ensure that the directory ARGPATH exists.
85 Remove any trailing slashes from ARGPATH before calling this function.
87 Create any leading directories that don't already exist, with
88 permissions PARENT_MODE.
89 If the last element of ARGPATH does not exist, create it as
90 a new directory with permissions MODE.
91 If OWNER and GROUP are non-negative, use them to set the UID and GID of
92 any created directories.
93 If VERBOSE_FMT_STRING is nonzero, use it as a printf format
94 string for printing a message after successfully making a directory,
95 with the name of the directory that was just made as an argument.
96 If PRESERVE_EXISTING is non-zero and ARGPATH is an existing directory,
97 then do not attempt to set its permissions and ownership.
99 Return 0 if ARGPATH exists as a directory with the proper
100 ownership and permissions when done, otherwise 1. */
104 make_path (const char *argpath,
109 int preserve_existing,
110 const char *verbose_fmt_string)
113 make_path (argpath, mode, parent_mode, owner, group, preserve_existing,
120 int preserve_existing;
121 const char *verbose_fmt_string;
124 char *dirpath; /* A copy we can scribble NULs on. */
127 int oldmask = umask (0);
129 /* FIXME: move this alloca and strcpy into the if-block.
130 Set dirpath to argpath in the else-block. */
131 dirpath = (char *) alloca (strlen (argpath) + 1);
132 strcpy (dirpath, argpath);
134 if (stat (dirpath, &stats))
137 int tmp_mode; /* Initial perms for leading dirs. */
138 int re_protect; /* Should leading dirs be unwritable? */
142 struct ptr_list *next;
144 struct ptr_list *p, *leading_dirs = NULL;
146 /* If leading directories shouldn't be writable or executable,
147 or should have set[ug]id or sticky bits set and we are setting
148 their owners, we need to fix their permissions after making them. */
149 if (((parent_mode & 0300) != 0300)
150 || (owner != (uid_t) -1 && group != (gid_t) -1
151 && (parent_mode & 07000) != 0))
158 tmp_mode = parent_mode;
163 while (*slash == '/')
165 while ((slash = strchr (slash, '/')))
168 if (stat (dirpath, &stats))
170 if (mkdir (dirpath, tmp_mode))
172 error (0, errno, "cannot create directory `%s'", dirpath);
178 if (verbose_fmt_string != NULL)
179 error (0, 0, verbose_fmt_string, dirpath);
181 if (owner != (uid_t) -1 && group != (gid_t) -1
182 && chown (dirpath, owner, group)
183 #if defined(AFS) && defined (EPERM)
188 error (0, errno, "%s", dirpath);
193 struct ptr_list *new = (struct ptr_list *)
194 alloca (sizeof (struct ptr_list));
195 new->dirname_end = slash;
196 new->next = leading_dirs;
201 else if (!S_ISDIR (stats.st_mode))
203 error (0, 0, "`%s' exists but is not a directory", dirpath);
210 /* Avoid unnecessary calls to `stat' when given
211 pathnames containing multiple adjacent slashes. */
212 while (*slash == '/')
216 /* We're done making leading directories.
217 Create the final component of the path. */
219 /* The path could end in "/." or contain "/..", so test
220 if we really have to create the directory. */
222 if (stat (dirpath, &stats) && mkdir (dirpath, mode))
224 error (0, errno, "cannot create directory `%s'", dirpath);
228 if (verbose_fmt_string != NULL)
229 error (0, 0, verbose_fmt_string, dirpath);
231 if (owner != (uid_t) -1 && group != (gid_t) -1)
233 if (chown (dirpath, owner, group)
239 error (0, errno, "%s", dirpath);
242 /* chown may have turned off some permission bits we wanted. */
243 if ((mode & 07000) != 0 && chmod (dirpath, mode))
245 error (0, errno, "%s", dirpath);
250 /* If the mode for leading directories didn't include owner "wx"
251 privileges, we have to reset their protections to the correct
253 for (p = leading_dirs; p != NULL; p = p->next)
255 *(p->dirname_end) = '\0';
256 if (chmod (dirpath, parent_mode))
258 error (0, errno, "%s", dirpath);
265 /* We get here if the entire path already exists. */
267 if (!S_ISDIR (stats.st_mode))
269 error (0, 0, "`%s' exists but is not a directory", dirpath);
274 if (!preserve_existing)
276 /* chown must precede chmod because on some systems,
277 chown clears the set[ug]id bits for non-superusers,
278 resulting in incorrect permissions.
279 On System V, users can give away files with chown and then not
280 be able to chmod them. So don't give files away. */
282 if (owner != (uid_t) -1 && group != (gid_t) -1
283 && chown (dirpath, owner, group)
289 error (0, errno, "%s", dirpath);
292 if (chmod (dirpath, mode))
294 error (0, errno, "%s", dirpath);