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. */
30 #define _(msgid) gettext (msgid)
32 #include "dirchownmod.c"
35 #include "mkancesdirs.h"
36 #include "stat-macros.h"
38 /* Ensure that the directory DIR exists.
40 If MAKE_ANCESTOR is not null, create any ancestor directories that
41 don't already exist, by invoking MAKE_ANCESTOR (ANCESTOR, OPTIONS).
42 This function should return zero if successful, -1 (setting errno)
43 otherwise. In this case, DIR may be modified by storing '\0' bytes
44 into it, to access the ancestor directories, and this modification
45 is retained on return if the ancestor directories could not be
48 Create DIR as a new directory with using mkdir with permissions
49 MODE. It is also OK if MAKE_ANCESTOR_DIR is not null and a
50 directory DIR already exists.
52 Call ANNOUNCE (DIR, OPTIONS) just after successfully making DIR,
53 even if some of the following actions fail.
55 Set DIR's owner to OWNER and group to GROUP, but leave the owner
56 alone if OWNER is (uid_t) -1, and similarly for GROUP.
58 Set DIR's mode bits to MODE, except preserve any of the bits that
59 correspond to zero bits in MODE_BITS. In other words, MODE_BITS is
60 a mask that specifies which of DIR's mode bits should be set or
61 cleared. MODE should be a subset of MODE_BITS, which in turn
62 should be a subset of CHMOD_MODE_BITS. Changing the mode in this
63 way is necessary if DIR already existed or if MODE and MODE_BITS
64 specify non-permissions bits like S_ISUID.
66 However, if PRESERVE_EXISTING is true and DIR already exists,
67 do not attempt to set DIR's ownership and file mode bits.
69 This implementation assumes the current umask is zero.
71 Return true if DIR exists as a directory with the proper ownership
72 and file mode bits when done. Report a diagnostic and return false
73 on failure, storing '\0' into *DIR if an ancestor directory had
77 make_dir_parents (char *dir,
78 int (*make_ancestor) (char const *, void *),
81 void (*announce) (char const *, void *),
85 bool preserve_existing)
87 bool made_dir = (mkdir (dir, mode) == 0);
89 if (!made_dir && make_ancestor && errno == ENOENT)
91 if (mkancesdirs (dir, make_ancestor, options) == 0)
92 made_dir = (mkdir (dir, mode) == 0);
95 /* mkancestdirs updated DIR for a better-looking
96 diagnostic, so don't try to stat DIR below. */
103 announce (dir, options);
105 (owner == (uid_t) -1 && group == (gid_t) -1
106 && ! ((mode_bits & (S_ISUID | S_ISGID)) | (mode & S_ISVTX)));
110 int mkdir_errno = errno;
112 if (! (make_ancestor && mkdir_errno != ENOENT
113 && stat (dir, &st) == 0 && S_ISDIR (st.st_mode)))
115 error (0, mkdir_errno, _("cannot create directory %s"), quote (dir));
120 if (! preserve_existing
121 && (dirchownmod (dir, (made_dir ? mode : (mode_t) -1),
122 owner, group, mode, mode_bits)
126 _(owner == (uid_t) -1 && group == (gid_t) -1
127 ? "cannot change permissions of %s"
128 : "cannot change owner and permissions of %s"),