Blame SOURCES/findutils-4.6.0-fts-update.patch

2b3154
From f3337786e55909538aacfd7c29b1cf58ff444fbf Mon Sep 17 00:00:00 2001
2b3154
From: Kamil Dudka <kdudka@redhat.com>
2b3154
Date: Mon, 12 Feb 2018 12:45:36 +0100
2b3154
Subject: [PATCH 1/4] import gnulib's FTS module from upstream commit 281b825e
2b3154
2b3154
---
2b3154
 gl/lib/fts.c  | 424 +++++++++++++++++++++++++++++-----------------------------
2b3154
 gl/lib/fts_.h |  10 +-
2b3154
 2 files changed, 221 insertions(+), 213 deletions(-)
2b3154
2b3154
diff --git a/gl/lib/fts.c b/gl/lib/fts.c
2b3154
index c91d7a1..bfa73e3 100644
2b3154
--- a/gl/lib/fts.c
2b3154
+++ b/gl/lib/fts.c
2b3154
@@ -1,6 +1,6 @@
2b3154
 /* Traverse a file hierarchy.
2b3154
 
2b3154
-   Copyright (C) 2004-2015 Free Software Foundation, Inc.
2b3154
+   Copyright (C) 2004-2018 Free Software Foundation, Inc.
2b3154
 
2b3154
    This program is free software: you can redistribute it and/or modify
2b3154
    it under the terms of the GNU General Public License as published by
2b3154
@@ -13,7 +13,7 @@
2b3154
    GNU General Public License for more details.
2b3154
 
2b3154
    You should have received a copy of the GNU General Public License
2b3154
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
2b3154
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
2b3154
 
2b3154
 /*-
2b3154
  * Copyright (c) 1990, 1993, 1994
2b3154
@@ -46,9 +46,9 @@
2b3154
 
2b3154
 #include <config.h>
2b3154
 
2b3154
-#if defined(LIBC_SCCS) && !defined(lint)
2b3154
+#if defined LIBC_SCCS && !defined GCC_LINT && !defined lint
2b3154
 static char sccsid[] = "@(#)fts.c       8.6 (Berkeley) 8/14/94";
2b3154
-#endif /* LIBC_SCCS and not lint */
2b3154
+#endif
2b3154
 
2b3154
 #include "fts_.h"
2b3154
 
2b3154
@@ -71,11 +71,7 @@ static char sccsid[] = "@(#)fts.c       8.6 (Berkeley) 8/14/94";
2b3154
 
2b3154
 #if ! _LIBC
2b3154
 # include "fcntl--.h"
2b3154
-# include "dirent--.h"
2b3154
-# include "unistd--.h"
2b3154
-/* FIXME - use fcntl(F_DUPFD_CLOEXEC)/openat(O_CLOEXEC) once they are
2b3154
-   supported.  */
2b3154
-# include "cloexec.h"
2b3154
+# include "flexmember.h"
2b3154
 # include "openat.h"
2b3154
 # include "same-inode.h"
2b3154
 #endif
2b3154
@@ -202,6 +198,14 @@ enum Fts_stat
2b3154
     while (false)
2b3154
 #endif
2b3154
 
2b3154
+#ifndef FALLTHROUGH
2b3154
+# if __GNUC__ < 7
2b3154
+#  define FALLTHROUGH ((void) 0)
2b3154
+# else
2b3154
+#  define FALLTHROUGH __attribute__ ((__fallthrough__))
2b3154
+# endif
2b3154
+#endif
2b3154
+
2b3154
 static FTSENT   *fts_alloc (FTS *, const char *, size_t) internal_function;
2b3154
 static FTSENT   *fts_build (FTS *, int) internal_function;
2b3154
 static void      fts_lfree (FTSENT *) internal_function;
2b3154
@@ -296,14 +300,13 @@ static DIR *
2b3154
 internal_function
2b3154
 opendirat (int fd, char const *dir, int extra_flags, int *pdir_fd)
