* lib/mkdir-p.c (HAVE_FCHMOD): Define to false if not already
[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 #include <unistd.h>
29
30 #include "gettext.h"
31 #define _(msgid) gettext (msgid)
32
33 #include "dirchownmod.h"
34 #include "dirname.h"
35 #include "error.h"
36 #include "quote.h"
37 #include "mkancesdirs.h"
38 #include "savewd.h"
39 #include "stat-macros.h"
40
41 #ifndef HAVE_FCHMOD
42 # define HAVE_FCHMOD false
43 #endif
44
45 /* Ensure that the directory DIR exists.
46
47    WD is the working directory, as in savewd.c.
48
49    If MAKE_ANCESTOR is not null, create any ancestor directories that
50    don't already exist, by invoking MAKE_ANCESTOR (DIR, ANCESTOR, OPTIONS).
51    This function should return zero if successful, -1 (setting errno)
52    otherwise.  In this case, DIR may be modified by storing '\0' bytes
53    into it, to access the ancestor directories, and this modification
54    is retained on return if the ancestor directories could not be
55    created.
56
57    Create DIR as a new directory with using mkdir with permissions
58    MODE.  It is also OK if MAKE_ANCESTOR is not null and a
59    directory DIR already exists.
60
61    Call ANNOUNCE (DIR, OPTIONS) just after successfully making DIR,
62    even if some of the following actions fail.
63
64    Set DIR's owner to OWNER and group to GROUP, but leave the owner
65    alone if OWNER is (uid_t) -1, and similarly for GROUP.
66
67    Set DIR's mode bits to MODE, except preserve any of the bits that
68    correspond to zero bits in MODE_BITS.  In other words, MODE_BITS is
69    a mask that specifies which of DIR's mode bits should be set or
70    cleared.  MODE should be a subset of MODE_BITS, which in turn
71    should be a subset of CHMOD_MODE_BITS.  Changing the mode in this
72    way is necessary if DIR already existed or if MODE and MODE_BITS
73    specify non-permissions bits like S_ISUID.
74
75    However, if PRESERVE_EXISTING is true and DIR already exists,
76    do not attempt to set DIR's ownership and file mode bits.
77
78    This implementation assumes the current umask is zero.
79
80    Return true if DIR exists as a directory with the proper ownership
81    and file mode bits when done, or if a child process has been
82    dispatched to do the real work (though the child process may not
83    have finished yet -- it is the caller's responsibility to handle
84    this).  Report a diagnostic and return false on failure, storing
85    '\0' into *DIR if an ancestor directory had problems.  */
86
87 bool
88 make_dir_parents (char *dir,
89                   struct savewd *wd,
90                   int (*make_ancestor) (char const *, char const *, void *),
91                   void *options,
92                   mode_t mode,
93                   void (*announce) (char const *, void *),
94                   mode_t mode_bits,
95                   uid_t owner,
96                   gid_t group,
97                   bool preserve_existing)
98 {
99   int mkdir_errno = (IS_ABSOLUTE_FILE_NAME (dir) ? 0 : savewd_errno (wd));
100
101   if (mkdir_errno == 0)
102     {
103       ptrdiff_t prefix_len = 0;
104       int savewd_chdir_options = (HAVE_FCHMOD ? SAVEWD_CHDIR_SKIP_READABLE : 0);
105
106       if (make_ancestor)
107         {
108           prefix_len = mkancesdirs (dir, wd, make_ancestor, options);
109           if (prefix_len < 0)
110             {
111               if (prefix_len < -1)
112                 return true;
113               mkdir_errno = errno;
114             }
115         }
116
117       if (0 <= prefix_len)
118         {
119           if (mkdir (dir + prefix_len, mode) == 0)
120             {
121               announce (dir, options);
122               preserve_existing =
123                 (owner == (uid_t) -1 && group == (gid_t) -1
124                  && ! ((mode_bits & (S_ISUID | S_ISGID)) | (mode & S_ISVTX)));
125               savewd_chdir_options |=
126                 (SAVEWD_CHDIR_NOFOLLOW
127                  | (mode & S_IRUSR ? SAVEWD_CHDIR_READABLE : 0));
128             }
129           else
130             mkdir_errno = errno;
131
132           if (preserve_existing)
133             {
134               struct stat st;
135               if (mkdir_errno == 0
136                   || (mkdir_errno != ENOENT && make_ancestor
137                       && stat (dir + prefix_len, &st) == 0
138                       && S_ISDIR (st.st_mode)))
139                 return true;
140             }
141           else
142             {
143               int open_result[2];
144               int chdir_result =
145                 savewd_chdir (wd, dir + prefix_len,
146                               savewd_chdir_options, open_result);
147               if (chdir_result < -1)
148                 return true;
149               else
150                 {
151                   bool chdir_ok = (chdir_result == 0);
152                   int chdir_errno = errno;
153                   int fd = open_result[0];
154                   bool chdir_failed_unexpectedly =
155                     (mkdir_errno == 0
156                      && ((! chdir_ok && (mode & S_IXUSR))
157                          || (fd < 0 && (mode & S_IRUSR))));
158
159                   if (chdir_failed_unexpectedly)
160                     {
161                       /* No need to save errno here; it's irrelevant.  */
162                       if (0 <= fd)
163                         close (fd);
164                     }
165                   else
166                     {
167                       mode_t mkdir_mode = (mkdir_errno == 0 ? mode : -1);
168                       char const *subdir = (chdir_ok ? "." : dir + prefix_len);
169                       if (dirchownmod (fd, subdir, mkdir_mode, owner, group,
170                                        mode, mode_bits)
171                           == 0)
172                         return true;
173                     }
174
175                   if (mkdir_errno == 0
176                       || (mkdir_errno != ENOENT && make_ancestor
177                           && errno != ENOTDIR))
178                     {
179                       error (0,
180                              (! chdir_failed_unexpectedly ? errno
181                               : ! chdir_ok && (mode & S_IXUSR) ? chdir_errno
182                               : open_result[1]),
183                              _(owner == (uid_t) -1 && group == (gid_t) -1
184                                ? "cannot change permissions of %s"
185                                : "cannot change owner and permissions of %s"),
186                              quote (dir));
187                       return false;
188                     }
189                 }
190             }
191         }
192     }
193
194   error (0, mkdir_errno, _("cannot create directory %s"), quote (dir));
195   return false;
196 }