]> git.saurik.com Git - apple/libc.git/blame - stdlib/FreeBSD/realpath.c
Libc-1244.50.9.tar.gz
[apple/libc.git] / stdlib / FreeBSD / realpath.c
CommitLineData
9385eb3d
A
1/*
2 * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The names of the authors may not be used to endorse or promote
13 * products derived from this software without specific prior written
14 * permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#if defined(LIBC_SCCS) && !defined(lint)
30static char sccsid[] = "@(#)realpath.c 8.1 (Berkeley) 2/16/94";
31#endif /* LIBC_SCCS and not lint */
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: src/lib/libc/stdlib/realpath.c,v 1.20 2003/05/28 08:23:01 fjoe Exp $");
34
35#include "namespace.h"
36#include <sys/param.h>
37#include <sys/stat.h>
ad3c9f2a 38#include <sys/mount.h>
9385eb3d
A
39
40#include <errno.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
ad3c9f2a
A
44#include <sys/attr.h>
45#include <sys/vnode.h>
9385eb3d
A
46#include "un-namespace.h"
47
ad3c9f2a
A
48struct attrs {
49 u_int32_t len;
50 attrreference_t name;
51 dev_t dev;
52 fsobj_type_t type;
53 fsobj_id_t id;
54 char buf[PATH_MAX];
55};
56
57#ifndef BUILDING_VARIANT
6465356a 58__private_extern__ const struct attrlist _rp_alist = {
ad3c9f2a
A
59 ATTR_BIT_MAP_COUNT,
60 0,
61 ATTR_CMN_NAME | ATTR_CMN_DEVID | ATTR_CMN_OBJTYPE | ATTR_CMN_OBJID,
62 0,
63 0,
64 0,
65 0,
66};
67#else /* BUILDING_VARIANT */
b061a43b 68extern const struct attrlist _rp_alist;
ad3c9f2a
A
69#endif /* BUILDING_VARIANT */
70
71extern char * __private_getcwd(char *, size_t, int);
72
9385eb3d
A
73/*
74 * char *realpath(const char *path, char resolved[PATH_MAX]);
75 *
76 * Find the real name of path, by removing all ".", ".." and symlink
77 * components. Returns (resolved) on success, or (NULL) on failure,
78 * in which case the path which caused trouble is left in (resolved).
79 */
80char *
ad3c9f2a 81realpath(const char *path, char inresolved[PATH_MAX])
9385eb3d 82{
ad3c9f2a 83 struct attrs attrs;
9385eb3d 84 struct stat sb;
b061a43b
A
85 char *p, *q;
86 size_t left_len, resolved_len, save_resolved_len, next_token_len;
9385eb3d 87 unsigned symlinks;
b061a43b
A
88 int serrno, useattrs, islink;
89 ssize_t slen;
9385eb3d 90 char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
ad3c9f2a
A
91 dev_t dev, lastdev;
92 struct statfs sfs;
93 static dev_t rootdev;
94 static int rootdev_inited = 0;
95 ino_t inode;
96 char *resolved;
9385eb3d 97
ad3c9f2a
A
98 if (path == NULL) {
99 errno = EINVAL;
100 return (NULL);
101 }
102#if __DARWIN_UNIX03
103 if (*path == 0) {
104 errno = ENOENT;
105 return (NULL);
106 }
107#endif /* __DARWIN_UNIX03 */
108 /*
109 * Extension to the standard; if inresolved == NULL, allocate memory
110 */
111 if (!inresolved) {
112 if ((resolved = malloc(PATH_MAX)) == NULL) return (NULL);
113 } else {
114 resolved = inresolved;
115 }
116 if (!rootdev_inited) {
117 rootdev_inited = 1;
118 if (stat("/", &sb) < 0) {
119error_return:
120 if (!inresolved) {
121 int e = errno;
122 free(resolved);
123 errno = e;
124 }
125 return (NULL);
126 }
127 rootdev = sb.st_dev;
128 }
9385eb3d
A
129 serrno = errno;
130 symlinks = 0;
131 if (path[0] == '/') {
132 resolved[0] = '/';
133 resolved[1] = '\0';
ad3c9f2a 134 if (path[1] == '\0') {
9385eb3d 135 return (resolved);
ad3c9f2a 136 }
9385eb3d
A
137 resolved_len = 1;
138 left_len = strlcpy(left, path + 1, sizeof(left));
139 } else {
ad3c9f2a
A
140#if !defined(VARIANT_DARWINEXTSN) && __DARWIN_UNIX03
141 /* 4447159: don't use GETPATH, so this will fail if */
142 /* if parent directories are not readable, as per POSIX */
143 if (__private_getcwd(resolved, PATH_MAX, 0) == NULL)
144#else /* VARIANT_DARWINEXTSN || !__DARWIN_UNIX03 */
145 if (__private_getcwd(resolved, PATH_MAX, 1) == NULL)
146#endif /* !VARIANT_DARWINEXTSN && __DARWIN_UNIX03 */
147 {
9385eb3d 148 strlcpy(resolved, ".", PATH_MAX);
ad3c9f2a 149 goto error_return;
9385eb3d
A
150 }
151 resolved_len = strlen(resolved);
152 left_len = strlcpy(left, path, sizeof(left));
153 }
154 if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
155 errno = ENAMETOOLONG;
ad3c9f2a 156 goto error_return;
9385eb3d 157 }
ad3c9f2a
A
158 if (resolved_len > 1) {
159 if (stat(resolved, &sb) < 0) {
160 goto error_return;
161 }
162 lastdev = sb.st_dev;
163 } else
164 lastdev = rootdev;
9385eb3d
A
165
166 /*
167 * Iterate over path components in `left'.
168 */
169 while (left_len != 0) {
170 /*
171 * Extract the next path component and adjust `left'
172 * and its length.
173 */
174 p = strchr(left, '/');
b061a43b
A
175 next_token_len = p ? p - left : left_len;
176 memcpy(next_token, left, next_token_len);
177 next_token[next_token_len] = '\0';
178
179 if (p != NULL) {
180 left_len -= next_token_len + 1;
181 memmove(left, p + 1, left_len + 1);
182 } else {
183 left[0] = '\0';
184 left_len = 0;
9385eb3d 185 }
b061a43b 186
9385eb3d
A
187 if (resolved[resolved_len - 1] != '/') {
188 if (resolved_len + 1 >= PATH_MAX) {
189 errno = ENAMETOOLONG;
ad3c9f2a 190 goto error_return;
9385eb3d
A
191 }
192 resolved[resolved_len++] = '/';
193 resolved[resolved_len] = '\0';
194 }
b061a43b
A
195 if (next_token[0] == '\0') {
196 /* Handle consequential slashes. */
9385eb3d 197 continue;
b061a43b 198 } else if (strcmp(next_token, ".") == 0) {
9385eb3d 199 continue;
b061a43b 200 } else if (strcmp(next_token, "..") == 0) {
9385eb3d
A
201 /*
202 * Strip the last path component except when we have
203 * single "/"
204 */
205 if (resolved_len > 1) {
206 resolved[resolved_len - 1] = '\0';
207 q = strrchr(resolved, '/') + 1;
208 *q = '\0';
209 resolved_len = q - resolved;
210 }
211 continue;
212 }
213
ad3c9f2a
A
214 /*
215 * Save resolved_len, so that we can later null out
216 * the the appended next_token, and replace with the
217 * real name (matters on case-insensitive filesystems).
218 */
219 save_resolved_len = resolved_len;
220
9385eb3d
A
221 /*
222 * Append the next path component and lstat() it. If
223 * lstat() fails we still can return successfully if
224 * there are no more path components left.
225 */
226 resolved_len = strlcat(resolved, next_token, PATH_MAX);
227 if (resolved_len >= PATH_MAX) {
228 errno = ENAMETOOLONG;
ad3c9f2a 229 goto error_return;
9385eb3d 230 }
6465356a 231 if (getattrlist(resolved, (void *)&_rp_alist, &attrs, sizeof(attrs), FSOPT_NOFOLLOW) == 0) {
ad3c9f2a
A
232 useattrs = 1;
233 islink = (attrs.type == VLNK);
234 dev = attrs.dev;
235 inode = attrs.id.fid_objno;
236 } else if (errno == ENOTSUP || errno == EINVAL) {
237 if ((useattrs = lstat(resolved, &sb)) == 0) {
238 islink = S_ISLNK(sb.st_mode);
239 dev = sb.st_dev;
240 inode = sb.st_ino;
241 }
242 } else
243 useattrs = -1;
244 if (useattrs < 0) {
245#if !__DARWIN_UNIX03
9385eb3d
A
246 if (errno == ENOENT && p == NULL) {
247 errno = serrno;
248 return (resolved);
249 }
ad3c9f2a
A
250#endif /* !__DARWIN_UNIX03 */
251 goto error_return;
9385eb3d 252 }
ad3c9f2a
A
253 if (dev != lastdev) {
254 /*
255 * We have crossed a mountpoint. For volumes like UDF
256 * the getattrlist name may not match the actual
257 * mountpoint, so we just copy the mountpoint directly.
258 * (3703138). However, the mountpoint may not be
259 * accessible, as when chroot-ed, so check first.
260 * There may be a file on the chroot-ed volume with
261 * the same name as the mountpoint, so compare device
262 * and inode numbers.
263 */
264 lastdev = dev;
265 if (statfs(resolved, &sfs) == 0 && lstat(sfs.f_mntonname, &sb) == 0 && dev == sb.st_dev && inode == sb.st_ino) {
266 /*
267 * However, it's possible that the mountpoint
268 * path matches, even though it isn't the real
269 * path in the chroot-ed environment, so check
270 * that each component of the mountpoint
271 * is a directory (and not a symlink)
272 */
273 char temp[sizeof(sfs.f_mntonname)];
274 char *cp;
275 int ok = 1;
276
277 strcpy(temp, sfs.f_mntonname);
278 for(;;) {
279 if ((cp = strrchr(temp, '/')) == NULL) {
280 ok = 0;
281 break;
282 }
283 if (cp <= temp)
284 break;
285 *cp = 0;
286 if (lstat(temp, &sb) < 0 || (sb.st_mode & S_IFMT) != S_IFDIR) {
287 ok = 0;
288 break;
289 }
290 }
291 if (ok) {
292 resolved_len = strlcpy(resolved, sfs.f_mntonname, PATH_MAX);
293 continue;
294 }
295 }
296 /* if we fail, use the other methods. */
297 }
298 if (islink) {
9385eb3d
A
299 if (symlinks++ > MAXSYMLINKS) {
300 errno = ELOOP;
ad3c9f2a 301 goto error_return;
9385eb3d 302 }
b061a43b
A
303 slen = readlink(resolved, symlink, sizeof(symlink));
304 if (slen <= 0 || slen >= sizeof(symlink)) {
305 if (slen < 0)
306 ; /* keep errno from readlink(2) call */
307 else if (slen == 0)
308 errno = ENOENT;
309 else
310 errno = ENAMETOOLONG;
ad3c9f2a
A
311 goto error_return;
312 }
9385eb3d
A
313 symlink[slen] = '\0';
314 if (symlink[0] == '/') {
315 resolved[1] = 0;
316 resolved_len = 1;
ad3c9f2a 317 lastdev = rootdev;
b061a43b 318 } else {
9385eb3d 319 /* Strip the last path component. */
9385eb3d
A
320 q = strrchr(resolved, '/') + 1;
321 *q = '\0';
322 resolved_len = q - resolved;
323 }
324
325 /*
326 * If there are any path components left, then
327 * append them to symlink. The result is placed
328 * in `left'.
329 */
330 if (p != NULL) {
331 if (symlink[slen - 1] != '/') {
332 if (slen + 1 >= sizeof(symlink)) {
333 errno = ENAMETOOLONG;
ad3c9f2a 334 goto error_return;
9385eb3d
A
335 }
336 symlink[slen] = '/';
337 symlink[slen + 1] = 0;
338 }
6465356a 339 left_len = strlcat(symlink, left, sizeof(symlink));
b061a43b 340 if (left_len >= sizeof(symlink)) {
9385eb3d 341 errno = ENAMETOOLONG;
ad3c9f2a 342 goto error_return;
9385eb3d
A
343 }
344 }
345 left_len = strlcpy(left, symlink, sizeof(left));
ad3c9f2a
A
346 } else if (useattrs) {
347 /*
348 * attrs already has the real name.
349 */
350
351 resolved[save_resolved_len] = '\0';
352 resolved_len = strlcat(resolved, (const char *)&attrs.name + attrs.name.attr_dataoffset, PATH_MAX);
353 if (resolved_len >= PATH_MAX) {
354 errno = ENAMETOOLONG;
355 goto error_return;
356 }
9385eb3d 357 }
ad3c9f2a
A
358 /*
359 * For the case of useattrs == 0, we could scan the directory
360 * and try to match the inode. There are many problems with
361 * this: (1) the directory may not be readable, (2) for multiple
362 * hard links, we would find the first, but not necessarily
363 * the one specified in the path, (3) we can't try to do
364 * a case-insensitive search to match the right one in (2),
365 * because the underlying filesystem may do things like
366 * decompose composed characters. For most cases, doing
367 * nothing is the right thing when useattrs == 0, so we punt
368 * for now.
369 */
9385eb3d
A
370 }
371
372 /*
373 * Remove trailing slash except when the resolved pathname
374 * is a single "/".
375 */
376 if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
377 resolved[resolved_len - 1] = '\0';
378 return (resolved);
379}