2b3154
 {
2b3154
-  int new_fd = openat (fd, dir,
2b3154
-                       (O_RDONLY | O_DIRECTORY | O_NOCTTY | O_NONBLOCK
2b3154
-                        | extra_flags));
2b3154
+  int open_flags = (O_RDONLY | O_CLOEXEC | O_DIRECTORY | O_NOCTTY
2b3154
+                    | O_NONBLOCK | extra_flags);
2b3154
+  int new_fd = openat (fd, dir, open_flags);
2b3154
   DIR *dirp;
2b3154
 
2b3154
   if (new_fd < 0)
2b3154
     return NULL;
2b3154
-  set_cloexec_flag (new_fd, true);
2b3154
   dirp = fdopendir (new_fd);
2b3154
   if (dirp)
2b3154
     *pdir_fd = new_fd;
2b3154
@@ -366,15 +369,13 @@ static int
2b3154
 internal_function
2b3154
 diropen (FTS const *sp, char const *dir)
2b3154
 {
2b3154
-  int open_flags = (O_SEARCH | O_DIRECTORY | O_NOCTTY | O_NONBLOCK
2b3154
+  int open_flags = (O_SEARCH | O_CLOEXEC | O_DIRECTORY | O_NOCTTY | O_NONBLOCK
2b3154
                     | (ISSET (FTS_PHYSICAL) ? O_NOFOLLOW : 0)
2b3154
                     | (ISSET (FTS_NOATIME) ? O_NOATIME : 0));
2b3154
 
2b3154
   int fd = (ISSET (FTS_CWDFD)
2b3154
             ? openat (sp->fts_cwd_fd, dir, open_flags)
2b3154
             : open (dir, open_flags));
2b3154
-  if (0 <= fd)
2b3154
-    set_cloexec_flag (fd, true);
2b3154
   return fd;
2b3154
 }
2b3154
 
2b3154
@@ -470,6 +471,7 @@ fts_open (char * const *argv,
2b3154
                 if ((parent = fts_alloc(sp, "", 0)) == NULL)
2b3154
                         goto mem2;
2b3154
                 parent->fts_level = FTS_ROOTPARENTLEVEL;
2b3154
+                parent->fts_n_dirs_remaining = -1;
2b3154
           }
2b3154
 
2b3154
         /* The classic fts implementation would call fts_stat with
2b3154
@@ -656,39 +658,139 @@ fts_close (FTS *sp)
2b3154
         return (0);
2b3154
 }
2b3154
 
2b3154
+/* Minimum link count of a traditional Unix directory.  When leaf
2b3154
+   optimization is OK and MIN_DIR_NLINK <= st_nlink, then st_nlink is
2b3154
+   an upper bound on the number of subdirectories (counting "." and
2b3154
+   "..").  */
2b3154
+enum { MIN_DIR_NLINK = 2 };
2b3154
+
2b3154
+/* Whether leaf optimization is OK for a directory.  */
2b3154
+enum leaf_optimization
2b3154
+  {
2b3154
+    /* st_nlink is not reliable for this directory's subdirectories.  */
2b3154
+    NO_LEAF_OPTIMIZATION,
2b3154
+
2b3154
+    /* Leaf optimization is OK, but is not useful for avoiding stat calls.  */
2b3154
+    OK_LEAF_OPTIMIZATION,
2b3154
+
2b3154
+    /* Leaf optimization is not only OK: it is useful for avoiding
2b3154
+       stat calls, because dirent.d_type does not work.  */
2b3154
+    NOSTAT_LEAF_OPTIMIZATION
2b3154
+  };
2b3154
+
2b3154
 #if defined __linux__ \
2b3154
   && HAVE_SYS_VFS_H && HAVE_FSTATFS && HAVE_STRUCT_STATFS_F_TYPE
2b3154
 
2b3154
 # include <sys/vfs.h>
2b3154
 
2b3154
 /* Linux-specific constants from coreutils' src/fs.h */
2b3154
-# define S_MAGIC_TMPFS 0x1021994
2b3154
+# define S_MAGIC_AFS 0x5346414F
2b3154
 # define S_MAGIC_NFS 0x6969
2b3154
+# define S_MAGIC_PROC 0x9FA0
2b3154
 # define S_MAGIC_REISERFS 0x52654973
2b3154
+# define S_MAGIC_TMPFS 0x1021994
2b3154
 # define S_MAGIC_XFS 0x58465342
2b3154
-# define S_MAGIC_PROC 0x9FA0
2b3154
 
2b3154
-/* Return false if it is easy to determine the file system type of
2b3154
-   the directory on which DIR_FD is open, and sorting dirents on
2b3154
-   inode numbers is known not to improve traversal performance with
2b3154
-   that type of file system.  Otherwise, return true.  */
2b3154
+# ifdef HAVE___FSWORD_T
2b3154
+typedef __fsword_t fsword;
2b3154
+# else
2b3154
+typedef long int fsword;
2b3154
+# endif
2b3154
+
2b3154
+/* Map a stat.st_dev number to a file system type number f_ftype.  */
2b3154
+struct dev_type
2b3154
+{
2b3154
+  dev_t st_dev;
2b3154
+  fsword f_type;
2b3154
+};
2b3154
+
2b3154
+/* Use a tiny initial size.  If a traversal encounters more than
2b3154
+   a few devices, the cost of growing/rehashing this table will be
2b3154
+   rendered negligible by the number of inodes processed.  */
2b3154
+enum { DEV_TYPE_HT_INITIAL_SIZE = 13 };
2b3154
+
2b3154
+static size_t
2b3154
+dev_type_hash (void const *x, size_t table_size)
2b3154
+{
2b3154
+  struct dev_type const *ax = x;
2b3154
+  uintmax_t dev = ax->st_dev;
2b3154
+  return dev % table_size;
2b3154
+}
2b3154
+
2b3154
 static bool
2b3154
-dirent_inode_sort_may_be_useful (int dir_fd)
2b3154
+dev_type_compare (void const *x, void const *y)
2b3154
+{
2b3154
+  struct dev_type const *ax = x;
2b3154
+  struct dev_type const *ay = y;
2b3154
+  return ax->st_dev == ay->st_dev;
2b3154
+}
2b3154
+
2b3154
+/* Return the file system type of P, or 0 if not known.
2b3154
+   Try to cache known values.  */
2b3154
+
2b3154
+static fsword
2b3154
+filesystem_type (FTSENT const *p)
2b3154
+{
2b3154
+  FTS *sp = p->fts_fts;
2b3154
+  Hash_table *h = sp->fts_leaf_optimization_works_ht;
2b3154
+  struct dev_type *ent;
2b3154
+  struct statfs fs_buf;
2b3154
+
2b3154
+  /* If we're not in CWDFD mode, don't bother with this optimization,
2b3154
+     since the caller is not serious about performance.  */
2b3154
+  if (!ISSET (FTS_CWDFD))
2b3154
+    return 0;
2b3154
+
2b3154
+  if (! h)
2b3154
+    h = sp->fts_leaf_optimization_works_ht
2b3154
+      = hash_initialize (DEV_TYPE_HT_INITIAL_SIZE, NULL, dev_type_hash,
2b3154
+                         dev_type_compare, free);
2b3154
+  if (h)
2b3154
+    {
2b3154
+      struct dev_type tmp;
2b3154
+      tmp.st_dev = p->fts_statp->st_dev;
2b3154
+      ent = hash_lookup (h, &tmp);
2b3154
+      if (ent)
2b3154
+        return ent->f_type;
2b3154
+    }
2b3154
+
2b3154
+  /* Look-up failed.  Query directly and cache the result.  */
2b3154
+  if (fstatfs (p->fts_fts->fts_cwd_fd, &fs_buf) != 0)
2b3154
+    return 0;
2b3154
+
2b3154
+  if (h)
2b3154
+    {
2b3154
+      struct dev_type *t2 = malloc (sizeof *t2);
2b3154
+      if (t2)
2b3154
+        {
2b3154
+          t2->st_dev = p->fts_statp->st_dev;
2b3154
+          t2->f_type = fs_buf.f_type;
2b3154
+
2b3154
+          ent = hash_insert (h, t2);
2b3154
+          if (ent)
2b3154
+            fts_assert (ent == t2);
2b3154
+          else
2b3154
+            free (t2);
2b3154
+        }
2b3154
+    }
2b3154
+
2b3154
+  return fs_buf.f_type;
2b3154
+}
2b3154
+
2b3154
+/* Return false if it is easy to determine the file system type of the
2b3154
+   directory P, and sorting dirents on inode numbers is known not to
2b3154
+   improve traversal performance with that type of file system.
2b3154
+   Otherwise, return true.  */
2b3154
+static bool
2b3154
+dirent_inode_sort_may_be_useful (FTSENT const *p)
2b3154
 {
2b3154
   /* Skip the sort only if we can determine efficiently
2b3154
      that skipping it is the right thing to do.
2b3154
      The cost of performing an unnecessary sort is negligible,
2b3154
      while the cost of *not* performing it can be O(N^2) with
2b3154
      a very large constant.  */
2b3154
-  struct statfs fs_buf;
2b3154
-
2b3154
-  /* If fstatfs fails, assume sorting would be useful.  */
2b3154
-  if (fstatfs (dir_fd, &fs_buf) != 0)
2b3154
-    return true;
2b3154
 
2b3154
-  /* FIXME: what about when f_type is not an integral type?
2b3154
-     deal with that if/when it's encountered.  */
2b3154
-  switch (fs_buf.f_type)
2b3154
+  switch (filesystem_type (p))
2b3154
     {
2b3154
     case S_MAGIC_TMPFS:
2b3154
     case S_MAGIC_NFS:
2b3154
@@ -701,133 +803,58 @@ dirent_inode_sort_may_be_useful (int dir_fd)
2b3154
     }
2b3154
 }
2b3154
 
2b3154
-/* Given a file descriptor DIR_FD open on a directory D,
2b3154
-   return true if it is valid to apply the leaf-optimization
2b3154
-   technique of counting directories in D via stat.st_nlink.  */
2b3154
-static bool
2b3154
-leaf_optimization_applies (int dir_fd)
2b3154
+/* Given an FTS entry P for a directory D,
2b3154
+   return true if it is both useful and valid to apply leaf optimization.
2b3154
+   The optimization is useful only for file systems that lack usable
2b3154
+   dirent.d_type info.  The optimization is valid if an st_nlink value
2b3154
+   of at least MIN_DIR_NLINK is an upper bound on the number of
2b3154
+   subdirectories of D, counting "." and ".."  as subdirectories.  */
2b3154
+static enum leaf_optimization
2b3154
+leaf_optimization (FTSENT const *p)
2b3154
 {
2b3154
-  struct statfs fs_buf;
2b3154
-
2b3154
-  /* If fstatfs fails, assume we can't use the optimization.  */
2b3154
-  if (fstatfs (dir_fd, &fs_buf) != 0)
2b3154
-    return false;
2b3154
-
2b3154
-  /* FIXME: do we need to detect AFS mount points?  I doubt it,
2b3154
-     unless fstatfs can report S_MAGIC_REISERFS for such a directory.  */
2b3154
-
2b3154
-  switch (fs_buf.f_type)
2b3154
+  switch (filesystem_type (p))
2b3154
     {
2b3154
-    case S_MAGIC_NFS:
2b3154
-      /* NFS provides usable dirent.d_type but not necessarily for all entries
2b3154
-         of large directories.  See <https://bugzilla.redhat.com/1252549>.  */
2b3154
-      return true;
2b3154
-
2b3154
-      /* List here the file system types that lack usable dirent.d_type
2b3154
+      /* List here the file system types that may lack usable dirent.d_type
2b3154
          info, yet for which the optimization does apply.  */
2b3154
     case S_MAGIC_REISERFS:
2b3154
-    case S_MAGIC_XFS:
2b3154
-      return true;
2b3154
-
2b3154
+    case S_MAGIC_XFS: /* XFS lacked it until 2013-08-22 commit.  */
2b3154
+      return NOSTAT_LEAF_OPTIMIZATION;
2b3154
+
2b3154
+    case 0:
2b3154
+      /* Leaf optimization is unsafe if the file system type is unknown.  */
2b3154
+      FALLTHROUGH;
2b3154
+    case S_MAGIC_AFS:
2b3154
+      /* Although AFS mount points are not counted in st_nlink, they
2b3154
+         act like directories.  See <https://bugs.debian.org/143111>.  */
2b3154
+      FALLTHROUGH;
2b3154
+    case S_MAGIC_NFS:
2b3154
+      /* NFS provides usable dirent.d_type but not necessarily for all entries
2b3154
+         of large directories, so as per <https://bugzilla.redhat.com/1252549>
2b3154
+         NFS should return true.  However st_nlink values are not accurate on
2b3154
+         all implementations as per <https://bugzilla.redhat.com/1299169>.  */
2b3154
+      FALLTHROUGH;
2b3154
     case S_MAGIC_PROC:
2b3154
-      /* Explicitly listing this or any other file system type for which
2b3154
-         the optimization is not applicable is not necessary, but we leave
2b3154
-         it here to document the risk.  Per http://bugs.debian.org/143111,
2b3154
-         /proc may have bogus stat.st_nlink values.  */
2b3154
-      /* fall through */
2b3154
+      /* Per <https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=143111> /proc
2b3154
+         may have bogus stat.st_nlink values.  */
2b3154
+      return NO_LEAF_OPTIMIZATION;
2b3154
+
2b3154
     default:
2b3154
-      return false;
2b3154
+      return OK_LEAF_OPTIMIZATION;
2b3154
     }
2b3154
 }
2b3154
 
2b3154
 #else
2b3154
 static bool
2b3154
-dirent_inode_sort_may_be_useful (int dir_fd _GL_UNUSED) { return true; }
2b3154
-static bool
2b3154
-leaf_optimization_applies (int dir_fd _GL_UNUSED) { return false; }
2b3154
-#endif
2b3154
-
2b3154
-/* link-count-optimization entry:
2b3154
-   map a stat.st_dev number to a boolean: leaf_optimization_works */
2b3154
-struct LCO_ent
2b3154
-{
2b3154
-  dev_t st_dev;
2b3154
-  bool opt_ok;
2b3154
-};
2b3154
-
2b3154
-/* Use a tiny initial size.  If a traversal encounters more than
2b3154
-   a few devices, the cost of growing/rehashing this table will be
2b3154
-   rendered negligible by the number of inodes processed.  */
2b3154
-enum { LCO_HT_INITIAL_SIZE = 13 };
2b3154
-
2b3154
-static size_t
2b3154
-LCO_hash (void const *x, size_t table_size)
2b3154
-{
2b3154
-  struct LCO_ent const *ax = x;
2b3154
-  return (uintmax_t) ax->st_dev % table_size;
2b3154
-}
2b3154
-
2b3154
-static bool
2b3154
-LCO_compare (void const *x, void const *y)
2b3154
+dirent_inode_sort_may_be_useful (FTSENT const *p _GL_UNUSED)
2b3154
 {
2b3154
-  struct LCO_ent const *ax = x;
2b3154
-  struct LCO_ent const *ay = y;
2b3154
-  return ax->st_dev == ay->st_dev;
2b3154
+  return true;
2b3154
 }
2b3154
-
2b3154
-/* Ask the same question as leaf_optimization_applies, but query
2b3154
-   the cache first (FTS.fts_leaf_optimization_works_ht), and if necessary,
2b3154
-   update that cache.  */
2b3154
-static bool
2b3154
-link_count_optimize_ok (FTSENT const *p)
2b3154
+static enum leaf_optimization
2b3154
+leaf_optimization (FTSENT const *p _GL_UNUSED)
2b3154
 {
2b3154
-  FTS *sp = p->fts_fts;
2b3154
-  Hash_table *h = sp->fts_leaf_optimization_works_ht;
2b3154
-  struct LCO_ent tmp;
2b3154
-  struct LCO_ent *ent;
2b3154
-  bool opt_ok;
2b3154
-  struct LCO_ent *t2;
2b3154
-
2b3154
-  /* If we're not in CWDFD mode, don't bother with this optimization,
2b3154
-     since the caller is not serious about performance. */
2b3154
-  if (!ISSET(FTS_CWDFD))
2b3154
-    return false;
2b3154
-
2b3154
-  /* map st_dev to the boolean, leaf_optimization_works */
2b3154
-  if (h == NULL)
2b3154
-    {
2b3154
-      h = sp->fts_leaf_optimization_works_ht
2b3154
-        = hash_initialize (LCO_HT_INITIAL_SIZE, NULL, LCO_hash,
2b3154
-                           LCO_compare, free);
2b3154
-      if (h == NULL)
2b3154
-        return false;
2b3154
-    }
2b3154
-  tmp.st_dev = p->fts_statp->st_dev;
2b3154
-  ent = hash_lookup (h, &tmp);
2b3154
-  if (ent)
2b3154
-    return ent->opt_ok;
2b3154
-
2b3154
-  /* Look-up failed.  Query directly and cache the result.  */
2b3154
-  t2 = malloc (sizeof *t2);
2b3154
-  if (t2 == NULL)
2b3154
-    return false;
2b3154
-
2b3154
-  /* Is it ok to perform the optimization in the dir, FTS_CWD_FD?  */
2b3154
-  opt_ok = leaf_optimization_applies (sp->fts_cwd_fd);
2b3154
-  t2->opt_ok = opt_ok;
2b3154
-  t2->st_dev = p->fts_statp->st_dev;
2b3154
-
2b3154
-  ent = hash_insert (h, t2);
2b3154
-  if (ent == NULL)
2b3154
-    {
2b3154
-      /* insertion failed */
2b3154
-      free (t2);
2b3154
-      return false;
2b3154
-    }
2b3154
-  fts_assert (ent == t2);
2b3154
-
2b3154
-  return opt_ok;
2b3154
+  return NO_LEAF_OPTIMIZATION;
2b3154
 }
2b3154
+#endif
2b3154
 
2b3154
 /*
2b3154
  * Special case of "/" at the end of the file name so that slashes aren't
2b3154
@@ -1014,13 +1041,11 @@ check_for_dir:
2b3154
                     if (p->fts_statp->st_size == FTS_STAT_REQUIRED)
2b3154
                       {
2b3154
                         FTSENT *parent = p->fts_parent;
2b3154
-                        if (FTS_ROOTLEVEL < p->fts_level
2b3154
-                            /* ->fts_n_dirs_remaining is not valid
2b3154
-                               for command-line-specified names.  */
2b3154
-                            && parent->fts_n_dirs_remaining == 0
2b3154
+                        if (parent->fts_n_dirs_remaining == 0
2b3154
                             && ISSET(FTS_NOSTAT)
2b3154
                             && ISSET(FTS_PHYSICAL)
2b3154
-                            && link_count_optimize_ok (parent))
2b3154
+                            && (leaf_optimization (parent)
2b3154
+                                == NOSTAT_LEAF_OPTIMIZATION))
2b3154
                           {
2b3154
                             /* nothing more needed */
2b3154
                           }
2b3154
@@ -1029,7 +1054,8 @@ check_for_dir:
2b3154
                             p->fts_info = fts_stat(sp, p, false);
2b3154
                             if (S_ISDIR(p->fts_statp->st_mode)
2b3154
                                 && p->fts_level != FTS_ROOTLEVEL
2b3154
-                                && parent->fts_n_dirs_remaining)
2b3154
+                                && 0 < parent->fts_n_dirs_remaining
2b3154
+                                && parent->fts_n_dirs_remaining != (nlink_t) -1)
2b3154
                                   parent->fts_n_dirs_remaining--;
