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