* _fpending.c: Include <config.h> unconditionally, since we no
[gnulib.git] / lib / mkdir-p.c
1 /* mkdir-p.c -- Ensure that a directory and its parents exist.
2
3    Copyright (C) 1990, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006
4    Free Software Foundation, Inc.
5
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)
9    any later version.
10
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.
15
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.  */
19
20 /* Written by Paul Eggert, David MacKenzie, and Jim Meyering.  */
21
22 #include <config.h>
23
24 #include "mkdir-p.h"
25
26 #include <errno.h>
27 #include <sys/stat.h>
28
29 #include "gettext.h"
30 #define _(msgid) gettext (msgid)
31
32 #include "dirchownmod.c"
33 #include "error.h"
34 #include "quote.h"
35 #include "mkancesdirs.h"
36 #include "stat-macros.h"
37
38 /* Ensure that the directory DIR exists.
39
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
46    created.
47
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.
51
52    Call ANNOUNCE (DIR, OPTIONS) just after successfully making DIR,
53    even if some of the following actions fail.
54
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.
57
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.
65
66    However, if PRESERVE_EXISTING is true and DIR already exists,
67    do not attempt to set DIR's ownership and file mode bits.
68
69    This implementation assumes the current umask is zero.
70
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
74    problems.  */
75
76 bool
77 make_dir_parents (char *dir,
78                   int (*make_ancestor) (char const *, void *),
79                   void *options,
80                   mode_t mode,
81                   void (*announce) (char const *, void *),
82                   mode_t mode_bits,
83                   uid_t owner,
84                   gid_t group,
85                   bool preserve_existing)
86 {
87   bool made_dir = (mkdir (dir, mode) == 0);
88
89   if (!made_dir && make_ancestor && errno == ENOENT)
90     {
91       if (mkancesdirs (dir, make_ancestor, options) == 0)
92         made_dir = (mkdir (dir, mode) == 0);
93       else
94         {
95           /* mkancestdirs updated DIR for a better-looking
96              diagnostic, so don't try to stat DIR below.  */
97           make_ancestor = NULL;
98         }
99     }
100
101   if (made_dir)
102     {
103       announce (dir, options);
104       preserve_existing =
105         (owner == (uid_t) -1 && group == (gid_t) -1
106          && ! ((mode_bits & (S_ISUID | S_ISGID)) | (mode & S_ISVTX)));
107     }
108   else
109     {
110       int mkdir_errno = errno;
111       struct stat st;
112       if (! (make_ancestor && mkdir_errno != ENOENT
113              && stat (dir, &st) == 0 && S_ISDIR (st.st_mode)))
114         {
115           error (0, mkdir_errno, _("cannot create directory %s"), quote (dir));
116           return false;
117         }
118     }
119
120   if (! preserve_existing
121       && (dirchownmod (dir, (made_dir ? mode : (mode_t) -1),
122                        owner, group, mode, mode_bits)
123           != 0))
124     {
125       error (0, errno,
126              _(owner == (uid_t) -1 && group == (gid_t) -1
127                ? "cannot change permissions of %s"
128                : "cannot change owner and permissions of %s"),
129              quote (dir));
130       return false;
131     }
132
133   return true;
134 }