Mercurial > gnulib
annotate lib/fts.c @ 40244:c39a0edd90d7
fts: minor simplification
* lib/fts.c (fts_safe_changedir): Remove redundant assignment.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Sun, 17 Mar 2019 12:39:50 -0700 |
parents | 5be6b690b930 |
children |
rev | line source |
---|---|
5858 | 1 /* Traverse a file hierarchy. |
2 | |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
39948
diff
changeset
|
3 Copyright (C) 2004-2019 Free Software Foundation, Inc. |
5858 | 4 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9129
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
5858 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9129
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9129
diff
changeset
|
8 (at your option) any later version. |
5858 | 9 |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
19190 | 16 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
5858 | 17 |
18 /*- | |
19 * Copyright (c) 1990, 1993, 1994 | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
20 * The Regents of the University of California. All rights reserved. |
5858 | 21 * |
22 * Redistribution and use in source and binary forms, with or without | |
23 * modification, are permitted provided that the following conditions | |
24 * are met: | |
25 * 1. Redistributions of source code must retain the above copyright | |
26 * notice, this list of conditions and the following disclaimer. | |
27 * 2. Redistributions in binary form must reproduce the above copyright | |
28 * notice, this list of conditions and the following disclaimer in the | |
29 * documentation and/or other materials provided with the distribution. | |
30 * 4. Neither the name of the University nor the names of its contributors | |
31 * may be used to endorse or promote products derived from this software | |
32 * without specific prior written permission. | |
33 * | |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
5858 | 35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
44 * SUCH DAMAGE. | |
45 */ | |
46 | |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
7225
diff
changeset
|
47 #include <config.h> |
5858 | 48 |
18325 | 49 #if defined LIBC_SCCS && !defined GCC_LINT && !defined lint |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
50 static char sccsid[] = "@(#)fts.c 8.6 (Berkeley) 8/14/94"; |
18325 | 51 #endif |
5858 | 52 |
5867
67b499052f7f
* fts.c: Include fts_.h first, to check interface.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5858
diff
changeset
|
53 #include "fts_.h" |
67b499052f7f
* fts.c: Include fts_.h first, to check interface.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5858
diff
changeset
|
54 |
5858 | 55 #if HAVE_SYS_PARAM_H || defined _LIBC |
56 # include <sys/param.h> | |
57 #endif | |
58 #ifdef _LIBC | |
59 # include <include/sys/stat.h> | |
60 #else | |
61 # include <sys/stat.h> | |
62 #endif | |
63 #include <fcntl.h> | |
64 #include <errno.h> | |
18026
1a086f523d28
fts: avoid reading beyond the heap allocation
Pádraig Brady <P@draigBrady.com>
parents:
17974
diff
changeset
|
65 #include <stdalign.h> |
5867
67b499052f7f
* fts.c: Include fts_.h first, to check interface.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5858
diff
changeset
|
66 #include <stdbool.h> |
18137
d76e9e9d345e
fts: port to C11 alignof
Paul Eggert <eggert@cs.ucla.edu>
parents:
18026
diff
changeset
|
67 #include <stddef.h> |
5858 | 68 #include <stdlib.h> |
69 #include <string.h> | |
70 #include <unistd.h> | |
71 | |
6034
96149b1bbb32
(fts_cross_check) [FTS_DEBUG]: s/active_dir_ht/fts_cycle.ht/.
Jim Meyering <jim@meyering.net>
parents:
5907
diff
changeset
|
72 #if ! _LIBC |
7151 | 73 # include "fcntl--.h" |
18403
4434607ac1ce
flexmember: port better to GCC + valgrind
Paul Eggert <eggert@cs.ucla.edu>
parents:
18338
diff
changeset
|
74 # include "flexmember.h" |
11946 | 75 # include "openat.h" |
39948
c01fb99adfe2
backupfile: new dir_fd args
Paul Eggert <eggert@cs.ucla.edu>
parents:
39901
diff
changeset
|
76 # include "opendirat.h" |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
77 # include "same-inode.h" |
6034
96149b1bbb32
(fts_cross_check) [FTS_DEBUG]: s/active_dir_ht/fts_cycle.ht/.
Jim Meyering <jim@meyering.net>
parents:
5907
diff
changeset
|
78 #endif |
96149b1bbb32
(fts_cross_check) [FTS_DEBUG]: s/active_dir_ht/fts_cycle.ht/.
Jim Meyering <jim@meyering.net>
parents:
5907
diff
changeset
|
79 |
6949
96af1224c6fa
* backupfile.c, dirfd.h, fts.c, getcwd.c, glob.c, glob_.h:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6934
diff
changeset
|
80 #include <dirent.h> |
17642
8af917141d49
fts: avoid unnecessary strlen calls
Eric Blake <eblake@redhat.com>
parents:
17641
diff
changeset
|
81 #ifndef _D_EXACT_NAMLEN |
8af917141d49
fts: avoid unnecessary strlen calls
Eric Blake <eblake@redhat.com>
parents:
17641
diff
changeset
|
82 # define _D_EXACT_NAMLEN(dirent) strlen ((dirent)->d_name) |
8af917141d49
fts: avoid unnecessary strlen calls
Eric Blake <eblake@redhat.com>
parents:
17641
diff
changeset
|
83 #endif |
5858 | 84 |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
85 #if HAVE_STRUCT_DIRENT_D_TYPE |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
86 /* True if the type of the directory entry D is known. */ |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
87 # define DT_IS_KNOWN(d) ((d)->d_type != DT_UNKNOWN) |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
88 /* True if the type of the directory entry D must be T. */ |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
89 # define DT_MUST_BE(d, t) ((d)->d_type == (t)) |
10843
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
90 # define D_TYPE(d) ((d)->d_type) |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
91 #else |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
92 # define DT_IS_KNOWN(d) false |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
93 # define DT_MUST_BE(d, t) false |
10843
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
94 # define D_TYPE(d) DT_UNKNOWN |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
95 |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
96 # undef DT_UNKNOWN |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
97 # define DT_UNKNOWN 0 |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
98 |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
99 /* Any nonzero values will do here, so long as they're distinct. |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
100 Undef any existing macros out of the way. */ |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
101 # undef DT_BLK |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
102 # undef DT_CHR |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
103 # undef DT_DIR |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
104 # undef DT_FIFO |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
105 # undef DT_LNK |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
106 # undef DT_REG |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
107 # undef DT_SOCK |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
108 # define DT_BLK 1 |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
109 # define DT_CHR 2 |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
110 # define DT_DIR 3 |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
111 # define DT_FIFO 4 |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
112 # define DT_LNK 5 |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
113 # define DT_REG 6 |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
114 # define DT_SOCK 7 |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
115 #endif |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
116 |
10863
054d026819f0
work around mingw's lack of some S_IF definitions
Jim Meyering <meyering@redhat.com>
parents:
10844
diff
changeset
|
117 #ifndef S_IFLNK |
054d026819f0
work around mingw's lack of some S_IF definitions
Jim Meyering <meyering@redhat.com>
parents:
10844
diff
changeset
|
118 # define S_IFLNK 0 |
054d026819f0
work around mingw's lack of some S_IF definitions
Jim Meyering <meyering@redhat.com>
parents:
10844
diff
changeset
|
119 #endif |
054d026819f0
work around mingw's lack of some S_IF definitions
Jim Meyering <meyering@redhat.com>
parents:
10844
diff
changeset
|
120 #ifndef S_IFSOCK |
054d026819f0
work around mingw's lack of some S_IF definitions
Jim Meyering <meyering@redhat.com>
parents:
10844
diff
changeset
|
121 # define S_IFSOCK 0 |
054d026819f0
work around mingw's lack of some S_IF definitions
Jim Meyering <meyering@redhat.com>
parents:
10844
diff
changeset
|
122 #endif |
054d026819f0
work around mingw's lack of some S_IF definitions
Jim Meyering <meyering@redhat.com>
parents:
10844
diff
changeset
|
123 |
10481
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
124 enum |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
125 { |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
126 NOT_AN_INODE_NUMBER = 0 |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
127 }; |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
128 |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
129 #ifdef D_INO_IN_DIRENT |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
130 # define D_INO(dp) (dp)->d_ino |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
131 #else |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
132 /* Some systems don't have inodes, so fake them to avoid lots of ifdefs. */ |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
133 # define D_INO(dp) NOT_AN_INODE_NUMBER |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
134 #endif |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
135 |
15531
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
136 /* If possible (see max_entries, below), read no more than this many directory |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
137 entries at a time. Without this limit (i.e., when using non-NULL |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
138 fts_compar), processing a directory with 4,000,000 entries requires ~1GiB |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
139 of memory, and handling 64M entries would require 16GiB of memory. */ |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
140 #ifndef FTS_MAX_READDIR_ENTRIES |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
141 # define FTS_MAX_READDIR_ENTRIES 100000 |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
142 #endif |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
143 |
10481
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
144 /* If there are more than this many entries in a directory, |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
145 and the conditions mentioned below are satisfied, then sort |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
146 the entries on inode number before any further processing. */ |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
147 #ifndef FTS_INODE_SORT_DIR_ENTRIES_THRESHOLD |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
148 # define FTS_INODE_SORT_DIR_ENTRIES_THRESHOLD 10000 |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
149 #endif |
15531
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
150 |
10481
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
151 enum |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
152 { |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
153 _FTS_INODE_SORT_DIR_ENTRIES_THRESHOLD = FTS_INODE_SORT_DIR_ENTRIES_THRESHOLD |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
154 }; |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
155 |
7684
784e80c3d895
Work around a compile-time error from the HP-UX 11.00 /bin/cc.
Jim Meyering <jim@meyering.net>
parents:
7681
diff
changeset
|
156 enum Fts_stat |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
157 { |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
158 FTS_NO_STAT_REQUIRED = 1, |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
159 FTS_STAT_REQUIRED = 2 |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
160 }; |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
161 |
5858 | 162 #ifdef _LIBC |
163 # undef close | |
164 # define close __close | |
165 # undef closedir | |
166 # define closedir __closedir | |
167 # undef fchdir | |
168 # define fchdir __fchdir | |
169 # undef open | |
170 # define open __open | |
171 # undef readdir | |
172 # define readdir __readdir | |
173 #else | |
174 # undef internal_function | |
175 # define internal_function /* empty */ | |
176 #endif | |
177 | |
178 #ifndef __set_errno | |
179 # define __set_errno(Val) errno = (Val) | |
180 #endif | |
181 | |
7151 | 182 /* If this host provides the openat function, then we can avoid |
183 attempting to open "." in some initialization code below. */ | |
184 #ifdef HAVE_OPENAT | |
185 # define HAVE_OPENAT_SUPPORT 1 | |
186 #else | |
187 # define HAVE_OPENAT_SUPPORT 0 | |
188 #endif | |
5858 | 189 |
7785
3246679850d5
Slight readability improvement: use an assert-like macro
Jim Meyering <jim@meyering.net>
parents:
7707
diff
changeset
|
190 #ifdef NDEBUG |
17840 | 191 # define fts_assert(expr) ((void) (0 && (expr))) |
7785
3246679850d5
Slight readability improvement: use an assert-like macro
Jim Meyering <jim@meyering.net>
parents:
7707
diff
changeset
|
192 #else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
193 # define fts_assert(expr) \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
194 do \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
195 { \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
196 if (!(expr)) \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
197 abort (); \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
198 } \ |
7785
3246679850d5
Slight readability improvement: use an assert-like macro
Jim Meyering <jim@meyering.net>
parents:
7707
diff
changeset
|
199 while (false) |
3246679850d5
Slight readability improvement: use an assert-like macro
Jim Meyering <jim@meyering.net>
parents:
7707
diff
changeset
|
200 #endif |
3246679850d5
Slight readability improvement: use an assert-like macro
Jim Meyering <jim@meyering.net>
parents:
7707
diff
changeset
|
201 |
18930
50d1e186bf78
fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents:
18626
diff
changeset
|
202 #ifndef FALLTHROUGH |
50d1e186bf78
fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents:
18626
diff
changeset
|
203 # if __GNUC__ < 7 |
50d1e186bf78
fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents:
18626
diff
changeset
|
204 # define FALLTHROUGH ((void) 0) |
50d1e186bf78
fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents:
18626
diff
changeset
|
205 # else |
50d1e186bf78
fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents:
18626
diff
changeset
|
206 # define FALLTHROUGH __attribute__ ((__fallthrough__)) |
50d1e186bf78
fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents:
18626
diff
changeset
|
207 # endif |
50d1e186bf78
fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents:
18626
diff
changeset
|
208 #endif |
50d1e186bf78
fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents:
18626
diff
changeset
|
209 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
210 static FTSENT *fts_alloc (FTS *, const char *, size_t) internal_function; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
211 static FTSENT *fts_build (FTS *, int) internal_function; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
212 static void fts_lfree (FTSENT *) internal_function; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
213 static void fts_load (FTS *, FTSENT *) internal_function; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
214 static size_t fts_maxarglen (char * const *) internal_function; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
215 static void fts_padjust (FTS *, FTSENT *) internal_function; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
216 static bool fts_palloc (FTS *, size_t) internal_function; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
217 static FTSENT *fts_sort (FTS *, FTSENT *, size_t) internal_function; |
5858 | 218 static unsigned short int fts_stat (FTS *, FTSENT *, bool) internal_function; |
219 static int fts_safe_changedir (FTS *, FTSENT *, int, const char *) | |
220 internal_function; | |
221 | |
13091
3af48780d248
Remove vestiges of fts-lgpl module.
Bruno Haible <bruno@clisp.org>
parents:
12772
diff
changeset
|
222 #include "fts-cycle.c" |
5872 | 223 |
5858 | 224 #ifndef MAX |
225 # define MAX(a,b) ((a) > (b) ? (a) : (b)) | |
226 #endif | |
227 | |
228 #ifndef SIZE_MAX | |
229 # define SIZE_MAX ((size_t) -1) | |
230 #endif | |
231 | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
232 #define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2]))) |
12772
2ea3e3fab7ee
define STREQ(a,b) consistently, removing useless parentheses
Jim Meyering <meyering@redhat.com>
parents:
12559
diff
changeset
|
233 #define STREQ(a, b) (strcmp (a, b) == 0) |
5858 | 234 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
235 #define CLR(opt) (sp->fts_options &= ~(opt)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
236 #define ISSET(opt) (sp->fts_options & (opt)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
237 #define SET(opt) (sp->fts_options |= (opt)) |
5858 | 238 |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
239 /* FIXME: FTS_NOCHDIR is now misnamed. |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
240 Call it FTS_USE_FULL_RELATIVE_FILE_NAMES instead. */ |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
241 #define FCHDIR(sp, fd) \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
242 (!ISSET(FTS_NOCHDIR) && (ISSET(FTS_CWDFD) \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
243 ? (cwd_advance_fd ((sp), (fd), true), 0) \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
244 : fchdir (fd))) |
7151 | 245 |
5858 | 246 |
247 /* fts_build flags */ | |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
248 /* FIXME: make this an enum */ |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
249 #define BCHILD 1 /* fts_children */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
250 #define BNAMES 2 /* fts_children, names only */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
251 #define BREAD 3 /* fts_read */ |
5858 | 252 |
253 #if FTS_DEBUG | |
5894
d09af5fbd9f2
* modules/fts (Files): Remove m4/inttypes-pri.m4.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5877
diff
changeset
|
254 # include <inttypes.h> |
d09af5fbd9f2
* modules/fts (Files): Remove m4/inttypes-pri.m4.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5877
diff
changeset
|
255 # include <stdint.h> |
d09af5fbd9f2
* modules/fts (Files): Remove m4/inttypes-pri.m4.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5877
diff
changeset
|
256 # include <stdio.h> |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
257 # include "getcwdat.h" |
5858 | 258 bool fts_debug = false; |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
259 # define Dprintf(x) do { if (fts_debug) printf x; } while (false) |
5858 | 260 #else |
261 # define Dprintf(x) | |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
262 # define fd_ring_check(x) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
263 # define fd_ring_print(a, b, c) |
5858 | 264 #endif |
265 | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
266 #define LEAVE_DIR(Fts, Ent, Tag) \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
267 do \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
268 { \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
269 Dprintf ((" %s-leaving: %s\n", Tag, (Ent)->fts_path)); \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
270 leave_dir (Fts, Ent); \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
271 fd_ring_check (Fts); \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
272 } \ |
5872 | 273 while (false) |
5858 | 274 |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
275 static void |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
276 fd_ring_clear (I_ring *fd_ring) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
277 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
278 while ( ! i_ring_empty (fd_ring)) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
279 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
280 int fd = i_ring_pop (fd_ring); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
281 if (0 <= fd) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
282 close (fd); |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
283 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
284 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
285 |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
286 /* Overload the fts_statp->st_size member (otherwise unused, when |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
287 fts_info is FTS_NSOK) to indicate whether fts_read should stat |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
288 this entry or not. */ |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
289 static void |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
290 fts_set_stat_required (FTSENT *p, bool required) |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
291 { |
7785
3246679850d5
Slight readability improvement: use an assert-like macro
Jim Meyering <jim@meyering.net>
parents:
7707
diff
changeset
|
292 fts_assert (p->fts_info == FTS_NSOK); |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
293 p->fts_statp->st_size = (required |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
294 ? FTS_STAT_REQUIRED |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
295 : FTS_NO_STAT_REQUIRED); |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
296 } |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
297 |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
298 /* Virtual fchdir. Advance SP's working directory file descriptor, |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
299 SP->fts_cwd_fd, to FD, and push the previous value onto the fd_ring. |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
300 CHDIR_DOWN_ONE is true if FD corresponds to an entry in the directory |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
301 open on sp->fts_cwd_fd; i.e., to move the working directory one level |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
302 down. */ |
7151 | 303 static void |
304 internal_function | |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
305 cwd_advance_fd (FTS *sp, int fd, bool chdir_down_one) |
7151 | 306 { |
307 int old = sp->fts_cwd_fd; | |
7785
3246679850d5
Slight readability improvement: use an assert-like macro
Jim Meyering <jim@meyering.net>
parents:
7707
diff
changeset
|
308 fts_assert (old != fd || old == AT_FDCWD); |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
309 |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
310 if (chdir_down_one) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
311 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
312 /* Push "old" onto the ring. |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
313 If the displaced file descriptor is non-negative, close it. */ |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
314 int prev_fd_in_slot = i_ring_push (&sp->fts_fd_ring, old); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
315 fd_ring_print (sp, stderr, "post-push"); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
316 if (0 <= prev_fd_in_slot) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
317 close (prev_fd_in_slot); /* ignore any close failure */ |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
318 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
319 else if ( ! ISSET (FTS_NOCHDIR)) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
320 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
321 if (0 <= old) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
322 close (old); /* ignore any close failure */ |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
323 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
324 |
7151 | 325 sp->fts_cwd_fd = fd; |
326 } | |
327 | |
15988
cd7ac59d8eb5
fts: close parent dir FD before returning from post-traversal fts_read
Jim Meyering <meyering@redhat.com>
parents:
15531
diff
changeset
|
328 /* Restore the initial, pre-traversal, "working directory". |
cd7ac59d8eb5
fts: close parent dir FD before returning from post-traversal fts_read
Jim Meyering <meyering@redhat.com>
parents:
15531
diff
changeset
|
329 In FTS_CWDFD mode, we merely call cwd_advance_fd, otherwise, |
cd7ac59d8eb5
fts: close parent dir FD before returning from post-traversal fts_read
Jim Meyering <meyering@redhat.com>
parents:
15531
diff
changeset
|
330 we may actually change the working directory. |
cd7ac59d8eb5
fts: close parent dir FD before returning from post-traversal fts_read
Jim Meyering <meyering@redhat.com>
parents:
15531
diff
changeset
|
331 Return 0 upon success. Upon failure, set errno and return nonzero. */ |
cd7ac59d8eb5
fts: close parent dir FD before returning from post-traversal fts_read
Jim Meyering <meyering@redhat.com>
parents:
15531
diff
changeset
|
332 static int |
cd7ac59d8eb5
fts: close parent dir FD before returning from post-traversal fts_read
Jim Meyering <meyering@redhat.com>
parents:
15531
diff
changeset
|
333 restore_initial_cwd (FTS *sp) |
cd7ac59d8eb5
fts: close parent dir FD before returning from post-traversal fts_read
Jim Meyering <meyering@redhat.com>
parents:
15531
diff
changeset
|
334 { |
cd7ac59d8eb5
fts: close parent dir FD before returning from post-traversal fts_read
Jim Meyering <meyering@redhat.com>
parents:
15531
diff
changeset
|
335 int fail = FCHDIR (sp, ISSET (FTS_CWDFD) ? AT_FDCWD : sp->fts_rfd); |
cd7ac59d8eb5
fts: close parent dir FD before returning from post-traversal fts_read
Jim Meyering <meyering@redhat.com>
parents:
15531
diff
changeset
|
336 fd_ring_clear (&(sp->fts_fd_ring)); |
cd7ac59d8eb5
fts: close parent dir FD before returning from post-traversal fts_read
Jim Meyering <meyering@redhat.com>
parents:
15531
diff
changeset
|
337 return fail; |
cd7ac59d8eb5
fts: close parent dir FD before returning from post-traversal fts_read
Jim Meyering <meyering@redhat.com>
parents:
15531
diff
changeset
|
338 } |
cd7ac59d8eb5
fts: close parent dir FD before returning from post-traversal fts_read
Jim Meyering <meyering@redhat.com>
parents:
15531
diff
changeset
|
339 |
5858 | 340 /* Open the directory DIR if possible, and return a file |
341 descriptor. Return -1 and set errno on failure. It doesn't matter | |
342 whether the file descriptor has read or write access. */ | |
343 | |
17188
d7a44f0f7a15
ftruncate, fts, lstat, openat, raise: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
17160
diff
changeset
|
344 static int |
5858 | 345 internal_function |
7151 | 346 diropen (FTS const *sp, char const *dir) |
5858 | 347 { |
19047 | 348 int open_flags = (O_SEARCH | O_CLOEXEC | O_DIRECTORY | O_NOCTTY | O_NONBLOCK |
39901
33cf8a9f314a
fts: remove FTS_NOATIME
Bernhard Voelker <mail@bernhard-voelker.de>
parents:
19683
diff
changeset
|
349 | (ISSET (FTS_PHYSICAL) ? O_NOFOLLOW : 0)); |
7151 | 350 |
11940 | 351 int fd = (ISSET (FTS_CWDFD) |
352 ? openat (sp->fts_cwd_fd, dir, open_flags) | |
353 : open (dir, open_flags)); | |
354 return fd; | |
5858 | 355 } |
356 | |
357 FTS * | |
358 fts_open (char * const *argv, | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
359 register int options, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
360 int (*compar) (FTSENT const **, FTSENT const **)) |
5858 | 361 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
362 register FTS *sp; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
363 register FTSENT *p, *root; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
364 register size_t nitems; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
365 FTSENT *parent = NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
366 FTSENT *tmp = NULL; /* pacify gcc */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
367 bool defer_stat; |
5858 | 368 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
369 /* Options check. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
370 if (options & ~FTS_OPTIONMASK) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
371 __set_errno (EINVAL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
372 return (NULL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
373 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
374 if ((options & FTS_NOCHDIR) && (options & FTS_CWDFD)) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
375 __set_errno (EINVAL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
376 return (NULL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
377 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
378 if ( ! (options & (FTS_LOGICAL | FTS_PHYSICAL))) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
379 __set_errno (EINVAL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
380 return (NULL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
381 } |
5858 | 382 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
383 /* Allocate/initialize the stream */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
384 if ((sp = malloc(sizeof(FTS))) == NULL) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
385 return (NULL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
386 memset(sp, 0, sizeof(FTS)); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
387 sp->fts_compar = compar; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
388 sp->fts_options = options; |
5858 | 389 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
390 /* Logical walks turn on NOCHDIR; symbolic links are too hard. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
391 if (ISSET(FTS_LOGICAL)) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
392 SET(FTS_NOCHDIR); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
393 CLR(FTS_CWDFD); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
394 } |
7151 | 395 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
396 /* Initialize fts_cwd_fd. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
397 sp->fts_cwd_fd = AT_FDCWD; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
398 if ( ISSET(FTS_CWDFD) && ! HAVE_OPENAT_SUPPORT) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
399 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
400 /* While it isn't technically necessary to open "." this |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
401 early, doing it here saves us the trouble of ensuring |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
402 later (where it'd be messier) that "." can in fact |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
403 be opened. If not, revert to FTS_NOCHDIR mode. */ |
39901
33cf8a9f314a
fts: remove FTS_NOATIME
Bernhard Voelker <mail@bernhard-voelker.de>
parents:
19683
diff
changeset
|
404 int fd = open (".", O_SEARCH); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
405 if (fd < 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
406 { |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
407 /* Even if "." is unreadable, don't revert to FTS_NOCHDIR mode |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
408 on systems like Linux+PROC_FS, where our openat emulation |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
409 is good enough. Note: on a system that emulates |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
410 openat via /proc, this technique can still fail, but |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
411 only in extreme conditions, e.g., when the working |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
412 directory cannot be saved (i.e. save_cwd fails) -- |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
413 and that happens on Linux only when "." is unreadable |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
414 and the CWD would be longer than PATH_MAX. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
415 FIXME: once Linux kernel openat support is well established, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
416 replace the above open call and this entire if/else block |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
417 with the body of the if-block below. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
418 if ( openat_needs_fchdir ()) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
419 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
420 SET(FTS_NOCHDIR); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
421 CLR(FTS_CWDFD); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
422 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
423 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
424 else |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
425 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
426 close (fd); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
427 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
428 } |
5858 | 429 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
430 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
431 * Start out with 1K of file name space, and enough, in any case, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
432 * to hold the user's file names. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
433 */ |
5858 | 434 #ifndef MAXPATHLEN |
435 # define MAXPATHLEN 1024 | |
436 #endif | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
437 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
438 size_t maxarglen = fts_maxarglen(argv); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
439 if (! fts_palloc(sp, MAX(maxarglen, MAXPATHLEN))) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
440 goto mem1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
441 } |
5858 | 442 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
443 /* Allocate/initialize root's parent. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
444 if (*argv != NULL) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
445 if ((parent = fts_alloc(sp, "", 0)) == NULL) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
446 goto mem2; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
447 parent->fts_level = FTS_ROOTPARENTLEVEL; |
19008
a6fc0157c611
fts: nlink_t signedness fixups
Paul Eggert <eggert@cs.ucla.edu>
parents:
18930
diff
changeset
|
448 parent->fts_n_dirs_remaining = -1; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
449 } |
5858 | 450 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
451 /* The classic fts implementation would call fts_stat with |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
452 a new entry for each iteration of the loop below. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
453 If the comparison function is not specified or if the |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
454 FTS_DEFER_STAT option is in effect, don't stat any entry |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
455 in this loop. This is an attempt to minimize the interval |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
456 between the initial stat/lstat/fstatat and the point at which |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
457 a directory argument is first opened. This matters for any |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
458 directory command line argument that resides on a file system |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
459 without genuine i-nodes. If you specify FTS_DEFER_STAT along |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
460 with a comparison function, that function must not access any |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
461 data via the fts_statp pointer. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
462 defer_stat = (compar == NULL || ISSET(FTS_DEFER_STAT)); |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
463 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
464 /* Allocate/initialize root(s). */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
465 for (root = NULL, nitems = 0; *argv != NULL; ++argv, ++nitems) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
466 /* *Do* allow zero-length file names. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
467 size_t len = strlen(*argv); |
17074
d4ccb0388f18
fts: reduce two or more trailing slashes to just one, usually
Jim Meyering <meyering@redhat.com>
parents:
17073
diff
changeset
|
468 |
17160
72f4bab621be
fts: introduce FTS_VERBATIM
Dmitry V. Levin <ldv@altlinux.org>
parents:
17074
diff
changeset
|
469 if ( ! (options & FTS_VERBATIM)) |
72f4bab621be
fts: introduce FTS_VERBATIM
Dmitry V. Levin <ldv@altlinux.org>
parents:
17074
diff
changeset
|
470 { |
72f4bab621be
fts: introduce FTS_VERBATIM
Dmitry V. Levin <ldv@altlinux.org>
parents:
17074
diff
changeset
|
471 /* If there are two or more trailing slashes, trim all but one, |
72f4bab621be
fts: introduce FTS_VERBATIM
Dmitry V. Levin <ldv@altlinux.org>
parents:
17074
diff
changeset
|
472 but don't change "//" to "/", and do map "///" to "/". */ |
72f4bab621be
fts: introduce FTS_VERBATIM
Dmitry V. Levin <ldv@altlinux.org>
parents:
17074
diff
changeset
|
473 char const *v = *argv; |
72f4bab621be
fts: introduce FTS_VERBATIM
Dmitry V. Levin <ldv@altlinux.org>
parents:
17074
diff
changeset
|
474 if (2 < len && v[len - 1] == '/') |
72f4bab621be
fts: introduce FTS_VERBATIM
Dmitry V. Levin <ldv@altlinux.org>
parents:
17074
diff
changeset
|
475 while (1 < len && v[len - 2] == '/') |
72f4bab621be
fts: introduce FTS_VERBATIM
Dmitry V. Levin <ldv@altlinux.org>
parents:
17074
diff
changeset
|
476 --len; |
72f4bab621be
fts: introduce FTS_VERBATIM
Dmitry V. Levin <ldv@altlinux.org>
parents:
17074
diff
changeset
|
477 } |
17074
d4ccb0388f18
fts: reduce two or more trailing slashes to just one, usually
Jim Meyering <meyering@redhat.com>
parents:
17073
diff
changeset
|
478 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
479 if ((p = fts_alloc(sp, *argv, len)) == NULL) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
480 goto mem3; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
481 p->fts_level = FTS_ROOTLEVEL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
482 p->fts_parent = parent; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
483 p->fts_accpath = p->fts_name; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
484 /* Even when defer_stat is true, be sure to stat the first |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
485 command line argument, since fts_read (at least with |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
486 FTS_XDEV) requires that. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
487 if (defer_stat && root != NULL) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
488 p->fts_info = FTS_NSOK; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
489 fts_set_stat_required(p, true); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
490 } else { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
491 p->fts_info = fts_stat(sp, p, false); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
492 } |
5858 | 493 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
494 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
495 * If comparison routine supplied, traverse in sorted |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
496 * order; otherwise traverse in the order specified. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
497 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
498 if (compar) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
499 p->fts_link = root; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
500 root = p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
501 } else { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
502 p->fts_link = NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
503 if (root == NULL) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
504 tmp = root = p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
505 else { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
506 tmp->fts_link = p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
507 tmp = p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
508 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
509 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
510 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
511 if (compar && nitems > 1) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
512 root = fts_sort(sp, root, nitems); |
5858 | 513 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
514 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
515 * Allocate a dummy pointer and make fts_read think that we've just |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
516 * finished the node before the root(s); set p->fts_info to FTS_INIT |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
517 * so that everything about the "current" node is ignored. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
518 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
519 if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
520 goto mem3; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
521 sp->fts_cur->fts_link = root; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
522 sp->fts_cur->fts_info = FTS_INIT; |
19683
c4547aaaa88f
fts: avoid a memory leak edge case
Pádraig Brady <P@draigBrady.com>
parents:
19585
diff
changeset
|
523 sp->fts_cur->fts_level = 1; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
524 if (! setup_dir (sp)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
525 goto mem3; |
5858 | 526 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
527 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
528 * If using chdir(2), grab a file descriptor pointing to dot to ensure |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
529 * that we can get back here; this could be avoided for some file names, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
530 * but almost certainly not worth the effort. Slashes, symbolic links, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
531 * and ".." are all fairly nasty problems. Note, if we can't get the |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
532 * descriptor we run anyway, just more slowly. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
533 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
534 if (!ISSET(FTS_NOCHDIR) && !ISSET(FTS_CWDFD) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
535 && (sp->fts_rfd = diropen (sp, ".")) < 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
536 SET(FTS_NOCHDIR); |
5858 | 537 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
538 i_ring_init (&sp->fts_fd_ring, -1); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
539 return (sp); |
5858 | 540 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
541 mem3: fts_lfree(root); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
542 free(parent); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
543 mem2: free(sp->fts_path); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
544 mem1: free(sp); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
545 return (NULL); |
5858 | 546 } |
547 | |
548 static void | |
549 internal_function | |
550 fts_load (FTS *sp, register FTSENT *p) | |
551 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
552 register size_t len; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
553 register char *cp; |
5858 | 554 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
555 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
556 * Load the stream structure for the next traversal. Since we don't |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
557 * actually enter the directory until after the preorder visit, set |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
558 * the fts_accpath field specially so the chdir gets done to the right |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
559 * place and the user can access the first node. From fts_open it's |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
560 * known that the file name will fit. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
561 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
562 len = p->fts_pathlen = p->fts_namelen; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
563 memmove(sp->fts_path, p->fts_name, len + 1); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
564 if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
565 len = strlen(++cp); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
566 memmove(p->fts_name, cp, len + 1); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
567 p->fts_namelen = len; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
568 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
569 p->fts_accpath = p->fts_path = sp->fts_path; |
5858 | 570 } |
571 | |
572 int | |
573 fts_close (FTS *sp) | |
574 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
575 register FTSENT *freep, *p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
576 int saved_errno = 0; |
5858 | 577 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
578 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
579 * This still works if we haven't read anything -- the dummy structure |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
580 * points to the root list, so we step through to the end of the root |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
581 * list which has a valid parent pointer. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
582 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
583 if (sp->fts_cur) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
584 for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
585 freep = p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
586 p = p->fts_link != NULL ? p->fts_link : p->fts_parent; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
587 free(freep); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
588 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
589 free(p); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
590 } |
5858 | 591 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
592 /* Free up child linked list, sort array, file name buffer. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
593 if (sp->fts_child) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
594 fts_lfree(sp->fts_child); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
595 free(sp->fts_array); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
596 free(sp->fts_path); |
5858 | 597 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
598 if (ISSET(FTS_CWDFD)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
599 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
600 if (0 <= sp->fts_cwd_fd) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
601 if (close (sp->fts_cwd_fd)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
602 saved_errno = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
603 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
604 else if (!ISSET(FTS_NOCHDIR)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
605 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
606 /* Return to original directory, save errno if necessary. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
607 if (fchdir(sp->fts_rfd)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
608 saved_errno = errno; |
11924
10d064d93551
fts: fts_close now fails also when closing a dir file descriptor fails
Jim Meyering <meyering@redhat.com>
parents:
11722
diff
changeset
|
609 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
610 /* If close fails, record errno only if saved_errno is zero, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
611 so that we report the probably-more-meaningful fchdir errno. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
612 if (close (sp->fts_rfd)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
613 if (saved_errno == 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
614 saved_errno = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
615 } |
5858 | 616 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
617 fd_ring_clear (&sp->fts_fd_ring); |
11164
90715da41753
fts: arrange not to stat non-directories in more cases
Jim Meyering <meyering@redhat.com>
parents:
11163
diff
changeset
|
618 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
619 if (sp->fts_leaf_optimization_works_ht) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
620 hash_free (sp->fts_leaf_optimization_works_ht); |
11164
90715da41753
fts: arrange not to stat non-directories in more cases
Jim Meyering <meyering@redhat.com>
parents:
11163
diff
changeset
|
621 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
622 free_dir (sp); |
5858 | 623 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
624 /* Free up the stream pointer. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
625 free(sp); |
5858 | 626 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
627 /* Set errno and return. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
628 if (saved_errno) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
629 __set_errno (saved_errno); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
630 return (-1); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
631 } |
5858 | 632 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
633 return (0); |
5858 | 634 } |
635 | |
19009
7201d8037bb3
fts: introduce MIN_DIR_NLINK
Paul Eggert <eggert@cs.ucla.edu>
parents:
19008
diff
changeset
|
636 /* Minimum link count of a traditional Unix directory. When leaf |
7201d8037bb3
fts: introduce MIN_DIR_NLINK
Paul Eggert <eggert@cs.ucla.edu>
parents:
19008
diff
changeset
|
637 optimization is OK and MIN_DIR_NLINK <= st_nlink, then st_nlink is |
7201d8037bb3
fts: introduce MIN_DIR_NLINK
Paul Eggert <eggert@cs.ucla.edu>
parents:
19008
diff
changeset
|
638 an upper bound on the number of subdirectories (counting "." and |
7201d8037bb3
fts: introduce MIN_DIR_NLINK
Paul Eggert <eggert@cs.ucla.edu>
parents:
19008
diff
changeset
|
639 ".."). */ |
7201d8037bb3
fts: introduce MIN_DIR_NLINK
Paul Eggert <eggert@cs.ucla.edu>
parents:
19008
diff
changeset
|
640 enum { MIN_DIR_NLINK = 2 }; |
7201d8037bb3
fts: introduce MIN_DIR_NLINK
Paul Eggert <eggert@cs.ucla.edu>
parents:
19008
diff
changeset
|
641 |
19011
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
642 /* Whether leaf optimization is OK for a directory. */ |
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
643 enum leaf_optimization |
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
644 { |
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
645 /* st_nlink is not reliable for this directory's subdirectories. */ |
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
646 NO_LEAF_OPTIMIZATION, |
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
647 |
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
648 /* Leaf optimization is OK, but is not useful for avoiding stat calls. */ |
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
649 OK_LEAF_OPTIMIZATION, |
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
650 |
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
651 /* Leaf optimization is not only OK: it is useful for avoiding |
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
652 stat calls, because dirent.d_type does not work. */ |
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
653 NOSTAT_LEAF_OPTIMIZATION |
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
654 }; |
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
655 |
40148 | 656 #if (defined __linux__ || defined __ANDROID__) \ |
11163
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
657 && HAVE_SYS_VFS_H && HAVE_FSTATFS && HAVE_STRUCT_STATFS_F_TYPE |
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
658 |
14379
2330aac2ae54
maint: adjust cpp indentation to reflect nesting depth
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
659 # include <sys/vfs.h> |
11163
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
660 |
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
661 /* Linux-specific constants from coreutils' src/fs.h */ |
19011
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
662 # define S_MAGIC_AFS 0x5346414F |
19580
1fa2f90267d3
fts: treat CIFS like NFS
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
663 # define S_MAGIC_CIFS 0xFF534D42 |
11163
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
664 # define S_MAGIC_NFS 0x6969 |
19011
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
665 # define S_MAGIC_PROC 0x9FA0 |
11164
90715da41753
fts: arrange not to stat non-directories in more cases
Jim Meyering <meyering@redhat.com>
parents:
11163
diff
changeset
|
666 # define S_MAGIC_REISERFS 0x52654973 |
19011
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
667 # define S_MAGIC_TMPFS 0x1021994 |
18179
4b6df746d1f3
fts: enable leaf optimization for XFS
Pádraig Brady <P@draigBrady.com>
parents:
18137
diff
changeset
|
668 # define S_MAGIC_XFS 0x58465342 |
11163
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
669 |
19045
bf63563adfd2
fts: port recent changes to CentOS 6
Paul Eggert <eggert@cs.ucla.edu>
parents:
19027
diff
changeset
|
670 # ifdef HAVE___FSWORD_T |
bf63563adfd2
fts: port recent changes to CentOS 6
Paul Eggert <eggert@cs.ucla.edu>
parents:
19027
diff
changeset
|
671 typedef __fsword_t fsword; |
bf63563adfd2
fts: port recent changes to CentOS 6
Paul Eggert <eggert@cs.ucla.edu>
parents:
19027
diff
changeset
|
672 # else |
bf63563adfd2
fts: port recent changes to CentOS 6
Paul Eggert <eggert@cs.ucla.edu>
parents:
19027
diff
changeset
|
673 typedef long int fsword; |
bf63563adfd2
fts: port recent changes to CentOS 6
Paul Eggert <eggert@cs.ucla.edu>
parents:
19027
diff
changeset
|
674 # endif |
bf63563adfd2
fts: port recent changes to CentOS 6
Paul Eggert <eggert@cs.ucla.edu>
parents:
19027
diff
changeset
|
675 |
19010
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
676 /* Map a stat.st_dev number to a file system type number f_ftype. */ |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
677 struct dev_type |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
678 { |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
679 dev_t st_dev; |
19045
bf63563adfd2
fts: port recent changes to CentOS 6
Paul Eggert <eggert@cs.ucla.edu>
parents:
19027
diff
changeset
|
680 fsword f_type; |
19010
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
681 }; |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
682 |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
683 /* Use a tiny initial size. If a traversal encounters more than |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
684 a few devices, the cost of growing/rehashing this table will be |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
685 rendered negligible by the number of inodes processed. */ |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
686 enum { DEV_TYPE_HT_INITIAL_SIZE = 13 }; |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
687 |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
688 static size_t |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
689 dev_type_hash (void const *x, size_t table_size) |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
690 { |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
691 struct dev_type const *ax = x; |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
692 uintmax_t dev = ax->st_dev; |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
693 return dev % table_size; |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
694 } |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
695 |
11163
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
696 static bool |
19010
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
697 dev_type_compare (void const *x, void const *y) |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
698 { |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
699 struct dev_type const *ax = x; |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
700 struct dev_type const *ay = y; |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
701 return ax->st_dev == ay->st_dev; |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
702 } |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
703 |
19584
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
704 /* Return the file system type of P with file descriptor FD, or 0 if not known. |
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
705 If FD is negative, P's file descriptor is unavailable. |
19010
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
706 Try to cache known values. */ |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
707 |
19045
bf63563adfd2
fts: port recent changes to CentOS 6
Paul Eggert <eggert@cs.ucla.edu>
parents:
19027
diff
changeset
|
708 static fsword |
19584
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
709 filesystem_type (FTSENT const *p, int fd) |
19010
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
710 { |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
711 FTS *sp = p->fts_fts; |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
712 Hash_table *h = sp->fts_leaf_optimization_works_ht; |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
713 struct dev_type *ent; |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
714 struct statfs fs_buf; |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
715 |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
716 /* If we're not in CWDFD mode, don't bother with this optimization, |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
717 since the caller is not serious about performance. */ |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
718 if (!ISSET (FTS_CWDFD)) |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
719 return 0; |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
720 |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
721 if (! h) |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
722 h = sp->fts_leaf_optimization_works_ht |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
723 = hash_initialize (DEV_TYPE_HT_INITIAL_SIZE, NULL, dev_type_hash, |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
724 dev_type_compare, free); |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
725 if (h) |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
726 { |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
727 struct dev_type tmp; |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
728 tmp.st_dev = p->fts_statp->st_dev; |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
729 ent = hash_lookup (h, &tmp); |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
730 if (ent) |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
731 return ent->f_type; |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
732 } |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
733 |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
734 /* Look-up failed. Query directly and cache the result. */ |
19584
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
735 if (fd < 0 || fstatfs (fd, &fs_buf) != 0) |
19010
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
736 return 0; |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
737 |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
738 if (h) |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
739 { |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
740 struct dev_type *t2 = malloc (sizeof *t2); |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
741 if (t2) |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
742 { |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
743 t2->st_dev = p->fts_statp->st_dev; |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
744 t2->f_type = fs_buf.f_type; |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
745 |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
746 ent = hash_insert (h, t2); |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
747 if (ent) |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
748 fts_assert (ent == t2); |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
749 else |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
750 free (t2); |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
751 } |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
752 } |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
753 |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
754 return fs_buf.f_type; |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
755 } |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
756 |
19584
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
757 /* Return true if sorting dirents on inode numbers is known to improve |
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
758 traversal performance for the directory P with descriptor DIR_FD. |
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
759 Return false otherwise. When in doubt, return true. |
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
760 DIR_FD is negative if unavailable. */ |
19010
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
761 static bool |
19584
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
762 dirent_inode_sort_may_be_useful (FTSENT const *p, int dir_fd) |
11163
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
763 { |
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
764 /* Skip the sort only if we can determine efficiently |
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
765 that skipping it is the right thing to do. |
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
766 The cost of performing an unnecessary sort is negligible, |
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
767 while the cost of *not* performing it can be O(N^2) with |
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
768 a very large constant. */ |
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
769 |
19584
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
770 switch (filesystem_type (p, dir_fd)) |
11163
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
771 { |
19580
1fa2f90267d3
fts: treat CIFS like NFS
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
772 case S_MAGIC_CIFS: |
1fa2f90267d3
fts: treat CIFS like NFS
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
773 case S_MAGIC_NFS: |
11163
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
774 case S_MAGIC_TMPFS: |
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
775 /* On a file system of any of these types, sorting |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
776 is unnecessary, and hence wasteful. */ |
11163
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
777 return false; |
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
778 |
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
779 default: |
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
780 return true; |
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
781 } |
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
782 } |
11164
90715da41753
fts: arrange not to stat non-directories in more cases
Jim Meyering <meyering@redhat.com>
parents:
11163
diff
changeset
|
783 |
19584
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
784 /* Given an FTS entry P for a directory with descriptor DIR_FD, |
19009
7201d8037bb3
fts: introduce MIN_DIR_NLINK
Paul Eggert <eggert@cs.ucla.edu>
parents:
19008
diff
changeset
|
785 return true if it is both useful and valid to apply leaf optimization. |
7201d8037bb3
fts: introduce MIN_DIR_NLINK
Paul Eggert <eggert@cs.ucla.edu>
parents:
19008
diff
changeset
|
786 The optimization is useful only for file systems that lack usable |
7201d8037bb3
fts: introduce MIN_DIR_NLINK
Paul Eggert <eggert@cs.ucla.edu>
parents:
19008
diff
changeset
|
787 dirent.d_type info. The optimization is valid if an st_nlink value |
7201d8037bb3
fts: introduce MIN_DIR_NLINK
Paul Eggert <eggert@cs.ucla.edu>
parents:
19008
diff
changeset
|
788 of at least MIN_DIR_NLINK is an upper bound on the number of |
19584
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
789 subdirectories of D, counting "." and ".." as subdirectories. |
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
790 DIR_FD is negative if unavailable. */ |
19011
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
791 static enum leaf_optimization |
19584
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
792 leaf_optimization (FTSENT const *p, int dir_fd) |
11164
90715da41753
fts: arrange not to stat non-directories in more cases
Jim Meyering <meyering@redhat.com>
parents:
11163
diff
changeset
|
793 { |
19584
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
794 switch (filesystem_type (p, dir_fd)) |
11164
90715da41753
fts: arrange not to stat non-directories in more cases
Jim Meyering <meyering@redhat.com>
parents:
11163
diff
changeset
|
795 { |
19136
504d2bd44d07
* lib/fts.c: Fix XFS comments.
Paul Eggert <eggert@cs.ucla.edu>
parents:
19047
diff
changeset
|
796 /* List here the file system types that may lack usable dirent.d_type |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
797 info, yet for which the optimization does apply. */ |
11164
90715da41753
fts: arrange not to stat non-directories in more cases
Jim Meyering <meyering@redhat.com>
parents:
11163
diff
changeset
|
798 case S_MAGIC_REISERFS: |
19136
504d2bd44d07
* lib/fts.c: Fix XFS comments.
Paul Eggert <eggert@cs.ucla.edu>
parents:
19047
diff
changeset
|
799 case S_MAGIC_XFS: /* XFS lacked it until 2013-08-22 commit. */ |
19011
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
800 return NOSTAT_LEAF_OPTIMIZATION; |
11164
90715da41753
fts: arrange not to stat non-directories in more cases
Jim Meyering <meyering@redhat.com>
parents:
11163
diff
changeset
|
801 |
19011
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
802 case 0: |
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
803 /* Leaf optimization is unsafe if the file system type is unknown. */ |
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
804 FALLTHROUGH; |
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
805 case S_MAGIC_AFS: |
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
806 /* Although AFS mount points are not counted in st_nlink, they |
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
807 act like directories. See <https://bugs.debian.org/143111>. */ |
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
808 FALLTHROUGH; |
19580
1fa2f90267d3
fts: treat CIFS like NFS
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
809 case S_MAGIC_CIFS: |
1fa2f90267d3
fts: treat CIFS like NFS
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
810 /* Leaf optimization causes 'find' to abort. See |
1fa2f90267d3
fts: treat CIFS like NFS
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
811 <https://lists.gnu.org/r/bug-gnulib/2018-04/msg00015.html>. */ |
1fa2f90267d3
fts: treat CIFS like NFS
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
812 FALLTHROUGH; |
18216
973834698be5
fts: don't unconditionally use leaf optimization for NFS
Pádraig Brady <P@draigBrady.com>
parents:
18189
diff
changeset
|
813 case S_MAGIC_NFS: |
973834698be5
fts: don't unconditionally use leaf optimization for NFS
Pádraig Brady <P@draigBrady.com>
parents:
18189
diff
changeset
|
814 /* NFS provides usable dirent.d_type but not necessarily for all entries |
973834698be5
fts: don't unconditionally use leaf optimization for NFS
Pádraig Brady <P@draigBrady.com>
parents:
18189
diff
changeset
|
815 of large directories, so as per <https://bugzilla.redhat.com/1252549> |
973834698be5
fts: don't unconditionally use leaf optimization for NFS
Pádraig Brady <P@draigBrady.com>
parents:
18189
diff
changeset
|
816 NFS should return true. However st_nlink values are not accurate on |
973834698be5
fts: don't unconditionally use leaf optimization for NFS
Pádraig Brady <P@draigBrady.com>
parents:
18189
diff
changeset
|
817 all implementations as per <https://bugzilla.redhat.com/1299169>. */ |
18930
50d1e186bf78
fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents:
18626
diff
changeset
|
818 FALLTHROUGH; |
11164
90715da41753
fts: arrange not to stat non-directories in more cases
Jim Meyering <meyering@redhat.com>
parents:
11163
diff
changeset
|
819 case S_MAGIC_PROC: |
19192
d86e08b1f555
all: Replace many more http URLs by https URLs. Update stale URLs.
Bruno Haible <bruno@clisp.org>
parents:
19190
diff
changeset
|
820 /* Per <https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=143111> /proc |
d86e08b1f555
all: Replace many more http URLs by https URLs. Update stale URLs.
Bruno Haible <bruno@clisp.org>
parents:
19190
diff
changeset
|
821 may have bogus stat.st_nlink values. */ |
19011
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
822 return NO_LEAF_OPTIMIZATION; |
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
823 |
11164
90715da41753
fts: arrange not to stat non-directories in more cases
Jim Meyering <meyering@redhat.com>
parents:
11163
diff
changeset
|
824 default: |
19011
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
825 return OK_LEAF_OPTIMIZATION; |
11164
90715da41753
fts: arrange not to stat non-directories in more cases
Jim Meyering <meyering@redhat.com>
parents:
11163
diff
changeset
|
826 } |
90715da41753
fts: arrange not to stat non-directories in more cases
Jim Meyering <meyering@redhat.com>
parents:
11163
diff
changeset
|
827 } |
90715da41753
fts: arrange not to stat non-directories in more cases
Jim Meyering <meyering@redhat.com>
parents:
11163
diff
changeset
|
828 |
11163
f448fc8fe832
fts: move a function definition "up" (no semantic change)
Jim Meyering <meyering@redhat.com>
parents:
11162
diff
changeset
|
829 #else |
12050 | 830 static bool |
19584
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
831 dirent_inode_sort_may_be_useful (FTSENT const *p _GL_UNUSED, |
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
832 int dir_fd _GL_UNUSED) |
11164
90715da41753
fts: arrange not to stat non-directories in more cases
Jim Meyering <meyering@redhat.com>
parents:
11163
diff
changeset
|
833 { |
19010
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
834 return true; |
11164
90715da41753
fts: arrange not to stat non-directories in more cases
Jim Meyering <meyering@redhat.com>
parents:
11163
diff
changeset
|
835 } |
19011
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
836 static enum leaf_optimization |
19584
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
837 leaf_optimization (FTSENT const *p _GL_UNUSED, int dir_fd _GL_UNUSED) |
19010
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
838 { |
19011
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
839 return NO_LEAF_OPTIMIZATION; |
19010
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
840 } |
53fb2576bf3c
fts: cache dirent_inode_sort_may_be_useful too
Paul Eggert <eggert@cs.ucla.edu>
parents:
19009
diff
changeset
|
841 #endif |
11164
90715da41753
fts: arrange not to stat non-directories in more cases
Jim Meyering <meyering@redhat.com>
parents:
11163
diff
changeset
|
842 |
5858 | 843 /* |
5907 | 844 * Special case of "/" at the end of the file name so that slashes aren't |
845 * appended which would cause file names to be written as "....//foo". | |
5858 | 846 */ |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
847 #define NAPPEND(p) \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
848 (p->fts_path[p->fts_pathlen - 1] == '/' \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
849 ? p->fts_pathlen - 1 : p->fts_pathlen) |
5858 | 850 |
851 FTSENT * | |
852 fts_read (register FTS *sp) | |
853 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
854 register FTSENT *p, *tmp; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
855 register unsigned short int instr; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
856 register char *t; |
5858 | 857 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
858 /* If finished or unrecoverable error, return NULL. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
859 if (sp->fts_cur == NULL || ISSET(FTS_STOP)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
860 return (NULL); |
5858 | 861 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
862 /* Set current node pointer. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
863 p = sp->fts_cur; |
5858 | 864 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
865 /* Save and zero out user instructions. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
866 instr = p->fts_instr; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
867 p->fts_instr = FTS_NOINSTR; |
5858 | 868 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
869 /* Any type of file may be re-visited; re-stat and re-turn. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
870 if (instr == FTS_AGAIN) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
871 p->fts_info = fts_stat(sp, p, false); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
872 return (p); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
873 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
874 Dprintf (("fts_read: p=%s\n", |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
875 p->fts_info == FTS_INIT ? "" : p->fts_path)); |
5858 | 876 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
877 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
878 * Following a symlink -- SLNONE test allows application to see |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
879 * SLNONE and recover. If indirecting through a symlink, have |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
880 * keep a pointer to current location. If unable to get that |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
881 * pointer, follow fails. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
882 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
883 if (instr == FTS_FOLLOW && |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
884 (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
885 p->fts_info = fts_stat(sp, p, true); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
886 if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
887 if ((p->fts_symfd = diropen (sp, ".")) < 0) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
888 p->fts_errno = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
889 p->fts_info = FTS_ERR; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
890 } else |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
891 p->fts_flags |= FTS_SYMFOLLOW; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
892 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
893 goto check_for_dir; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
894 } |
5858 | 895 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
896 /* Directory in pre-order. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
897 if (p->fts_info == FTS_D) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
898 /* If skipped or crossed mount point, do post-order visit. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
899 if (instr == FTS_SKIP || |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
900 (ISSET(FTS_XDEV) && p->fts_statp->st_dev != sp->fts_dev)) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
901 if (p->fts_flags & FTS_SYMFOLLOW) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
902 (void)close(p->fts_symfd); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
903 if (sp->fts_child) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
904 fts_lfree(sp->fts_child); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
905 sp->fts_child = NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
906 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
907 p->fts_info = FTS_DP; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
908 LEAVE_DIR (sp, p, "1"); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
909 return (p); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
910 } |
5858 | 911 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
912 /* Rebuild if only read the names and now traversing. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
913 if (sp->fts_child != NULL && ISSET(FTS_NAMEONLY)) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
914 CLR(FTS_NAMEONLY); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
915 fts_lfree(sp->fts_child); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
916 sp->fts_child = NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
917 } |
5858 | 918 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
919 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
920 * Cd to the subdirectory. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
921 * |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
922 * If have already read and now fail to chdir, whack the list |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
923 * to make the names come out right, and set the parent errno |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
924 * so the application will eventually get an error condition. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
925 * Set the FTS_DONTCHDIR flag so that when we logically change |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
926 * directories back to the parent we don't do a chdir. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
927 * |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
928 * If haven't read do so. If the read fails, fts_build sets |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
929 * FTS_STOP or the fts_info field of the node. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
930 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
931 if (sp->fts_child != NULL) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
932 if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
933 p->fts_errno = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
934 p->fts_flags |= FTS_DONTCHDIR; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
935 for (p = sp->fts_child; p != NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
936 p = p->fts_link) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
937 p->fts_accpath = |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
938 p->fts_parent->fts_accpath; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
939 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
940 } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
941 if (ISSET(FTS_STOP)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
942 return (NULL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
943 /* If fts_build's call to fts_safe_changedir failed |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
944 because it was not able to fchdir into a |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
945 subdirectory, tell the caller. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
946 if (p->fts_errno && p->fts_info != FTS_DNR) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
947 p->fts_info = FTS_ERR; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
948 LEAVE_DIR (sp, p, "2"); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
949 return (p); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
950 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
951 p = sp->fts_child; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
952 sp->fts_child = NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
953 goto name; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
954 } |
5858 | 955 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
956 /* Move to the next node on this level. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
957 next: tmp = p; |
15531
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
958 |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
959 /* If we have so many directory entries that we're reading them |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
960 in batches, and we've reached the end of the current batch, |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
961 read in a new batch. */ |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
962 if (p->fts_link == NULL && p->fts_parent->fts_dirp) |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
963 { |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
964 p = tmp->fts_parent; |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
965 sp->fts_cur = p; |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
966 sp->fts_path[p->fts_pathlen] = '\0'; |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
967 |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
968 if ((p = fts_build (sp, BREAD)) == NULL) |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
969 { |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
970 if (ISSET(FTS_STOP)) |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
971 return NULL; |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
972 goto cd_dot_dot; |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
973 } |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
974 |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
975 free(tmp); |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
976 goto name; |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
977 } |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
978 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
979 if ((p = p->fts_link) != NULL) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
980 sp->fts_cur = p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
981 free(tmp); |
5858 | 982 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
983 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
984 * If reached the top, return to the original directory (or |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
985 * the root of the tree), and load the file names for the next |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
986 * root. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
987 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
988 if (p->fts_level == FTS_ROOTLEVEL) { |
15988
cd7ac59d8eb5
fts: close parent dir FD before returning from post-traversal fts_read
Jim Meyering <meyering@redhat.com>
parents:
15531
diff
changeset
|
989 if (restore_initial_cwd(sp)) { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
990 SET(FTS_STOP); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
991 return (NULL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
992 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
993 free_dir(sp); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
994 fts_load(sp, p); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
995 setup_dir(sp); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
996 goto check_for_dir; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
997 } |
5858 | 998 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
999 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1000 * User may have called fts_set on the node. If skipped, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1001 * ignore. If followed, get a file descriptor so we can |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1002 * get back if necessary. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1003 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1004 if (p->fts_instr == FTS_SKIP) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1005 goto next; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1006 if (p->fts_instr == FTS_FOLLOW) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1007 p->fts_info = fts_stat(sp, p, true); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1008 if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1009 if ((p->fts_symfd = diropen (sp, ".")) < 0) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1010 p->fts_errno = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1011 p->fts_info = FTS_ERR; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1012 } else |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1013 p->fts_flags |= FTS_SYMFOLLOW; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1014 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1015 p->fts_instr = FTS_NOINSTR; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1016 } |
5858 | 1017 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1018 name: t = sp->fts_path + NAPPEND(p->fts_parent); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1019 *t++ = '/'; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1020 memmove(t, p->fts_name, p->fts_namelen + 1); |
5872 | 1021 check_for_dir: |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1022 sp->fts_cur = p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1023 if (p->fts_info == FTS_NSOK) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1024 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1025 if (p->fts_statp->st_size == FTS_STAT_REQUIRED) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1026 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1027 FTSENT *parent = p->fts_parent; |
19008
a6fc0157c611
fts: nlink_t signedness fixups
Paul Eggert <eggert@cs.ucla.edu>
parents:
18930
diff
changeset
|
1028 if (parent->fts_n_dirs_remaining == 0 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1029 && ISSET(FTS_NOSTAT) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1030 && ISSET(FTS_PHYSICAL) |
19584
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
1031 && (leaf_optimization (parent, sp->fts_cwd_fd) |
19011
c152c191727b
fts: three levels of leaf optimization
Paul Eggert <eggert@cs.ucla.edu>
parents:
19010
diff
changeset
|
1032 == NOSTAT_LEAF_OPTIMIZATION)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1033 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1034 /* nothing more needed */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1035 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1036 else |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1037 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1038 p->fts_info = fts_stat(sp, p, false); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1039 if (S_ISDIR(p->fts_statp->st_mode) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1040 && p->fts_level != FTS_ROOTLEVEL |
19008
a6fc0157c611
fts: nlink_t signedness fixups
Paul Eggert <eggert@cs.ucla.edu>
parents:
18930
diff
changeset
|
1041 && 0 < parent->fts_n_dirs_remaining |
a6fc0157c611
fts: nlink_t signedness fixups
Paul Eggert <eggert@cs.ucla.edu>
parents:
18930
diff
changeset
|
1042 && parent->fts_n_dirs_remaining != (nlink_t) -1) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1043 parent->fts_n_dirs_remaining--; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1044 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1045 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1046 else |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1047 fts_assert (p->fts_statp->st_size == FTS_NO_STAT_REQUIRED); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1048 } |
7707
3f57a32346a2
* lib/fts.c (fts_load): Don't set sp->fts_dev here, since
Jim Meyering <jim@meyering.net>
parents:
7684
diff
changeset
|
1049 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1050 if (p->fts_info == FTS_D) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1051 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1052 /* Now that P->fts_statp is guaranteed to be valid, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1053 if this is a command-line directory, record its |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1054 device number, to be used for FTS_XDEV. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1055 if (p->fts_level == FTS_ROOTLEVEL) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1056 sp->fts_dev = p->fts_statp->st_dev; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1057 Dprintf ((" entering: %s\n", p->fts_path)); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1058 if (! enter_dir (sp, p)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1059 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1060 __set_errno (ENOMEM); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1061 return NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1062 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1063 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1064 return p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1065 } |
15531
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1066 cd_dot_dot: |
5858 | 1067 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1068 /* Move up to the parent node. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1069 p = tmp->fts_parent; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1070 sp->fts_cur = p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1071 free(tmp); |
5858 | 1072 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1073 if (p->fts_level == FTS_ROOTPARENTLEVEL) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1074 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1075 * Done; free everything up and set errno to 0 so the user |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1076 * can distinguish between error and EOF. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1077 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1078 free(p); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1079 __set_errno (0); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1080 return (sp->fts_cur = NULL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1081 } |
5858 | 1082 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1083 fts_assert (p->fts_info != FTS_NSOK); |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1084 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1085 /* NUL terminate the file name. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1086 sp->fts_path[p->fts_pathlen] = '\0'; |
5858 | 1087 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1088 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1089 * Return to the parent directory. If at a root node, restore |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1090 * the initial working directory. If we came through a symlink, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1091 * go back through the file descriptor. Otherwise, move up |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1092 * one level, via "..". |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1093 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1094 if (p->fts_level == FTS_ROOTLEVEL) { |
15988
cd7ac59d8eb5
fts: close parent dir FD before returning from post-traversal fts_read
Jim Meyering <meyering@redhat.com>
parents:
15531
diff
changeset
|
1095 if (restore_initial_cwd(sp)) { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1096 p->fts_errno = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1097 SET(FTS_STOP); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1098 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1099 } else if (p->fts_flags & FTS_SYMFOLLOW) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1100 if (FCHDIR(sp, p->fts_symfd)) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1101 p->fts_errno = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1102 SET(FTS_STOP); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1103 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1104 (void)close(p->fts_symfd); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1105 } else if (!(p->fts_flags & FTS_DONTCHDIR) && |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1106 fts_safe_changedir(sp, p->fts_parent, -1, "..")) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1107 p->fts_errno = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1108 SET(FTS_STOP); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1109 } |
17914
644307da6af9
fts: avoid crash when a cycle is added while traversing
Kamil Dudka <kdudka@redhat.com>
parents:
17848
diff
changeset
|
1110 |
644307da6af9
fts: avoid crash when a cycle is added while traversing
Kamil Dudka <kdudka@redhat.com>
parents:
17848
diff
changeset
|
1111 /* If the directory causes a cycle, preserve the FTS_DC flag and keep |
644307da6af9
fts: avoid crash when a cycle is added while traversing
Kamil Dudka <kdudka@redhat.com>
parents:
17848
diff
changeset
|
1112 the corresponding dev/ino pair in the hash table. It is going to be |
644307da6af9
fts: avoid crash when a cycle is added while traversing
Kamil Dudka <kdudka@redhat.com>
parents:
17848
diff
changeset
|
1113 removed when leaving the original directory. */ |
644307da6af9
fts: avoid crash when a cycle is added while traversing
Kamil Dudka <kdudka@redhat.com>
parents:
17848
diff
changeset
|
1114 if (p->fts_info != FTS_DC) { |
644307da6af9
fts: avoid crash when a cycle is added while traversing
Kamil Dudka <kdudka@redhat.com>
parents:
17848
diff
changeset
|
1115 p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP; |
644307da6af9
fts: avoid crash when a cycle is added while traversing
Kamil Dudka <kdudka@redhat.com>
parents:
17848
diff
changeset
|
1116 if (p->fts_errno == 0) |
644307da6af9
fts: avoid crash when a cycle is added while traversing
Kamil Dudka <kdudka@redhat.com>
parents:
17848
diff
changeset
|
1117 LEAVE_DIR (sp, p, "3"); |
644307da6af9
fts: avoid crash when a cycle is added while traversing
Kamil Dudka <kdudka@redhat.com>
parents:
17848
diff
changeset
|
1118 } |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1119 return ISSET(FTS_STOP) ? NULL : p; |
5858 | 1120 } |
1121 | |
1122 /* | |
1123 * Fts_set takes the stream as an argument although it's not used in this | |
1124 * implementation; it would be necessary if anyone wanted to add global | |
1125 * semantics to fts using fts_set. An error return is allowed for similar | |
1126 * reasons. | |
1127 */ | |
1128 /* ARGSUSED */ | |
1129 int | |
12482
11bc92a9e2bc
gnulib-common: prefer _GL_UNUSED over _UNUSED_PARAMETER_
Eric Blake <ebb9@byu.net>
parents:
12421
diff
changeset
|
1130 fts_set(FTS *sp _GL_UNUSED, FTSENT *p, int instr) |
5858 | 1131 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1132 if (instr != 0 && instr != FTS_AGAIN && instr != FTS_FOLLOW && |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1133 instr != FTS_NOINSTR && instr != FTS_SKIP) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1134 __set_errno (EINVAL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1135 return (1); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1136 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1137 p->fts_instr = instr; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1138 return (0); |
5858 | 1139 } |
1140 | |
1141 FTSENT * | |
1142 fts_children (register FTS *sp, int instr) | |
1143 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1144 register FTSENT *p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1145 int fd; |
5858 | 1146 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1147 if (instr != 0 && instr != FTS_NAMEONLY) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1148 __set_errno (EINVAL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1149 return (NULL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1150 } |
5858 | 1151 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1152 /* Set current node pointer. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1153 p = sp->fts_cur; |
5858 | 1154 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1155 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1156 * Errno set to 0 so user can distinguish empty directory from |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1157 * an error. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1158 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1159 __set_errno (0); |
5858 | 1160 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1161 /* Fatal errors stop here. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1162 if (ISSET(FTS_STOP)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1163 return (NULL); |
5858 | 1164 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1165 /* Return logical hierarchy of user's arguments. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1166 if (p->fts_info == FTS_INIT) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1167 return (p->fts_link); |
5858 | 1168 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1169 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1170 * If not a directory being visited in pre-order, stop here. Could |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1171 * allow FTS_DNR, assuming the user has fixed the problem, but the |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1172 * same effect is available with FTS_AGAIN. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1173 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1174 if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1175 return (NULL); |
5858 | 1176 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1177 /* Free up any previous child list. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1178 if (sp->fts_child != NULL) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1179 fts_lfree(sp->fts_child); |
5858 | 1180 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1181 if (instr == FTS_NAMEONLY) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1182 SET(FTS_NAMEONLY); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1183 instr = BNAMES; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1184 } else |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1185 instr = BCHILD; |
5858 | 1186 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1187 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1188 * If using chdir on a relative file name and called BEFORE fts_read |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1189 * does its chdir to the root of a traversal, we can lose -- we need to |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1190 * chdir into the subdirectory, and we don't know where the current |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1191 * directory is, so we can't get back so that the upcoming chdir by |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1192 * fts_read will work. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1193 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1194 if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' || |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1195 ISSET(FTS_NOCHDIR)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1196 return (sp->fts_child = fts_build(sp, instr)); |
5858 | 1197 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1198 if ((fd = diropen (sp, ".")) < 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1199 return (sp->fts_child = NULL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1200 sp->fts_child = fts_build(sp, instr); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1201 if (ISSET(FTS_CWDFD)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1202 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1203 cwd_advance_fd (sp, fd, true); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1204 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1205 else |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1206 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1207 if (fchdir(fd)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1208 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1209 int saved_errno = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1210 close (fd); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1211 __set_errno (saved_errno); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1212 return NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1213 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1214 close (fd); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1215 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1216 return (sp->fts_child); |
5858 | 1217 } |
1218 | |
10481
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
1219 /* A comparison function to sort on increasing inode number. |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
1220 For some file system types, sorting either way makes a huge |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
1221 performance difference for a directory with very many entries, |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
1222 but sorting on increasing values is slightly better than sorting |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
1223 on decreasing values. The difference is in the 5% range. */ |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
1224 static int |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
1225 fts_compare_ino (struct _ftsent const **a, struct _ftsent const **b) |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
1226 { |
10482
e8d1a5b2c49c
fts: tweak inode comparison function
Jim Meyering <meyering@redhat.com>
parents:
10481
diff
changeset
|
1227 return (a[0]->fts_statp->st_ino < b[0]->fts_statp->st_ino ? -1 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1228 : b[0]->fts_statp->st_ino < a[0]->fts_statp->st_ino ? 1 : 0); |
10481
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
1229 } |
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
1230 |
10843
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1231 /* Map the dirent.d_type value, DTYPE, to the corresponding stat.st_mode |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1232 S_IF* bit and set ST.st_mode, thus clearing all other bits in that field. */ |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1233 static void |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1234 set_stat_type (struct stat *st, unsigned int dtype) |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1235 { |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1236 mode_t type; |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1237 switch (dtype) |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1238 { |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1239 case DT_BLK: |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1240 type = S_IFBLK; |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1241 break; |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1242 case DT_CHR: |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1243 type = S_IFCHR; |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1244 break; |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1245 case DT_DIR: |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1246 type = S_IFDIR; |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1247 break; |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1248 case DT_FIFO: |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1249 type = S_IFIFO; |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1250 break; |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1251 case DT_LNK: |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1252 type = S_IFLNK; |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1253 break; |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1254 case DT_REG: |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1255 type = S_IFREG; |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1256 break; |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1257 case DT_SOCK: |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1258 type = S_IFSOCK; |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1259 break; |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1260 default: |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1261 type = 0; |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1262 } |
10844 | 1263 st->st_mode = type; |
10843
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1264 } |
36479bd141ee
fts: provide dirent.d_type via FTSENT.fts_statp, when possible
Jim Meyering <meyering@redhat.com>
parents:
10688
diff
changeset
|
1265 |
15529
894e5e6ae541
fts: add/use new struct member, fts_dirp
Jim Meyering <meyering@redhat.com>
parents:
15528
diff
changeset
|
1266 #define closedir_and_clear(dirp) \ |
894e5e6ae541
fts: add/use new struct member, fts_dirp
Jim Meyering <meyering@redhat.com>
parents:
15528
diff
changeset
|
1267 do \ |
894e5e6ae541
fts: add/use new struct member, fts_dirp
Jim Meyering <meyering@redhat.com>
parents:
15528
diff
changeset
|
1268 { \ |
894e5e6ae541
fts: add/use new struct member, fts_dirp
Jim Meyering <meyering@redhat.com>
parents:
15528
diff
changeset
|
1269 closedir (dirp); \ |
894e5e6ae541
fts: add/use new struct member, fts_dirp
Jim Meyering <meyering@redhat.com>
parents:
15528
diff
changeset
|
1270 dirp = NULL; \ |
894e5e6ae541
fts: add/use new struct member, fts_dirp
Jim Meyering <meyering@redhat.com>
parents:
15528
diff
changeset
|
1271 } \ |
894e5e6ae541
fts: add/use new struct member, fts_dirp
Jim Meyering <meyering@redhat.com>
parents:
15528
diff
changeset
|
1272 while (0) |
894e5e6ae541
fts: add/use new struct member, fts_dirp
Jim Meyering <meyering@redhat.com>
parents:
15528
diff
changeset
|
1273 |
15528
8203b056cc79
maint: fts: give __opendir2 a new parameter and rename
Jim Meyering <meyering@redhat.com>
parents:
15527
diff
changeset
|
1274 #define fts_opendir(file, Pdir_fd) \ |
15525
792927a7e09d
maint: fts.c: move __opendir2 #define "up" out of function body
Jim Meyering <meyering@redhat.com>
parents:
15524
diff
changeset
|
1275 opendirat((! ISSET(FTS_NOCHDIR) && ISSET(FTS_CWDFD) \ |
792927a7e09d
maint: fts.c: move __opendir2 #define "up" out of function body
Jim Meyering <meyering@redhat.com>
parents:
15524
diff
changeset
|
1276 ? sp->fts_cwd_fd : AT_FDCWD), \ |
792927a7e09d
maint: fts.c: move __opendir2 #define "up" out of function body
Jim Meyering <meyering@redhat.com>
parents:
15524
diff
changeset
|
1277 file, \ |
792927a7e09d
maint: fts.c: move __opendir2 #define "up" out of function body
Jim Meyering <meyering@redhat.com>
parents:
15524
diff
changeset
|
1278 (((ISSET(FTS_PHYSICAL) \ |
792927a7e09d
maint: fts.c: move __opendir2 #define "up" out of function body
Jim Meyering <meyering@redhat.com>
parents:
15524
diff
changeset
|
1279 && ! (ISSET(FTS_COMFOLLOW) \ |
792927a7e09d
maint: fts.c: move __opendir2 #define "up" out of function body
Jim Meyering <meyering@redhat.com>
parents:
15524
diff
changeset
|
1280 && cur->fts_level == FTS_ROOTLEVEL)) \ |
39901
33cf8a9f314a
fts: remove FTS_NOATIME
Bernhard Voelker <mail@bernhard-voelker.de>
parents:
19683
diff
changeset
|
1281 ? O_NOFOLLOW : 0)), \ |
15528
8203b056cc79
maint: fts: give __opendir2 a new parameter and rename
Jim Meyering <meyering@redhat.com>
parents:
15527
diff
changeset
|
1282 Pdir_fd) |
15525
792927a7e09d
maint: fts.c: move __opendir2 #define "up" out of function body
Jim Meyering <meyering@redhat.com>
parents:
15524
diff
changeset
|
1283 |
5858 | 1284 /* |
1285 * This is the tricky part -- do not casually change *anything* in here. The | |
1286 * idea is to build the linked list of entries that are used by fts_children | |
1287 * and fts_read. There are lots of special cases. | |
1288 * | |
1289 * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is | |
1290 * set and it's a physical walk (so that symbolic links can't be directories), | |
1291 * we can do things quickly. First, if it's a 4.4BSD file system, the type | |
1292 * of the file is in the directory entry. Otherwise, we assume that the number | |
1293 * of subdirectories in a node is equal to the number of links to the parent. | |
1294 * The former skips all stat calls. The latter skips stat calls in any leaf | |
1295 * directories and for any files after the subdirectories in the directory have | |
1296 * been found, cutting the stat calls by about 2/3. | |
1297 */ | |
1298 static FTSENT * | |
1299 internal_function | |
1300 fts_build (register FTS *sp, int type) | |
1301 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1302 register FTSENT *p, *head; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1303 register size_t nitems; |
15529
894e5e6ae541
fts: add/use new struct member, fts_dirp
Jim Meyering <meyering@redhat.com>
parents:
15528
diff
changeset
|
1304 FTSENT *tail; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1305 void *oldaddr; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1306 int saved_errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1307 bool descend; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1308 bool doadjust; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1309 ptrdiff_t level; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1310 size_t len, maxlen, new_len; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1311 char *cp; |
13676
223800d7ac7d
fts, getcwd, glob: audit for dirfd returning -1
Paul Eggert <eggert@cs.ucla.edu>
parents:
13674
diff
changeset
|
1312 int dir_fd; |
15529
894e5e6ae541
fts: add/use new struct member, fts_dirp
Jim Meyering <meyering@redhat.com>
parents:
15528
diff
changeset
|
1313 FTSENT *cur = sp->fts_cur; |
15531
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1314 bool continue_readdir = !!cur->fts_dirp; |
19584
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
1315 bool sort_by_inode = false; |
17797 | 1316 size_t max_entries; |
5858 | 1317 |
15531
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1318 /* When cur->fts_dirp is non-NULL, that means we should |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1319 continue calling readdir on that existing DIR* pointer |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1320 rather than opening a new one. */ |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1321 if (continue_readdir) |
15526
95cf436ab3b8
maint: fts.c: correct off-by-one indentation
Jim Meyering <meyering@redhat.com>
parents:
15525
diff
changeset
|
1322 { |
15531
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1323 DIR *dp = cur->fts_dirp; |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1324 dir_fd = dirfd (dp); |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1325 if (dir_fd < 0) |
15526
95cf436ab3b8
maint: fts.c: correct off-by-one indentation
Jim Meyering <meyering@redhat.com>
parents:
15525
diff
changeset
|
1326 { |
15531
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1327 closedir_and_clear (cur->fts_dirp); |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1328 if (type == BREAD) |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1329 { |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1330 cur->fts_info = FTS_DNR; |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1331 cur->fts_errno = errno; |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1332 } |
15526
95cf436ab3b8
maint: fts.c: correct off-by-one indentation
Jim Meyering <meyering@redhat.com>
parents:
15525
diff
changeset
|
1333 return NULL; |
95cf436ab3b8
maint: fts.c: correct off-by-one indentation
Jim Meyering <meyering@redhat.com>
parents:
15525
diff
changeset
|
1334 } |
95cf436ab3b8
maint: fts.c: correct off-by-one indentation
Jim Meyering <meyering@redhat.com>
parents:
15525
diff
changeset
|
1335 } |
15531
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1336 else |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1337 { |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1338 /* Open the directory for reading. If this fails, we're done. |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1339 If being called from fts_read, set the fts_info field. */ |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1340 if ((cur->fts_dirp = fts_opendir(cur->fts_accpath, &dir_fd)) == NULL) |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1341 { |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1342 if (type == BREAD) |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1343 { |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1344 cur->fts_info = FTS_DNR; |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1345 cur->fts_errno = errno; |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1346 } |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1347 return NULL; |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1348 } |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1349 /* Rather than calling fts_stat for each and every entry encountered |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1350 in the readdir loop (below), stat each directory only right after |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1351 opening it. */ |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1352 if (cur->fts_info == FTS_NSOK) |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1353 cur->fts_info = fts_stat(sp, cur, false); |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1354 else if (sp->fts_options & FTS_TIGHT_CYCLE_CHECK) |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1355 { |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1356 /* Now read the stat info again after opening a directory to |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1357 reveal eventual changes caused by a submount triggered by |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1358 the traversal. But do it only for utilities which use |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1359 FTS_TIGHT_CYCLE_CHECK. Therefore, only find and du |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1360 benefit/suffer from this feature for now. */ |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1361 LEAVE_DIR (sp, cur, "4"); |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1362 fts_stat (sp, cur, false); |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1363 if (! enter_dir (sp, cur)) |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1364 { |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1365 __set_errno (ENOMEM); |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1366 return NULL; |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1367 } |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1368 } |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1369 } |
5858 | 1370 |
15531
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1371 /* Maximum number of readdir entries to read at one time. This |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1372 limitation is to avoid reading millions of entries into memory |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1373 at once. When an fts_compar function is specified, we have no |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1374 choice: we must read all entries into memory before calling that |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1375 function. But when no such function is specified, we can read |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1376 entries in batches that are large enough to help us with inode- |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1377 sorting, yet not so large that we risk exhausting memory. */ |
17797 | 1378 max_entries = sp->fts_compar ? SIZE_MAX : FTS_MAX_READDIR_ENTRIES; |
15531
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1379 |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1380 /* |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1381 * If we're going to need to stat anything or we want to descend |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1382 * and stay in the directory, chdir. If this fails we keep going, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1383 * but set a flag so we don't chdir after the post-order visit. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1384 * We won't be able to stat anything, but we can still return the |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1385 * names themselves. Note, that since fts_read won't be able to |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1386 * chdir into the directory, it will have to return different file |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1387 * names than before, i.e. "a/b" instead of "b". Since the node |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1388 * has already been visited in pre-order, have to wait until the |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1389 * post-order visit to return the error. There is a special case |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1390 * here, if there was nothing to stat then it's not an error to |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1391 * not be able to stat. This is all fairly nasty. If a program |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1392 * needed sorted entries or stat information, they had better be |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1393 * checking FTS_NS on the returned nodes. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1394 */ |
15531
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1395 if (continue_readdir) |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1396 { |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1397 /* When resuming a short readdir run, we already have |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1398 the required dirp and dir_fd. */ |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1399 descend = true; |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1400 } |
19012 | 1401 else |
1402 { | |
1403 /* Try to descend unless it is a names-only fts_children, | |
19136
504d2bd44d07
* lib/fts.c: Fix XFS comments.
Paul Eggert <eggert@cs.ucla.edu>
parents:
19047
diff
changeset
|
1404 or the directory is known to lack subdirectories. */ |
19012 | 1405 descend = (type != BNAMES |
1406 && ! (ISSET (FTS_NOSTAT) && ISSET (FTS_PHYSICAL) | |
1407 && ! ISSET (FTS_SEEDOT) | |
1408 && cur->fts_statp->st_nlink == MIN_DIR_NLINK | |
19584
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
1409 && (leaf_optimization (cur, dir_fd) |
19012 | 1410 != NO_LEAF_OPTIMIZATION))); |
1411 if (descend || type == BREAD) | |
1412 { | |
13676
223800d7ac7d
fts, getcwd, glob: audit for dirfd returning -1
Paul Eggert <eggert@cs.ucla.edu>
parents:
13674
diff
changeset
|
1413 if (ISSET(FTS_CWDFD)) |
19047 | 1414 dir_fd = fcntl (dir_fd, F_DUPFD_CLOEXEC, STDERR_FILENO + 1); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1415 if (dir_fd < 0 || fts_safe_changedir(sp, cur, dir_fd, NULL)) { |
19012 | 1416 if (descend && type == BREAD) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1417 cur->fts_errno = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1418 cur->fts_flags |= FTS_DONTCHDIR; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1419 descend = false; |
15529
894e5e6ae541
fts: add/use new struct member, fts_dirp
Jim Meyering <meyering@redhat.com>
parents:
15528
diff
changeset
|
1420 closedir_and_clear(cur->fts_dirp); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1421 if (ISSET(FTS_CWDFD) && 0 <= dir_fd) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1422 close (dir_fd); |
15529
894e5e6ae541
fts: add/use new struct member, fts_dirp
Jim Meyering <meyering@redhat.com>
parents:
15528
diff
changeset
|
1423 cur->fts_dirp = NULL; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1424 } else |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1425 descend = true; |
19012 | 1426 } |
1427 } | |
5858 | 1428 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1429 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1430 * Figure out the max file name length that can be stored in the |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1431 * current buffer -- the inner loop allocates more space as necessary. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1432 * We really wouldn't have to do the maxlen calculations here, we |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1433 * could do them in fts_read before returning the name, but it's a |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1434 * lot easier here since the length is part of the dirent structure. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1435 * |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1436 * If not changing directories set a pointer so that can just append |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1437 * each new component into the file name. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1438 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1439 len = NAPPEND(cur); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1440 if (ISSET(FTS_NOCHDIR)) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1441 cp = sp->fts_path + len; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1442 *cp++ = '/'; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1443 } else { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1444 /* GCC, you're too verbose. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1445 cp = NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1446 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1447 len++; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1448 maxlen = sp->fts_pathlen - len; |
5858 | 1449 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1450 level = cur->fts_level + 1; |
5858 | 1451 |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
1452 /* Read the directory, attaching each entry to the "link" pointer. */ |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1453 doadjust = false; |
15530
ddc76c261e25
maint: fts: move decl of `dp' down into while loop; split a long line
Jim Meyering <meyering@redhat.com>
parents:
15529
diff
changeset
|
1454 head = NULL; |
ddc76c261e25
maint: fts: move decl of `dp' down into while loop; split a long line
Jim Meyering <meyering@redhat.com>
parents:
15529
diff
changeset
|
1455 tail = NULL; |
ddc76c261e25
maint: fts: move decl of `dp' down into while loop; split a long line
Jim Meyering <meyering@redhat.com>
parents:
15529
diff
changeset
|
1456 nitems = 0; |
ddc76c261e25
maint: fts: move decl of `dp' down into while loop; split a long line
Jim Meyering <meyering@redhat.com>
parents:
15529
diff
changeset
|
1457 while (cur->fts_dirp) { |
17640
aa5bda4ec2ae
fts: avoid unnecessary strlen calls
Bernhard Voelker <mail@bernhard-voelker.de>
parents:
17576
diff
changeset
|
1458 size_t d_namelen; |
18338
7276fe79480b
fts: handle readdir() errors
Pádraig Brady <P@draigBrady.com>
parents:
18325
diff
changeset
|
1459 __set_errno (0); |
15530
ddc76c261e25
maint: fts: move decl of `dp' down into while loop; split a long line
Jim Meyering <meyering@redhat.com>
parents:
15529
diff
changeset
|
1460 struct dirent *dp = readdir(cur->fts_dirp); |
18338
7276fe79480b
fts: handle readdir() errors
Pádraig Brady <P@draigBrady.com>
parents:
18325
diff
changeset
|
1461 if (dp == NULL) { |
7276fe79480b
fts: handle readdir() errors
Pádraig Brady <P@draigBrady.com>
parents:
18325
diff
changeset
|
1462 if (errno) { |
7276fe79480b
fts: handle readdir() errors
Pádraig Brady <P@draigBrady.com>
parents:
18325
diff
changeset
|
1463 cur->fts_errno = errno; |
7276fe79480b
fts: handle readdir() errors
Pádraig Brady <P@draigBrady.com>
parents:
18325
diff
changeset
|
1464 /* If we've not read any items yet, treat |
7276fe79480b
fts: handle readdir() errors
Pádraig Brady <P@draigBrady.com>
parents:
18325
diff
changeset
|
1465 the error as if we can't access the dir. */ |
7276fe79480b
fts: handle readdir() errors
Pádraig Brady <P@draigBrady.com>
parents:
18325
diff
changeset
|
1466 cur->fts_info = (continue_readdir || nitems) |
7276fe79480b
fts: handle readdir() errors
Pádraig Brady <P@draigBrady.com>
parents:
18325
diff
changeset
|
1467 ? FTS_ERR : FTS_DNR; |
7276fe79480b
fts: handle readdir() errors
Pádraig Brady <P@draigBrady.com>
parents:
18325
diff
changeset
|
1468 } |
15530
ddc76c261e25
maint: fts: move decl of `dp' down into while loop; split a long line
Jim Meyering <meyering@redhat.com>
parents:
15529
diff
changeset
|
1469 break; |
18338
7276fe79480b
fts: handle readdir() errors
Pádraig Brady <P@draigBrady.com>
parents:
18325
diff
changeset
|
1470 } |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1471 if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1472 continue; |
5858 | 1473 |
17641
019200939aa7
fts: avoid unnecessary strlen calls
Paul Eggert <eggert@cs.ucla.edu>
parents:
17640
diff
changeset
|
1474 d_namelen = _D_EXACT_NAMLEN (dp); |
019200939aa7
fts: avoid unnecessary strlen calls
Paul Eggert <eggert@cs.ucla.edu>
parents:
17640
diff
changeset
|
1475 p = fts_alloc (sp, dp->d_name, d_namelen); |
019200939aa7
fts: avoid unnecessary strlen calls
Paul Eggert <eggert@cs.ucla.edu>
parents:
17640
diff
changeset
|
1476 if (!p) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1477 goto mem1; |
17640
aa5bda4ec2ae
fts: avoid unnecessary strlen calls
Bernhard Voelker <mail@bernhard-voelker.de>
parents:
17576
diff
changeset
|
1478 if (d_namelen >= maxlen) { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1479 /* include space for NUL */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1480 oldaddr = sp->fts_path; |
17640
aa5bda4ec2ae
fts: avoid unnecessary strlen calls
Bernhard Voelker <mail@bernhard-voelker.de>
parents:
17576
diff
changeset
|
1481 if (! fts_palloc(sp, d_namelen + len + 1)) { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1482 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1483 * No more memory. Save |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1484 * errno, free up the current structure and the |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1485 * structures already allocated. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1486 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1487 mem1: saved_errno = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1488 free(p); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1489 fts_lfree(head); |
15529
894e5e6ae541
fts: add/use new struct member, fts_dirp
Jim Meyering <meyering@redhat.com>
parents:
15528
diff
changeset
|
1490 closedir_and_clear(cur->fts_dirp); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1491 cur->fts_info = FTS_ERR; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1492 SET(FTS_STOP); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1493 __set_errno (saved_errno); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1494 return (NULL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1495 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1496 /* Did realloc() change the pointer? */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1497 if (oldaddr != sp->fts_path) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1498 doadjust = true; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1499 if (ISSET(FTS_NOCHDIR)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1500 cp = sp->fts_path + len; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1501 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1502 maxlen = sp->fts_pathlen - len; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1503 } |
5858 | 1504 |
17640
aa5bda4ec2ae
fts: avoid unnecessary strlen calls
Bernhard Voelker <mail@bernhard-voelker.de>
parents:
17576
diff
changeset
|
1505 new_len = len + d_namelen; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1506 if (new_len < len) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1507 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1508 * In the unlikely event that we would end up |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1509 * with a file name longer than SIZE_MAX, free up |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1510 * the current structure and the structures already |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1511 * allocated, then error out with ENAMETOOLONG. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1512 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1513 free(p); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1514 fts_lfree(head); |
15529
894e5e6ae541
fts: add/use new struct member, fts_dirp
Jim Meyering <meyering@redhat.com>
parents:
15528
diff
changeset
|
1515 closedir_and_clear(cur->fts_dirp); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1516 cur->fts_info = FTS_ERR; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1517 SET(FTS_STOP); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1518 __set_errno (ENAMETOOLONG); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1519 return (NULL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1520 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1521 p->fts_level = level; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1522 p->fts_parent = sp->fts_cur; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1523 p->fts_pathlen = new_len; |
5858 | 1524 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1525 /* Store dirent.d_ino, in case we need to sort |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1526 entries before processing them. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1527 p->fts_statp->st_ino = D_INO (dp); |
5858 | 1528 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1529 /* Build a file name for fts_stat to stat. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1530 if (ISSET(FTS_NOCHDIR)) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1531 p->fts_accpath = p->fts_path; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1532 memmove(cp, p->fts_name, p->fts_namelen + 1); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1533 } else |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1534 p->fts_accpath = p->fts_name; |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1535 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1536 if (sp->fts_compar == NULL || ISSET(FTS_DEFER_STAT)) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1537 /* Record what fts_read will have to do with this |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1538 entry. In many cases, it will simply fts_stat it, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1539 but we can take advantage of any d_type information |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1540 to optimize away the unnecessary stat calls. I.e., |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1541 if FTS_NOSTAT is in effect and we're not following |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1542 symlinks (FTS_PHYSICAL) and d_type indicates this |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1543 is *not* a directory, then we won't have to stat it |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1544 at all. If it *is* a directory, then (currently) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1545 we stat it regardless, in order to get device and |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1546 inode numbers. Some day we might optimize that |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1547 away, too, for directories where d_ino is known to |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1548 be valid. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1549 bool skip_stat = (ISSET(FTS_PHYSICAL) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1550 && ISSET(FTS_NOSTAT) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1551 && DT_IS_KNOWN(dp) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1552 && ! DT_MUST_BE(dp, DT_DIR)); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1553 p->fts_info = FTS_NSOK; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1554 /* Propagate dirent.d_type information back |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1555 to caller, when possible. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1556 set_stat_type (p->fts_statp, D_TYPE (dp)); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1557 fts_set_stat_required(p, !skip_stat); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1558 } else { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1559 p->fts_info = fts_stat(sp, p, false); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1560 } |
5858 | 1561 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1562 /* We walk in directory order so "ls -f" doesn't get upset. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1563 p->fts_link = NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1564 if (head == NULL) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1565 head = tail = p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1566 else { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1567 tail->fts_link = p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1568 tail = p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1569 } |
19584
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
1570 |
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
1571 /* If there are many entries, no sorting function has been |
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
1572 specified, and this file system is of a type that may be |
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
1573 slow with a large number of entries, arrange to sort the |
19585 | 1574 directory entries on increasing inode numbers. |
1575 | |
1576 The NITEMS comparison uses ==, not >, because the test | |
1577 needs to be tried at most once once, and NITEMS will exceed | |
1578 the threshold after it is incremented below. */ | |
19584
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
1579 if (nitems == _FTS_INODE_SORT_DIR_ENTRIES_THRESHOLD |
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
1580 && !sp->fts_compar) |
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
1581 sort_by_inode = dirent_inode_sort_may_be_useful (cur, dir_fd); |
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
1582 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1583 ++nitems; |
15531
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1584 if (max_entries <= nitems) { |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1585 /* When there are too many dir entries, leave |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1586 fts_dirp open, so that a subsequent fts_read |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1587 can take up where we leave off. */ |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1588 goto break_without_closedir; |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1589 } |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1590 } |
15531
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1591 |
15529
894e5e6ae541
fts: add/use new struct member, fts_dirp
Jim Meyering <meyering@redhat.com>
parents:
15528
diff
changeset
|
1592 if (cur->fts_dirp) |
894e5e6ae541
fts: add/use new struct member, fts_dirp
Jim Meyering <meyering@redhat.com>
parents:
15528
diff
changeset
|
1593 closedir_and_clear(cur->fts_dirp); |
5858 | 1594 |
15531
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1595 break_without_closedir: |
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1596 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1597 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1598 * If realloc() changed the address of the file name, adjust the |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1599 * addresses for the rest of the tree and the dir list. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1600 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1601 if (doadjust) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1602 fts_padjust(sp, head); |
5858 | 1603 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1604 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1605 * If not changing directories, reset the file name back to original |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1606 * state. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1607 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1608 if (ISSET(FTS_NOCHDIR)) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1609 if (len == sp->fts_pathlen || nitems == 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1610 --cp; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1611 *cp = '\0'; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1612 } |
5858 | 1613 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1614 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1615 * If descended after called from fts_children or after called from |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1616 * fts_read and nothing found, get back. At the root level we use |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1617 * the saved fd; if one of fts_open()'s arguments is a relative name |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1618 * to an empty directory, we wind up here with no other way back. If |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1619 * can't get back, we're done. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1620 */ |
15531
65fb6cc5f301
fts: do not exhaust memory when processing million-entry directories
Jim Meyering <meyering@redhat.com>
parents:
15530
diff
changeset
|
1621 if (!continue_readdir && descend && (type == BCHILD || !nitems) && |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1622 (cur->fts_level == FTS_ROOTLEVEL |
15988
cd7ac59d8eb5
fts: close parent dir FD before returning from post-traversal fts_read
Jim Meyering <meyering@redhat.com>
parents:
15531
diff
changeset
|
1623 ? restore_initial_cwd(sp) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1624 : fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1625 cur->fts_info = FTS_ERR; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1626 SET(FTS_STOP); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1627 fts_lfree(head); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1628 return (NULL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1629 } |
5858 | 1630 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1631 /* If didn't find anything, return NULL. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1632 if (!nitems) { |
18338
7276fe79480b
fts: handle readdir() errors
Pádraig Brady <P@draigBrady.com>
parents:
18325
diff
changeset
|
1633 if (type == BREAD |
7276fe79480b
fts: handle readdir() errors
Pádraig Brady <P@draigBrady.com>
parents:
18325
diff
changeset
|
1634 && cur->fts_info != FTS_DNR && cur->fts_info != FTS_ERR) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1635 cur->fts_info = FTS_DP; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1636 fts_lfree(head); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1637 return (NULL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1638 } |
5858 | 1639 |
19584
4f93d7f4b6ae
fts: fix bug in find across filesystems
Paul Eggert <eggert@cs.ucla.edu>
parents:
19580
diff
changeset
|
1640 if (sort_by_inode) { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1641 sp->fts_compar = fts_compare_ino; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1642 head = fts_sort (sp, head, nitems); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1643 sp->fts_compar = NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1644 } |
10481
47fe4e48e158
fts: sort dirent entries on inode number before traversing
Jim Meyering <meyering@redhat.com>
parents:
9718
diff
changeset
|
1645 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1646 /* Sort the entries. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1647 if (sp->fts_compar && nitems > 1) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1648 head = fts_sort(sp, head, nitems); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1649 return (head); |
5858 | 1650 } |
1651 | |
1652 #if FTS_DEBUG | |
1653 | |
1654 /* Walk ->fts_parent links starting at E_CURR, until the root of the | |
1655 current hierarchy. There should be a directory with dev/inode | |
1656 matching those of AD. If not, print a lot of diagnostics. */ | |
1657 static void | |
1658 find_matching_ancestor (FTSENT const *e_curr, struct Active_dir const *ad) | |
1659 { | |
1660 FTSENT const *ent; | |
1661 for (ent = e_curr; ent->fts_level >= FTS_ROOTLEVEL; ent = ent->fts_parent) | |
1662 { | |
1663 if (ad->ino == ent->fts_statp->st_ino | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1664 && ad->dev == ent->fts_statp->st_dev) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1665 return; |
5858 | 1666 } |
1667 printf ("ERROR: tree dir, %s, not active\n", ad->fts_ent->fts_accpath); | |
1668 printf ("active dirs:\n"); | |
1669 for (ent = e_curr; | |
1670 ent->fts_level >= FTS_ROOTLEVEL; ent = ent->fts_parent) | |
1671 printf (" %s(%"PRIuMAX"/%"PRIuMAX") to %s(%"PRIuMAX"/%"PRIuMAX")...\n", | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1672 ad->fts_ent->fts_accpath, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1673 (uintmax_t) ad->dev, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1674 (uintmax_t) ad->ino, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1675 ent->fts_accpath, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1676 (uintmax_t) ent->fts_statp->st_dev, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1677 (uintmax_t) ent->fts_statp->st_ino); |
5858 | 1678 } |
1679 | |
1680 void | |
1681 fts_cross_check (FTS const *sp) | |
1682 { | |
1683 FTSENT const *ent = sp->fts_cur; | |
1684 FTSENT const *t; | |
1685 if ( ! ISSET (FTS_TIGHT_CYCLE_CHECK)) | |
1686 return; | |
1687 | |
1688 Dprintf (("fts-cross-check cur=%s\n", ent->fts_path)); | |
1689 /* Make sure every parent dir is in the tree. */ | |
1690 for (t = ent->fts_parent; t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent) | |
1691 { | |
1692 struct Active_dir ad; | |
1693 ad.ino = t->fts_statp->st_ino; | |
1694 ad.dev = t->fts_statp->st_dev; | |
6034
96149b1bbb32
(fts_cross_check) [FTS_DEBUG]: s/active_dir_ht/fts_cycle.ht/.
Jim Meyering <jim@meyering.net>
parents:
5907
diff
changeset
|
1695 if ( ! hash_lookup (sp->fts_cycle.ht, &ad)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1696 printf ("ERROR: active dir, %s, not in tree\n", t->fts_path); |
5858 | 1697 } |
1698 | |
1699 /* Make sure every dir in the tree is an active dir. | |
1700 But ENT is not necessarily a directory. If so, just skip this part. */ | |
1701 if (ent->fts_parent->fts_level >= FTS_ROOTLEVEL | |
1702 && (ent->fts_info == FTS_DP | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1703 || ent->fts_info == FTS_D)) |
5858 | 1704 { |
1705 struct Active_dir *ad; | |
6034
96149b1bbb32
(fts_cross_check) [FTS_DEBUG]: s/active_dir_ht/fts_cycle.ht/.
Jim Meyering <jim@meyering.net>
parents:
5907
diff
changeset
|
1706 for (ad = hash_get_first (sp->fts_cycle.ht); ad != NULL; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1707 ad = hash_get_next (sp->fts_cycle.ht, ad)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1708 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1709 find_matching_ancestor (ent, ad); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1710 } |
5858 | 1711 } |
1712 } | |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1713 |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1714 static bool |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1715 same_fd (int fd1, int fd2) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1716 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1717 struct stat sb1, sb2; |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1718 return (fstat (fd1, &sb1) == 0 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1719 && fstat (fd2, &sb2) == 0 |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1720 && SAME_INODE (sb1, sb2)); |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1721 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1722 |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1723 static void |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1724 fd_ring_print (FTS const *sp, FILE *stream, char const *msg) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1725 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1726 I_ring const *fd_ring = &sp->fts_fd_ring; |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1727 unsigned int i = fd_ring->fts_front; |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1728 char *cwd = getcwdat (sp->fts_cwd_fd, NULL, 0); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1729 fprintf (stream, "=== %s ========== %s\n", msg, cwd); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1730 free (cwd); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1731 if (i_ring_empty (fd_ring)) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1732 return; |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1733 |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1734 while (true) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1735 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1736 int fd = fd_ring->fts_fd_ring[i]; |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1737 if (fd < 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1738 fprintf (stream, "%d: %d:\n", i, fd); |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1739 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1740 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1741 char *wd = getcwdat (fd, NULL, 0); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1742 fprintf (stream, "%d: %d: %s\n", i, fd, wd); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1743 free (wd); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1744 } |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1745 if (i == fd_ring->fts_back) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1746 break; |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1747 i = (i + I_RING_SIZE - 1) % I_RING_SIZE; |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1748 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1749 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1750 |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1751 /* Ensure that each file descriptor on the fd_ring matches a |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1752 parent, grandparent, etc. of the current working directory. */ |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1753 static void |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1754 fd_ring_check (FTS const *sp) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1755 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1756 if (!fts_debug) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1757 return; |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1758 |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1759 /* Make a writable copy. */ |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1760 I_ring fd_w = sp->fts_fd_ring; |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1761 |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1762 int cwd_fd = sp->fts_cwd_fd; |
19047 | 1763 cwd_fd = fcntl (cwd_fd, F_DUPFD_CLOEXEC, STDERR_FILENO + 1); |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1764 char *dot = getcwdat (cwd_fd, NULL, 0); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1765 error (0, 0, "===== check ===== cwd: %s", dot); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1766 free (dot); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1767 while ( ! i_ring_empty (&fd_w)) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1768 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1769 int fd = i_ring_pop (&fd_w); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1770 if (0 <= fd) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1771 { |
39901
33cf8a9f314a
fts: remove FTS_NOATIME
Bernhard Voelker <mail@bernhard-voelker.de>
parents:
19683
diff
changeset
|
1772 int open_flags = O_SEARCH | O_CLOEXEC; |
19047 | 1773 int parent_fd = openat (cwd_fd, "..", open_flags); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1774 if (parent_fd < 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1775 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1776 // Warn? |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1777 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1778 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1779 if (!same_fd (fd, parent_fd)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1780 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1781 char *cwd = getcwdat (fd, NULL, 0); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1782 error (0, errno, "ring : %s", cwd); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1783 char *c2 = getcwdat (parent_fd, NULL, 0); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1784 error (0, errno, "parent: %s", c2); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1785 free (cwd); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1786 free (c2); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1787 fts_assert (0); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1788 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1789 close (cwd_fd); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1790 cwd_fd = parent_fd; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1791 } |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1792 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1793 close (cwd_fd); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1794 } |
5858 | 1795 #endif |
1796 | |
1797 static unsigned short int | |
1798 internal_function | |
1799 fts_stat(FTS *sp, register FTSENT *p, bool follow) | |
1800 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1801 struct stat *sbp = p->fts_statp; |
5858 | 1802 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1803 if (p->fts_level == FTS_ROOTLEVEL && ISSET(FTS_COMFOLLOW)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1804 follow = true; |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1805 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1806 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1807 * If doing a logical walk, or application requested FTS_FOLLOW, do |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1808 * a stat(2). If that fails, check for a non-existent symlink. If |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1809 * fail, set the errno from the stat call. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1810 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1811 if (ISSET(FTS_LOGICAL) || follow) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1812 if (stat(p->fts_accpath, sbp)) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1813 if (errno == ENOENT |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1814 && lstat(p->fts_accpath, sbp) == 0) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1815 __set_errno (0); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1816 return (FTS_SLNONE); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1817 } |
19027
a444c9308698
Don't interpret EOVERFLOW to mean nonexistence
Paul Eggert <eggert@cs.ucla.edu>
parents:
19012
diff
changeset
|
1818 p->fts_errno = errno; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1819 goto err; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1820 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1821 } else if (fstatat(sp->fts_cwd_fd, p->fts_accpath, sbp, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1822 AT_SYMLINK_NOFOLLOW)) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1823 p->fts_errno = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1824 err: memset(sbp, 0, sizeof(struct stat)); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1825 return (FTS_NS); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1826 } |
5858 | 1827 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1828 if (S_ISDIR(sbp->st_mode)) { |
19008
a6fc0157c611
fts: nlink_t signedness fixups
Paul Eggert <eggert@cs.ucla.edu>
parents:
18930
diff
changeset
|
1829 p->fts_n_dirs_remaining |
19009
7201d8037bb3
fts: introduce MIN_DIR_NLINK
Paul Eggert <eggert@cs.ucla.edu>
parents:
19008
diff
changeset
|
1830 = ((sbp->st_nlink < MIN_DIR_NLINK |
19008
a6fc0157c611
fts: nlink_t signedness fixups
Paul Eggert <eggert@cs.ucla.edu>
parents:
18930
diff
changeset
|
1831 || p->fts_level <= FTS_ROOTLEVEL) |
a6fc0157c611
fts: nlink_t signedness fixups
Paul Eggert <eggert@cs.ucla.edu>
parents:
18930
diff
changeset
|
1832 ? -1 |
19009
7201d8037bb3
fts: introduce MIN_DIR_NLINK
Paul Eggert <eggert@cs.ucla.edu>
parents:
19008
diff
changeset
|
1833 : sbp->st_nlink - (ISSET (FTS_SEEDOT) ? 0 : MIN_DIR_NLINK)); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1834 if (ISDOT(p->fts_name)) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1835 /* Command-line "." and ".." are real directories. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1836 return (p->fts_level == FTS_ROOTLEVEL ? FTS_D : FTS_DOT); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1837 } |
5872 | 1838 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1839 return (FTS_D); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1840 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1841 if (S_ISLNK(sbp->st_mode)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1842 return (FTS_SL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1843 if (S_ISREG(sbp->st_mode)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1844 return (FTS_F); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1845 return (FTS_DEFAULT); |
5858 | 1846 } |
1847 | |
1848 static int | |
1849 fts_compar (void const *a, void const *b) | |
1850 { | |
1851 /* Convert A and B to the correct types, to pacify the compiler, and | |
1852 for portability to bizarre hosts where "void const *" and "FTSENT | |
1853 const **" differ in runtime representation. The comparison | |
1854 function cannot modify *a and *b, but there is no compile-time | |
1855 check for this. */ | |
1856 FTSENT const **pa = (FTSENT const **) a; | |
1857 FTSENT const **pb = (FTSENT const **) b; | |
1858 return pa[0]->fts_fts->fts_compar (pa, pb); | |
1859 } | |
1860 | |
1861 static FTSENT * | |
1862 internal_function | |
1863 fts_sort (FTS *sp, FTSENT *head, register size_t nitems) | |
1864 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1865 register FTSENT **ap, *p; |
5858 | 1866 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1867 /* On most modern hosts, void * and FTSENT ** have the same |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1868 run-time representation, and one can convert sp->fts_compar to |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1869 the type qsort expects without problem. Use the heuristic that |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1870 this is OK if the two pointer types are the same size, and if |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1871 converting FTSENT ** to long int is the same as converting |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1872 FTSENT ** to void * and then to long int. This heuristic isn't |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1873 valid in general but we don't know of any counterexamples. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1874 FTSENT *dummy; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1875 int (*compare) (void const *, void const *) = |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1876 ((sizeof &dummy == sizeof (void *) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1877 && (long int) &dummy == (long int) (void *) &dummy) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1878 ? (int (*) (void const *, void const *)) sp->fts_compar |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1879 : fts_compar); |
5858 | 1880 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1881 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1882 * Construct an array of pointers to the structures and call qsort(3). |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1883 * Reassemble the array in the order returned by qsort. If unable to |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1884 * sort for memory reasons, return the directory entries in their |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1885 * current order. Allocate enough space for the current needs plus |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1886 * 40 so don't realloc one entry at a time. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1887 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1888 if (nitems > sp->fts_nitems) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1889 FTSENT **a; |
5858 | 1890 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1891 sp->fts_nitems = nitems + 40; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1892 if (SIZE_MAX / sizeof *a < sp->fts_nitems |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1893 || ! (a = realloc (sp->fts_array, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1894 sp->fts_nitems * sizeof *a))) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1895 free(sp->fts_array); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1896 sp->fts_array = NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1897 sp->fts_nitems = 0; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1898 return (head); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1899 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1900 sp->fts_array = a; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1901 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1902 for (ap = sp->fts_array, p = head; p; p = p->fts_link) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1903 *ap++ = p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1904 qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), compare); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1905 for (head = *(ap = sp->fts_array); --nitems; ++ap) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1906 ap[0]->fts_link = ap[1]; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1907 ap[0]->fts_link = NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1908 return (head); |
5858 | 1909 } |
1910 | |
1911 static FTSENT * | |
1912 internal_function | |
1913 fts_alloc (FTS *sp, const char *name, register size_t namelen) | |
1914 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1915 register FTSENT *p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1916 size_t len; |
5858 | 1917 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1918 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1919 * The file name is a variable length array. Allocate the FTSENT |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1920 * structure and the file name in one chunk. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1921 */ |
18403
4434607ac1ce
flexmember: port better to GCC + valgrind
Paul Eggert <eggert@cs.ucla.edu>
parents:
18338
diff
changeset
|
1922 len = FLEXSIZEOF(FTSENT, fts_name, namelen + 1); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1923 if ((p = malloc(len)) == NULL) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1924 return (NULL); |
5858 | 1925 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1926 /* Copy the name and guarantee NUL termination. */ |
17073
60627a1a95ff
fts: when there is no risk of overlap, use memcpy, not memmove
Jim Meyering <meyering@redhat.com>
parents:
16358
diff
changeset
|
1927 memcpy(p->fts_name, name, namelen); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1928 p->fts_name[namelen] = '\0'; |
5858 | 1929 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1930 p->fts_namelen = namelen; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1931 p->fts_fts = sp; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1932 p->fts_path = sp->fts_path; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1933 p->fts_errno = 0; |
15529
894e5e6ae541
fts: add/use new struct member, fts_dirp
Jim Meyering <meyering@redhat.com>
parents:
15528
diff
changeset
|
1934 p->fts_dirp = NULL; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1935 p->fts_flags = 0; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1936 p->fts_instr = FTS_NOINSTR; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1937 p->fts_number = 0; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1938 p->fts_pointer = NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1939 return (p); |
5858 | 1940 } |
1941 | |
1942 static void | |
1943 internal_function | |
1944 fts_lfree (register FTSENT *head) | |
1945 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1946 register FTSENT *p; |
5858 | 1947 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1948 /* Free a linked list of structures. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1949 while ((p = head)) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1950 head = head->fts_link; |
15529
894e5e6ae541
fts: add/use new struct member, fts_dirp
Jim Meyering <meyering@redhat.com>
parents:
15528
diff
changeset
|
1951 if (p->fts_dirp) |
894e5e6ae541
fts: add/use new struct member, fts_dirp
Jim Meyering <meyering@redhat.com>
parents:
15528
diff
changeset
|
1952 closedir (p->fts_dirp); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1953 free(p); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1954 } |
5858 | 1955 } |
1956 | |
1957 /* | |
5907 | 1958 * Allow essentially unlimited file name lengths; find, rm, ls should |
1959 * all work on any tree. Most systems will allow creation of file | |
1960 * names much longer than MAXPATHLEN, even though the kernel won't | |
1961 * resolve them. Add the size (not just what's needed) plus 256 bytes | |
1962 * so don't realloc the file name 2 bytes at a time. | |
5858 | 1963 */ |
1964 static bool | |
1965 internal_function | |
1966 fts_palloc (FTS *sp, size_t more) | |
1967 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1968 char *p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1969 size_t new_len = sp->fts_pathlen + more + 256; |
5858 | 1970 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1971 /* |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1972 * See if fts_pathlen would overflow. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1973 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1974 if (new_len < sp->fts_pathlen) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1975 free(sp->fts_path); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1976 sp->fts_path = NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1977 __set_errno (ENAMETOOLONG); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1978 return false; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1979 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1980 sp->fts_pathlen = new_len; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1981 p = realloc(sp->fts_path, sp->fts_pathlen); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1982 if (p == NULL) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1983 free(sp->fts_path); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1984 sp->fts_path = NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1985 return false; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1986 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1987 sp->fts_path = p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1988 return true; |
5858 | 1989 } |
1990 | |
1991 /* | |
5907 | 1992 * When the file name is realloc'd, have to fix all of the pointers in |
1993 * structures already returned. | |
5858 | 1994 */ |
1995 static void | |
1996 internal_function | |
1997 fts_padjust (FTS *sp, FTSENT *head) | |
1998 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
1999 FTSENT *p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2000 char *addr = sp->fts_path; |
5858 | 2001 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2002 #define ADJUST(p) do { \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2003 if ((p)->fts_accpath != (p)->fts_name) { \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2004 (p)->fts_accpath = \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2005 (char *)addr + ((p)->fts_accpath - (p)->fts_path); \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2006 } \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2007 (p)->fts_path = addr; \ |
5858 | 2008 } while (0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2009 /* Adjust the current set of children. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2010 for (p = sp->fts_child; p; p = p->fts_link) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2011 ADJUST(p); |
5858 | 2012 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2013 /* Adjust the rest of the tree, including the current level. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2014 for (p = head; p->fts_level >= FTS_ROOTLEVEL;) { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2015 ADJUST(p); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2016 p = p->fts_link ? p->fts_link : p->fts_parent; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2017 } |
5858 | 2018 } |
2019 | |
2020 static size_t | |
16128
6beadb731202
mark functions with const and pure attributes
Jim Meyering <meyering@redhat.com>
parents:
15988
diff
changeset
|
2021 internal_function _GL_ATTRIBUTE_PURE |
5858 | 2022 fts_maxarglen (char * const *argv) |
2023 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2024 size_t len, max; |
5858 | 2025 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2026 for (max = 0; *argv; ++argv) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2027 if ((len = strlen(*argv)) > max) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2028 max = len; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2029 return (max + 1); |
5858 | 2030 } |
2031 | |
2032 /* | |
5907 | 2033 * Change to dir specified by fd or file name without getting |
5858 | 2034 * tricked by someone changing the world out from underneath us. |
2035 * Assumes p->fts_statp->st_dev and p->fts_statp->st_ino are filled in. | |
7151 | 2036 * If FD is non-negative, expect it to be used after this function returns, |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
2037 * and to be closed eventually. So don't pass e.g., 'dirfd(dirp)' and then |
7151 | 2038 * do closedir(dirp), because that would invalidate the saved FD. |
2039 * Upon failure, close FD immediately and return nonzero. | |
5858 | 2040 */ |
2041 static int | |
2042 internal_function | |
5907 | 2043 fts_safe_changedir (FTS *sp, FTSENT *p, int fd, char const *dir) |
5858 | 2044 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2045 int ret; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2046 bool is_dotdot = dir && STREQ (dir, ".."); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2047 int newfd; |
7623
5bfad7d666d6
* lib/fts.c (fts_safe_changedir): Add a comment.
Jim Meyering <jim@meyering.net>
parents:
7621
diff
changeset
|
2048 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2049 /* This clause handles the unusual case in which FTS_NOCHDIR |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2050 is specified, along with FTS_CWDFD. In that case, there is |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2051 no need to change even the virtual cwd file descriptor. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2052 However, if FD is non-negative, we do close it here. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2053 if (ISSET (FTS_NOCHDIR)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2054 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2055 if (ISSET (FTS_CWDFD) && 0 <= fd) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2056 close (fd); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2057 return 0; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2058 } |
5858 | 2059 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2060 if (fd < 0 && is_dotdot && ISSET (FTS_CWDFD)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2061 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2062 /* When possible, skip the diropen and subsequent fstat+dev/ino |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2063 comparison. I.e., when changing to parent directory |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2064 (chdir ("..")), use a file descriptor from the ring and |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2065 save the overhead of diropen+fstat, as well as avoiding |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2066 failure when we lack "x" access to the virtual cwd. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2067 if ( ! i_ring_empty (&sp->fts_fd_ring)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2068 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2069 int parent_fd; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2070 fd_ring_print (sp, stderr, "pre-pop"); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2071 parent_fd = i_ring_pop (&sp->fts_fd_ring); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2072 if (0 <= parent_fd) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2073 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2074 fd = parent_fd; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2075 dir = NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2076 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2077 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2078 } |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
2079 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2080 newfd = fd; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2081 if (fd < 0 && (newfd = diropen (sp, dir)) < 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2082 return -1; |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
2083 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2084 /* The following dev/inode check is necessary if we're doing a |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
2085 "logical" traversal (through symlinks, a la chown -L), if the |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2086 system lacks O_NOFOLLOW support, or if we're changing to ".." |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2087 (but not via a popped file descriptor). When changing to the |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2088 name "..", O_NOFOLLOW can't help. In general, when the target is |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2089 not "..", diropen's use of O_NOFOLLOW ensures we don't mistakenly |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2090 follow a symlink, so we can avoid the expense of this fstat. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2091 if (ISSET(FTS_LOGICAL) || ! HAVE_WORKING_O_NOFOLLOW |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2092 || (dir && STREQ (dir, ".."))) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2093 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2094 struct stat sb; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2095 if (fstat(newfd, &sb)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2096 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2097 ret = -1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2098 goto bail; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2099 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2100 if (p->fts_statp->st_dev != sb.st_dev |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2101 || p->fts_statp->st_ino != sb.st_ino) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2102 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2103 __set_errno (ENOENT); /* disinformation */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2104 ret = -1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2105 goto bail; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2106 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2107 } |
7151 | 2108 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2109 if (ISSET(FTS_CWDFD)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2110 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2111 cwd_advance_fd (sp, newfd, ! is_dotdot); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2112 return 0; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2113 } |
7151 | 2114 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2115 ret = fchdir(newfd); |
5858 | 2116 bail: |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2117 if (fd < 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2118 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2119 int oerrno = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2120 (void)close(newfd); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2121 __set_errno (oerrno); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2122 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12378
diff
changeset
|
2123 return ret; |
5858 | 2124 } |