]> git.saurik.com Git - apple/libc.git/blob - stdlib/FreeBSD/realpath.c.patch
2d154b1051f8f2a43ff7da618f9513d05d0f4dfd
[apple/libc.git] / stdlib / FreeBSD / realpath.c.patch
1 --- realpath.c.orig 2008-04-04 14:39:39.000000000 -0700
2 +++ realpath.c 2008-04-04 19:59:19.000000000 -0700
3 @@ -35,13 +35,41 @@ __FBSDID("$FreeBSD: src/lib/libc/stdlib/
4 #include "namespace.h"
5 #include <sys/param.h>
6 #include <sys/stat.h>
7 +#include <sys/mount.h>
8
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 +#include <sys/attr.h>
14 +#include <sys/vnode.h>
15 #include "un-namespace.h"
16
17 +struct attrs {
18 + u_int32_t len;
19 + attrreference_t name;
20 + dev_t dev;
21 + fsobj_type_t type;
22 + fsobj_id_t id;
23 + char buf[PATH_MAX];
24 +};
25 +
26 +#ifndef BUILDING_VARIANT
27 +__private_extern__ struct attrlist _rp_alist = {
28 + ATTR_BIT_MAP_COUNT,
29 + 0,
30 + ATTR_CMN_NAME | ATTR_CMN_DEVID | ATTR_CMN_OBJTYPE | ATTR_CMN_OBJID,
31 + 0,
32 + 0,
33 + 0,
34 + 0,
35 +};
36 +#else /* BUILDING_VARIANT */
37 +__private_extern__ struct attrlist _rp_alist;
38 +#endif /* BUILDING_VARIANT */
39 +
40 +extern char * __private_getcwd(char *, size_t, int);
41 +
42 /*
43 * char *realpath(const char *path, char resolved[PATH_MAX]);
44 *
45 @@ -50,26 +78,67 @@ __FBSDID("$FreeBSD: src/lib/libc/stdlib/
46 * in which case the path which caused trouble is left in (resolved).
47 */
48 char *
49 -realpath(const char *path, char resolved[PATH_MAX])
50 +realpath(const char *path, char inresolved[PATH_MAX])
51 {
52 + struct attrs attrs;
53 struct stat sb;
54 char *p, *q, *s;
55 - size_t left_len, resolved_len;
56 + size_t left_len, resolved_len, save_resolved_len;
57 unsigned symlinks;
58 - int serrno, slen;
59 + int serrno, slen, useattrs, islink;
60 char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
61 + dev_t dev, lastdev;
62 + struct statfs sfs;
63 + static dev_t rootdev;
64 + static int rootdev_inited = 0;
65 + ino_t inode;
66 + char *resolved;
67
68 + if (path == NULL) {
69 + errno = EINVAL;
70 + return (NULL);
71 + }
72 +#if __DARWIN_UNIX03
73 + if (*path == 0) {
74 + errno = ENOENT;
75 + return (NULL);
76 + }
77 +#endif /* __DARWIN_UNIX03 */
78 + /*
79 + * Extension to the standard; if inresolved == NULL, allocate memory
80 + * (first on the stack, then use strdup())
81 + */
82 + if (!inresolved) {
83 + if ((resolved = alloca(PATH_MAX)) == NULL) return (NULL);
84 + } else {
85 + resolved = inresolved;
86 + }
87 + if (!rootdev_inited) {
88 + rootdev_inited = 1;
89 + if (stat("/", &sb) < 0) {
90 + return (NULL);
91 + }
92 + rootdev = sb.st_dev;
93 + }
94 serrno = errno;
95 symlinks = 0;
96 if (path[0] == '/') {
97 resolved[0] = '/';
98 resolved[1] = '\0';
99 - if (path[1] == '\0')
100 + if (path[1] == '\0') {
101 return (resolved);
102 + }
103 resolved_len = 1;
104 left_len = strlcpy(left, path + 1, sizeof(left));
105 } else {
106 - if (getcwd(resolved, PATH_MAX) == NULL) {
107 +#if !defined(VARIANT_DARWINEXTSN) && __DARWIN_UNIX03
108 + /* 4447159: don't use GETPATH, so this will fail if */
109 + /* if parent directories are not readable, as per POSIX */
110 + if (__private_getcwd(resolved, PATH_MAX, 0) == NULL)
111 +#else /* VARIANT_DARWINEXTSN || !__DARWIN_UNIX03 */
112 + if (__private_getcwd(resolved, PATH_MAX, 1) == NULL)
113 +#endif /* !VARIANT_DARWINEXTSN && __DARWIN_UNIX03 */
114 + {
115 strlcpy(resolved, ".", PATH_MAX);
116 return (NULL);
117 }
118 @@ -80,6 +149,13 @@ realpath(const char *path, char resolved
119 errno = ENAMETOOLONG;
120 return (NULL);
121 }
122 + if (resolved_len > 1) {
123 + if (stat(resolved, &sb) < 0) {
124 + return (NULL);
125 + }
126 + lastdev = sb.st_dev;
127 + } else
128 + lastdev = rootdev;
129
130 /*
131 * Iterate over path components in `left'.
132 @@ -127,6 +203,13 @@ realpath(const char *path, char resolved
133 }
134
135 /*
136 + * Save resolved_len, so that we can later null out
137 + * the the appended next_token, and replace with the
138 + * real name (matters on case-insensitive filesystems).
139 + */
140 + save_resolved_len = resolved_len;
141 +
142 + /*
143 * Append the next path component and lstat() it. If
144 * lstat() fails we still can return successfully if
145 * there are no more path components left.
146 @@ -136,25 +219,87 @@ realpath(const char *path, char resolved
147 errno = ENAMETOOLONG;
148 return (NULL);
149 }
150 - if (lstat(resolved, &sb) != 0) {
151 + if (getattrlist(resolved, &_rp_alist, &attrs, sizeof(attrs), FSOPT_NOFOLLOW) == 0) {
152 + useattrs = 1;
153 + islink = (attrs.type == VLNK);
154 + dev = attrs.dev;
155 + inode = attrs.id.fid_objno;
156 + } else if (errno == ENOTSUP || errno == EINVAL) {
157 + if ((useattrs = lstat(resolved, &sb)) == 0) {
158 + islink = S_ISLNK(sb.st_mode);
159 + dev = sb.st_dev;
160 + inode = sb.st_ino;
161 + }
162 + } else
163 + useattrs = -1;
164 + if (useattrs < 0) {
165 +#if !__DARWIN_UNIX03
166 if (errno == ENOENT && p == NULL) {
167 errno = serrno;
168 return (resolved);
169 }
170 +#endif /* !__DARWIN_UNIX03 */
171 return (NULL);
172 }
173 - if (S_ISLNK(sb.st_mode)) {
174 + if (dev != lastdev) {
175 + /*
176 + * We have crossed a mountpoint. For volumes like UDF
177 + * the getattrlist name may not match the actual
178 + * mountpoint, so we just copy the mountpoint directly.
179 + * (3703138). However, the mountpoint may not be
180 + * accessible, as when chroot-ed, so check first.
181 + * There may be a file on the chroot-ed volume with
182 + * the same name as the mountpoint, so compare device
183 + * and inode numbers.
184 + */
185 + lastdev = dev;
186 + if (statfs(resolved, &sfs) == 0 && lstat(sfs.f_mntonname, &sb) == 0 && dev == sb.st_dev && inode == sb.st_ino) {
187 + /*
188 + * However, it's possible that the mountpoint
189 + * path matches, even though it isn't the real
190 + * path in the chroot-ed environment, so check
191 + * that each component of the mountpoint
192 + * is a directory (and not a symlink)
193 + */
194 + char temp[sizeof(sfs.f_mntonname)];
195 + char *cp;
196 + int ok = 1;
197 +
198 + strcpy(temp, sfs.f_mntonname);
199 + for(;;) {
200 + if ((cp = strrchr(temp, '/')) == NULL) {
201 + ok = 0;
202 + break;
203 + }
204 + if (cp <= temp)
205 + break;
206 + *cp = 0;
207 + if (lstat(temp, &sb) < 0 || (sb.st_mode & S_IFMT) != S_IFDIR) {
208 + ok = 0;
209 + break;
210 + }
211 + }
212 + if (ok) {
213 + resolved_len = strlcpy(resolved, sfs.f_mntonname, PATH_MAX);
214 + continue;
215 + }
216 + }
217 + /* if we fail, use the other methods. */
218 + }
219 + if (islink) {
220 if (symlinks++ > MAXSYMLINKS) {
221 errno = ELOOP;
222 return (NULL);
223 }
224 slen = readlink(resolved, symlink, sizeof(symlink) - 1);
225 - if (slen < 0)
226 + if (slen < 0) {
227 return (NULL);
228 + }
229 symlink[slen] = '\0';
230 if (symlink[0] == '/') {
231 resolved[1] = 0;
232 resolved_len = 1;
233 + lastdev = rootdev;
234 } else if (resolved_len > 1) {
235 /* Strip the last path component. */
236 resolved[resolved_len - 1] = '\0';
237 @@ -184,7 +329,30 @@ realpath(const char *path, char resolved
238 }
239 }
240 left_len = strlcpy(left, symlink, sizeof(left));
241 + } else if (useattrs) {
242 + /*
243 + * attrs already has the real name.
244 + */
245 +
246 + resolved[save_resolved_len] = '\0';
247 + resolved_len = strlcat(resolved, (const char *)&attrs.name + attrs.name.attr_dataoffset, PATH_MAX);
248 + if (resolved_len >= PATH_MAX) {
249 + errno = ENAMETOOLONG;
250 + return (NULL);
251 + }
252 }
253 + /*
254 + * For the case of useattrs == 0, we could scan the directory
255 + * and try to match the inode. There are many problems with
256 + * this: (1) the directory may not be readable, (2) for multiple
257 + * hard links, we would find the first, but not necessarily
258 + * the one specified in the path, (3) we can't try to do
259 + * a case-insensitive search to match the right one in (2),
260 + * because the underlying filesystem may do things like
261 + * decompose composed characters. For most cases, doing
262 + * nothing is the right thing when useattrs == 0, so we punt
263 + * for now.
264 + */
265 }
266
267 /*
268 @@ -193,5 +361,6 @@ realpath(const char *path, char resolved
269 */
270 if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
271 resolved[resolved_len - 1] = '\0';
272 + if (!inresolved) resolved = strdup(resolved);
273 return (resolved);
274 }