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