]> git.saurik.com Git - apple/libc.git/blob - stdlib/FreeBSD/realpath.c.patch
Libc-391.4.1.tar.gz
[apple/libc.git] / stdlib / FreeBSD / realpath.c.patch
1 --- realpath.c.orig 2003-08-15 19:22:17.000000000 -0700
2 +++ realpath.c 2004-12-04 14:47:52.000000000 -0800
3 @@ -35,13 +35,35 @@
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 +static struct attrlist alist = {
27 + ATTR_BIT_MAP_COUNT,
28 + 0,
29 + ATTR_CMN_NAME | ATTR_CMN_DEVID | ATTR_CMN_OBJTYPE | ATTR_CMN_OBJID,
30 + 0,
31 + 0,
32 + 0,
33 + 0,
34 +};
35 +
36 /*
37 * char *realpath(const char *path, char resolved[PATH_MAX]);
38 *
39 @@ -52,13 +74,25 @@
40 char *
41 realpath(const char *path, char resolved[PATH_MAX])
42 {
43 + struct attrs attrs;
44 struct stat sb;
45 char *p, *q, *s;
46 - size_t left_len, resolved_len;
47 + size_t left_len, resolved_len, save_resolved_len;
48 unsigned symlinks;
49 - int serrno, slen;
50 + int serrno, slen, useattrs, islink;
51 char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
52 + dev_t dev, lastdev;
53 + struct statfs sfs;
54 + static dev_t rootdev;
55 + static int rootdev_inited = 0;
56 + ino_t inode;
57
58 + if (!rootdev_inited) {
59 + rootdev_inited = 1;
60 + if (stat("/", &sb) < 0)
61 + return (NULL);
62 + rootdev = sb.st_dev;
63 + }
64 serrno = errno;
65 symlinks = 0;
66 if (path[0] == '/') {
67 @@ -80,6 +114,12 @@
68 errno = ENAMETOOLONG;
69 return (NULL);
70 }
71 + if (resolved_len > 1) {
72 + if (stat(resolved, &sb) < 0)
73 + return (NULL);
74 + lastdev = sb.st_dev;
75 + } else
76 + lastdev = rootdev;
77
78 /*
79 * Iterate over path components in `left'.
80 @@ -127,6 +167,13 @@
81 }
82
83 /*
84 + * Save resolved_len, so that we can later null out
85 + * the the appended next_token, and replace with the
86 + * real name (matters on case-insensitive filesystems).
87 + */
88 + save_resolved_len = resolved_len;
89 +
90 + /*
91 * Append the next path component and lstat() it. If
92 * lstat() fails we still can return successfully if
93 * there are no more path components left.
94 @@ -136,14 +183,72 @@
95 errno = ENAMETOOLONG;
96 return (NULL);
97 }
98 - if (lstat(resolved, &sb) != 0) {
99 + if (getattrlist(resolved, &alist, &attrs, sizeof(attrs), FSOPT_NOFOLLOW) == 0) {
100 + useattrs = 1;
101 + islink = (attrs.type == VLNK);
102 + dev = attrs.dev;
103 + inode = attrs.id.fid_objno;
104 + } else if (errno == EOPNOTSUPP || errno == EINVAL) {
105 + if ((useattrs = lstat(resolved, &sb)) == 0) {
106 + islink = S_ISLNK(sb.st_mode);
107 + dev = sb.st_dev;
108 + inode = sb.st_ino;
109 + }
110 + } else
111 + useattrs = -1;
112 + if (useattrs < 0) {
113 if (errno == ENOENT && p == NULL) {
114 errno = serrno;
115 return (resolved);
116 }
117 return (NULL);
118 }
119 - if (S_ISLNK(sb.st_mode)) {
120 + if (dev != lastdev) {
121 + /*
122 + * We have crossed a mountpoint. For volumes like UDF
123 + * the getattrlist name may not match the actual
124 + * mountpoint, so we just copy the mountpoint directly.
125 + * (3703138). However, the mountpoint may not be
126 + * accessible, as when chroot-ed, so check first.
127 + * There may be a file on the chroot-ed volume with
128 + * the same name as the mountpoint, so compare device
129 + * and inode numbers.
130 + */
131 + lastdev = dev;
132 + if (statfs(resolved, &sfs) == 0 && lstat(sfs.f_mntonname, &sb) == 0 && dev == sb.st_dev && inode == sb.st_ino) {
133 + /*
134 + * However, it's possible that the mountpoint
135 + * path matches, even though it isn't the real
136 + * path in the chroot-ed environment, so check
137 + * that each component of the mountpoint
138 + * is a directory (and not a symlink)
139 + */
140 + char temp[MNAMELEN];
141 + char *cp;
142 + int ok = 1;
143 +
144 + strcpy(temp, sfs.f_mntonname);
145 + for(;;) {
146 + if ((cp = strrchr(temp, '/')) == NULL) {
147 + ok = 0;
148 + break;
149 + }
150 + if (cp <= temp)
151 + break;
152 + *cp = 0;
153 + if (lstat(temp, &sb) < 0 || (sb.st_mode & S_IFMT) != S_IFDIR) {
154 + ok = 0;
155 + break;
156 + }
157 + }
158 + if (ok) {
159 + resolved_len = strlcpy(resolved, sfs.f_mntonname, PATH_MAX);
160 + continue;
161 + }
162 + }
163 + /* if we fail, use the other methods. */
164 + }
165 + if (islink) {
166 if (symlinks++ > MAXSYMLINKS) {
167 errno = ELOOP;
168 return (NULL);
169 @@ -155,6 +260,7 @@
170 if (symlink[0] == '/') {
171 resolved[1] = 0;
172 resolved_len = 1;
173 + lastdev = rootdev;
174 } else if (resolved_len > 1) {
175 /* Strip the last path component. */
176 resolved[resolved_len - 1] = '\0';
177 @@ -184,7 +290,30 @@
178 }
179 }
180 left_len = strlcpy(left, symlink, sizeof(left));
181 + } else if (useattrs) {
182 + /*
183 + * attrs already has the real name.
184 + */
185 +
186 + resolved[save_resolved_len] = '\0';
187 + resolved_len = strlcat(resolved, (const char *)&attrs.name + attrs.name.attr_dataoffset, PATH_MAX);
188 + if (resolved_len >= PATH_MAX) {
189 + errno = ENAMETOOLONG;
190 + return (NULL);
191 + }
192 }
193 + /*
194 + * For the case of useattrs == 0, we could scan the directory
195 + * and try to match the inode. There are many problems with
196 + * this: (1) the directory may not be readable, (2) for multiple
197 + * hard links, we would find the first, but not necessarily
198 + * the one specified in the path, (3) we can't try to do
199 + * a case-insensitive search to match the right one in (2),
200 + * because the underlying filesystem may do things like
201 + * decompose composed characters. For most cases, doing
202 + * nothing is the right thing when useattrs == 0, so we punt
203 + * for now.
204 + */
205 }
206
207 /*