jorton f0cc37
--- apr-0.9.6/file_io/unix/dir.c.readdir64
jorton f0cc37
+++ apr-0.9.6/file_io/unix/dir.c
jorton f0cc37
@@ -77,24 +77,24 @@
jorton f0cc37
      * one-byte array.  Note: gcc evaluates this at compile time.
jorton f0cc37
      */
jorton f0cc37
     apr_size_t dirent_size = 
jorton f0cc37
-        (sizeof((*new)->entry->d_name) > 1 ? 
jorton f0cc37
-         sizeof(struct dirent) : sizeof (struct dirent) + 255);
jorton f0cc37
+        sizeof(*(*new)->entry) +
jorton f0cc37
+        (sizeof((*new)->entry->d_name) > 1 ? 0 : 255);
jorton f0cc37
+    DIR *dir = opendir(dirname);
jorton f0cc37
+
jorton f0cc37
+    if (!dir) {
jorton f0cc37
+        return errno;
jorton f0cc37
+    }
jorton f0cc37
 
jorton f0cc37
     (*new) = (apr_dir_t *)apr_palloc(pool, sizeof(apr_dir_t));
jorton f0cc37
 
jorton f0cc37
     (*new)->pool = pool;
jorton f0cc37
     (*new)->dirname = apr_pstrdup(pool, dirname);
jorton f0cc37
-    (*new)->dirstruct = opendir(dirname);
jorton f0cc37
+    (*new)->dirstruct = dir;
jorton f0cc37
     (*new)->entry = apr_pcalloc(pool, dirent_size);
jorton f0cc37
 
jorton f0cc37
-    if ((*new)->dirstruct == NULL) {
jorton f0cc37
-        return errno;
jorton f0cc37
-    }    
jorton f0cc37
-    else {
jorton f0cc37
-        apr_pool_cleanup_register((*new)->pool, (void *)(*new), dir_cleanup,
jorton f0cc37
-	                          apr_pool_cleanup_null);
jorton f0cc37
-        return APR_SUCCESS;
jorton f0cc37
-    }
jorton f0cc37
+    apr_pool_cleanup_register((*new)->pool, *new, dir_cleanup,
jorton f0cc37
+                              apr_pool_cleanup_null);
jorton f0cc37
+    return APR_SUCCESS;
jorton f0cc37
 }
jorton f0cc37
 
jorton f0cc37
 apr_status_t apr_dir_close(apr_dir_t *thedir)
jorton f0cc37
@@ -139,9 +139,28 @@
jorton f0cc37
 #endif
jorton f0cc37
 #if APR_HAS_THREADS && defined(_POSIX_THREAD_SAFE_FUNCTIONS) \
jorton f0cc37
                     && !defined(READDIR_IS_THREAD_SAFE)
jorton f0cc37
+#ifdef HAVE_READDIR64_R
jorton f0cc37
+    struct dirent64 *retent;
jorton f0cc37
+
jorton f0cc37
+    /* If LFS is enabled and readdir64_r is available, readdir64_r is
jorton f0cc37
+     * used in preference to readdir_r.  This allows directories to be
jorton f0cc37
+     * read which contain a (64-bit) inode number which doesn't fit
jorton f0cc37
+     * into the 32-bit apr_ino_t, iff the caller doesn't actually care
jorton f0cc37
+     * about the inode number (i.e. wanted & APR_FINFO_INODE == 0).
jorton f0cc37
+     * (such inodes may be seen in some wonky NFS environments)
jorton f0cc37
+     *
jorton f0cc37
+     * Similarly, if the d_off field cannot be reprented in a 32-bit
jorton f0cc37
+     * offset, the libc readdir_r() would barf; using readdir64_r
jorton f0cc37
+     * bypasses that case entirely since APR does not care about
jorton f0cc37
+     * d_off. */
jorton f0cc37
+
jorton f0cc37
+    ret = readdir64_r(thedir->dirstruct, thedir->entry, &retent);
jorton f0cc37
+#else
jorton f0cc37
+
jorton f0cc37
     struct dirent *retent;
jorton f0cc37
 
jorton f0cc37
     ret = readdir_r(thedir->dirstruct, thedir->entry, &retent);
jorton f0cc37
+#endif
jorton f0cc37
 
