Mercurial > gnulib
annotate lib/fts.c @ 7707:3f57a32346a2
* lib/fts.c (fts_load): Don't set sp->fts_dev here, since
p->fts_statp may not yet be defined.
(fts_read): Instead, set it in the caller, once p->fts_statp is
sure to be defined, and corresponds to a top-level directory.
author | Jim Meyering <jim@meyering.net> |
---|---|
date | Sun, 03 Dec 2006 10:23:10 +0000 |
parents | 784e80c3d895 |
children | 3246679850d5 |
rev | line source |
---|---|
5858 | 1 /* Traverse a file hierarchy. |
2 | |
6527 | 3 Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc. |
5858 | 4 |
5 This program is free software; you can redistribute it and/or modify | |
6 it under the terms of the GNU General Public License as published by | |
7 the Free Software Foundation; either version 2, or (at your option) | |
8 any later version. | |
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 | |
16 along with this program; if not, write to the Free Software Foundation, | |
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ | |
18 | |
19 /*- | |
20 * Copyright (c) 1990, 1993, 1994 | |
21 * The Regents of the University of California. All rights reserved. | |
22 * | |
23 * Redistribution and use in source and binary forms, with or without | |
24 * modification, are permitted provided that the following conditions | |
25 * are met: | |
26 * 1. Redistributions of source code must retain the above copyright | |
27 * notice, this list of conditions and the following disclaimer. | |
28 * 2. Redistributions in binary form must reproduce the above copyright | |
29 * notice, this list of conditions and the following disclaimer in the | |
30 * documentation and/or other materials provided with the distribution. | |
31 * 4. Neither the name of the University nor the names of its contributors | |
32 * may be used to endorse or promote products derived from this software | |
33 * without specific prior written permission. | |
34 * | |
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
45 * SUCH DAMAGE. | |
46 */ | |
47 | |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
7225
diff
changeset
|
48 #include <config.h> |
5858 | 49 |
50 #if defined(LIBC_SCCS) && !defined(lint) | |
51 static char sccsid[] = "@(#)fts.c 8.6 (Berkeley) 8/14/94"; | |
52 #endif /* LIBC_SCCS and not lint */ | |
53 | |
5867
67b499052f7f
* fts.c: Include fts_.h first, to check interface.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5858
diff
changeset
|
54 #include "fts_.h" |
67b499052f7f
* fts.c: Include fts_.h first, to check interface.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5858
diff
changeset
|
55 |
5858 | 56 #if HAVE_SYS_PARAM_H || defined _LIBC |
57 # include <sys/param.h> | |
58 #endif | |
59 #ifdef _LIBC | |
60 # include <include/sys/stat.h> | |
61 #else | |
62 # include <sys/stat.h> | |
63 #endif | |
64 #include <fcntl.h> | |
65 #include <errno.h> | |
66 #include "dirfd.h" | |
5867
67b499052f7f
* fts.c: Include fts_.h first, to check interface.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5858
diff
changeset
|
67 #include <stdbool.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" |
6034
96149b1bbb32
(fts_cross_check) [FTS_DEBUG]: s/active_dir_ht/fts_cycle.ht/.
Jim Meyering <jim@meyering.net>
parents:
5907
diff
changeset
|
74 # include "lstat.h" |
7151 | 75 # include "openat.h" |
76 # include "unistd--.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> |
96af1224c6fa
* backupfile.c, dirfd.h, fts.c, getcwd.c, glob.c, glob_.h:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6934
diff
changeset
|
81 #ifndef _D_EXACT_NAMLEN |
96af1224c6fa
* backupfile.c, dirfd.h, fts.c, getcwd.c, glob.c, glob_.h:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6934
diff
changeset
|
82 # define _D_EXACT_NAMLEN(dirent) strlen ((dirent)->d_name) |
5858 | 83 #endif |
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)) |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
90 #else |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
91 # 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
|
92 # define DT_MUST_BE(d, t) false |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
93 #endif |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
94 |
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
|
95 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
|
96 { |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
97 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
|
98 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
|
99 }; |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
100 |
5858 | 101 #ifdef _LIBC |
102 # undef close | |
103 # define close __close | |
104 # undef closedir | |
105 # define closedir __closedir | |
106 # undef fchdir | |
107 # define fchdir __fchdir | |
108 # undef open | |
109 # define open __open | |
110 # undef opendir | |
111 # define opendir __opendir | |
112 # undef readdir | |
113 # define readdir __readdir | |
114 #else | |
115 # undef internal_function | |
116 # define internal_function /* empty */ | |
117 #endif | |
118 | |
119 #ifndef __set_errno | |
120 # define __set_errno(Val) errno = (Val) | |
121 #endif | |
122 | |
123 #ifndef __attribute__ | |
124 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__ | |
125 # define __attribute__(x) /* empty */ | |
126 # endif | |
127 #endif | |
128 | |
129 #ifndef ATTRIBUTE_UNUSED | |
130 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__)) | |
131 #endif | |
132 | |
7151 | 133 /* If this host provides the openat function, then we can avoid |
134 attempting to open "." in some initialization code below. */ | |
135 #ifdef HAVE_OPENAT | |
136 # define HAVE_OPENAT_SUPPORT 1 | |
137 #else | |
138 # define HAVE_OPENAT_SUPPORT 0 | |
139 #endif | |
5858 | 140 |
141 static FTSENT *fts_alloc (FTS *, const char *, size_t) internal_function; | |
142 static FTSENT *fts_build (FTS *, int) internal_function; | |
143 static void fts_lfree (FTSENT *) internal_function; | |
144 static void fts_load (FTS *, FTSENT *) internal_function; | |
145 static size_t fts_maxarglen (char * const *) internal_function; | |
146 static void fts_padjust (FTS *, FTSENT *) internal_function; | |
147 static bool fts_palloc (FTS *, size_t) internal_function; | |
148 static FTSENT *fts_sort (FTS *, FTSENT *, size_t) internal_function; | |
149 static unsigned short int fts_stat (FTS *, FTSENT *, bool) internal_function; | |
150 static int fts_safe_changedir (FTS *, FTSENT *, int, const char *) | |
151 internal_function; | |
152 | |
5872 | 153 #if _LGPL_PACKAGE |
154 static bool enter_dir (FTS *fts, FTSENT *ent) { return true; } | |
155 static void leave_dir (FTS *fts, FTSENT *ent) {} | |
156 static bool setup_dir (FTS *fts) { return true; } | |
157 static void free_dir (FTS *fts) {} | |
158 #else | |
159 # include "fts-cycle.c" | |
160 #endif | |
161 | |
5858 | 162 #ifndef MAX |
163 # define MAX(a,b) ((a) > (b) ? (a) : (b)) | |
164 #endif | |
165 | |
166 #ifndef SIZE_MAX | |
167 # define SIZE_MAX ((size_t) -1) | |
168 #endif | |
169 | |
170 #ifndef O_DIRECTORY | |
171 # define O_DIRECTORY 0 | |
172 #endif | |
173 | |
174 #define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2]))) | |
7151 | 175 #define STREQ(a, b) (strcmp ((a), (b)) == 0) |
5858 | 176 |
177 #define CLR(opt) (sp->fts_options &= ~(opt)) | |
178 #define ISSET(opt) (sp->fts_options & (opt)) | |
179 #define SET(opt) (sp->fts_options |= (opt)) | |
180 | |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
181 /* FIXME: make this a function */ |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
182 #define RESTORE_INITIAL_CWD(sp) \ |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
183 (fd_ring_clear (&((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
|
184 FCHDIR ((sp), (ISSET (FTS_CWDFD) ? AT_FDCWD : (sp)->fts_rfd))) |
7151 | 185 |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
186 /* 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
|
187 Call it FTS_USE_FULL_RELATIVE_FILE_NAMES instead. */ |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
188 #define FCHDIR(sp, fd) \ |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
189 (!ISSET(FTS_NOCHDIR) && (ISSET(FTS_CWDFD) \ |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
190 ? (cwd_advance_fd ((sp), (fd), true), 0) \ |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
191 : fchdir (fd))) |
7151 | 192 |
5858 | 193 |
194 /* 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
|
195 /* FIXME: make this an enum */ |
5858 | 196 #define BCHILD 1 /* fts_children */ |
197 #define BNAMES 2 /* fts_children, names only */ | |
198 #define BREAD 3 /* fts_read */ | |
199 | |
200 #if FTS_DEBUG | |
5894
d09af5fbd9f2
* modules/fts (Files): Remove m4/inttypes-pri.m4.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5877
diff
changeset
|
201 # include <inttypes.h> |
d09af5fbd9f2
* modules/fts (Files): Remove m4/inttypes-pri.m4.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5877
diff
changeset
|
202 # include <stdint.h> |
d09af5fbd9f2
* modules/fts (Files): Remove m4/inttypes-pri.m4.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5877
diff
changeset
|
203 # 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
|
204 # include "getcwdat.h" |
5858 | 205 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
|
206 # define Dprintf(x) do { if (fts_debug) printf x; } while (false) |
5858 | 207 #else |
208 # 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
|
209 # 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
|
210 # define fd_ring_print(a, b, c) |
5858 | 211 #endif |
212 | |
213 #define LEAVE_DIR(Fts, Ent, Tag) \ | |
5872 | 214 do \ |
215 { \ | |
5858 | 216 Dprintf ((" %s-leaving: %s\n", Tag, (Ent)->fts_path)); \ |
5872 | 217 leave_dir (Fts, Ent); \ |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
218 fd_ring_check (Fts); \ |
5872 | 219 } \ |
220 while (false) | |
5858 | 221 |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
222 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
|
223 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
|
224 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
225 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
|
226 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
227 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
|
228 if (0 <= fd) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
229 close (fd); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
230 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
231 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
232 |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
233 /* 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
|
234 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
|
235 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
|
236 static void |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
237 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
|
238 { |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
239 if (p->fts_info != FTS_NSOK) |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
240 abort (); |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
241 p->fts_statp->st_size = (required |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
242 ? FTS_STAT_REQUIRED |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
243 : FTS_NO_STAT_REQUIRED); |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
244 } |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
245 |
7151 | 246 /* file-descriptor-relative opendir. */ |
247 /* FIXME: if others need this function, move it into lib/openat.c */ | |
248 static inline DIR * | |
249 internal_function | |
250 opendirat (int fd, char const *dir) | |
251 { | |
252 int new_fd = openat (fd, dir, O_RDONLY); | |
253 DIR *dirp; | |
254 | |
255 if (new_fd < 0) | |
256 return NULL; | |
257 dirp = fdopendir (new_fd); | |
258 if (dirp == NULL) | |
259 { | |
260 int saved_errno = errno; | |
261 close (new_fd); | |
262 errno = saved_errno; | |
263 } | |
264 return dirp; | |
265 } | |
266 | |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
267 /* 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
|
268 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
|
269 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
|
270 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
|
271 down. */ |
7151 | 272 static void |
273 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
|
274 cwd_advance_fd (FTS *sp, int fd, bool chdir_down_one) |
7151 | 275 { |
276 int old = sp->fts_cwd_fd; | |
277 if (old == fd && old != AT_FDCWD) | |
278 abort (); | |
7639
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 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
|
281 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
282 /* Push "old" onto the ring. |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
283 If the displaced file descriptor is non-negative, close it. */ |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
284 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
|
285 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
|
286 if (0 <= prev_fd_in_slot) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
287 close (prev_fd_in_slot); /* ignore any close failure */ |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
288 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
289 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
|
290 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
291 if (0 <= old) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
292 close (old); /* ignore any close failure */ |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
293 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
294 |
7151 | 295 sp->fts_cwd_fd = fd; |
296 } | |
297 | |
5858 | 298 /* Open the directory DIR if possible, and return a file |
299 descriptor. Return -1 and set errno on failure. It doesn't matter | |
300 whether the file descriptor has read or write access. */ | |
301 | |
7151 | 302 static inline int |
5858 | 303 internal_function |
7151 | 304 diropen (FTS const *sp, char const *dir) |
5858 | 305 { |
7151 | 306 int open_flags = (O_RDONLY | O_DIRECTORY | O_NOCTTY | O_NONBLOCK |
307 | (ISSET (FTS_PHYSICAL) ? O_NOFOLLOW : 0)); | |
308 | |
309 return (ISSET (FTS_CWDFD) | |
310 ? openat (sp->fts_cwd_fd, dir, open_flags) | |
311 : open (dir, open_flags)); | |
5858 | 312 } |
313 | |
314 FTS * | |
315 fts_open (char * const *argv, | |
316 register int options, | |
317 int (*compar) (FTSENT const **, FTSENT const **)) | |
318 { | |
319 register FTS *sp; | |
320 register FTSENT *p, *root; | |
321 register size_t nitems; | |
7151 | 322 FTSENT *parent = NULL; |
323 FTSENT *tmp = NULL; /* pacify gcc */ | |
5858 | 324 size_t len; |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
325 bool defer_stat; |
5858 | 326 |
327 /* Options check. */ | |
328 if (options & ~FTS_OPTIONMASK) { | |
329 __set_errno (EINVAL); | |
330 return (NULL); | |
331 } | |
7151 | 332 if ((options & FTS_NOCHDIR) && (options & FTS_CWDFD)) { |
333 __set_errno (EINVAL); | |
334 return (NULL); | |
335 } | |
336 if ( ! (options & (FTS_LOGICAL | FTS_PHYSICAL))) { | |
337 __set_errno (EINVAL); | |
338 return (NULL); | |
339 } | |
5858 | 340 |
341 /* Allocate/initialize the stream */ | |
342 if ((sp = malloc(sizeof(FTS))) == NULL) | |
343 return (NULL); | |
344 memset(sp, 0, sizeof(FTS)); | |
345 sp->fts_compar = compar; | |
346 sp->fts_options = options; | |
347 | |
348 /* Logical walks turn on NOCHDIR; symbolic links are too hard. */ | |
7151 | 349 if (ISSET(FTS_LOGICAL)) { |
5858 | 350 SET(FTS_NOCHDIR); |
7151 | 351 CLR(FTS_CWDFD); |
352 } | |
353 | |
354 /* Initialize fts_cwd_fd. */ | |
355 sp->fts_cwd_fd = AT_FDCWD; | |
356 if ( ISSET(FTS_CWDFD) && ! HAVE_OPENAT_SUPPORT) | |
357 { | |
358 /* While it isn't technically necessary to open "." this | |
359 early, doing it here saves us the trouble of ensuring | |
360 later (where it'd be messier) that "." can in fact | |
361 be opened. If not, revert to FTS_NOCHDIR mode. */ | |
362 int fd = open (".", O_RDONLY); | |
363 if (fd < 0) | |
364 { | |
365 /* Even if `.' is unreadable, don't revert to FTS_NOCHDIR mode | |
366 on systems like Linux+PROC_FS, where our openat emulation | |
367 is good enough. Note: on a system that emulates | |
368 openat via /proc, this technique can still fail, but | |
369 only in extreme conditions, e.g., when the working | |
370 directory cannot be saved (i.e. save_cwd fails) -- | |
7402
82dc76cfd521
* fts.c (fts_open): Tiny comment change.
Jim Meyering <jim@meyering.net>
parents:
7396
diff
changeset
|
371 and that happens on Linux only when "." is unreadable |
7151 | 372 and the CWD would be longer than PATH_MAX. |
373 FIXME: once Linux kernel openat support is well established, | |
374 replace the above open call and this entire if/else block | |
375 with the body of the if-block below. */ | |
376 if ( openat_needs_fchdir ()) | |
377 { | |
378 SET(FTS_NOCHDIR); | |
379 CLR(FTS_CWDFD); | |
380 } | |
381 } | |
382 else | |
383 { | |
384 close (fd); | |
385 } | |
386 } | |
5858 | 387 |
388 /* | |
5907 | 389 * Start out with 1K of file name space, and enough, in any case, |
390 * to hold the user's file names. | |
5858 | 391 */ |
392 #ifndef MAXPATHLEN | |
393 # define MAXPATHLEN 1024 | |
394 #endif | |
6611
e4456c51dde7
Sync from the stable (b5) branch of coreutils:
Jim Meyering <jim@meyering.net>
parents:
6540
diff
changeset
|
395 { |
e4456c51dde7
Sync from the stable (b5) branch of coreutils:
Jim Meyering <jim@meyering.net>
parents:
6540
diff
changeset
|
396 size_t maxarglen = fts_maxarglen(argv); |
e4456c51dde7
Sync from the stable (b5) branch of coreutils:
Jim Meyering <jim@meyering.net>
parents:
6540
diff
changeset
|
397 if (! fts_palloc(sp, MAX(maxarglen, MAXPATHLEN))) |
e4456c51dde7
Sync from the stable (b5) branch of coreutils:
Jim Meyering <jim@meyering.net>
parents:
6540
diff
changeset
|
398 goto mem1; |
e4456c51dde7
Sync from the stable (b5) branch of coreutils:
Jim Meyering <jim@meyering.net>
parents:
6540
diff
changeset
|
399 } |
5858 | 400 |
401 /* Allocate/initialize root's parent. */ | |
7151 | 402 if (*argv != NULL) { |
403 if ((parent = fts_alloc(sp, "", 0)) == NULL) | |
404 goto mem2; | |
405 parent->fts_level = FTS_ROOTPARENTLEVEL; | |
406 } | |
5858 | 407 |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
408 /* The classic fts implementation would call fts_stat with |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
409 a new entry for each iteration of the loop below. |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
410 If the comparison function is not specified or if the |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
411 FTS_DEFER_STAT option is in effect, don't stat any entry |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
412 in this loop. This is an attempt to minimize the interval |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
413 between the initial stat/lstat/fstatat and the point at which |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
414 a directory argument is first opened. This matters for any |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
415 directory command line argument that resides on a file system |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
416 without genuine i-nodes. If you specify FTS_DEFER_STAT along |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
417 with a comparison function, that function must not access any |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
418 data via the fts_statp pointer. */ |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
419 defer_stat = (compar == NULL || ISSET(FTS_DEFER_STAT)); |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
420 |
5858 | 421 /* Allocate/initialize root(s). */ |
422 for (root = NULL, nitems = 0; *argv != NULL; ++argv, ++nitems) { | |
5907 | 423 /* Don't allow zero-length file names. */ |
5858 | 424 if ((len = strlen(*argv)) == 0) { |
425 __set_errno (ENOENT); | |
426 goto mem3; | |
427 } | |
428 | |
429 if ((p = fts_alloc(sp, *argv, len)) == NULL) | |
430 goto mem3; | |
431 p->fts_level = FTS_ROOTLEVEL; | |
432 p->fts_parent = parent; | |
433 p->fts_accpath = p->fts_name; | |
7499
67ce0bed0a20
Fix a bug in yesterday's change.
Jim Meyering <jim@meyering.net>
parents:
7483
diff
changeset
|
434 /* Even when defer_stat is true, be sure to stat the first |
67ce0bed0a20
Fix a bug in yesterday's change.
Jim Meyering <jim@meyering.net>
parents:
7483
diff
changeset
|
435 command line argument, since fts_read (at least with |
67ce0bed0a20
Fix a bug in yesterday's change.
Jim Meyering <jim@meyering.net>
parents:
7483
diff
changeset
|
436 FTS_XDEV) requires that. */ |
67ce0bed0a20
Fix a bug in yesterday's change.
Jim Meyering <jim@meyering.net>
parents:
7483
diff
changeset
|
437 if (defer_stat && root != NULL) { |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
438 p->fts_info = FTS_NSOK; |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
439 fts_set_stat_required(p, true); |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
440 } else { |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
441 p->fts_info = fts_stat(sp, p, false); |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
442 } |
5858 | 443 |
444 /* | |
445 * If comparison routine supplied, traverse in sorted | |
446 * order; otherwise traverse in the order specified. | |
447 */ | |
448 if (compar) { | |
449 p->fts_link = root; | |
450 root = p; | |
451 } else { | |
452 p->fts_link = NULL; | |
453 if (root == NULL) | |
454 tmp = root = p; | |
455 else { | |
456 tmp->fts_link = p; | |
457 tmp = p; | |
458 } | |
459 } | |
460 } | |
461 if (compar && nitems > 1) | |
462 root = fts_sort(sp, root, nitems); | |
463 | |
464 /* | |
465 * Allocate a dummy pointer and make fts_read think that we've just | |
466 * finished the node before the root(s); set p->fts_info to FTS_INIT | |
467 * so that everything about the "current" node is ignored. | |
468 */ | |
469 if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL) | |
470 goto mem3; | |
471 sp->fts_cur->fts_link = root; | |
472 sp->fts_cur->fts_info = FTS_INIT; | |
5872 | 473 if (! setup_dir (sp)) |
7483
2433e5f6bfd0
* lib/fts.c (fts_open): Use consistent indentation.
Jim Meyering <jim@meyering.net>
parents:
7482
diff
changeset
|
474 goto mem3; |
5858 | 475 |
476 /* | |
477 * If using chdir(2), grab a file descriptor pointing to dot to ensure | |
5907 | 478 * that we can get back here; this could be avoided for some file names, |
5858 | 479 * but almost certainly not worth the effort. Slashes, symbolic links, |
480 * and ".." are all fairly nasty problems. Note, if we can't get the | |
481 * descriptor we run anyway, just more slowly. | |
482 */ | |
7151 | 483 if (!ISSET(FTS_NOCHDIR) && !ISSET(FTS_CWDFD) |
484 && (sp->fts_rfd = diropen (sp, ".")) < 0) | |
5858 | 485 SET(FTS_NOCHDIR); |
486 | |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
487 i_ring_init (&sp->fts_fd_ring, -1); |
5858 | 488 return (sp); |
489 | |
490 mem3: fts_lfree(root); | |
491 free(parent); | |
492 mem2: free(sp->fts_path); | |
493 mem1: free(sp); | |
494 return (NULL); | |
495 } | |
496 | |
497 static void | |
498 internal_function | |
499 fts_load (FTS *sp, register FTSENT *p) | |
500 { | |
501 register size_t len; | |
502 register char *cp; | |
503 | |
504 /* | |
505 * Load the stream structure for the next traversal. Since we don't | |
506 * actually enter the directory until after the preorder visit, set | |
507 * the fts_accpath field specially so the chdir gets done to the right | |
508 * place and the user can access the first node. From fts_open it's | |
5907 | 509 * known that the file name will fit. |
5858 | 510 */ |
511 len = p->fts_pathlen = p->fts_namelen; | |
512 memmove(sp->fts_path, p->fts_name, len + 1); | |
513 if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) { | |
514 len = strlen(++cp); | |
515 memmove(p->fts_name, cp, len + 1); | |
516 p->fts_namelen = len; | |
517 } | |
518 p->fts_accpath = p->fts_path = sp->fts_path; | |
519 } | |
520 | |
521 int | |
522 fts_close (FTS *sp) | |
523 { | |
524 register FTSENT *freep, *p; | |
525 int saved_errno = 0; | |
526 | |
527 /* | |
528 * This still works if we haven't read anything -- the dummy structure | |
529 * points to the root list, so we step through to the end of the root | |
530 * list which has a valid parent pointer. | |
531 */ | |
532 if (sp->fts_cur) { | |
533 for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { | |
534 freep = p; | |
535 p = p->fts_link != NULL ? p->fts_link : p->fts_parent; | |
536 free(freep); | |
537 } | |
538 free(p); | |
539 } | |
540 | |
5907 | 541 /* Free up child linked list, sort array, file name buffer. */ |
5858 | 542 if (sp->fts_child) |
543 fts_lfree(sp->fts_child); | |
7396
6f79ccad009d
2006-10-02 Dmitry V. Levin <ldv@altlinux.org>
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
544 free(sp->fts_array); |
5858 | 545 free(sp->fts_path); |
546 | |
7151 | 547 if (ISSET(FTS_CWDFD)) |
548 { | |
549 if (0 <= sp->fts_cwd_fd) | |
550 close (sp->fts_cwd_fd); | |
551 } | |
552 else if (!ISSET(FTS_NOCHDIR)) | |
553 { | |
554 /* Return to original directory, save errno if necessary. */ | |
555 if (fchdir(sp->fts_rfd)) | |
556 saved_errno = errno; | |
557 close(sp->fts_rfd); | |
558 } | |
5858 | 559 |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
560 fd_ring_clear (&sp->fts_fd_ring); |
5872 | 561 free_dir (sp); |
5858 | 562 |
563 /* Free up the stream pointer. */ | |
564 free(sp); | |
565 | |
566 /* Set errno and return. */ | |
567 if (saved_errno) { | |
568 __set_errno (saved_errno); | |
569 return (-1); | |
570 } | |
571 | |
572 return (0); | |
573 } | |
574 | |
575 /* | |
5907 | 576 * Special case of "/" at the end of the file name so that slashes aren't |
577 * appended which would cause file names to be written as "....//foo". | |
5858 | 578 */ |
579 #define NAPPEND(p) \ | |
580 (p->fts_path[p->fts_pathlen - 1] == '/' \ | |
581 ? p->fts_pathlen - 1 : p->fts_pathlen) | |
582 | |
583 FTSENT * | |
584 fts_read (register FTS *sp) | |
585 { | |
586 register FTSENT *p, *tmp; | |
587 register unsigned short int instr; | |
588 register char *t; | |
589 | |
590 /* If finished or unrecoverable error, return NULL. */ | |
591 if (sp->fts_cur == NULL || ISSET(FTS_STOP)) | |
592 return (NULL); | |
593 | |
594 /* Set current node pointer. */ | |
595 p = sp->fts_cur; | |
596 | |
597 /* Save and zero out user instructions. */ | |
598 instr = p->fts_instr; | |
599 p->fts_instr = FTS_NOINSTR; | |
600 | |
601 /* Any type of file may be re-visited; re-stat and re-turn. */ | |
602 if (instr == FTS_AGAIN) { | |
603 p->fts_info = fts_stat(sp, p, false); | |
604 return (p); | |
605 } | |
606 Dprintf (("fts_read: p=%s\n", | |
607 p->fts_info == FTS_INIT ? "" : p->fts_path)); | |
608 | |
609 /* | |
610 * Following a symlink -- SLNONE test allows application to see | |
611 * SLNONE and recover. If indirecting through a symlink, have | |
612 * keep a pointer to current location. If unable to get that | |
613 * pointer, follow fails. | |
614 */ | |
615 if (instr == FTS_FOLLOW && | |
616 (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) { | |
617 p->fts_info = fts_stat(sp, p, true); | |
618 if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { | |
7151 | 619 if ((p->fts_symfd = diropen (sp, ".")) < 0) { |
5858 | 620 p->fts_errno = errno; |
621 p->fts_info = FTS_ERR; | |
622 } else | |
623 p->fts_flags |= FTS_SYMFOLLOW; | |
624 } | |
5872 | 625 goto check_for_dir; |
5858 | 626 } |
627 | |
628 /* Directory in pre-order. */ | |
629 if (p->fts_info == FTS_D) { | |
630 /* If skipped or crossed mount point, do post-order visit. */ | |
631 if (instr == FTS_SKIP || | |
632 (ISSET(FTS_XDEV) && p->fts_statp->st_dev != sp->fts_dev)) { | |
633 if (p->fts_flags & FTS_SYMFOLLOW) | |
634 (void)close(p->fts_symfd); | |
635 if (sp->fts_child) { | |
636 fts_lfree(sp->fts_child); | |
637 sp->fts_child = NULL; | |
638 } | |
639 p->fts_info = FTS_DP; | |
640 LEAVE_DIR (sp, p, "1"); | |
641 return (p); | |
642 } | |
643 | |
644 /* Rebuild if only read the names and now traversing. */ | |
645 if (sp->fts_child != NULL && ISSET(FTS_NAMEONLY)) { | |
646 CLR(FTS_NAMEONLY); | |
647 fts_lfree(sp->fts_child); | |
648 sp->fts_child = NULL; | |
649 } | |
650 | |
651 /* | |
652 * Cd to the subdirectory. | |
653 * | |
654 * If have already read and now fail to chdir, whack the list | |
655 * to make the names come out right, and set the parent errno | |
656 * so the application will eventually get an error condition. | |
657 * Set the FTS_DONTCHDIR flag so that when we logically change | |
658 * directories back to the parent we don't do a chdir. | |
659 * | |
660 * If haven't read do so. If the read fails, fts_build sets | |
661 * FTS_STOP or the fts_info field of the node. | |
662 */ | |
663 if (sp->fts_child != NULL) { | |
664 if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) { | |
665 p->fts_errno = errno; | |
666 p->fts_flags |= FTS_DONTCHDIR; | |
667 for (p = sp->fts_child; p != NULL; | |
668 p = p->fts_link) | |
669 p->fts_accpath = | |
670 p->fts_parent->fts_accpath; | |
671 } | |
672 } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) { | |
673 if (ISSET(FTS_STOP)) | |
674 return (NULL); | |
675 /* If fts_build's call to fts_safe_changedir failed | |
676 because it was not able to fchdir into a | |
677 subdirectory, tell the caller. */ | |
678 if (p->fts_errno) | |
679 p->fts_info = FTS_ERR; | |
680 LEAVE_DIR (sp, p, "2"); | |
681 return (p); | |
682 } | |
683 p = sp->fts_child; | |
684 sp->fts_child = NULL; | |
685 goto name; | |
686 } | |
687 | |
688 /* Move to the next node on this level. */ | |
689 next: tmp = p; | |
690 if ((p = p->fts_link) != NULL) { | |
691 free(tmp); | |
692 | |
693 /* | |
694 * If reached the top, return to the original directory (or | |
5907 | 695 * the root of the tree), and load the file names for the next |
696 * root. | |
5858 | 697 */ |
698 if (p->fts_level == FTS_ROOTLEVEL) { | |
7151 | 699 if (RESTORE_INITIAL_CWD(sp)) { |
5858 | 700 SET(FTS_STOP); |
6540 | 701 sp->fts_cur = p; |
5858 | 702 return (NULL); |
703 } | |
704 fts_load(sp, p); | |
5872 | 705 goto check_for_dir; |
5858 | 706 } |
707 | |
708 /* | |
709 * User may have called fts_set on the node. If skipped, | |
710 * ignore. If followed, get a file descriptor so we can | |
711 * get back if necessary. | |
712 */ | |
713 if (p->fts_instr == FTS_SKIP) | |
714 goto next; | |
715 if (p->fts_instr == FTS_FOLLOW) { | |
716 p->fts_info = fts_stat(sp, p, true); | |
717 if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { | |
7151 | 718 if ((p->fts_symfd = diropen (sp, ".")) < 0) { |
5858 | 719 p->fts_errno = errno; |
720 p->fts_info = FTS_ERR; | |
721 } else | |
722 p->fts_flags |= FTS_SYMFOLLOW; | |
723 } | |
724 p->fts_instr = FTS_NOINSTR; | |
725 } | |
726 | |
727 name: t = sp->fts_path + NAPPEND(p->fts_parent); | |
728 *t++ = '/'; | |
729 memmove(t, p->fts_name, p->fts_namelen + 1); | |
5872 | 730 check_for_dir: |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
731 if (p->fts_info == FTS_NSOK) |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
732 { |
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
|
733 enum Fts_stat need_stat = p->fts_statp->st_size; |
784e80c3d895
Work around a compile-time error from the HP-UX 11.00 /bin/cc.
Jim Meyering <jim@meyering.net>
parents:
7681
diff
changeset
|
734 switch (need_stat) |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
735 { |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
736 case FTS_STAT_REQUIRED: |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
737 p->fts_info = fts_stat(sp, p, false); |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
738 break; |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
739 case FTS_NO_STAT_REQUIRED: |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
740 break; |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
741 default: |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
742 abort (); |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
743 } |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
744 } |
7707
3f57a32346a2
* lib/fts.c (fts_load): Don't set sp->fts_dev here, since
Jim Meyering <jim@meyering.net>
parents:
7684
diff
changeset
|
745 |
5872 | 746 sp->fts_cur = p; |
5858 | 747 if (p->fts_info == FTS_D) |
5872 | 748 { |
7707
3f57a32346a2
* lib/fts.c (fts_load): Don't set sp->fts_dev here, since
Jim Meyering <jim@meyering.net>
parents:
7684
diff
changeset
|
749 /* Now that P->fts_statp is guaranteed to be valid, |
3f57a32346a2
* lib/fts.c (fts_load): Don't set sp->fts_dev here, since
Jim Meyering <jim@meyering.net>
parents:
7684
diff
changeset
|
750 if this is a command-line directory, record its |
3f57a32346a2
* lib/fts.c (fts_load): Don't set sp->fts_dev here, since
Jim Meyering <jim@meyering.net>
parents:
7684
diff
changeset
|
751 device number, to be used for FTS_XDEV. */ |
3f57a32346a2
* lib/fts.c (fts_load): Don't set sp->fts_dev here, since
Jim Meyering <jim@meyering.net>
parents:
7684
diff
changeset
|
752 if (p->fts_level == FTS_ROOTLEVEL) |
3f57a32346a2
* lib/fts.c (fts_load): Don't set sp->fts_dev here, since
Jim Meyering <jim@meyering.net>
parents:
7684
diff
changeset
|
753 sp->fts_dev = p->fts_statp->st_dev; |
7635
7f65b3a0e7a4
* lib/fts.c [FTS_DEBUG]: Don't try to print a pointer via %s.
Jim Meyering <jim@meyering.net>
parents:
7623
diff
changeset
|
754 Dprintf ((" entering: %s\n", p->fts_path)); |
5872 | 755 if (! enter_dir (sp, p)) |
756 { | |
757 __set_errno (ENOMEM); | |
758 return NULL; | |
759 } | |
760 } | |
761 return p; | |
5858 | 762 } |
763 | |
764 /* Move up to the parent node. */ | |
765 p = tmp->fts_parent; | |
766 free(tmp); | |
767 | |
768 if (p->fts_level == FTS_ROOTPARENTLEVEL) { | |
769 /* | |
770 * Done; free everything up and set errno to 0 so the user | |
771 * can distinguish between error and EOF. | |
772 */ | |
773 free(p); | |
774 __set_errno (0); | |
775 return (sp->fts_cur = NULL); | |
776 } | |
777 | |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
778 if (p->fts_info == FTS_NSOK) |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
779 abort (); |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
780 |
5907 | 781 /* NUL terminate the file name. */ |
5858 | 782 sp->fts_path[p->fts_pathlen] = '\0'; |
783 | |
784 /* | |
7151 | 785 * Return to the parent directory. If at a root node, restore |
786 * the initial working directory. If we came through a symlink, | |
787 * go back through the file descriptor. Otherwise, move up | |
788 * one level, via "..". | |
5858 | 789 */ |
790 if (p->fts_level == FTS_ROOTLEVEL) { | |
7151 | 791 if (RESTORE_INITIAL_CWD(sp)) { |
5858 | 792 p->fts_errno = errno; |
793 SET(FTS_STOP); | |
794 } | |
795 } else if (p->fts_flags & FTS_SYMFOLLOW) { | |
796 if (FCHDIR(sp, p->fts_symfd)) { | |
7151 | 797 int saved_errno = errno; |
5858 | 798 (void)close(p->fts_symfd); |
799 __set_errno (saved_errno); | |
800 p->fts_errno = errno; | |
801 SET(FTS_STOP); | |
802 } | |
803 (void)close(p->fts_symfd); | |
804 } else if (!(p->fts_flags & FTS_DONTCHDIR) && | |
805 fts_safe_changedir(sp, p->fts_parent, -1, "..")) { | |
806 p->fts_errno = errno; | |
807 SET(FTS_STOP); | |
808 } | |
809 p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP; | |
810 if (p->fts_errno == 0) | |
811 LEAVE_DIR (sp, p, "3"); | |
812 sp->fts_cur = p; | |
813 return ISSET(FTS_STOP) ? NULL : p; | |
814 } | |
815 | |
816 /* | |
817 * Fts_set takes the stream as an argument although it's not used in this | |
818 * implementation; it would be necessary if anyone wanted to add global | |
819 * semantics to fts using fts_set. An error return is allowed for similar | |
820 * reasons. | |
821 */ | |
822 /* ARGSUSED */ | |
823 int | |
824 fts_set(FTS *sp ATTRIBUTE_UNUSED, FTSENT *p, int instr) | |
825 { | |
826 if (instr != 0 && instr != FTS_AGAIN && instr != FTS_FOLLOW && | |
827 instr != FTS_NOINSTR && instr != FTS_SKIP) { | |
828 __set_errno (EINVAL); | |
829 return (1); | |
830 } | |
831 p->fts_instr = instr; | |
832 return (0); | |
833 } | |
834 | |
835 FTSENT * | |
836 fts_children (register FTS *sp, int instr) | |
837 { | |
838 register FTSENT *p; | |
839 int fd; | |
840 | |
841 if (instr != 0 && instr != FTS_NAMEONLY) { | |
842 __set_errno (EINVAL); | |
843 return (NULL); | |
844 } | |
845 | |
846 /* Set current node pointer. */ | |
847 p = sp->fts_cur; | |
848 | |
849 /* | |
850 * Errno set to 0 so user can distinguish empty directory from | |
851 * an error. | |
852 */ | |
853 __set_errno (0); | |
854 | |
855 /* Fatal errors stop here. */ | |
856 if (ISSET(FTS_STOP)) | |
857 return (NULL); | |
858 | |
859 /* Return logical hierarchy of user's arguments. */ | |
860 if (p->fts_info == FTS_INIT) | |
861 return (p->fts_link); | |
862 | |
863 /* | |
864 * If not a directory being visited in pre-order, stop here. Could | |
865 * allow FTS_DNR, assuming the user has fixed the problem, but the | |
866 * same effect is available with FTS_AGAIN. | |
867 */ | |
868 if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */) | |
869 return (NULL); | |
870 | |
871 /* Free up any previous child list. */ | |
872 if (sp->fts_child != NULL) | |
873 fts_lfree(sp->fts_child); | |
874 | |
875 if (instr == FTS_NAMEONLY) { | |
876 SET(FTS_NAMEONLY); | |
877 instr = BNAMES; | |
878 } else | |
879 instr = BCHILD; | |
880 | |
881 /* | |
5907 | 882 * If using chdir on a relative file name and called BEFORE fts_read |
883 * does its chdir to the root of a traversal, we can lose -- we need to | |
5858 | 884 * chdir into the subdirectory, and we don't know where the current |
885 * directory is, so we can't get back so that the upcoming chdir by | |
886 * fts_read will work. | |
887 */ | |
888 if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' || | |
889 ISSET(FTS_NOCHDIR)) | |
890 return (sp->fts_child = fts_build(sp, instr)); | |
891 | |
7151 | 892 if ((fd = diropen (sp, ".")) < 0) |
5858 | 893 return (sp->fts_child = NULL); |
894 sp->fts_child = fts_build(sp, instr); | |
7151 | 895 if (ISSET(FTS_CWDFD)) |
896 { | |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
897 cwd_advance_fd (sp, fd, true); |
7151 | 898 } |
899 else | |
900 { | |
901 if (fchdir(fd)) | |
902 { | |
6611
e4456c51dde7
Sync from the stable (b5) branch of coreutils:
Jim Meyering <jim@meyering.net>
parents:
6540
diff
changeset
|
903 int saved_errno = errno; |
7151 | 904 close (fd); |
6611
e4456c51dde7
Sync from the stable (b5) branch of coreutils:
Jim Meyering <jim@meyering.net>
parents:
6540
diff
changeset
|
905 __set_errno (saved_errno); |
7151 | 906 return NULL; |
907 } | |
908 close (fd); | |
909 } | |
5858 | 910 return (sp->fts_child); |
911 } | |
912 | |
913 /* | |
914 * This is the tricky part -- do not casually change *anything* in here. The | |
915 * idea is to build the linked list of entries that are used by fts_children | |
916 * and fts_read. There are lots of special cases. | |
917 * | |
918 * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is | |
919 * set and it's a physical walk (so that symbolic links can't be directories), | |
920 * we can do things quickly. First, if it's a 4.4BSD file system, the type | |
921 * of the file is in the directory entry. Otherwise, we assume that the number | |
922 * of subdirectories in a node is equal to the number of links to the parent. | |
923 * The former skips all stat calls. The latter skips stat calls in any leaf | |
924 * directories and for any files after the subdirectories in the directory have | |
925 * been found, cutting the stat calls by about 2/3. | |
926 */ | |
927 static FTSENT * | |
928 internal_function | |
929 fts_build (register FTS *sp, int type) | |
930 { | |
931 register struct dirent *dp; | |
932 register FTSENT *p, *head; | |
933 register size_t nitems; | |
934 FTSENT *cur, *tail; | |
935 DIR *dirp; | |
936 void *oldaddr; | |
937 int saved_errno; | |
938 bool descend; | |
939 bool doadjust; | |
940 ptrdiff_t level; | |
941 nlink_t nlinks; | |
942 bool nostat; | |
943 size_t len, maxlen, new_len; | |
944 char *cp; | |
945 | |
946 /* Set current node pointer. */ | |
947 cur = sp->fts_cur; | |
948 | |
949 /* | |
950 * Open the directory for reading. If this fails, we're done. | |
951 * If being called from fts_read, set the fts_info field. | |
952 */ | |
953 #if defined FTS_WHITEOUT && 0 | |
954 if (ISSET(FTS_WHITEOUT)) | |
955 oflag = DTF_NODUP|DTF_REWIND; | |
956 else | |
957 oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND; | |
958 #else | |
7151 | 959 # define __opendir2(file, flag) \ |
960 ( ! ISSET(FTS_NOCHDIR) && ISSET(FTS_CWDFD) \ | |
961 ? opendirat(sp->fts_cwd_fd, file) \ | |
962 : opendir(file)) | |
5858 | 963 #endif |
964 if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) { | |
965 if (type == BREAD) { | |
966 cur->fts_info = FTS_DNR; | |
967 cur->fts_errno = errno; | |
968 } | |
969 return (NULL); | |
970 } | |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
971 /* Rather than calling fts_stat for each and every entry encountered |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
972 in the readdir loop (below), stat each directory only right after |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
973 opening it. */ |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
974 if (cur->fts_info == FTS_NSOK) |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
975 cur->fts_info = fts_stat(sp, cur, false); |
5858 | 976 |
977 /* | |
978 * Nlinks is the number of possible entries of type directory in the | |
979 * directory if we're cheating on stat calls, 0 if we're not doing | |
980 * any stat calls at all, (nlink_t) -1 if we're statting everything. | |
981 */ | |
982 if (type == BNAMES) { | |
983 nlinks = 0; | |
984 /* Be quiet about nostat, GCC. */ | |
985 nostat = false; | |
986 } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) { | |
987 nlinks = (cur->fts_statp->st_nlink | |
988 - (ISSET(FTS_SEEDOT) ? 0 : 2)); | |
989 nostat = true; | |
990 } else { | |
991 nlinks = -1; | |
992 nostat = false; | |
993 } | |
994 | |
995 /* | |
996 * If we're going to need to stat anything or we want to descend | |
997 * and stay in the directory, chdir. If this fails we keep going, | |
998 * but set a flag so we don't chdir after the post-order visit. | |
999 * We won't be able to stat anything, but we can still return the | |
1000 * names themselves. Note, that since fts_read won't be able to | |
5907 | 1001 * chdir into the directory, it will have to return different file |
5858 | 1002 * names than before, i.e. "a/b" instead of "b". Since the node |
1003 * has already been visited in pre-order, have to wait until the | |
1004 * post-order visit to return the error. There is a special case | |
1005 * here, if there was nothing to stat then it's not an error to | |
1006 * not be able to stat. This is all fairly nasty. If a program | |
1007 * needed sorted entries or stat information, they had better be | |
1008 * checking FTS_NS on the returned nodes. | |
1009 */ | |
1010 if (nlinks || type == BREAD) { | |
7151 | 1011 int dir_fd = dirfd(dirp); |
1012 if (ISSET(FTS_CWDFD) && 0 <= dir_fd) | |
1013 dir_fd = dup (dir_fd); | |
1014 if (dir_fd < 0 || fts_safe_changedir(sp, cur, dir_fd, NULL)) { | |
5858 | 1015 if (nlinks && type == BREAD) |
1016 cur->fts_errno = errno; | |
1017 cur->fts_flags |= FTS_DONTCHDIR; | |
1018 descend = false; | |
5867
67b499052f7f
* fts.c: Include fts_.h first, to check interface.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5858
diff
changeset
|
1019 closedir(dirp); |
7151 | 1020 if (ISSET(FTS_CWDFD) && 0 <= dir_fd) |
1021 close (dir_fd); | |
5858 | 1022 dirp = NULL; |
1023 } else | |
1024 descend = true; | |
1025 } else | |
1026 descend = false; | |
1027 | |
1028 /* | |
1029 * Figure out the max file name length that can be stored in the | |
5907 | 1030 * current buffer -- the inner loop allocates more space as necessary. |
5858 | 1031 * We really wouldn't have to do the maxlen calculations here, we |
5907 | 1032 * could do them in fts_read before returning the name, but it's a |
5858 | 1033 * lot easier here since the length is part of the dirent structure. |
1034 * | |
1035 * If not changing directories set a pointer so that can just append | |
5907 | 1036 * each new component into the file name. |
5858 | 1037 */ |
1038 len = NAPPEND(cur); | |
1039 if (ISSET(FTS_NOCHDIR)) { | |
1040 cp = sp->fts_path + len; | |
1041 *cp++ = '/'; | |
1042 } else { | |
1043 /* GCC, you're too verbose. */ | |
1044 cp = NULL; | |
1045 } | |
1046 len++; | |
1047 maxlen = sp->fts_pathlen - len; | |
1048 | |
1049 level = cur->fts_level + 1; | |
1050 | |
1051 /* Read the directory, attaching each entry to the `link' pointer. */ | |
1052 doadjust = false; | |
1053 for (head = tail = NULL, nitems = 0; dirp && (dp = readdir(dirp));) { | |
7555
0da62e3fa6c7
(fts_build): Move variable declaration, for C89 compliance.
Bruno Haible <bruno@clisp.org>
parents:
7499
diff
changeset
|
1054 bool is_dir; |
0da62e3fa6c7
(fts_build): Move variable declaration, for C89 compliance.
Bruno Haible <bruno@clisp.org>
parents:
7499
diff
changeset
|
1055 |
5858 | 1056 if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) |
1057 continue; | |
1058 | |
6949
96af1224c6fa
* backupfile.c, dirfd.h, fts.c, getcwd.c, glob.c, glob_.h:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6934
diff
changeset
|
1059 if ((p = fts_alloc (sp, dp->d_name, |
96af1224c6fa
* backupfile.c, dirfd.h, fts.c, getcwd.c, glob.c, glob_.h:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6934
diff
changeset
|
1060 _D_EXACT_NAMLEN (dp))) == NULL) |
5858 | 1061 goto mem1; |
6949
96af1224c6fa
* backupfile.c, dirfd.h, fts.c, getcwd.c, glob.c, glob_.h:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6934
diff
changeset
|
1062 if (_D_EXACT_NAMLEN (dp) >= maxlen) { |
96af1224c6fa
* backupfile.c, dirfd.h, fts.c, getcwd.c, glob.c, glob_.h:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6934
diff
changeset
|
1063 /* include space for NUL */ |
5858 | 1064 oldaddr = sp->fts_path; |
6949
96af1224c6fa
* backupfile.c, dirfd.h, fts.c, getcwd.c, glob.c, glob_.h:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6934
diff
changeset
|
1065 if (! fts_palloc(sp, _D_EXACT_NAMLEN (dp) + len + 1)) { |
5858 | 1066 /* |
5907 | 1067 * No more memory. Save |
5858 | 1068 * errno, free up the current structure and the |
1069 * structures already allocated. | |
1070 */ | |
1071 mem1: saved_errno = errno; | |
7396
6f79ccad009d
2006-10-02 Dmitry V. Levin <ldv@altlinux.org>
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
1072 free(p); |
5858 | 1073 fts_lfree(head); |
5867
67b499052f7f
* fts.c: Include fts_.h first, to check interface.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5858
diff
changeset
|
1074 closedir(dirp); |
5858 | 1075 cur->fts_info = FTS_ERR; |
1076 SET(FTS_STOP); | |
1077 __set_errno (saved_errno); | |
1078 return (NULL); | |
1079 } | |
1080 /* Did realloc() change the pointer? */ | |
1081 if (oldaddr != sp->fts_path) { | |
1082 doadjust = true; | |
1083 if (ISSET(FTS_NOCHDIR)) | |
1084 cp = sp->fts_path + len; | |
1085 } | |
1086 maxlen = sp->fts_pathlen - len; | |
1087 } | |
1088 | |
6949
96af1224c6fa
* backupfile.c, dirfd.h, fts.c, getcwd.c, glob.c, glob_.h:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6934
diff
changeset
|
1089 new_len = len + _D_EXACT_NAMLEN (dp); |
5858 | 1090 if (new_len < len) { |
1091 /* | |
1092 * In the unlikely even that we would end up | |
5907 | 1093 * with a file name longer than SIZE_MAX, free up |
5858 | 1094 * the current structure and the structures already |
1095 * allocated, then error out with ENAMETOOLONG. | |
1096 */ | |
1097 free(p); | |
1098 fts_lfree(head); | |
5867
67b499052f7f
* fts.c: Include fts_.h first, to check interface.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5858
diff
changeset
|
1099 closedir(dirp); |
5858 | 1100 cur->fts_info = FTS_ERR; |
1101 SET(FTS_STOP); | |
1102 __set_errno (ENAMETOOLONG); | |
1103 return (NULL); | |
1104 } | |
1105 p->fts_level = level; | |
1106 p->fts_parent = sp->fts_cur; | |
1107 p->fts_pathlen = new_len; | |
1108 | |
1109 #if defined FTS_WHITEOUT && 0 | |
1110 if (dp->d_type == DT_WHT) | |
1111 p->fts_flags |= FTS_ISW; | |
1112 #endif | |
1113 | |
7151 | 1114 /* Build a file name for fts_stat to stat. */ |
1115 if (ISSET(FTS_NOCHDIR)) { | |
1116 p->fts_accpath = p->fts_path; | |
1117 memmove(cp, p->fts_name, p->fts_namelen + 1); | |
1118 } else | |
1119 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
|
1120 |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1121 if (sp->fts_compar == NULL || ISSET(FTS_DEFER_STAT)) { |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1122 /* Record what fts_read will have to do with this |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1123 entry. In many cases, it will simply fts_stat it, |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1124 but we can take advantage of any d_type information |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1125 to optimize away the unnecessary stat calls. I.e., |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1126 if FTS_NOSTAT is in effect and we're not following |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1127 symlinks (FTS_PHYSICAL) and d_type indicates this |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1128 is *not* a directory, then we won't have to stat it |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1129 at all. If it *is* a directory, then (currently) |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1130 we stat it regardless, in order to get device and |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1131 inode numbers. Some day we might optimize that |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1132 away, too, for directories where d_ino is known to |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1133 be valid. */ |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1134 bool skip_stat = (ISSET(FTS_PHYSICAL) |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1135 && ISSET(FTS_NOSTAT) |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1136 && DT_IS_KNOWN(dp) |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1137 && ! DT_MUST_BE(dp, DT_DIR)); |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1138 p->fts_info = FTS_NSOK; |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1139 fts_set_stat_required(p, !skip_stat); |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1140 is_dir = (ISSET(FTS_PHYSICAL) && ISSET(FTS_NOSTAT) |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1141 && DT_MUST_BE(dp, DT_DIR)); |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1142 } else { |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1143 p->fts_info = fts_stat(sp, p, false); |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1144 is_dir = (p->fts_info == FTS_D |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1145 || p->fts_info == FTS_DC |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1146 || p->fts_info == FTS_DOT); |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1147 } |
5858 | 1148 |
7151 | 1149 /* Decrement link count if applicable. */ |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1150 if (nlinks > 0 && is_dir) |
7151 | 1151 nlinks -= nostat; |
5858 | 1152 |
1153 /* We walk in directory order so "ls -f" doesn't get upset. */ | |
1154 p->fts_link = NULL; | |
1155 if (head == NULL) | |
1156 head = tail = p; | |
1157 else { | |
1158 tail->fts_link = p; | |
1159 tail = p; | |
1160 } | |
1161 ++nitems; | |
1162 } | |
1163 if (dirp) | |
5867
67b499052f7f
* fts.c: Include fts_.h first, to check interface.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5858
diff
changeset
|
1164 closedir(dirp); |
5858 | 1165 |
1166 /* | |
5907 | 1167 * If realloc() changed the address of the file name, adjust the |
5858 | 1168 * addresses for the rest of the tree and the dir list. |
1169 */ | |
1170 if (doadjust) | |
1171 fts_padjust(sp, head); | |
1172 | |
1173 /* | |
5907 | 1174 * If not changing directories, reset the file name back to original |
5858 | 1175 * state. |
1176 */ | |
1177 if (ISSET(FTS_NOCHDIR)) { | |
1178 if (len == sp->fts_pathlen || nitems == 0) | |
1179 --cp; | |
1180 *cp = '\0'; | |
1181 } | |
1182 | |
1183 /* | |
1184 * If descended after called from fts_children or after called from | |
1185 * fts_read and nothing found, get back. At the root level we use | |
5907 | 1186 * the saved fd; if one of fts_open()'s arguments is a relative name |
5858 | 1187 * to an empty directory, we wind up here with no other way back. If |
1188 * can't get back, we're done. | |
1189 */ | |
1190 if (descend && (type == BCHILD || !nitems) && | |
7151 | 1191 (cur->fts_level == FTS_ROOTLEVEL |
1192 ? RESTORE_INITIAL_CWD(sp) | |
1193 : fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) { | |
5858 | 1194 cur->fts_info = FTS_ERR; |
1195 SET(FTS_STOP); | |
7151 | 1196 fts_lfree(head); |
5858 | 1197 return (NULL); |
1198 } | |
1199 | |
1200 /* If didn't find anything, return NULL. */ | |
1201 if (!nitems) { | |
1202 if (type == BREAD) | |
1203 cur->fts_info = FTS_DP; | |
7151 | 1204 fts_lfree(head); |
5858 | 1205 return (NULL); |
1206 } | |
1207 | |
1208 /* Sort the entries. */ | |
1209 if (sp->fts_compar && nitems > 1) | |
1210 head = fts_sort(sp, head, nitems); | |
1211 return (head); | |
1212 } | |
1213 | |
1214 #if FTS_DEBUG | |
1215 | |
1216 /* Walk ->fts_parent links starting at E_CURR, until the root of the | |
1217 current hierarchy. There should be a directory with dev/inode | |
1218 matching those of AD. If not, print a lot of diagnostics. */ | |
1219 static void | |
1220 find_matching_ancestor (FTSENT const *e_curr, struct Active_dir const *ad) | |
1221 { | |
1222 FTSENT const *ent; | |
1223 for (ent = e_curr; ent->fts_level >= FTS_ROOTLEVEL; ent = ent->fts_parent) | |
1224 { | |
1225 if (ad->ino == ent->fts_statp->st_ino | |
1226 && ad->dev == ent->fts_statp->st_dev) | |
1227 return; | |
1228 } | |
1229 printf ("ERROR: tree dir, %s, not active\n", ad->fts_ent->fts_accpath); | |
1230 printf ("active dirs:\n"); | |
1231 for (ent = e_curr; | |
1232 ent->fts_level >= FTS_ROOTLEVEL; ent = ent->fts_parent) | |
1233 printf (" %s(%"PRIuMAX"/%"PRIuMAX") to %s(%"PRIuMAX"/%"PRIuMAX")...\n", | |
1234 ad->fts_ent->fts_accpath, | |
1235 (uintmax_t) ad->dev, | |
1236 (uintmax_t) ad->ino, | |
1237 ent->fts_accpath, | |
1238 (uintmax_t) ent->fts_statp->st_dev, | |
1239 (uintmax_t) ent->fts_statp->st_ino); | |
1240 } | |
1241 | |
1242 void | |
1243 fts_cross_check (FTS const *sp) | |
1244 { | |
1245 FTSENT const *ent = sp->fts_cur; | |
1246 FTSENT const *t; | |
1247 if ( ! ISSET (FTS_TIGHT_CYCLE_CHECK)) | |
1248 return; | |
1249 | |
1250 Dprintf (("fts-cross-check cur=%s\n", ent->fts_path)); | |
1251 /* Make sure every parent dir is in the tree. */ | |
1252 for (t = ent->fts_parent; t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent) | |
1253 { | |
1254 struct Active_dir ad; | |
1255 ad.ino = t->fts_statp->st_ino; | |
1256 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
|
1257 if ( ! hash_lookup (sp->fts_cycle.ht, &ad)) |
5858 | 1258 printf ("ERROR: active dir, %s, not in tree\n", t->fts_path); |
1259 } | |
1260 | |
1261 /* Make sure every dir in the tree is an active dir. | |
1262 But ENT is not necessarily a directory. If so, just skip this part. */ | |
1263 if (ent->fts_parent->fts_level >= FTS_ROOTLEVEL | |
1264 && (ent->fts_info == FTS_DP | |
1265 || ent->fts_info == FTS_D)) | |
1266 { | |
1267 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
|
1268 for (ad = hash_get_first (sp->fts_cycle.ht); ad != NULL; |
96149b1bbb32
(fts_cross_check) [FTS_DEBUG]: s/active_dir_ht/fts_cycle.ht/.
Jim Meyering <jim@meyering.net>
parents:
5907
diff
changeset
|
1269 ad = hash_get_next (sp->fts_cycle.ht, ad)) |
5858 | 1270 { |
1271 find_matching_ancestor (ent, ad); | |
1272 } | |
1273 } | |
1274 } | |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1275 |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1276 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
|
1277 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
|
1278 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1279 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
|
1280 return (fstat (fd1, &sb1) == 0 |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1281 && fstat (fd2, &sb2) == 0 |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1282 && SAME_INODE (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
|
1283 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1284 |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1285 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
|
1286 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
|
1287 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1288 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
|
1289 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
|
1290 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
|
1291 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
|
1292 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
|
1293 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
|
1294 return; |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1295 |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1296 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
|
1297 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1298 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
|
1299 if (fd < 0) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1300 fprintf (stream, "%d: %d:\n", i, fd); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1301 else |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1302 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1303 char *wd = getcwdat (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
|
1304 fprintf (stream, "%d: %d: %s\n", i, fd, wd); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1305 free (wd); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1306 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1307 if (i == fd_ring->fts_back) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1308 break; |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1309 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
|
1310 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1311 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1312 |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1313 /* 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
|
1314 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
|
1315 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
|
1316 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
|
1317 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1318 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
|
1319 return; |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1320 |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1321 /* 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
|
1322 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
|
1323 |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1324 int cwd_fd = sp->fts_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
|
1325 cwd_fd = dup (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
|
1326 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
|
1327 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
|
1328 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
|
1329 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
|
1330 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1331 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
|
1332 if (0 <= fd) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1333 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1334 int parent_fd = openat (cwd_fd, "..", O_RDONLY); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1335 if (parent_fd < 0) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1336 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1337 // Warn? |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1338 break; |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1339 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1340 if (!same_fd (fd, parent_fd)) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1341 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1342 char *cwd = getcwdat (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
|
1343 error (0, errno, "ring : %s", cwd); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1344 char *c2 = getcwdat (parent_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
|
1345 error (0, errno, "parent: %s", c2); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1346 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
|
1347 free (c2); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1348 abort (); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1349 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1350 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
|
1351 cwd_fd = parent_fd; |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1352 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1353 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1354 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
|
1355 } |
5858 | 1356 #endif |
1357 | |
1358 static unsigned short int | |
1359 internal_function | |
1360 fts_stat(FTS *sp, register FTSENT *p, bool follow) | |
1361 { | |
1362 struct stat *sbp = p->fts_statp; | |
1363 int saved_errno; | |
1364 | |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1365 if (p->fts_level == FTS_ROOTLEVEL && ISSET(FTS_COMFOLLOW)) |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1366 follow = true; |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1367 |
5858 | 1368 #if defined FTS_WHITEOUT && 0 |
1369 /* check for whiteout */ | |
1370 if (p->fts_flags & FTS_ISW) { | |
1371 memset(sbp, '\0', sizeof (*sbp)); | |
1372 sbp->st_mode = S_IFWHT; | |
1373 return (FTS_W); | |
1374 } | |
1375 #endif | |
1376 | |
1377 /* | |
1378 * If doing a logical walk, or application requested FTS_FOLLOW, do | |
1379 * a stat(2). If that fails, check for a non-existent symlink. If | |
1380 * fail, set the errno from the stat call. | |
1381 */ | |
1382 if (ISSET(FTS_LOGICAL) || follow) { | |
1383 if (stat(p->fts_accpath, sbp)) { | |
1384 saved_errno = errno; | |
6611
e4456c51dde7
Sync from the stable (b5) branch of coreutils:
Jim Meyering <jim@meyering.net>
parents:
6540
diff
changeset
|
1385 if (errno == ENOENT |
e4456c51dde7
Sync from the stable (b5) branch of coreutils:
Jim Meyering <jim@meyering.net>
parents:
6540
diff
changeset
|
1386 && lstat(p->fts_accpath, sbp) == 0) { |
5858 | 1387 __set_errno (0); |
1388 return (FTS_SLNONE); | |
1389 } | |
1390 p->fts_errno = saved_errno; | |
1391 goto err; | |
1392 } | |
7151 | 1393 } else if (fstatat(sp->fts_cwd_fd, p->fts_accpath, sbp, |
1394 AT_SYMLINK_NOFOLLOW)) { | |
5858 | 1395 p->fts_errno = errno; |
1396 err: memset(sbp, 0, sizeof(struct stat)); | |
1397 return (FTS_NS); | |
1398 } | |
1399 | |
1400 if (S_ISDIR(sbp->st_mode)) { | |
7482
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1401 if (ISDOT(p->fts_name)) { |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1402 /* Command-line "." and ".." are real directories. */ |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1403 return (p->fts_level == FTS_ROOTLEVEL ? FTS_D : FTS_DOT); |
26914593f079
Big performance improvement for fts-based tools that use FTS_NOSTAT.
Jim Meyering <jim@meyering.net>
parents:
7408
diff
changeset
|
1404 } |
5872 | 1405 |
1406 #if _LGPL_PACKAGE | |
1407 { | |
1408 /* | |
1409 * Cycle detection is done by brute force when the directory | |
1410 * is first encountered. If the tree gets deep enough or the | |
1411 * number of symbolic links to directories is high enough, | |
1412 * something faster might be worthwhile. | |
1413 */ | |
1414 FTSENT *t; | |
1415 | |
1416 for (t = p->fts_parent; | |
1417 t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent) | |
1418 if (sbp->st_ino == t->fts_statp->st_ino | |
1419 && sbp->st_dev == t->fts_statp->st_dev) | |
1420 { | |
1421 p->fts_cycle = t; | |
1422 return (FTS_DC); | |
1423 } | |
1424 } | |
1425 #endif | |
1426 | |
5858 | 1427 return (FTS_D); |
1428 } | |
1429 if (S_ISLNK(sbp->st_mode)) | |
1430 return (FTS_SL); | |
1431 if (S_ISREG(sbp->st_mode)) | |
1432 return (FTS_F); | |
1433 return (FTS_DEFAULT); | |
1434 } | |
1435 | |
1436 static int | |
1437 fts_compar (void const *a, void const *b) | |
1438 { | |
1439 /* Convert A and B to the correct types, to pacify the compiler, and | |
1440 for portability to bizarre hosts where "void const *" and "FTSENT | |
1441 const **" differ in runtime representation. The comparison | |
1442 function cannot modify *a and *b, but there is no compile-time | |
1443 check for this. */ | |
1444 FTSENT const **pa = (FTSENT const **) a; | |
1445 FTSENT const **pb = (FTSENT const **) b; | |
1446 return pa[0]->fts_fts->fts_compar (pa, pb); | |
1447 } | |
1448 | |
1449 static FTSENT * | |
1450 internal_function | |
1451 fts_sort (FTS *sp, FTSENT *head, register size_t nitems) | |
1452 { | |
1453 register FTSENT **ap, *p; | |
1454 | |
1455 /* On most modern hosts, void * and FTSENT ** have the same | |
1456 run-time representation, and one can convert sp->fts_compar to | |
1457 the type qsort expects without problem. Use the heuristic that | |
1458 this is OK if the two pointer types are the same size, and if | |
1459 converting FTSENT ** to long int is the same as converting | |
1460 FTSENT ** to void * and then to long int. This heuristic isn't | |
1461 valid in general but we don't know of any counterexamples. */ | |
1462 FTSENT *dummy; | |
1463 int (*compare) (void const *, void const *) = | |
1464 ((sizeof &dummy == sizeof (void *) | |
1465 && (long int) &dummy == (long int) (void *) &dummy) | |
1466 ? (int (*) (void const *, void const *)) sp->fts_compar | |
1467 : fts_compar); | |
1468 | |
1469 /* | |
1470 * Construct an array of pointers to the structures and call qsort(3). | |
1471 * Reassemble the array in the order returned by qsort. If unable to | |
1472 * sort for memory reasons, return the directory entries in their | |
1473 * current order. Allocate enough space for the current needs plus | |
1474 * 40 so don't realloc one entry at a time. | |
1475 */ | |
1476 if (nitems > sp->fts_nitems) { | |
1477 struct _ftsent **a; | |
1478 | |
1479 sp->fts_nitems = nitems + 40; | |
1480 if (SIZE_MAX / sizeof *a < sp->fts_nitems | |
1481 || ! (a = realloc (sp->fts_array, | |
1482 sp->fts_nitems * sizeof *a))) { | |
1483 free(sp->fts_array); | |
1484 sp->fts_array = NULL; | |
1485 sp->fts_nitems = 0; | |
1486 return (head); | |
1487 } | |
1488 sp->fts_array = a; | |
1489 } | |
1490 for (ap = sp->fts_array, p = head; p; p = p->fts_link) | |
1491 *ap++ = p; | |
1492 qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), compare); | |
1493 for (head = *(ap = sp->fts_array); --nitems; ++ap) | |
1494 ap[0]->fts_link = ap[1]; | |
1495 ap[0]->fts_link = NULL; | |
1496 return (head); | |
1497 } | |
1498 | |
1499 static FTSENT * | |
1500 internal_function | |
1501 fts_alloc (FTS *sp, const char *name, register size_t namelen) | |
1502 { | |
1503 register FTSENT *p; | |
1504 size_t len; | |
1505 | |
1506 /* | |
1507 * The file name is a variable length array. Allocate the FTSENT | |
1508 * structure and the file name in one chunk. | |
1509 */ | |
1510 len = sizeof(FTSENT) + namelen; | |
1511 if ((p = malloc(len)) == NULL) | |
1512 return (NULL); | |
1513 | |
1514 /* Copy the name and guarantee NUL termination. */ | |
1515 memmove(p->fts_name, name, namelen); | |
1516 p->fts_name[namelen] = '\0'; | |
1517 | |
1518 p->fts_namelen = namelen; | |
1519 p->fts_fts = sp; | |
1520 p->fts_path = sp->fts_path; | |
1521 p->fts_errno = 0; | |
1522 p->fts_flags = 0; | |
1523 p->fts_instr = FTS_NOINSTR; | |
1524 p->fts_number = 0; | |
1525 p->fts_pointer = NULL; | |
1526 return (p); | |
1527 } | |
1528 | |
1529 static void | |
1530 internal_function | |
1531 fts_lfree (register FTSENT *head) | |
1532 { | |
1533 register FTSENT *p; | |
1534 | |
1535 /* Free a linked list of structures. */ | |
1536 while ((p = head)) { | |
1537 head = head->fts_link; | |
1538 free(p); | |
1539 } | |
1540 } | |
1541 | |
1542 /* | |
5907 | 1543 * Allow essentially unlimited file name lengths; find, rm, ls should |
1544 * all work on any tree. Most systems will allow creation of file | |
1545 * names much longer than MAXPATHLEN, even though the kernel won't | |
1546 * resolve them. Add the size (not just what's needed) plus 256 bytes | |
1547 * so don't realloc the file name 2 bytes at a time. | |
5858 | 1548 */ |
1549 static bool | |
1550 internal_function | |
1551 fts_palloc (FTS *sp, size_t more) | |
1552 { | |
1553 char *p; | |
1554 size_t new_len = sp->fts_pathlen + more + 256; | |
1555 | |
1556 /* | |
1557 * See if fts_pathlen would overflow. | |
1558 */ | |
1559 if (new_len < sp->fts_pathlen) { | |
7396
6f79ccad009d
2006-10-02 Dmitry V. Levin <ldv@altlinux.org>
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
1560 free(sp->fts_path); |
5858 | 1561 sp->fts_path = NULL; |
1562 __set_errno (ENAMETOOLONG); | |
1563 return false; | |
1564 } | |
1565 sp->fts_pathlen = new_len; | |
1566 p = realloc(sp->fts_path, sp->fts_pathlen); | |
1567 if (p == NULL) { | |
1568 free(sp->fts_path); | |
1569 sp->fts_path = NULL; | |
1570 return false; | |
1571 } | |
1572 sp->fts_path = p; | |
1573 return true; | |
1574 } | |
1575 | |
1576 /* | |
5907 | 1577 * When the file name is realloc'd, have to fix all of the pointers in |
1578 * structures already returned. | |
5858 | 1579 */ |
1580 static void | |
1581 internal_function | |
1582 fts_padjust (FTS *sp, FTSENT *head) | |
1583 { | |
1584 FTSENT *p; | |
1585 char *addr = sp->fts_path; | |
1586 | |
1587 #define ADJUST(p) do { \ | |
1588 if ((p)->fts_accpath != (p)->fts_name) { \ | |
1589 (p)->fts_accpath = \ | |
1590 (char *)addr + ((p)->fts_accpath - (p)->fts_path); \ | |
1591 } \ | |
1592 (p)->fts_path = addr; \ | |
1593 } while (0) | |
1594 /* Adjust the current set of children. */ | |
1595 for (p = sp->fts_child; p; p = p->fts_link) | |
1596 ADJUST(p); | |
1597 | |
1598 /* Adjust the rest of the tree, including the current level. */ | |
1599 for (p = head; p->fts_level >= FTS_ROOTLEVEL;) { | |
1600 ADJUST(p); | |
1601 p = p->fts_link ? p->fts_link : p->fts_parent; | |
1602 } | |
1603 } | |
1604 | |
1605 static size_t | |
1606 internal_function | |
1607 fts_maxarglen (char * const *argv) | |
1608 { | |
1609 size_t len, max; | |
1610 | |
1611 for (max = 0; *argv; ++argv) | |
1612 if ((len = strlen(*argv)) > max) | |
1613 max = len; | |
1614 return (max + 1); | |
1615 } | |
1616 | |
1617 /* | |
5907 | 1618 * Change to dir specified by fd or file name without getting |
5858 | 1619 * tricked by someone changing the world out from underneath us. |
1620 * Assumes p->fts_statp->st_dev and p->fts_statp->st_ino are filled in. | |
7151 | 1621 * If FD is non-negative, expect it to be used after this function returns, |
1622 * and to be closed eventually. So don't pass e.g., `dirfd(dirp)' and then | |
1623 * do closedir(dirp), because that would invalidate the saved FD. | |
1624 * Upon failure, close FD immediately and return nonzero. | |
5858 | 1625 */ |
1626 static int | |
1627 internal_function | |
5907 | 1628 fts_safe_changedir (FTS *sp, FTSENT *p, int fd, char const *dir) |
5858 | 1629 { |
7151 | 1630 int ret; |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1631 bool is_dotdot = dir && STREQ (dir, ".."); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1632 int newfd; |
7623
5bfad7d666d6
* lib/fts.c (fts_safe_changedir): Add a comment.
Jim Meyering <jim@meyering.net>
parents:
7621
diff
changeset
|
1633 |
5bfad7d666d6
* lib/fts.c (fts_safe_changedir): Add a comment.
Jim Meyering <jim@meyering.net>
parents:
7621
diff
changeset
|
1634 /* This clause handles the unusual case in which FTS_NOCHDIR |
5bfad7d666d6
* lib/fts.c (fts_safe_changedir): Add a comment.
Jim Meyering <jim@meyering.net>
parents:
7621
diff
changeset
|
1635 is specified, along with FTS_CWDFD. In that case, there is |
5bfad7d666d6
* lib/fts.c (fts_safe_changedir): Add a comment.
Jim Meyering <jim@meyering.net>
parents:
7621
diff
changeset
|
1636 no need to change even the virtual cwd file descriptor. |
5bfad7d666d6
* lib/fts.c (fts_safe_changedir): Add a comment.
Jim Meyering <jim@meyering.net>
parents:
7621
diff
changeset
|
1637 However, if FD is non-negative, we do close it here. */ |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1638 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
|
1639 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1640 if (ISSET (FTS_CWDFD) && 0 <= fd) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1641 close (fd); |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1642 return 0; |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1643 } |
5858 | 1644 |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1645 if (fd < 0 && is_dotdot && ISSET (FTS_CWDFD)) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1646 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1647 /* When possible, skip the diropen and subsequent fstat+dev/ino |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1648 comparison. I.e., when changing to parent directory |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1649 (chdir ("..")), use a file descriptor from the ring and |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1650 save the overhead of diropen+fstat, as well as avoiding |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1651 failure when we lack "x" access to the virtual cwd. */ |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1652 if ( ! i_ring_empty (&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
|
1653 { |
7681
a6b7b31e9ddf
* lib/fts.c (fts_safe_changedir): Move a declaration "up",
Jim Meyering <jim@meyering.net>
parents:
7639
diff
changeset
|
1654 int parent_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
|
1655 fd_ring_print (sp, stderr, "pre-pop"); |
7681
a6b7b31e9ddf
* lib/fts.c (fts_safe_changedir): Move a declaration "up",
Jim Meyering <jim@meyering.net>
parents:
7639
diff
changeset
|
1656 parent_fd = i_ring_pop (&sp->fts_fd_ring); |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1657 is_dotdot = true; |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1658 if (0 <= parent_fd) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1659 { |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1660 fd = parent_fd; |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1661 dir = NULL; |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1662 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1663 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1664 } |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1665 |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1666 newfd = fd; |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1667 if (fd < 0 && (newfd = diropen (sp, dir)) < 0) |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1668 return -1; |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1669 |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1670 /* The following dev/inode check is necessary if we're doing a |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1671 `logical' traversal (through symlinks, a la chown -L), if the |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1672 system lacks O_NOFOLLOW support, or if we're changing to ".." |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1673 (but not via a popped file descriptor). When changing to the |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1674 name "..", O_NOFOLLOW can't help. In general, when the target is |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1675 not "..", diropen's use of O_NOFOLLOW ensures we don't mistakenly |
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1676 follow a symlink, so we can avoid the expense of this fstat. */ |
7408 | 1677 if (ISSET(FTS_LOGICAL) || ! HAVE_WORKING_O_NOFOLLOW |
7151 | 1678 || (dir && STREQ (dir, ".."))) |
1679 { | |
1680 struct stat sb; | |
1681 if (fstat(newfd, &sb)) | |
1682 { | |
5858 | 1683 ret = -1; |
1684 goto bail; | |
7151 | 1685 } |
1686 if (p->fts_statp->st_dev != sb.st_dev | |
1687 || p->fts_statp->st_ino != sb.st_ino) | |
1688 { | |
5858 | 1689 __set_errno (ENOENT); /* disinformation */ |
1690 ret = -1; | |
1691 goto bail; | |
7151 | 1692 } |
1693 } | |
1694 | |
1695 if (ISSET(FTS_CWDFD)) | |
1696 { | |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1697 cwd_advance_fd (sp, newfd, ! is_dotdot); |
7151 | 1698 return 0; |
1699 } | |
1700 | |
5858 | 1701 ret = fchdir(newfd); |
1702 bail: | |
1703 if (fd < 0) | |
7151 | 1704 { |
1705 int oerrno = errno; | |
1706 (void)close(newfd); | |
1707 __set_errno (oerrno); | |
1708 } | |
7639
1670d42131d7
Make fts (in FTS_CWDFD mode) more efficient by caching a few open
Jim Meyering <jim@meyering.net>
parents:
7635
diff
changeset
|
1709 return ret; |
5858 | 1710 } |