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>. */
22 #define alloca __builtin_alloca
36 #include <sys/types.h>
41 #if !defined(S_ISDIR) && defined(S_IFDIR)
42 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
49 #if defined (STDC_HEADERS) || defined (HAVE_ERRNO_H)
55 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
71 /* Ensure that the directory ARGPATH exists.
72 Remove any trailing slashes from ARGPATH before calling this function.
74 Make any leading directories that don't already exist, with
75 permissions PARENT_MODE.
76 If the last element of ARGPATH does not exist, create it as
77 a new directory with permissions MODE.
78 If OWNER and GROUP are non-negative, make them the UID and GID of
80 If VERBOSE_FMT_STRING is nonzero, use it as a printf format
81 string for printing a message after successfully making a directory,
82 with the name of the directory that was just made as an argument.
84 Return 0 if ARGPATH exists as a directory with the proper
85 ownership and permissions when done, otherwise 1. */
88 make_path (argpath, mode, parent_mode, owner, group, verbose_fmt_string)
94 char *verbose_fmt_string;
96 char *dirpath; /* A copy we can scribble NULs on. */
99 int oldmask = umask (0);
101 dirpath = (char *) alloca (strlen (argpath) + 1);
102 strcpy (dirpath, argpath);
104 if (stat (dirpath, &stats))
107 int tmp_mode; /* Initial perms for leading dirs. */
108 int re_protect; /* Should leading dirs be unwritable? */
112 struct ptr_list *next;
114 struct ptr_list *p, *leading_dirs = NULL;
116 /* If leading directories shouldn't be writable or executable,
117 or should have set[ug]id or sticky bits set and we are setting
118 their owners, we need to fix their permissions after making them. */
119 if (((parent_mode & 0300) != 0300)
120 || (owner != (uid_t) -1 && group != (gid_t) -1
121 && (parent_mode & 07000) != 0))
128 tmp_mode = parent_mode;
133 while (*slash == '/')
135 while ((slash = index (slash, '/')))
138 if (stat (dirpath, &stats))
140 if (mkdir (dirpath, tmp_mode))
142 error (0, errno, "cannot make directory `%s'", dirpath);
148 if (verbose_fmt_string != NULL)
149 error (0, 0, verbose_fmt_string, dirpath);
151 if (owner != (uid_t) -1 && group != (gid_t) -1
152 && chown (dirpath, owner, group)
153 #if defined(AFS) && defined (EPERM)
158 error (0, errno, "%s", dirpath);
163 struct ptr_list *new = (struct ptr_list *)
164 alloca (sizeof (struct ptr_list));
165 new->dirname_end = slash;
166 new->next = leading_dirs;
171 else if (!S_ISDIR (stats.st_mode))
173 error (0, 0, "`%s' exists but is not a directory", dirpath);
180 /* Avoid unnecessary calls to `stat' when given
181 pathnames containing multiple adjacent slashes. */
182 while (*slash == '/')
186 /* We're done making leading directories.
187 Make the final component of the path. */
189 if (mkdir (dirpath, mode))
191 error (0, errno, "cannot make directory `%s'", dirpath);
195 if (verbose_fmt_string != NULL)
196 error (0, 0, verbose_fmt_string, dirpath);
198 if (owner != (uid_t) -1 && group != (gid_t) -1)
200 if (chown (dirpath, owner, group)
206 error (0, errno, "%s", dirpath);
209 /* chown may have turned off some permission bits we wanted. */
210 if ((mode & 07000) != 0 && chmod (dirpath, mode))
212 error (0, errno, "%s", dirpath);
217 /* If the mode for leading directories didn't include owner "wx"
218 privileges, we have to reset their protections to the correct
220 for (p = leading_dirs; p != NULL; p = p->next)
222 *(p->dirname_end) = '\0';
223 if (chmod (dirpath, parent_mode))
225 error (0, errno, "%s", dirpath);
232 /* We get here if the entire path already exists. */
234 if (!S_ISDIR (stats.st_mode))
236 error (0, 0, "`%s' exists but is not a directory", dirpath);
241 /* chown must precede chmod because on some systems,
242 chown clears the set[ug]id bits for non-superusers,
243 resulting in incorrect permissions.
244 On System V, users can give away files with chown and then not
245 be able to chmod them. So don't give files away. */
247 if (owner != (uid_t) -1 && group != (gid_t) -1
248 && chown (dirpath, owner, group)
254 error (0, errno, "%s", dirpath);
257 if (chmod (dirpath, mode))
259 error (0, errno, "%s", dirpath);