1 --- realpath.c.orig     2003-08-15 19:22:17.000000000 -0700
 
   2 +++ realpath.c  2004-12-04 14:47:52.000000000 -0800
 
   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 +static struct attrlist alist = {
 
  29 +       ATTR_CMN_NAME | ATTR_CMN_DEVID | ATTR_CMN_OBJTYPE | ATTR_CMN_OBJID,
 
  37   * char *realpath(const char *path, char resolved[PATH_MAX]);
 
  41  realpath(const char *path, char resolved[PATH_MAX])
 
  46 -       size_t left_len, resolved_len;
 
  47 +       size_t left_len, resolved_len, save_resolved_len;
 
  50 +       int serrno, slen, useattrs, islink;
 
  51         char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
 
  54 +       static dev_t rootdev;
 
  55 +       static int rootdev_inited = 0;
 
  58 +       if (!rootdev_inited) {
 
  60 +               if (stat("/", &sb) < 0)
 
  62 +               rootdev = sb.st_dev;
 
  71 +       if (resolved_len > 1) {
 
  72 +               if (stat(resolved, &sb) < 0)
 
  74 +               lastdev = sb.st_dev;
 
  79          * Iterate over path components in `left'.
 
  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).
 
  88 +               save_resolved_len = resolved_len;
 
  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.
 
  98 -               if (lstat(resolved, &sb) != 0) {
 
  99 +               if (getattrlist(resolved, &alist, &attrs, sizeof(attrs), FSOPT_NOFOLLOW) == 0) {
 
 101 +                       islink = (attrs.type == VLNK);
 
 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);
 
 112 +               if (useattrs < 0) {
 
 113                         if (errno == ENOENT && p == NULL) {
 
 119 -               if (S_ISLNK(sb.st_mode)) {
 
 120 +               if (dev != lastdev) {
 
 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.
 
 132 +                       if (statfs(resolved, &sfs) == 0 && lstat(sfs.f_mntonname, &sb) == 0 && dev == sb.st_dev && inode == sb.st_ino) {
 
 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)
 
 140 +                               char temp[MNAMELEN];
 
 144 +                               strcpy(temp, sfs.f_mntonname);
 
 146 +                                       if ((cp = strrchr(temp, '/')) == NULL) {
 
 153 +                                       if (lstat(temp, &sb) < 0 || (sb.st_mode & S_IFMT) != S_IFDIR) {
 
 159 +                                       resolved_len = strlcpy(resolved, sfs.f_mntonname, PATH_MAX);
 
 163 +                       /* if we fail, use the other methods. */
 
 166                         if (symlinks++ > MAXSYMLINKS) {
 
 170                         if (symlink[0] == '/') {
 
 174                         } else if (resolved_len > 1) {
 
 175                                 /* Strip the last path component. */
 
 176                                 resolved[resolved_len - 1] = '\0';
 
 180                         left_len = strlcpy(left, symlink, sizeof(left));
 
 181 +               } else if (useattrs) {
 
 183 +                        * attrs already has the real name.
 
 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;
 
 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