1 /* makepath.c -- Ensure that a directory path exists.
3 Copyright (C) 1990, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> and Jim Meyering. */
22 * Modified by Kern Sibbald for use in Bacula, December 2000
32 #if !defined(S_ISDIR) && defined(S_IFDIR)
33 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
37 # define S_IRWXUGO (S_IRWXU | S_IRWXG | S_IRWXO)
42 # define S_ISUID 04000
45 # define S_ISGID 02000
48 # define S_ISVTX 01000
61 # define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
64 #define WX_USR (S_IWUSR | S_IXUSR)
66 #define quote(path) path
68 extern void strip_trailing_slashes();
71 cleanup(struct saved_cwd *cwd)
74 int _fail = restore_cwd(cwd, NULL, NULL);
84 /* Attempt to create directory DIR (aka DIRPATH) with the specified MODE.
85 If CREATED_DIR_P is non-NULL, set *CREATED_DIR_P to non-zero if this
86 function creates DIR and to zero otherwise. Give a diagnostic and
87 return non-zero if DIR cannot be created or cannot be determined to
88 exist already. Use DIRPATH in any diagnostic, not DIR.
89 Note that if DIR already exists, this function will return zero
90 (indicating success) and will set *CREATED_DIR_P to zero. */
93 make_dir(JCR *jcr, const char *dir, const char *dirpath, mode_t mode, int *created_dir_p)
99 Dmsg2(300, "make_dir mode=%o dir=%s\n", mode, dir);
100 created_dir = (mkdir(dir, mode) == 0);
106 /* The mkdir and stat calls below may appear to be reversed.
107 They are not. It is important to call mkdir first and then to
108 call stat (to distinguish the three cases) only if mkdir fails.
109 The alternative to this approach is to `stat' each directory,
110 then to call mkdir if it doesn't exist. But if some other process
111 were to create the directory between the stat & mkdir, the mkdir
112 would fail with EEXIST. */
114 if (stat(dir, &stats)) {
116 be.set_errno(save_errno);
117 Jmsg(jcr, M_ERROR, 0, _("Cannot create directory %s: ERR=%s\n"),
118 dirpath, be.strerror());
120 } else if (!S_ISDIR(stats.st_mode)) {
121 Jmsg(jcr, M_ERROR, 0, _("%s exists but is not a directory\n"), quote(dirpath));
124 /* DIR (aka DIRPATH) already exists and is a directory. */
129 *created_dir_p = created_dir;
135 /* return non-zero if path is absolute or zero if relative. */
137 isAbsolute(const char *path)
139 #if defined(HAVE_WIN32)
140 return path[1] == ':' || *path == '/' || *path == '\\'; /* drivespec:/blah is absolute */
146 /* Ensure that the directory ARGPATH exists.
147 Remove any trailing slashes from ARGPATH before calling this function.
149 Create any leading directories that don't already exist, with
150 permissions PARENT_MODE.
152 If the last element of ARGPATH does not exist, create it as
153 a new directory with permissions MODE.
155 If OWNER and GROUP are non-negative, use them to set the UID and GID of
156 any created directories.
158 If VERBOSE_FMT_STRING is nonzero, use it as a printf format
159 string for printing a message after successfully making a directory,
160 with the name of the directory that was just made as an argument.
162 If PRESERVE_EXISTING is non-zero and ARGPATH is an existing directory,
163 then do not attempt to set its permissions and ownership.
165 Return 0 if ARGPATH exists as a directory with the proper
166 ownership and permissions when done, otherwise 1. */
176 int preserve_existing,
177 char *verbose_fmt_string)
182 if (stat(argpath, &stats)) {
184 int tmp_mode; /* Initial perms for leading dirs. */
185 int re_protect; /* Should leading dirs be unwritable? */
188 struct ptr_list *next;
190 struct ptr_list *p, *leading_dirs = NULL;
191 struct saved_cwd cwd;
195 /* Temporarily relax umask in case it's overly restrictive. */
196 mode_t oldmask = umask(0);
198 /* Make a copy of ARGPATH that we can scribble NULs on. */
199 dirpath = (char *)alloca(strlen(argpath) + 1);
200 strcpy (dirpath, argpath);
201 strip_trailing_slashes(dirpath);
203 /* If leading directories shouldn't be writable or executable,
204 or should have set[ug]id or sticky bits set and we are setting
205 their owners, we need to fix their permissions after making them. */
206 if (((parent_mode & WX_USR) != WX_USR)
207 || ((owner != (uid_t)-1 || group != (gid_t)-1)
208 && (parent_mode & (S_ISUID | S_ISGID | S_ISVTX)) != 0)) {
212 tmp_mode = parent_mode;
216 #if defined(HAVE_WIN32)
217 // chdir can fail if permissions are sufficiently restricted since I don't think
218 // backup/restore security rights affect ChangeWorkingDirectory
221 /* If we can record the current working directory, we may be able
222 to do the chdir optimization. */
223 cwd.do_chdir = !save_cwd(&cwd);
225 /* If we've saved the cwd and DIRPATH is an absolute pathname,
226 we must chdir to `/' in order to enable the chdir optimization.
227 So if chdir ("/") fails, turn off the optimization. */
228 if (cwd.do_chdir && isAbsolute(dirpath) && (chdir("/") < 0)) {
234 /* Skip over leading slashes. */
235 while (*slash == '/')
239 int newly_created_dir;
242 /* slash points to the leftmost unprocessed component of dirpath. */
243 basename_dir = slash;
245 slash = strchr (slash, '/');
250 /* If we're *not* doing chdir before each mkdir, then we have to refer
251 to the target using the full (multi-component) directory name. */
253 basename_dir = dirpath;
257 fail = make_dir(jcr, basename_dir, dirpath, tmp_mode, &newly_created_dir);
264 if (newly_created_dir) {
265 Dmsg0(300, "newly_created_dir\n");
267 if ((owner != (uid_t)-1 || group != (gid_t)-1)
268 && chown(basename_dir, owner, group)
269 #if defined(AFS) && defined (EPERM)
273 /* Note, if we are restoring as NON-root, this may not be fatal */
275 Jmsg(jcr, M_ERROR, 0, _("Cannot change owner and/or group of %s: ERR=%s\n"),
276 quote(dirpath), be.strerror());
278 Dmsg0(300, "Chown done.\n");
281 struct ptr_list *pnew = (struct ptr_list *)
282 alloca(sizeof (struct ptr_list));
283 pnew->dirname_end = slash;
284 pnew->next = leading_dirs;
286 Dmsg0(300, "re_protect\n");
290 /* If we were able to save the initial working directory,
291 then we can use chdir to change into each directory before
292 creating an entry in that directory. This avoids making
293 stat and mkdir process O(n^2) file name components. */
294 if (cwd.do_chdir && chdir(basename_dir) < 0) {
296 Jmsg(jcr, M_ERROR, 0, _("Cannot chdir to directory, %s: ERR=%s\n"),
297 quote(dirpath), be.strerror());
305 /* Avoid unnecessary calls to `stat' when given
306 pathnames containing multiple adjacent slashes. */
307 while (*slash == '/')
309 } /* end while (1) */
312 basename_dir = dirpath;
315 /* We're done making leading directories.
316 Create the final component of the path. */
318 Dmsg1(300, "Create final component. mode=%o\n", mode);
319 if (make_dir(jcr, basename_dir, dirpath, mode, NULL)) {
325 /* Done creating directories. Restore original umask. */
328 if (owner != (uid_t)-1 || group != (gid_t)-1) {
329 if (chown(basename_dir, owner, group)
336 Jmsg(jcr, M_WARNING, 0, _("Cannot change owner and/or group of %s: ERR=%s\n"),
337 quote(dirpath), be.strerror());
341 /* The above chown may have turned off some permission bits in MODE.
342 Another reason we may have to use chmod here is that mkdir(2) is
343 required to honor only the file permission bits. In particular,
344 it need not honor the `special' bits, so if MODE includes any
345 special bits, set them here. */
346 if (mode & ~S_IRWXUGO) {
347 Dmsg1(300, "Final chmod mode=%o\n", mode);
349 if ((mode & ~S_IRWXUGO) && chmod(basename_dir, mode)) {
351 Jmsg(jcr, M_WARNING, 0, _("Cannot change permissions of %s: ERR=%s\n"),
352 quote(dirpath), be.strerror());
359 /* If the mode for leading directories didn't include owner "wx"
360 privileges, we have to reset their protections to the correct
362 for (p = leading_dirs; p != NULL; p = p->next) {
363 *(p->dirname_end) = '\0';
364 Dmsg2(300, "Reset parent mode=%o dir=%s\n", parent_mode, dirpath);
365 if (chmod(dirpath, parent_mode)) {
367 Jmsg(jcr, M_WARNING, 0, _("Cannot change permissions of %s: ERR=%s\n"),
368 quote(dirpath), be.strerror());
372 /* We get here if the entire path already exists. */
374 const char *dirpath = argpath;
376 if (!S_ISDIR(stats.st_mode)) {
377 Jmsg(jcr, M_ERROR, 0, _("%s exists but is not a directory\n"), quote(dirpath));
381 if (!preserve_existing) {
382 Dmsg0(300, "Do not preserve existing.\n");
383 /* chown must precede chmod because on some systems,
384 chown clears the set[ug]id bits for non-superusers,
385 resulting in incorrect permissions.
386 On System V, users can give away files with chown and then not
387 be able to chmod them. So don't give files away. */
389 if ((owner != (uid_t)-1 || group != (gid_t)-1)
390 && chown(dirpath, owner, group)
396 Jmsg(jcr, M_WARNING, 0, _("Cannot change owner and/or group of %s: ERR=%s\n"),
397 quote(dirpath), be.strerror());
399 if (chmod(dirpath, mode)) {
401 Jmsg(jcr, M_WARNING, 0, _("Cannot change permissions of %s: ERR=%s\n"),
402 quote(dirpath), be.strerror());
404 Dmsg2(300, "pathexists chmod mode=%o dir=%s\n", mode, dirpath);