]>
git.saurik.com Git - apple/libc.git/blob - util/mkpath_np.c
77a1853cdba5114806be26f6637f36c552d44422
2 * Copyright (c) 2011 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
30 /* This extended version of mkpath_np is provided to help NSFileManager
31 * maintain binary compatibility. If firstdir is not NULL, *firstdir will be
32 * set to the path of the first created directory, and it is the caller's
33 * responsibility to free the returned string. This SPI is subject to removal
34 * once NSFileManager no longer has a need for it, and use in new code is
37 * See: <rdar://problem/9888987>
41 _mkpath_np(const char *path
, mode_t omode
, const char ** firstdir
)
43 char *apath
= NULL
, *opath
= NULL
, *s
, *sn
, *sl
;
44 unsigned int depth
= 0;
45 mode_t chmod_mode
= 0;
47 int old_errno
= errno
;
50 /* Try the trivial case first. */
51 if (0 == mkdir(path
, omode
)) {
53 *firstdir
= strdup(path
);
58 /* Anything other than an ENOENT, EEXIST, or EISDIR indicates an
59 * error that we need to send back to the caller. ENOENT indicates
60 * that we need to try a lower level.
66 if (stat(path
, &sbuf
) == 0) {
67 if (S_ISDIR(sbuf
.st_mode
)) {
76 case EISDIR
: /* <rdar://problem/10288022> */
90 sl
= s
= apath
+ strlen(apath
) - 1;
93 /* Strip off trailing /., see <rdar://problem/14351794> */
94 if (s
- 1 > apath
&& *s
== '.' && *(s
- 1) == '/')
96 /* Strip off trailing /, see <rdar://problem/11592386> */
97 if (s
> apath
&& *s
== '/')
102 path
= opath
= strdup(apath
);
110 /* Increase our depth and try making that directory */
111 s
= strrchr(apath
, '/');
113 /* We should never hit this under normal circumstances,
114 * but it can occur due to really unfortunate timing
122 if (0 == mkdir(apath
, S_IRWXU
| S_IRWXG
| S_IRWXO
)) {
123 /* Found our starting point */
126 * For each dir operand that does not name an existing
127 * directory, effects equivalent to those cased by the
128 * following command shall occcur:
130 * mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
131 * mkdir [-m mode] dir
135 if (-1 == stat(apath
, &dirstat
)) {
136 /* Really unfortunate timing ... */
141 if ((dirstat
.st_mode
& (S_IWUSR
| S_IXUSR
)) != (S_IWUSR
| S_IXUSR
)) {
142 chmod_mode
= dirstat
.st_mode
| S_IWUSR
| S_IXUSR
;
143 if (-1 == chmod(apath
, chmod_mode
)) {
144 /* Really unfortunate timing ... */
151 *firstdir
= strdup(apath
);
154 } else if (errno
== EEXIST
) {
155 /* Some other process won the race in creating this directory
156 * before we did. We will use this as our starting point.
157 * See: <rdar://problem/10279893>
159 if (stat(apath
, &sbuf
) == 0 &&
160 S_ISDIR(sbuf
.st_mode
)) {
163 *firstdir
= strdup(apath
);
170 } else if (errno
!= ENOENT
) {
177 /* Decrease our depth and make that directory */
178 s
= strrchr(apath
, '\0');
182 if (-1 == mkdir(apath
, S_IRWXU
| S_IRWXG
| S_IRWXO
)) {
183 /* This handles "." and ".." added to the new section of path */
191 if (-1 == chmod(apath
, chmod_mode
)) {
192 /* Really unfortunate timing ... */
199 if (-1 == mkdir(path
, omode
)) {
201 if (errno
== EEXIST
&&
202 stat(path
, &sbuf
) == 0 &&
203 !S_ISDIR(sbuf
.st_mode
)) {
216 int mkpath_np(const char *path
, mode_t omode
) {
217 return _mkpath_np(path
, omode
, NULL
);