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