maintained -> supported, since Solaris 7 is "supported" (e.g., you can
[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 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include "mkdir-p.h"
27
28 #include <errno.h>
29 #include <sys/stat.h>
30
31 #include "gettext.h"
32 #define _(msgid) gettext (msgid)
33
34 #include "dirchownmod.c"
35 #include "error.h"
36 #include "quote.h"
37 #include "mkancesdirs.h"
38 #include "stat-macros.h"
39
40 /* Ensure that the directory DIR exists.
41
42    If MAKE_ANCESTOR is not null, create any ancestor directories that
43    don't already exist, by invoking MAKE_ANCESTOR (ANCESTOR, OPTIONS).
44    This function should return zero if successful, -1 (setting errno)
45    otherwise.  In this case, DIR may be modified by storing '\0' bytes
46    into it, to access the ancestor directories, and this modification
47    is retained on return if the ancestor directories could not be
48    created.
49
50    Create DIR as a new directory with using mkdir with permissions
51    MODE.  It is also OK if MAKE_ANCESTOR_DIR is not null and a
52    directory DIR already exists.
53
54    Call ANNOUNCE (DIR, OPTIONS) just after successfully making DIR,
55    even if some of the following actions fail.
56
57    Set DIR's owner to OWNER and group to GROUP, but leave the owner
58    alone if OWNER is (uid_t) -1, and similarly for GROUP.
59
60    Set DIR's mode bits to MODE, except preserve any of the bits that
61    correspond to zero bits in MODE_BITS.  In other words, MODE_BITS is
62    a mask that specifies which of DIR's mode bits should be set or
63    cleared.  MODE should be a subset of MODE_BITS, which in turn
64    should be a subset of CHMOD_MODE_BITS.  Changing the mode in this
65    way is necessary if DIR already existed or if MODE and MODE_BITS
66    specify non-permissions bits like S_ISUID.
67
68    However, if PRESERVE_EXISTING is true and DIR already exists,
69    do not attempt to set DIR's ownership and file mode bits.
70
71    This implementation assumes the current umask is zero.
72
73    Return true if DIR exists as a directory with the proper ownership
74    and file mode bits when done.  Report a diagnostic and return false
75    on failure, storing '\0' into *DIR if an ancestor directory had
76    problems.  */
77
78 bool
79 make_dir_parents (char *dir,
80                   int (*make_ancestor) (char const *, void *),
81                   void *options,
82                   mode_t mode,
83                   void (*announce) (char const *, void *),
84                   mode_t mode_bits,
85                   uid_t owner,
86                   gid_t group,
87                   bool preserve_existing)
88 {
89   bool made_dir = (mkdir (dir, mode) == 0);
90
91   if (!made_dir && make_ancestor && errno == ENOENT)
92     {
93       if (mkancesdirs (dir, make_ancestor, options) == 0)
94         made_dir = (mkdir (dir, mode) == 0);
95       else
96         {
97           /* mkancestdirs updated DIR for a better-looking
98              diagnostic, so don't try to stat DIR below.  */
99           make_ancestor = NULL;
100         }
101     }
102
103   if (made_dir)
104     {
105       announce (dir, options);
106       preserve_existing =
107         (owner == (uid_t) -1 && group == (gid_t) -1
108          && ! ((mode_bits & (S_ISUID | S_ISGID)) | (mode & S_ISVTX)));
109     }
110   else
111     {
112       int mkdir_errno = errno;
113       struct stat st;
114       if (! (make_ancestor && mkdir_errno != ENOENT
115              && stat (dir, &st) == 0 && S_ISDIR (st.st_mode)))
116         {
117           error (0, mkdir_errno, _("cannot create directory %s"), quote (dir));
118           return false;
119         }
120     }
121
122   if (! preserve_existing
123       && (dirchownmod (dir, (made_dir ? mode : (mode_t) -1),
124                        owner, group, mode, mode_bits)
125           != 0))
126     {
127       error (0, errno,
128              _(owner == (uid_t) -1 && group == (gid_t) -1
129                ? "cannot change permissions of %s"
130                : "cannot change owner and permissions of %s"),
131              quote (dir));
132       return false;
133     }
134
135   return true;
136 }