jorton f0cc37
     /* Avoid the Linux problem where at end-of-directory thedir->entry
jorton f0cc37
      * is set to NULL, but ret = APR_SUCCESS.
jorton f0cc37
@@ -191,9 +210,22 @@
jorton f0cc37
 #endif
jorton f0cc37
 #ifdef DIRENT_INODE
jorton f0cc37
     if (thedir->entry->DIRENT_INODE && thedir->entry->DIRENT_INODE != -1) {
jorton f0cc37
+#ifdef HAVE_READDIR64_R
jorton f0cc37
+        /* If readdir64_r is used, check for the overflow case of trying
jorton f0cc37
+         * to fit a 64-bit integer into a 32-bit integer. */
jorton f0cc37
+        if (sizeof(apr_ino_t) >= sizeof(retent->DIRENT_INODE)
jorton f0cc37
+            || (apr_ino_t)retent->DIRENT_INODE == retent->DIRENT_INODE) {
jorton f0cc37
+            wanted &= ~APR_FINFO_INODE;
jorton f0cc37
+        } else {
jorton f0cc37
+            /* Prevent the fallback code below from filling in the
jorton f0cc37
+             * inode if the stat call fails. */
jorton f0cc37
+            retent->DIRENT_INODE = 0;
jorton f0cc37
+        }
jorton f0cc37
+#else
jorton f0cc37
         wanted &= ~APR_FINFO_INODE;
jorton f0cc37
+#endif /* HAVE_READDIR64_R */
jorton f0cc37
     }
jorton f0cc37
-#endif
jorton f0cc37
+#endif /* DIRENT_INODE */
jorton f0cc37
 
jorton f0cc37
     wanted &= ~APR_FINFO_NAME;
jorton f0cc37
 
jorton f0cc37
--- apr-0.9.6/file_io/unix/filestat.c.readdir64
jorton f0cc37
+++ apr-0.9.6/file_io/unix/filestat.c
jorton f0cc37
@@ -77,9 +77,18 @@
jorton f0cc37
     finfo->user = info->st_uid;
jorton f0cc37
     finfo->group = info->st_gid;
jorton f0cc37
     finfo->size = info->st_size;
jorton f0cc37
-    finfo->inode = info->st_ino;
jorton f0cc37
     finfo->device = info->st_dev;
jorton f0cc37
     finfo->nlink = info->st_nlink;
jorton f0cc37
+
jorton f0cc37
+    /* Check for overflow if storing a 64-bit st_ino in a 32-bit
jorton f0cc37
+     * apr_ino_t for LFS builds: */
jorton f0cc37
+    if (sizeof(apr_ino_t) >= sizeof(info->st_ino)
jorton f0cc37
+        || (apr_ino_t)info->st_ino == info->st_ino) {
jorton f0cc37
+        finfo->inode = info->st_ino;
jorton f0cc37
+    } else {
jorton f0cc37
+        finfo->valid &= ~APR_FINFO_INODE;
jorton f0cc37
+    }
jorton f0cc37
+
jorton f0cc37
     apr_time_ansi_put(&finfo->atime, info->st_atime);
jorton f0cc37
     apr_time_ansi_put(&finfo->mtime, info->st_mtime);
jorton f0cc37
     apr_time_ansi_put(&finfo->ctime, info->st_ctime);
jorton f0cc37
--- apr-0.9.6/include/arch/unix/apr_arch_file_io.h.readdir64
jorton f0cc37
+++ apr-0.9.6/include/arch/unix/apr_arch_file_io.h
jorton f0cc37
@@ -108,7 +108,11 @@
jorton f0cc37
     apr_pool_t *pool;
jorton f0cc37
     char *dirname;
jorton f0cc37
     DIR *dirstruct;
jorton f0cc37
+#ifdef HAVE_READDIR64_R
jorton f0cc37
+    struct dirent64 *entry;
jorton f0cc37
+#else
jorton f0cc37
     struct dirent *entry;
jorton f0cc37
+#endif
jorton f0cc37
 };
jorton f0cc37
 
jorton f0cc37
 apr_status_t apr_unix_file_cleanup(void *);
jorton f0cc37
--- apr-0.9.6/configure.in.readdir64
jorton f0cc37
+++ apr-0.9.6/configure.in
jorton f0cc37
@@ -1310,6 +1310,10 @@
jorton f0cc37
 AC_CHECK_FUNCS(memchr, have_memchr="1", have_memchr="0")
jorton f0cc37
 AC_CHECK_FUNCS($int64_strfn, have_int64_strfn="1", have_int64_strfn="0")
jorton f0cc37
 
jorton f0cc37
+if test "$ac_cv_sizeof_long" = "4"; then
jorton f0cc37
+   AC_CHECK_FUNCS([readdir64_r])
jorton f0cc37
+fi
jorton f0cc37
+
jorton f0cc37
 dnl ----------------------------- We have a fallback position
jorton f0cc37
 if test "$have_int64_strfn" = "0" && test "$int64_strfn" = "strtoll"; then
jorton f0cc37
     int64_strfn="strtoq"