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/
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]);
45 @@ -50,26 +78,67 @@ __FBSDID("$FreeBSD: src/lib/libc/stdlib/
46 * in which case the path which caused trouble is left in (resolved).
49 -realpath(const char *path, char resolved[PATH_MAX])
50 +realpath(const char *path, char inresolved[PATH_MAX])
55 - size_t left_len, resolved_len;
56 + size_t left_len, resolved_len, save_resolved_len;
59 + int serrno, slen, useattrs, islink;
60 char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
63 + static dev_t rootdev;
64 + static int rootdev_inited = 0;
77 +#endif /* __DARWIN_UNIX03 */
79 + * Extension to the standard; if inresolved == NULL, allocate memory
80 + * (first on the stack, then use strdup())
83 + if ((resolved = alloca(PATH_MAX)) == NULL) return (NULL);
85 + resolved = inresolved;
87 + if (!rootdev_inited) {
89 + if (stat("/", &sb) < 0) {
92 + rootdev = sb.st_dev;
99 - if (path[1] == '\0')
100 + if (path[1] == '\0') {
104 left_len = strlcpy(left, path + 1, sizeof(left));
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 */
115 strlcpy(resolved, ".", PATH_MAX);
118 @@ -80,6 +149,13 @@ realpath(const char *path, char resolved
119 errno = ENAMETOOLONG;
122 + if (resolved_len > 1) {
123 + if (stat(resolved, &sb) < 0) {
126 + lastdev = sb.st_dev;
131 * Iterate over path components in `left'.
132 @@ -127,6 +203,13 @@ realpath(const char *path, char resolved
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).
140 + save_resolved_len = resolved_len;
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;
150 - if (lstat(resolved, &sb) != 0) {
151 + if (getattrlist(resolved, &_rp_alist, &attrs, sizeof(attrs), FSOPT_NOFOLLOW) == 0) {
153 + islink = (attrs.type == VLNK);
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);
164 + if (useattrs < 0) {
165 +#if !__DARWIN_UNIX03
166 if (errno == ENOENT && p == NULL) {
170 +#endif /* !__DARWIN_UNIX03 */
173 - if (S_ISLNK(sb.st_mode)) {
174 + if (dev != lastdev) {
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.
186 + if (statfs(resolved, &sfs) == 0 && lstat(sfs.f_mntonname, &sb) == 0 && dev == sb.st_dev && inode == sb.st_ino) {
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)
194 + char temp[sizeof(sfs.f_mntonname)];
198 + strcpy(temp, sfs.f_mntonname);
200 + if ((cp = strrchr(temp, '/')) == NULL) {
207 + if (lstat(temp, &sb) < 0 || (sb.st_mode & S_IFMT) != S_IFDIR) {
213 + resolved_len = strlcpy(resolved, sfs.f_mntonname, PATH_MAX);
217 + /* if we fail, use the other methods. */
220 if (symlinks++ > MAXSYMLINKS) {
224 slen = readlink(resolved, symlink, sizeof(symlink) - 1);
229 symlink[slen] = '\0';
230 if (symlink[0] == '/') {
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
240 left_len = strlcpy(left, symlink, sizeof(left));
241 + } else if (useattrs) {
243 + * attrs already has the real name.
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;
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
268 @@ -193,5 +361,6 @@ realpath(const char *path, char resolved
270 if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
271 resolved[resolved_len - 1] = '\0';
272 + if (!inresolved) resolved = strdup(resolved);