2b3154
                           }
2b3154
                       }
2b3154
@@ -1298,8 +1324,6 @@ fts_build (register FTS *sp, int type)
2b3154
         bool descend;
2b3154
         bool doadjust;
2b3154
         ptrdiff_t level;
2b3154
-        nlink_t nlinks;
2b3154
-        bool nostat;
2b3154
         size_t len, maxlen, new_len;
2b3154
         char *cp;
2b3154
         int dir_fd;
2b3154
@@ -1369,24 +1393,6 @@ fts_build (register FTS *sp, int type)
2b3154
            sorting, yet not so large that we risk exhausting memory.  */
2b3154
         max_entries = sp->fts_compar ? SIZE_MAX : FTS_MAX_READDIR_ENTRIES;
2b3154
 
2b3154
-        /*
2b3154
-         * Nlinks is the number of possible entries of type directory in the
2b3154
-         * directory if we're cheating on stat calls, 0 if we're not doing
2b3154
-         * any stat calls at all, (nlink_t) -1 if we're statting everything.
2b3154
-         */
2b3154
-        if (type == BNAMES) {
2b3154
-                nlinks = 0;
2b3154
-                /* Be quiet about nostat, GCC. */
2b3154
-                nostat = false;
2b3154
-        } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) {
2b3154
-                nlinks = (cur->fts_statp->st_nlink
2b3154
-                          - (ISSET(FTS_SEEDOT) ? 0 : 2));
2b3154
-                nostat = true;
2b3154
-        } else {
2b3154
-                nlinks = -1;
2b3154
-                nostat = false;
2b3154
-        }
2b3154
-
2b3154
         /*
2b3154
          * If we're going to need to stat anything or we want to descend
2b3154
          * and stay in the directory, chdir.  If this fails we keep going,
2b3154
@@ -1408,15 +1414,22 @@ fts_build (register FTS *sp, int type)
2b3154
                the required dirp and dir_fd.  */
