1 /* mkdir-p.c -- Ensure that a directory and its parents exist.
3 Copyright (C) 1990, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006
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 Paul Eggert, David MacKenzie, and Jim Meyering. */
32 #define _(msgid) gettext (msgid)
34 #include "dirchownmod.c"
37 #include "mkancesdirs.h"
38 #include "stat-macros.h"
40 /* Ensure that the directory DIR exists.
42 If MAKE_ANCESTOR is not null, create any ancestor directories that
43 don't already exist, by invoking MAKE_ANCESTOR (ANCESTOR, OPTIONS).
44 This function should return zero if successful, -1 (setting errno)
45 otherwise. In this case, DIR may be modified by storing '\0' bytes
46 into it, to access the ancestor directories, and this modification
47 is retained on return if the ancestor directories could not be
50 Create DIR as a new directory with using mkdir with permissions
51 MODE. It is also OK if MAKE_ANCESTOR_DIR is not null and a
52 directory DIR already exists.
54 Call ANNOUNCE (DIR, OPTIONS) just after successfully making DIR,
55 even if some of the following actions fail.
57 Set DIR's owner to OWNER and group to GROUP, but leave the owner
58 alone if OWNER is (uid_t) -1, and similarly for GROUP.
60 Set DIR's mode bits to MODE, except preserve any of the bits that
61 correspond to zero bits in MODE_BITS. In other words, MODE_BITS is
62 a mask that specifies which of DIR's mode bits should be set or
63 cleared. MODE should be a subset of MODE_BITS, which in turn
64 should be a subset of CHMOD_MODE_BITS. Changing the mode in this
65 way is necessary if DIR already existed or if MODE and MODE_BITS
66 specify non-permissions bits like S_ISUID.
68 However, if PRESERVE_EXISTING is true and DIR already exists,
69 do not attempt to set DIR's ownership and file mode bits.
71 This implementation assumes the current umask is zero.
73 Return true if DIR exists as a directory with the proper ownership
74 and file mode bits when done. Report a diagnostic and return false
75 on failure, storing '\0' into *DIR if an ancestor directory had
79 make_dir_parents (char *dir,
80 int (*make_ancestor) (char const *, void *),
83 void (*announce) (char const *, void *),
87 bool preserve_existing)
89 bool made_dir = (mkdir (dir, mode) == 0);
91 if (!made_dir && make_ancestor && errno == ENOENT)
93 if (mkancesdirs (dir, make_ancestor, options) == 0)
94 made_dir = (mkdir (dir, mode) == 0);
97 /* mkancestdirs updated DIR for a better-looking
98 diagnostic, so don't try to stat DIR below. */
105 announce (dir, options);
107 (owner == (uid_t) -1 && group == (gid_t) -1
108 && ! ((mode_bits & (S_ISUID | S_ISGID)) | (mode & S_ISVTX)));
112 int mkdir_errno = errno;
114 if (! (make_ancestor && mkdir_errno != ENOENT
115 && stat (dir, &st) == 0 && S_ISDIR (st.st_mode)))
117 error (0, mkdir_errno, _("cannot create directory %s"), quote (dir));
122 if (! preserve_existing
123 && (dirchownmod (dir, (made_dir ? mode : (mode_t) -1),
124 owner, group, mode, mode_bits)
128 _(owner == (uid_t) -1 && group == (gid_t) -1
129 ? "cannot change permissions of %s"
130 : "cannot change owner and permissions of %s"),