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