2b3154
             descend = true;
2b3154
           }
2b3154
-        else if (nlinks || type == BREAD) {
2b3154
+        else
2b3154
+          {
2b3154
+            /* Try to descend unless it is a names-only fts_children,
2b3154
+               or the directory is known to lack subdirectories.  */
2b3154
+            descend = (type != BNAMES
2b3154
+                       && ! (ISSET (FTS_NOSTAT) && ISSET (FTS_PHYSICAL)
2b3154
+                             && ! ISSET (FTS_SEEDOT)
2b3154
+                             && cur->fts_statp->st_nlink == MIN_DIR_NLINK
2b3154
+                             && (leaf_optimization (cur)
2b3154
+                                 != NO_LEAF_OPTIMIZATION)));
2b3154
+            if (descend || type == BREAD)
2b3154
+              {
2b3154
                 if (ISSET(FTS_CWDFD))
2b3154
-                  {
2b3154
-                    dir_fd = dup (dir_fd);
2b3154
-                    if (0 <= dir_fd)
2b3154
-                      set_cloexec_flag (dir_fd, true);
2b3154
-                  }
2b3154
+                  dir_fd = fcntl (dir_fd, F_DUPFD_CLOEXEC, STDERR_FILENO + 1);
2b3154
                 if (dir_fd < 0 || fts_safe_changedir(sp, cur, dir_fd, NULL)) {
2b3154
-                        if (nlinks && type == BREAD)
2b3154
+                        if (descend && type == BREAD)
2b3154
                                 cur->fts_errno = errno;
2b3154
                         cur->fts_flags |= FTS_DONTCHDIR;
2b3154
                         descend = false;
2b3154
@@ -1426,8 +1439,8 @@ fts_build (register FTS *sp, int type)
2b3154
                         cur->fts_dirp = NULL;
2b3154
                 } else
2b3154
                         descend = true;
2b3154
-        } else
2b3154
-                descend = false;
2b3154
+              }
2b3154
+          }
2b3154
 
