1 --- realpath.c.orig 2006-09-16 19:12:28.000000000 -0700
2 +++ realpath.c 2006-09-16 20:18:25.000000000 -0700
7 +#include <sys/mount.h>
13 +#include <sys/attr.h>
14 +#include <sys/vnode.h>
15 #include "un-namespace.h"
19 + attrreference_t name;
26 +#ifndef BUILDING_VARIANT
27 +__private_extern__ struct attrlist _rp_alist = {
30 + ATTR_CMN_NAME | ATTR_CMN_DEVID | ATTR_CMN_OBJTYPE | ATTR_CMN_OBJID,
36 +#else /* BUILDING_VARIANT */
37 +__private_extern__ struct attrlist _rp_alist;
38 +#endif /* BUILDING_VARIANT */
40 +extern char * __private_getcwd(char *, size_t, int);
43 * char *realpath(const char *path, char resolved[PATH_MAX]);
47 realpath(const char *path, char resolved[PATH_MAX])
52 - size_t left_len, resolved_len;
53 + size_t left_len, resolved_len, save_resolved_len;
56 + int serrno, slen, useattrs, islink;
57 char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
60 + static dev_t rootdev;
61 + static int rootdev_inited = 0;
73 +#endif /* __DARWIN_UNIX03 */
74 + if (!rootdev_inited) {
76 + if (stat("/", &sb) < 0) {
79 + rootdev = sb.st_dev;
86 - if (path[1] == '\0')
87 + if (path[1] == '\0') {
91 left_len = strlcpy(left, path + 1, sizeof(left));
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 */
102 strlcpy(resolved, ".", PATH_MAX);
106 errno = ENAMETOOLONG;
109 + if (resolved_len > 1) {
110 + if (stat(resolved, &sb) < 0) {
113 + lastdev = sb.st_dev;
118 * Iterate over path components in `left'.
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).
127 + save_resolved_len = resolved_len;
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.
133 @@ -136,25 +209,87 @@
134 errno = ENAMETOOLONG;
137 - if (lstat(resolved, &sb) != 0) {
138 + if (getattrlist(resolved, &_rp_alist, &attrs, sizeof(attrs), FSOPT_NOFOLLOW) == 0) {
140 + islink = (attrs.type == VLNK);
142 + inode = attrs.id.fid_objno;
143 + } else if (errno == ENOTSUP || errno == EINVAL) {
144 + if ((useattrs = lstat(resolved, &sb)) == 0) {
145 + islink = S_ISLNK(sb.st_mode);
151 + if (useattrs < 0) {
152 +#if !__DARWIN_UNIX03
153 if (errno == ENOENT && p == NULL) {
157 +#endif /* !__DARWIN_UNIX03 */
160 - if (S_ISLNK(sb.st_mode)) {
161 + if (dev != lastdev) {
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.
173 + if (statfs(resolved, &sfs) == 0 && lstat(sfs.f_mntonname, &sb) == 0 && dev == sb.st_dev && inode == sb.st_ino) {
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)
181 + char temp[MNAMELEN];
185 + strcpy(temp, sfs.f_mntonname);
187 + if ((cp = strrchr(temp, '/')) == NULL) {
194 + if (lstat(temp, &sb) < 0 || (sb.st_mode & S_IFMT) != S_IFDIR) {
200 + resolved_len = strlcpy(resolved, sfs.f_mntonname, PATH_MAX);
204 + /* if we fail, use the other methods. */
207 if (symlinks++ > MAXSYMLINKS) {
211 slen = readlink(resolved, symlink, sizeof(symlink) - 1);
216 symlink[slen] = '\0';
217 if (symlink[0] == '/') {
221 } else if (resolved_len > 1) {
222 /* Strip the last path component. */
223 resolved[resolved_len - 1] = '\0';
227 left_len = strlcpy(left, symlink, sizeof(left));
228 + } else if (useattrs) {
230 + * attrs already has the real name.
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;
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