2b3154
         /*
2b3154
          * Figure out the max file name length that can be stored in the
2b3154
@@ -1458,11 +1471,19 @@ fts_build (register FTS *sp, int type)
2b3154
         tail = NULL;
2b3154
         nitems = 0;
2b3154
         while (cur->fts_dirp) {
2b3154
-                bool is_dir;
2b3154
                 size_t d_namelen;
2b3154
+                __set_errno (0);
2b3154
                 struct dirent *dp = readdir(cur->fts_dirp);
2b3154
-                if (dp == NULL)
2b3154
+                if (dp == NULL) {
2b3154
+                        if (errno) {
2b3154
+                                cur->fts_errno = errno;
2b3154
+                                /* If we've not read any items yet, treat
2b3154
+                                   the error as if we can't access the dir.  */
2b3154
+                                cur->fts_info = (continue_readdir || nitems)
2b3154
+                                                ? FTS_ERR : FTS_DNR;
2b3154
+                        }
2b3154
                         break;
2b3154
+                }
2b3154
                 if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
2b3154
                         continue;
2b3154
 
2b3154
@@ -1550,19 +1571,10 @@ mem1:                           saved_errno = errno;
2b3154
                            to caller, when possible.  */
2b3154
                         set_stat_type (p->fts_statp, D_TYPE (dp));
2b3154
                         fts_set_stat_required(p, !skip_stat);
2b3154
-                        is_dir = (ISSET(FTS_PHYSICAL)
2b3154
-                                  && DT_MUST_BE(dp, DT_DIR));
2b3154
                 } else {
2b3154
                         p->fts_info = fts_stat(sp, p, false);
2b3154
-                        is_dir = (p->fts_info == FTS_D
2b3154
-                                  || p->fts_info == FTS_DC
2b3154
-                                  || p->fts_info == FTS_DOT);
2b3154
                 }
2b3154
 
2b3154
-                /* Decrement link count if applicable. */
2b3154
-                if (nlinks > 0 && is_dir)
2b3154
-                        nlinks -= nostat;
2b3154
-
2b3154
                 /* We walk in directory order so "ls -f" doesn't get upset. */
2b3154
                 p->fts_link = NULL;
2b3154
                 if (head == NULL)
2b3154
@@ -1621,7 +1633,8 @@ mem1:                           saved_errno = errno;
2b3154
 
2b3154
         /* If didn't find anything, return NULL. */
2b3154
         if (!nitems) {
2b3154
-                if (type == BREAD)
2b3154
+                if (type == BREAD
2b3154
+                    && cur->fts_info != FTS_DNR && cur->fts_info != FTS_ERR)
2b3154
                         cur->fts_info = FTS_DP;
2b3154
                 fts_lfree(head);
2b3154
                 return (NULL);
2b3154
@@ -1633,8 +1646,7 @@ mem1:                           saved_errno = errno;
2b3154
            inode numbers.  */
2b3154
         if (nitems > _FTS_INODE_SORT_DIR_ENTRIES_THRESHOLD
2b3154
             && !sp->fts_compar
2b3154
-            && ISSET (FTS_CWDFD)
2b3154
-            && dirent_inode_sort_may_be_useful (sp->fts_cwd_fd)) {
2b3154
+            && dirent_inode_sort_may_be_useful (cur)) {
2b3154
                 sp->fts_compar = fts_compare_ino;
2b3154
                 head = fts_sort (sp, head, nitems);
2b3154
                 sp->fts_compar = NULL;
2b3154
@@ -1757,7 +1769,7 @@ fd_ring_check (FTS const *sp)
2b3154
   I_ring fd_w = sp->fts_fd_ring;
2b3154
 
2b3154
   int cwd_fd = sp->fts_cwd_fd;
2b3154
-  cwd_fd = dup (cwd_fd);
2b3154
+  cwd_fd = fcntl (cwd_fd, F_DUPFD_CLOEXEC, STDERR_FILENO + 1);
2b3154
   char *dot = getcwdat (cwd_fd, NULL, 0);
2b3154
   error (0, 0, "===== check ===== cwd: %s", dot);
2b3154
   free (dot);
2b3154
@@ -1766,7 +1778,8 @@ fd_ring_check (FTS const *sp)
2b3154
       int fd = i_ring_pop (&fd_w);
2b3154
       if (0 <= fd)
2b3154
         {
2b3154
-          int parent_fd = openat (cwd_fd, "..", O_SEARCH | O_NOATIME);
2b3154
+          int open_flags = O_SEARCH | O_CLOEXEC | O_NOATIME;
2b3154
+          int parent_fd = openat (cwd_fd, "..", open_flags);
2b3154
           if (parent_fd < 0)
2b3154
             {
2b3154
               // Warn?
2b3154
@@ -1795,7 +1808,6 @@ internal_function
2b3154
 fts_stat(FTS *sp, register FTSENT *p, bool follow)
2b3154
 {
2b3154
         struct stat *sbp = p->fts_statp;
2b3154
-        int saved_errno;
2b3154
 
2b3154
         if (p->fts_level == FTS_ROOTLEVEL && ISSET(FTS_COMFOLLOW))
2b3154
                 follow = true;
2b3154
@@ -1807,13 +1819,12 @@ fts_stat(FTS *sp, register FTSENT *p, bool follow)
2b3154
          */
2b3154
         if (ISSET(FTS_LOGICAL) || follow) {
2b3154
                 if (stat(p->fts_accpath, sbp)) {
2b3154
-                        saved_errno = errno;
2b3154
                         if (errno == ENOENT
2b3154
                             && lstat(p->fts_accpath, sbp) == 0) {
2b3154
                                 __set_errno (0);
2b3154
                                 return (FTS_SLNONE);
2b3154
                         }
2b3154
-                        p->fts_errno = saved_errno;
2b3154
+                        p->fts_errno = errno;
2b3154
                         goto err;
2b3154
                 }
2b3154
         } else if (fstatat(sp->fts_cwd_fd, p->fts_accpath, sbp,
2b3154
@@ -1824,8 +1835,11 @@ err:            memset(sbp, 0, sizeof(struct stat));
2b3154
         }
2b3154
 
2b3154
         if (S_ISDIR(sbp->st_mode)) {
2b3154
-                p->fts_n_dirs_remaining = (sbp->st_nlink
2b3154
-                                           - (ISSET(FTS_SEEDOT) ? 0 : 2));
2b3154
+                p->fts_n_dirs_remaining
2b3154
+                  = ((sbp->st_nlink < MIN_DIR_NLINK
2b3154
+                      || p->fts_level <= FTS_ROOTLEVEL)
2b3154
+                     ? -1
2b3154
+                     : sbp->st_nlink - (ISSET (FTS_SEEDOT) ? 0 : MIN_DIR_NLINK));
2b3154
                 if (ISDOT(p->fts_name)) {
2b3154
                         /* Command-line "." and ".." are real directories. */
2b3154
                         return (p->fts_level == FTS_ROOTLEVEL ? FTS_D : FTS_DOT);
2b3154
@@ -1914,17 +1928,7 @@ fts_alloc (FTS *sp, const char *name, register size_t namelen)
2b3154
          * The file name is a variable length array.  Allocate the FTSENT
2b3154
          * structure and the file name in one chunk.
2b3154
          */
2b3154
-        len = offsetof(FTSENT, fts_name) + namelen + 1;
2b3154
-        /* Align the allocation size so that it works for FTSENT,
2b3154
-           so that trailing padding may be referenced by direct access
2b3154
-           to the flexible array members, without triggering undefined behavior
2b3154
-           by accessing bytes beyond the heap allocation.  This implicit access
2b3154
-           was seen for example with ISDOT() and GCC 5.1.1 at -O2.
2b3154
-           Do not use alignof (FTSENT) here, since C11 prohibits
2b3154
-           taking the alignment of a structure containing a flexible
2b3154
-           array member.  */
2b3154
-        len += alignof (max_align_t) - 1;
2b3154
-        len &= ~ (alignof (max_align_t) - 1);
2b3154
+        len = FLEXSIZEOF(FTSENT, fts_name, namelen + 1);
2b3154
         if ((p = malloc(len)) == NULL)
2b3154
                 return (NULL);
2b3154
 
2b3154
diff --git a/gl/lib/fts_.h b/gl/lib/fts_.h
2b3154
index b9a3f12..70cc9e3 100644
2b3154
--- a/gl/lib/fts_.h
2b3154
+++ b/gl/lib/fts_.h
2b3154
@@ -1,6 +1,6 @@
2b3154
 /* Traverse a file hierarchy.
2b3154
 
2b3154
-   Copyright (C) 2004-2015 Free Software Foundation, Inc.
2b3154
+   Copyright (C) 2004-2018 Free Software Foundation, Inc.
2b3154
 
2b3154
    This program is free software: you can redistribute it and/or modify
2b3154
    it under the terms of the GNU General Public License as published by
2b3154
@@ -13,7 +13,7 @@
2b3154
    GNU General Public License for more details.
2b3154
 
2b3154
    You should have received a copy of the GNU General Public License
2b3154
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
2b3154
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
2b3154
 
2b3154
 /*
2b3154
  * Copyright (c) 1989, 1993
2b3154
@@ -220,7 +220,11 @@ typedef struct _ftsent {
2b3154
         ptrdiff_t fts_level;            /* depth (-1 to N) */
2b3154
 
2b3154
         size_t fts_namelen;             /* strlen(fts_name) */
2b3154
-        nlink_t fts_n_dirs_remaining;   /* count down from st_nlink */
2b3154
+
2b3154
+        /* If not (nlink_t) -1, an upper bound on the number of
2b3154
+           remaining subdirectories of interest.  If this becomes
2b3154
+           zero, some work can be avoided.  */
2b3154
+        nlink_t fts_n_dirs_remaining;
2b3154
 
2b3154
 # define FTS_D           1              /* preorder directory */
2b3154
 # define FTS_DC          2              /* directory that causes cycles */
2b3154
-- 
2b3154
2.13.6
2b3154
2b3154
2b3154
From ea88dd373c60feab541fe037369805f326dc3494 Mon Sep 17 00:00:00 2001
2b3154
From: rpm-build <rpm-build>
2b3154
Date: Mon, 12 Feb 2018 18:58:30 +0100
2b3154
Subject: [PATCH 2/4] fts: remove dependency on gnulib's fleximember.h
2b3154
2b3154
... by reverting upstream commit edb9d82948cb23f67a19e1b435047a0570225df3
2b3154
---
2b3154
 gl/lib/fts.c | 13 +++++++++++--
2b3154
 1 file changed, 11 insertions(+), 2 deletions(-)
2b3154
2b3154
diff --git a/gl/lib/fts.c b/gl/lib/fts.c
2b3154
index bfa73e3..c37ebe2 100644
2b3154
--- a/gl/lib/fts.c
2b3154
+++ b/gl/lib/fts.c
2b3154
@@ -71,7 +71,6 @@ static char sccsid[] = "@(#)fts.c       8.6 (Berkeley) 8/14/94";
2b3154
 
2b3154
 #if ! _LIBC
2b3154
 # include "fcntl--.h"
2b3154
-# include "flexmember.h"
2b3154
 # include "openat.h"
2b3154
 # include "same-inode.h"
2b3154
 #endif
2b3154
@@ -1928,7 +1927,17 @@ fts_alloc (FTS *sp, const char *name, register size_t namelen)
2b3154
          * The file name is a variable length array.  Allocate the FTSENT
2b3154
          * structure and the file name in one chunk.
2b3154
          */
2b3154
-        len = FLEXSIZEOF(FTSENT, fts_name, namelen + 1);
2b3154
+        len = offsetof(FTSENT, fts_name) + namelen + 1;
2b3154
+        /* Align the allocation size so that it works for FTSENT,
2b3154
+           so that trailing padding may be referenced by direct access
2b3154
+           to the flexible array members, without triggering undefined behavior
2b3154
+           by accessing bytes beyond the heap allocation.  This implicit access
2b3154
+           was seen for example with ISDOT() and GCC 5.1.1 at -O2.
2b3154
+           Do not use alignof (FTSENT) here, since C11 prohibits
2b3154
+           taking the alignment of a structure containing a flexible
2b3154
+           array member.  */
2b3154
+        len += alignof (max_align_t) - 1;
2b3154
+        len &= ~ (alignof (max_align_t) - 1);
2b3154
         if ((p = malloc(len)) == NULL)
2b3154
                 return (NULL);
2b3154
 
2b3154
-- 
2b3154
2.13.6
2b3154
2b3154
2b3154
From 9c1720c99bbf8998dfdaa5976bca8bdc6d93f8e7 Mon Sep 17 00:00:00 2001
2b3154
From: Paul Eggert <eggert@cs.ucla.edu>
2b3154
Date: Thu, 5 Apr 2018 08:48:01 -0700
2b3154
Subject: [PATCH 3/4] fts: treat CIFS like NFS
2b3154
2b3154
Problem reported by Kamil Dudka in:
2b3154
https://lists.gnu.org/r/bug-gnulib/2018-04/msg00015.html
2b3154
* lib/fts.c (S_MAGIC_CIFS): New macro.
2b3154
(dirent_inode_sort_may_be_useful, leaf_optimization):
2b3154
Treat CIFS like NFS.
2b3154
2b3154
Upstream-commit: 2e53df541a30d438859087ed4b5a396e04697b9b
2b3154
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
2b3154
---
2b3154
 gl/lib/fts.c | 8 +++++++-
2b3154
 1 file changed, 7 insertions(+), 1 deletion(-)
2b3154
2b3154
diff --git a/gl/lib/fts.c b/gl/lib/fts.c
2b3154
index c37ebe2..508ceac 100644
2b3154
--- a/gl/lib/fts.c
2b3154
+++ b/gl/lib/fts.c
2b3154
@@ -684,6 +684,7 @@ enum leaf_optimization
2b3154
 
2b3154
 /* Linux-specific constants from coreutils' src/fs.h */
2b3154
 # define S_MAGIC_AFS 0x5346414F
2b3154
+# define S_MAGIC_CIFS 0xFF534D42
2b3154
 # define S_MAGIC_NFS 0x6969
2b3154
 # define S_MAGIC_PROC 0x9FA0
2b3154
 # define S_MAGIC_REISERFS 0x52654973
2b3154
@@ -791,8 +792,9 @@ dirent_inode_sort_may_be_useful (FTSENT const *p)
2b3154
 
2b3154
   switch (filesystem_type (p))
2b3154
     {
2b3154
-    case S_MAGIC_TMPFS:
2b3154
+    case S_MAGIC_CIFS:
2b3154
     case S_MAGIC_NFS:
2b3154
+    case S_MAGIC_TMPFS:
2b3154
       /* On a file system of any of these types, sorting
2b3154
          is unnecessary, and hence wasteful.  */
2b3154
       return false;
2b3154
@@ -826,6 +828,10 @@ leaf_optimization (FTSENT const *p)
2b3154
       /* Although AFS mount points are not counted in st_nlink, they
2b3154
          act like directories.  See <https://bugs.debian.org/143111>.  */
2b3154
       FALLTHROUGH;
2b3154
+    case S_MAGIC_CIFS:
2b3154
+      /* Leaf optimization causes 'find' to abort.  See
2b3154
+         <https://lists.gnu.org/r/bug-gnulib/2018-04/msg00015.html>.  */
2b3154
+      FALLTHROUGH;
2b3154
     case S_MAGIC_NFS:
2b3154
       /* NFS provides usable dirent.d_type but not necessarily for all entries
2b3154
          of large directories, so as per <https://bugzilla.redhat.com/1252549>
2b3154
-- 
2b3154
2.14.3
2b3154
2b3154
2b3154
From ff64329a046e76ba553c15373ed61bbed814d286 Mon Sep 17 00:00:00 2001
2b3154
From: Paul Eggert <eggert@cs.ucla.edu>
2b3154
Date: Wed, 11 Apr 2018 12:50:35 -0700
2b3154
Subject: [PATCH 4/4] fts: fix bug in find across filesystems
2b3154
2b3154
This fixes a bug I introduced last summer.
2b3154
Problem reported by Kamil Dudka in:
2b3154
https://lists.gnu.org/r/bug-gnulib/2018-04/msg00033.html
2b3154
* lib/fts.c (filesystem_type, dirent_inode_sort_may_be_useful)
2b3154
(leaf_optimization):
2b3154
New arg for file descriptor.  All callers changed.
2b3154
(fts_build): Check for whether inodes should be sorted
2b3154
before closing the directory.
2b3154
2b3154
Upstream-commit: 81b8c0d3be98f5a77403599de3d06329b3e7673e
2b3154
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
2b3154
---
2b3154
 gl/lib/fts.c | 55 +++++++++++++++++++++++++++++++------------------------
2b3154
 1 file changed, 31 insertions(+), 24 deletions(-)
2b3154
2b3154
diff --git a/gl/lib/fts.c b/gl/lib/fts.c
2b3154
index 508ceac..175f12a 100644
2b3154
--- a/gl/lib/fts.c
2b3154
+++ b/gl/lib/fts.c
2b3154
@@ -725,11 +725,12 @@ dev_type_compare (void const *x, void const *y)
2b3154
   return ax->st_dev == ay->st_dev;
2b3154
 }
2b3154
 
2b3154
-/* Return the file system type of P, or 0 if not known.
2b3154
+/* Return the file system type of P with file descriptor FD, or 0 if not known.
2b3154
+   If FD is negative, P's file descriptor is unavailable.
2b3154
    Try to cache known values.  */
2b3154
 
2b3154
 static fsword
2b3154
-filesystem_type (FTSENT const *p)
2b3154
+filesystem_type (FTSENT const *p, int fd)
2b3154
 {
2b3154
   FTS *sp = p->fts_fts;
2b3154
   Hash_table *h = sp->fts_leaf_optimization_works_ht;
2b3154
@@ -755,7 +756,7 @@ filesystem_type (FTSENT const *p)
2b3154
     }
2b3154
 
2b3154
   /* Look-up failed.  Query directly and cache the result.  */
2b3154
-  if (fstatfs (p->fts_fts->fts_cwd_fd, &fs_buf) != 0)
2b3154
+  if (fd < 0 || fstatfs (fd, &fs_buf) != 0)
2b3154
     return 0;
2b3154
 
2b3154
   if (h)
2b3154
@@ -777,12 +778,12 @@ filesystem_type (FTSENT const *p)
2b3154
   return fs_buf.f_type;
2b3154
 }
2b3154
 
2b3154
-/* Return false if it is easy to determine the file system type of the
2b3154
-   directory P, and sorting dirents on inode numbers is known not to
2b3154
-   improve traversal performance with that type of file system.
2b3154
-   Otherwise, return true.  */
2b3154
+/* Return true if sorting dirents on inode numbers is known to improve
2b3154
+   traversal performance for the directory P with descriptor DIR_FD.
2b3154
+   Return false otherwise.  When in doubt, return true.
2b3154
+   DIR_FD is negative if unavailable.  */
2b3154
 static bool
2b3154
-dirent_inode_sort_may_be_useful (FTSENT const *p)
2b3154
+dirent_inode_sort_may_be_useful (FTSENT const *p, int dir_fd)
2b3154
 {
2b3154
   /* Skip the sort only if we can determine efficiently
2b3154
      that skipping it is the right thing to do.
2b3154
@@ -790,7 +791,7 @@ dirent_inode_sort_may_be_useful (FTSENT const *p)
2b3154
      while the cost of *not* performing it can be O(N^2) with
2b3154
      a very large constant.  */
2b3154
 
2b3154
-  switch (filesystem_type (p))
2b3154
+  switch (filesystem_type (p, dir_fd))
2b3154
     {
2b3154
     case S_MAGIC_CIFS:
2b3154
     case S_MAGIC_NFS:
2b3154
@@ -804,16 +805,17 @@ dirent_inode_sort_may_be_useful (FTSENT const *p)
2b3154
     }
2b3154
 }
2b3154
 
2b3154
-/* Given an FTS entry P for a directory D,
2b3154
+/* Given an FTS entry P for a directory with descriptor DIR_FD,
2b3154
    return true if it is both useful and valid to apply leaf optimization.
2b3154
    The optimization is useful only for file systems that lack usable
2b3154
    dirent.d_type info.  The optimization is valid if an st_nlink value
2b3154
    of at least MIN_DIR_NLINK is an upper bound on the number of
2b3154
-   subdirectories of D, counting "." and ".."  as subdirectories.  */
2b3154
+   subdirectories of D, counting "." and ".."  as subdirectories.
2b3154
+   DIR_FD is negative if unavailable.  */
2b3154
 static enum leaf_optimization
2b3154
-leaf_optimization (FTSENT const *p)
2b3154
+leaf_optimization (FTSENT const *p, int dir_fd)
2b3154
 {
2b3154
-  switch (filesystem_type (p))
2b3154
+  switch (filesystem_type (p, dir_fd))
2b3154
     {
2b3154
       /* List here the file system types that may lack usable dirent.d_type
2b3154
          info, yet for which the optimization does apply.  */
2b3154
@@ -850,12 +852,13 @@ leaf_optimization (FTSENT const *p)
2b3154
 
2b3154
 #else
2b3154
 static bool
2b3154
-dirent_inode_sort_may_be_useful (FTSENT const *p _GL_UNUSED)
2b3154
+dirent_inode_sort_may_be_useful (FTSENT const *p _GL_UNUSED,
2b3154
+                                 int dir_fd _GL_UNUSED)
2b3154
 {
2b3154
   return true;
2b3154
 }
2b3154
 static enum leaf_optimization
2b3154
-leaf_optimization (FTSENT const *p _GL_UNUSED)
2b3154
+leaf_optimization (FTSENT const *p _GL_UNUSED, int dir_fd _GL_UNUSED)
2b3154
 {
2b3154
   return NO_LEAF_OPTIMIZATION;
2b3154
 }
2b3154
@@ -1049,7 +1052,7 @@ check_for_dir:
2b3154
                         if (parent->fts_n_dirs_remaining == 0
2b3154
                             && ISSET(FTS_NOSTAT)
2b3154
                             && ISSET(FTS_PHYSICAL)
2b3154
-                            && (leaf_optimization (parent)
2b3154
+                            && (leaf_optimization (parent, sp->fts_cwd_fd)
2b3154
                                 == NOSTAT_LEAF_OPTIMIZATION))
2b3154
                           {
2b3154
                             /* nothing more needed */
2b3154
@@ -1334,6 +1337,7 @@ fts_build (register FTS *sp, int type)
2b3154
         int dir_fd;
2b3154
         FTSENT *cur = sp->fts_cur;
2b3154
         bool continue_readdir = !!cur->fts_dirp;
2b3154
+        bool sort_by_inode = false;
2b3154
         size_t max_entries;
2b3154
 
2b3154
         /* When cur->fts_dirp is non-NULL, that means we should
2b3154
@@ -1427,7 +1431,7 @@ fts_build (register FTS *sp, int type)
2b3154
                        && ! (ISSET (FTS_NOSTAT) && ISSET (FTS_PHYSICAL)
2b3154
                              && ! ISSET (FTS_SEEDOT)
2b3154
                              && cur->fts_statp->st_nlink == MIN_DIR_NLINK
2b3154
-                             && (leaf_optimization (cur)
2b3154
+                             && (leaf_optimization (cur, dir_fd)
2b3154
                                  != NO_LEAF_OPTIMIZATION)));
2b3154
             if (descend || type == BREAD)
2b3154
               {
2b3154
@@ -1588,6 +1592,15 @@ mem1:                           saved_errno = errno;
2b3154
                         tail->fts_link = p;
2b3154
                         tail = p;
2b3154
                 }
2b3154
+
2b3154
+                /* If there are many entries, no sorting function has been
2b3154
+                   specified, and this file system is of a type that may be
2b3154
+                   slow with a large number of entries, arrange to sort the
2b3154
+                   directory entries on increasing inode numbers.  */
2b3154
+                if (nitems == _FTS_INODE_SORT_DIR_ENTRIES_THRESHOLD
2b3154
+                    && !sp->fts_compar)
2b3154
+                  sort_by_inode = dirent_inode_sort_may_be_useful (cur, dir_fd);
2b3154
+
2b3154
                 ++nitems;
2b3154
                 if (max_entries <= nitems) {
2b3154
                         /* When there are too many dir entries, leave
2b3154
@@ -1645,13 +1658,7 @@ mem1:                           saved_errno = errno;
2b3154
                 return (NULL);
2b3154
         }
2b3154
 
2b3154
-        /* If there are many entries, no sorting function has been specified,
2b3154
-           and this file system is of a type that may be slow with a large
2b3154
-           number of entries, then sort the directory entries on increasing
2b3154
-           inode numbers.  */
2b3154
-        if (nitems > _FTS_INODE_SORT_DIR_ENTRIES_THRESHOLD
2b3154
-            && !sp->fts_compar
2b3154
-            && dirent_inode_sort_may_be_useful (cur)) {
2b3154
+        if (sort_by_inode) {
2b3154
                 sp->fts_compar = fts_compare_ino;
2b3154
                 head = fts_sort (sp, head, nitems);
2b3154
                 sp->fts_compar = NULL;
2b3154
-- 
2b3154
2.14.3
2b3154