1 | /* $NetBSD: __fts13.c,v 1.44 2005/01/19 00:59:48 mycroft Exp $ */
|
---|
2 |
|
---|
3 | /*-
|
---|
4 | * Copyright (c) 1990, 1993, 1994
|
---|
5 | * The Regents of the University of California. All rights reserved.
|
---|
6 | *
|
---|
7 | * Redistribution and use in source and binary forms, with or without
|
---|
8 | * modification, are permitted provided that the following conditions
|
---|
9 | * are met:
|
---|
10 | * 1. Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * 3. Neither the name of the University nor the names of its contributors
|
---|
16 | * may be used to endorse or promote products derived from this software
|
---|
17 | * without specific prior written permission.
|
---|
18 | *
|
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
29 | * SUCH DAMAGE.
|
---|
30 | */
|
---|
31 |
|
---|
32 | #if HAVE_NBTOOL_CONFIG_H
|
---|
33 | #include "nbtool_config.h"
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | /*#include <sys/cdefs.h>*/
|
---|
37 | #if defined(LIBC_SCCS) && !defined(lint)
|
---|
38 | #if 0
|
---|
39 | static char sccsid[] = "@(#)fts.c 8.6 (Berkeley) 8/14/94";
|
---|
40 | #else
|
---|
41 | __RCSID("$NetBSD: __fts13.c,v 1.44 2005/01/19 00:59:48 mycroft Exp $");
|
---|
42 | #endif
|
---|
43 | #endif /* LIBC_SCCS and not lint */
|
---|
44 |
|
---|
45 | /*#include "namespace.h"*/
|
---|
46 | #ifndef _MSC_VER
|
---|
47 | #include <sys/param.h>
|
---|
48 | #endif
|
---|
49 | #include <sys/stat.h>
|
---|
50 |
|
---|
51 | #include <assert.h>
|
---|
52 | #include <dirent.h>
|
---|
53 | #include <errno.h>
|
---|
54 | #include <fcntl.h>
|
---|
55 | #include "ftsfake.h"
|
---|
56 | #include <stdlib.h>
|
---|
57 | #include <string.h>
|
---|
58 | #include <unistd.h>
|
---|
59 |
|
---|
60 | #ifdef __sun__
|
---|
61 | # include "solfakes.h"
|
---|
62 | # define dirfd(dir) -1
|
---|
63 | #endif
|
---|
64 | #ifdef _MSC_VER
|
---|
65 | # include "mscfakes.h"
|
---|
66 | # define dirfd(dir) -1
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | #if ! HAVE_NBTOOL_CONFIG_H
|
---|
70 | #ifndef __sun__
|
---|
71 | #define HAVE_STRUCT_DIRENT_D_NAMLEN 1
|
---|
72 | #endif
|
---|
73 | #endif
|
---|
74 |
|
---|
75 | #if 0
|
---|
76 | #ifdef __weak_alias
|
---|
77 | #ifdef __LIBC12_SOURCE__
|
---|
78 | __weak_alias(fts_children,_fts_children)
|
---|
79 | __weak_alias(fts_close,_fts_close)
|
---|
80 | __weak_alias(fts_open,_fts_open)
|
---|
81 | __weak_alias(fts_read,_fts_read)
|
---|
82 | __weak_alias(fts_set,_fts_set)
|
---|
83 | #endif /* __LIBC12_SOURCE__ */
|
---|
84 | #endif /* __weak_alias */
|
---|
85 | #endif
|
---|
86 |
|
---|
87 | #ifdef __LIBC12_SOURCE__
|
---|
88 | #define STAT stat12
|
---|
89 | #else
|
---|
90 | #define STAT stat
|
---|
91 | #endif
|
---|
92 |
|
---|
93 | #ifdef __LIBC12_SOURCE__
|
---|
94 | __warn_references(fts_children,
|
---|
95 | "warning: reference to compatibility fts_children();"
|
---|
96 | " include <fts.h> for correct reference")
|
---|
97 | __warn_references(fts_close,
|
---|
98 | "warning: reference to compatibility fts_close();"
|
---|
99 | " include <fts.h> for correct reference")
|
---|
100 | __warn_references(fts_open,
|
---|
101 | "warning: reference to compatibility fts_open();"
|
---|
102 | " include <fts.h> for correct reference")
|
---|
103 | __warn_references(fts_read,
|
---|
104 | "warning: reference to compatibility fts_read();"
|
---|
105 | " include <fts.h> for correct reference")
|
---|
106 | __warn_references(fts_set,
|
---|
107 | "warning: reference to compatibility fts_set();"
|
---|
108 | " include <fts.h> for correct reference")
|
---|
109 | #endif
|
---|
110 |
|
---|
111 | static FTSENT *fts_alloc(FTS *, const char *, size_t);
|
---|
112 | static FTSENT *fts_build(FTS *, int);
|
---|
113 | static void fts_lfree(FTSENT *);
|
---|
114 | static void fts_load(FTS *, FTSENT *);
|
---|
115 | static size_t fts_maxarglen(char * const *);
|
---|
116 | static size_t fts_pow2(size_t);
|
---|
117 | static int fts_palloc(FTS *, size_t);
|
---|
118 | static void fts_padjust(FTS *, FTSENT *);
|
---|
119 | static FTSENT *fts_sort(FTS *, FTSENT *, size_t);
|
---|
120 | static u_short fts_stat(FTS *, FTSENT *, int);
|
---|
121 | static int fts_safe_changedir(const FTS *, const FTSENT *, int,
|
---|
122 | const char *);
|
---|
123 |
|
---|
124 | #ifdef _MSC_VER
|
---|
125 | #undef HAVE_STRUCT_DIRENT_D_NAMLEN
|
---|
126 | #undef HAVE_FCHDIR
|
---|
127 | #endif
|
---|
128 |
|
---|
129 | #if defined(__EMX__) || defined(_MSC_VER)
|
---|
130 | # define NEED_STRRSLASH
|
---|
131 | # define IS_SLASH(ch) ( (ch) == '/' || (ch) == '\\' )
|
---|
132 | #else
|
---|
133 | # define HAVE_FCHDIR
|
---|
134 | # define IS_SLASH(ch) ( (ch) == '/' )
|
---|
135 | #endif
|
---|
136 |
|
---|
137 | #define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
|
---|
138 |
|
---|
139 | #define CLR(opt) (sp->fts_options &= ~(opt))
|
---|
140 | #define ISSET(opt) (sp->fts_options & (opt))
|
---|
141 | #define SET(opt) (sp->fts_options |= (opt))
|
---|
142 |
|
---|
143 | #define CHDIR(sp, path) (!ISSET(FTS_NOCHDIR) && chdir(path))
|
---|
144 | #ifdef HAVE_FCHDIR
|
---|
145 | #define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd))
|
---|
146 | #else
|
---|
147 | #define FCHDIR(sp, rdir) CHDIR(sp, rdir)
|
---|
148 | #endif
|
---|
149 |
|
---|
150 |
|
---|
151 | /* fts_build flags */
|
---|
152 | #define BCHILD 1 /* fts_children */
|
---|
153 | #define BNAMES 2 /* fts_children, names only */
|
---|
154 | #define BREAD 3 /* fts_read */
|
---|
155 |
|
---|
156 | #ifndef DTF_HIDEW
|
---|
157 | #undef FTS_WHITEOUT
|
---|
158 | #endif
|
---|
159 |
|
---|
160 | #ifndef _DIAGASSERT
|
---|
161 | #define _DIAGASSERT assert
|
---|
162 | #endif
|
---|
163 |
|
---|
164 |
|
---|
165 | FTS *
|
---|
166 | fts_open(argv, options, compar)
|
---|
167 | char * const *argv;
|
---|
168 | int options;
|
---|
169 | int (*compar)(const FTSENT **, const FTSENT **);
|
---|
170 | {
|
---|
171 | FTS *sp;
|
---|
172 | FTSENT *p, *root;
|
---|
173 | size_t nitems;
|
---|
174 | FTSENT *parent, *tmp = NULL; /* pacify gcc */
|
---|
175 | size_t len;
|
---|
176 |
|
---|
177 | _DIAGASSERT(argv != NULL);
|
---|
178 |
|
---|
179 | /* Options check. */
|
---|
180 | if (options & ~FTS_OPTIONMASK) {
|
---|
181 | errno = EINVAL;
|
---|
182 | return (NULL);
|
---|
183 | }
|
---|
184 |
|
---|
185 | /* Allocate/initialize the stream */
|
---|
186 | if ((sp = malloc((u_int)sizeof(FTS))) == NULL)
|
---|
187 | return (NULL);
|
---|
188 | memset(sp, 0, sizeof(FTS));
|
---|
189 | sp->fts_compar = compar;
|
---|
190 | sp->fts_options = options;
|
---|
191 |
|
---|
192 | /* Logical walks turn on NOCHDIR; symbolic links are too hard. */
|
---|
193 | if (ISSET(FTS_LOGICAL))
|
---|
194 | SET(FTS_NOCHDIR);
|
---|
195 |
|
---|
196 | /*
|
---|
197 | * Start out with 1K of path space, and enough, in any case,
|
---|
198 | * to hold the user's paths.
|
---|
199 | */
|
---|
200 | if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN)))
|
---|
201 | goto mem1;
|
---|
202 |
|
---|
203 | /* Allocate/initialize root's parent. */
|
---|
204 | if ((parent = fts_alloc(sp, "", 0)) == NULL)
|
---|
205 | goto mem2;
|
---|
206 | parent->fts_level = FTS_ROOTPARENTLEVEL;
|
---|
207 |
|
---|
208 | /* Allocate/initialize root(s). */
|
---|
209 | for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) {
|
---|
210 | /* Don't allow zero-length paths. */
|
---|
211 | if ((len = strlen(*argv)) == 0) {
|
---|
212 | errno = ENOENT;
|
---|
213 | goto mem3;
|
---|
214 | }
|
---|
215 |
|
---|
216 | if ((p = fts_alloc(sp, *argv, len)) == NULL)
|
---|
217 | goto mem3;
|
---|
218 | p->fts_level = FTS_ROOTLEVEL;
|
---|
219 | p->fts_parent = parent;
|
---|
220 | p->fts_accpath = p->fts_name;
|
---|
221 | p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW));
|
---|
222 |
|
---|
223 | /* Command-line "." and ".." are real directories. */
|
---|
224 | if (p->fts_info == FTS_DOT)
|
---|
225 | p->fts_info = FTS_D;
|
---|
226 |
|
---|
227 | /*
|
---|
228 | * If comparison routine supplied, traverse in sorted
|
---|
229 | * order; otherwise traverse in the order specified.
|
---|
230 | */
|
---|
231 | if (compar) {
|
---|
232 | p->fts_link = root;
|
---|
233 | root = p;
|
---|
234 | } else {
|
---|
235 | p->fts_link = NULL;
|
---|
236 | if (root == NULL)
|
---|
237 | tmp = root = p;
|
---|
238 | else {
|
---|
239 | tmp->fts_link = p;
|
---|
240 | tmp = p;
|
---|
241 | }
|
---|
242 | }
|
---|
243 | }
|
---|
244 | if (compar && nitems > 1)
|
---|
245 | root = fts_sort(sp, root, nitems);
|
---|
246 |
|
---|
247 | /*
|
---|
248 | * Allocate a dummy pointer and make fts_read think that we've just
|
---|
249 | * finished the node before the root(s); set p->fts_info to FTS_INIT
|
---|
250 | * so that everything about the "current" node is ignored.
|
---|
251 | */
|
---|
252 | if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
|
---|
253 | goto mem3;
|
---|
254 | sp->fts_cur->fts_link = root;
|
---|
255 | sp->fts_cur->fts_info = FTS_INIT;
|
---|
256 |
|
---|
257 | /*
|
---|
258 | * If using chdir(2), grab a file descriptor pointing to dot to insure
|
---|
259 | * that we can get back here; this could be avoided for some paths,
|
---|
260 | * but almost certainly not worth the effort. Slashes, symbolic links,
|
---|
261 | * and ".." are all fairly nasty problems. Note, if we can't get the
|
---|
262 | * descriptor we run anyway, just more slowly.
|
---|
263 | */
|
---|
264 | if (!ISSET(FTS_NOCHDIR)) {
|
---|
265 | #ifdef HAVE_FCHDIR
|
---|
266 | if ((sp->fts_rfd = open(".", O_RDONLY, 0)) == -1)
|
---|
267 | SET(FTS_NOCHDIR);
|
---|
268 | else if (fcntl(sp->fts_rfd, F_SETFD, FD_CLOEXEC) == -1) {
|
---|
269 | close(sp->fts_rfd);
|
---|
270 | SET(FTS_NOCHDIR);
|
---|
271 | }
|
---|
272 | #else
|
---|
273 | if ((sp->fts_rdir = getcwd(NULL, 0)) != NULL)
|
---|
274 | SET(FTS_NOCHDIR);
|
---|
275 | #endif
|
---|
276 | }
|
---|
277 |
|
---|
278 | return (sp);
|
---|
279 |
|
---|
280 | mem3: fts_lfree(root);
|
---|
281 | free(parent);
|
---|
282 | mem2: free(sp->fts_path);
|
---|
283 | mem1: free(sp);
|
---|
284 | return (NULL);
|
---|
285 | }
|
---|
286 |
|
---|
287 | #ifdef NEED_STRRSLASH
|
---|
288 | static char *strrslash(register char *psz)
|
---|
289 | {
|
---|
290 | register char ch;
|
---|
291 | char *pszLast = NULL;
|
---|
292 | for (; (ch = *psz); psz++)
|
---|
293 | switch (ch)
|
---|
294 | {
|
---|
295 | case '/':
|
---|
296 | case '\\':
|
---|
297 | case ':':
|
---|
298 | pszLast = psz;
|
---|
299 | break;
|
---|
300 | }
|
---|
301 | return pszLast;
|
---|
302 | }
|
---|
303 | #endif
|
---|
304 |
|
---|
305 | static void
|
---|
306 | fts_load(sp, p)
|
---|
307 | FTS *sp;
|
---|
308 | FTSENT *p;
|
---|
309 | {
|
---|
310 | size_t len;
|
---|
311 | char *cp;
|
---|
312 |
|
---|
313 | _DIAGASSERT(sp != NULL);
|
---|
314 | _DIAGASSERT(p != NULL);
|
---|
315 |
|
---|
316 | /*
|
---|
317 | * Load the stream structure for the next traversal. Since we don't
|
---|
318 | * actually enter the directory until after the preorder visit, set
|
---|
319 | * the fts_accpath field specially so the chdir gets done to the right
|
---|
320 | * place and the user can access the first node. From fts_open it's
|
---|
321 | * known that the path will fit.
|
---|
322 | */
|
---|
323 | len = p->fts_pathlen = p->fts_namelen;
|
---|
324 | memmove(sp->fts_path, p->fts_name, len + 1);
|
---|
325 | #ifdef NEED_STRRSLASH
|
---|
326 | if ((cp = strrslash(p->fts_name)) && (cp != p->fts_name || cp[1])) {
|
---|
327 | #else
|
---|
328 | if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
|
---|
329 | #endif
|
---|
330 | len = strlen(++cp);
|
---|
331 | memmove(p->fts_name, cp, len + 1);
|
---|
332 | p->fts_namelen = len;
|
---|
333 | }
|
---|
334 | p->fts_accpath = p->fts_path = sp->fts_path;
|
---|
335 | sp->fts_dev = p->fts_dev;
|
---|
336 | }
|
---|
337 |
|
---|
338 | int
|
---|
339 | fts_close(sp)
|
---|
340 | FTS *sp;
|
---|
341 | {
|
---|
342 | FTSENT *freep, *p;
|
---|
343 | int saved_errno = 0;
|
---|
344 |
|
---|
345 | _DIAGASSERT(sp != NULL);
|
---|
346 |
|
---|
347 | /*
|
---|
348 | * This still works if we haven't read anything -- the dummy structure
|
---|
349 | * points to the root list, so we step through to the end of the root
|
---|
350 | * list which has a valid parent pointer.
|
---|
351 | */
|
---|
352 | if (sp->fts_cur) {
|
---|
353 | #ifndef _MSC_VER
|
---|
354 | if (ISSET(FTS_SYMFOLLOW))
|
---|
355 | (void)close(sp->fts_cur->fts_symfd);
|
---|
356 | #endif
|
---|
357 | for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
|
---|
358 | freep = p;
|
---|
359 | p = p->fts_link ? p->fts_link : p->fts_parent;
|
---|
360 | free(freep);
|
---|
361 | }
|
---|
362 | free(p);
|
---|
363 | }
|
---|
364 |
|
---|
365 | /* Free up child linked list, sort array, path buffer. */
|
---|
366 | if (sp->fts_child)
|
---|
367 | fts_lfree(sp->fts_child);
|
---|
368 | if (sp->fts_array)
|
---|
369 | free(sp->fts_array);
|
---|
370 | free(sp->fts_path);
|
---|
371 |
|
---|
372 | /* Return to original directory, save errno if necessary. */
|
---|
373 | if (!ISSET(FTS_NOCHDIR)) {
|
---|
374 | #ifdef HAVE_FCHDIR
|
---|
375 | if (fchdir(sp->fts_rfd))
|
---|
376 | saved_errno = errno;
|
---|
377 | (void)close(sp->fts_rfd);
|
---|
378 | #else
|
---|
379 | if (chdir(sp->fts_rdir))
|
---|
380 | saved_errno = errno;
|
---|
381 | free(sp->fts_rdir);
|
---|
382 | sp->fts_rdir = NULL;
|
---|
383 | #endif
|
---|
384 | }
|
---|
385 |
|
---|
386 | /* Free up the stream pointer. */
|
---|
387 | free(sp);
|
---|
388 | /* ISSET() is illegal after this, since the macro touches sp */
|
---|
389 |
|
---|
390 | /* Set errno and return. */
|
---|
391 | if (saved_errno) {
|
---|
392 | errno = saved_errno;
|
---|
393 | return (-1);
|
---|
394 | }
|
---|
395 | return (0);
|
---|
396 | }
|
---|
397 |
|
---|
398 | /*
|
---|
399 | * Special case a root of "/" so that slashes aren't appended which would
|
---|
400 | * cause paths to be written as "//foo".
|
---|
401 | */
|
---|
402 | #define NAPPEND(p) \
|
---|
403 | (p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 && \
|
---|
404 | p->fts_path[0] == '/' ? 0 : p->fts_pathlen)
|
---|
405 |
|
---|
406 | FTSENT *
|
---|
407 | fts_read(sp)
|
---|
408 | FTS *sp;
|
---|
409 | {
|
---|
410 | FTSENT *p, *tmp;
|
---|
411 | int instr;
|
---|
412 | char *t;
|
---|
413 | int saved_errno;
|
---|
414 |
|
---|
415 | _DIAGASSERT(sp != NULL);
|
---|
416 |
|
---|
417 | /* If finished or unrecoverable error, return NULL. */
|
---|
418 | if (sp->fts_cur == NULL || ISSET(FTS_STOP))
|
---|
419 | return (NULL);
|
---|
420 |
|
---|
421 | /* Set current node pointer. */
|
---|
422 | p = sp->fts_cur;
|
---|
423 |
|
---|
424 | /* Save and zero out user instructions. */
|
---|
425 | instr = p->fts_instr;
|
---|
426 | p->fts_instr = FTS_NOINSTR;
|
---|
427 |
|
---|
428 | /* Any type of file may be re-visited; re-stat and re-turn. */
|
---|
429 | if (instr == FTS_AGAIN) {
|
---|
430 | p->fts_info = fts_stat(sp, p, 0);
|
---|
431 | return (p);
|
---|
432 | }
|
---|
433 |
|
---|
434 | /*
|
---|
435 | * Following a symlink -- SLNONE test allows application to see
|
---|
436 | * SLNONE and recover. If indirecting through a symlink, have
|
---|
437 | * keep a pointer to current location. If unable to get that
|
---|
438 | * pointer, follow fails.
|
---|
439 | */
|
---|
440 | if (instr == FTS_FOLLOW &&
|
---|
441 | (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
|
---|
442 | p->fts_info = fts_stat(sp, p, 1);
|
---|
443 | if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
|
---|
444 | #ifdef HAVE_FCHDIR
|
---|
445 | if ((p->fts_symfd = open(".", O_RDONLY, 0)) == -1) {
|
---|
446 | p->fts_errno = errno;
|
---|
447 | p->fts_info = FTS_ERR;
|
---|
448 | } else if (fcntl(p->fts_symfd, F_SETFD, FD_CLOEXEC) == -1) {
|
---|
449 | p->fts_errno = errno;
|
---|
450 | p->fts_info = FTS_ERR;
|
---|
451 | close(p->fts_symfd);
|
---|
452 | } else
|
---|
453 | p->fts_flags |= FTS_SYMFOLLOW;
|
---|
454 | #endif
|
---|
455 | }
|
---|
456 | return (p);
|
---|
457 | }
|
---|
458 |
|
---|
459 | /* Directory in pre-order. */
|
---|
460 | if (p->fts_info == FTS_D) {
|
---|
461 | /* If skipped or crossed mount point, do post-order visit. */
|
---|
462 | if (instr == FTS_SKIP ||
|
---|
463 | (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
|
---|
464 | #ifdef HAVE_FCHDIR
|
---|
465 | if (p->fts_flags & FTS_SYMFOLLOW)
|
---|
466 | (void)close(p->fts_symfd);
|
---|
467 | #endif
|
---|
468 | if (sp->fts_child) {
|
---|
469 | fts_lfree(sp->fts_child);
|
---|
470 | sp->fts_child = NULL;
|
---|
471 | }
|
---|
472 | p->fts_info = FTS_DP;
|
---|
473 | return (p);
|
---|
474 | }
|
---|
475 |
|
---|
476 | /* Rebuild if only read the names and now traversing. */
|
---|
477 | if (sp->fts_child && ISSET(FTS_NAMEONLY)) {
|
---|
478 | CLR(FTS_NAMEONLY);
|
---|
479 | fts_lfree(sp->fts_child);
|
---|
480 | sp->fts_child = NULL;
|
---|
481 | }
|
---|
482 |
|
---|
483 | /*
|
---|
484 | * Cd to the subdirectory.
|
---|
485 | *
|
---|
486 | * If have already read and now fail to chdir, whack the list
|
---|
487 | * to make the names come out right, and set the parent errno
|
---|
488 | * so the application will eventually get an error condition.
|
---|
489 | * Set the FTS_DONTCHDIR flag so that when we logically change
|
---|
490 | * directories back to the parent we don't do a chdir.
|
---|
491 | *
|
---|
492 | * If haven't read do so. If the read fails, fts_build sets
|
---|
493 | * FTS_STOP or the fts_info field of the node.
|
---|
494 | */
|
---|
495 | if (sp->fts_child) {
|
---|
496 | if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) {
|
---|
497 | p->fts_errno = errno;
|
---|
498 | p->fts_flags |= FTS_DONTCHDIR;
|
---|
499 | for (p = sp->fts_child; p; p = p->fts_link)
|
---|
500 | p->fts_accpath =
|
---|
501 | p->fts_parent->fts_accpath;
|
---|
502 | }
|
---|
503 | } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) {
|
---|
504 | if (ISSET(FTS_STOP))
|
---|
505 | return (NULL);
|
---|
506 | return (p);
|
---|
507 | }
|
---|
508 | p = sp->fts_child;
|
---|
509 | sp->fts_child = NULL;
|
---|
510 | goto name;
|
---|
511 | }
|
---|
512 |
|
---|
513 | /* Move to the next node on this level. */
|
---|
514 | next: tmp = p;
|
---|
515 | if ((p = p->fts_link) != NULL) {
|
---|
516 | free(tmp);
|
---|
517 |
|
---|
518 | /*
|
---|
519 | * If reached the top, return to the original directory, and
|
---|
520 | * load the paths for the next root.
|
---|
521 | */
|
---|
522 | if (p->fts_level == FTS_ROOTLEVEL) {
|
---|
523 | #ifdef HAVE_FCHDIR
|
---|
524 | if (FCHDIR(sp, sp->fts_rfd)) {
|
---|
525 | #else
|
---|
526 | if (CHDIR(sp, sp->fts_rdir)) {
|
---|
527 | #endif
|
---|
528 | SET(FTS_STOP);
|
---|
529 | return (NULL);
|
---|
530 | }
|
---|
531 | fts_load(sp, p);
|
---|
532 | return (sp->fts_cur = p);
|
---|
533 | }
|
---|
534 |
|
---|
535 | /*
|
---|
536 | * User may have called fts_set on the node. If skipped,
|
---|
537 | * ignore. If followed, get a file descriptor so we can
|
---|
538 | * get back if necessary.
|
---|
539 | */
|
---|
540 | if (p->fts_instr == FTS_SKIP)
|
---|
541 | goto next;
|
---|
542 | if (p->fts_instr == FTS_FOLLOW) {
|
---|
543 | p->fts_info = fts_stat(sp, p, 1);
|
---|
544 | if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
|
---|
545 | #ifdef HAVE_FCHDIR
|
---|
546 | if ((p->fts_symfd =
|
---|
547 | open(".", O_RDONLY, 0)) == -1) {
|
---|
548 | p->fts_errno = errno;
|
---|
549 | p->fts_info = FTS_ERR;
|
---|
550 | } else if (fcntl(p->fts_symfd, F_SETFD, FD_CLOEXEC) == -1) {
|
---|
551 | p->fts_errno = errno;
|
---|
552 | p->fts_info = FTS_ERR;
|
---|
553 | close(p->fts_symfd);
|
---|
554 | } else
|
---|
555 | p->fts_flags |= FTS_SYMFOLLOW;
|
---|
556 | #endif
|
---|
557 | }
|
---|
558 | p->fts_instr = FTS_NOINSTR;
|
---|
559 | }
|
---|
560 |
|
---|
561 | name: t = sp->fts_path + NAPPEND(p->fts_parent);
|
---|
562 | *t++ = '/';
|
---|
563 | memmove(t, p->fts_name, (size_t)(p->fts_namelen + 1));
|
---|
564 | return (sp->fts_cur = p);
|
---|
565 | }
|
---|
566 |
|
---|
567 | /* Move up to the parent node. */
|
---|
568 | p = tmp->fts_parent;
|
---|
569 | free(tmp);
|
---|
570 |
|
---|
571 | if (p->fts_level == FTS_ROOTPARENTLEVEL) {
|
---|
572 | /*
|
---|
573 | * Done; free everything up and set errno to 0 so the user
|
---|
574 | * can distinguish between error and EOF.
|
---|
575 | */
|
---|
576 | free(p);
|
---|
577 | errno = 0;
|
---|
578 | return (sp->fts_cur = NULL);
|
---|
579 | }
|
---|
580 |
|
---|
581 | /* Nul terminate the pathname. */
|
---|
582 | sp->fts_path[p->fts_pathlen] = '\0';
|
---|
583 |
|
---|
584 | /*
|
---|
585 | * Return to the parent directory. If at a root node or came through
|
---|
586 | * a symlink, go back through the file descriptor. Otherwise, cd up
|
---|
587 | * one directory.
|
---|
588 | */
|
---|
589 | if (p->fts_level == FTS_ROOTLEVEL) {
|
---|
590 | #ifdef HAVE_FCHDIR
|
---|
591 | if (FCHDIR(sp, sp->fts_rfd)) {
|
---|
592 | #else
|
---|
593 | if (CHDIR(sp, sp->fts_rdir)) {
|
---|
594 | #endif
|
---|
595 | SET(FTS_STOP);
|
---|
596 | return (NULL);
|
---|
597 | }
|
---|
598 | #ifdef HAVE_FCHDIR
|
---|
599 | } else if (p->fts_flags & FTS_SYMFOLLOW) {
|
---|
600 | if (FCHDIR(sp, p->fts_symfd)) {
|
---|
601 | saved_errno = errno;
|
---|
602 | (void)close(p->fts_symfd);
|
---|
603 | errno = saved_errno;
|
---|
604 | SET(FTS_STOP);
|
---|
605 | return (NULL);
|
---|
606 | }
|
---|
607 | (void)close(p->fts_symfd);
|
---|
608 | #else
|
---|
609 | (void)saved_errno;
|
---|
610 | #endif
|
---|
611 | } else if (!(p->fts_flags & FTS_DONTCHDIR) &&
|
---|
612 | fts_safe_changedir(sp, p->fts_parent, -1, "..")) {
|
---|
613 | SET(FTS_STOP);
|
---|
614 | return (NULL);
|
---|
615 | }
|
---|
616 | p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
|
---|
617 | return (sp->fts_cur = p);
|
---|
618 | }
|
---|
619 |
|
---|
620 | /*
|
---|
621 | * Fts_set takes the stream as an argument although it's not used in this
|
---|
622 | * implementation; it would be necessary if anyone wanted to add global
|
---|
623 | * semantics to fts using fts_set. An error return is allowed for similar
|
---|
624 | * reasons.
|
---|
625 | */
|
---|
626 | /* ARGSUSED */
|
---|
627 | int
|
---|
628 | fts_set(sp, p, instr)
|
---|
629 | FTS *sp;
|
---|
630 | FTSENT *p;
|
---|
631 | int instr;
|
---|
632 | {
|
---|
633 |
|
---|
634 | _DIAGASSERT(sp != NULL);
|
---|
635 | _DIAGASSERT(p != NULL);
|
---|
636 |
|
---|
637 | if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
|
---|
638 | instr != FTS_NOINSTR && instr != FTS_SKIP) {
|
---|
639 | errno = EINVAL;
|
---|
640 | return (1);
|
---|
641 | }
|
---|
642 | p->fts_instr = instr;
|
---|
643 | return (0);
|
---|
644 | }
|
---|
645 |
|
---|
646 | FTSENT *
|
---|
647 | fts_children(sp, instr)
|
---|
648 | FTS *sp;
|
---|
649 | int instr;
|
---|
650 | {
|
---|
651 | FTSENT *p;
|
---|
652 | #ifdef HAVE_FCHDIR
|
---|
653 | int fd;
|
---|
654 | #else
|
---|
655 | char *pszRoot;
|
---|
656 | int rc;
|
---|
657 | #endif
|
---|
658 |
|
---|
659 | _DIAGASSERT(sp != NULL);
|
---|
660 |
|
---|
661 | if (instr && instr != FTS_NAMEONLY) {
|
---|
662 | errno = EINVAL;
|
---|
663 | return (NULL);
|
---|
664 | }
|
---|
665 |
|
---|
666 | /* Set current node pointer. */
|
---|
667 | p = sp->fts_cur;
|
---|
668 |
|
---|
669 | /*
|
---|
670 | * Errno set to 0 so user can distinguish empty directory from
|
---|
671 | * an error.
|
---|
672 | */
|
---|
673 | errno = 0;
|
---|
674 |
|
---|
675 | /* Fatal errors stop here. */
|
---|
676 | if (ISSET(FTS_STOP))
|
---|
677 | return (NULL);
|
---|
678 |
|
---|
679 | /* Return logical hierarchy of user's arguments. */
|
---|
680 | if (p->fts_info == FTS_INIT)
|
---|
681 | return (p->fts_link);
|
---|
682 |
|
---|
683 | /*
|
---|
684 | * If not a directory being visited in pre-order, stop here. Could
|
---|
685 | * allow FTS_DNR, assuming the user has fixed the problem, but the
|
---|
686 | * same effect is available with FTS_AGAIN.
|
---|
687 | */
|
---|
688 | if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */)
|
---|
689 | return (NULL);
|
---|
690 |
|
---|
691 | /* Free up any previous child list. */
|
---|
692 | if (sp->fts_child)
|
---|
693 | fts_lfree(sp->fts_child);
|
---|
694 |
|
---|
695 | if (instr == FTS_NAMEONLY) {
|
---|
696 | SET(FTS_NAMEONLY);
|
---|
697 | instr = BNAMES;
|
---|
698 | } else
|
---|
699 | instr = BCHILD;
|
---|
700 |
|
---|
701 | /*
|
---|
702 | * If using chdir on a relative path and called BEFORE fts_read does
|
---|
703 | * its chdir to the root of a traversal, we can lose -- we need to
|
---|
704 | * chdir into the subdirectory, and we don't know where the current
|
---|
705 | * directory is, so we can't get back so that the upcoming chdir by
|
---|
706 | * fts_read will work.
|
---|
707 | */
|
---|
708 | if (p->fts_level != FTS_ROOTLEVEL || IS_SLASH(p->fts_accpath[0]) ||
|
---|
709 | ISSET(FTS_NOCHDIR))
|
---|
710 | return (sp->fts_child = fts_build(sp, instr));
|
---|
711 |
|
---|
712 | #ifdef HAVE_FCHDIR
|
---|
713 | if ((fd = open(".", O_RDONLY, 0)) == -1)
|
---|
714 | #else
|
---|
715 | if ((pszRoot = getcwd(NULL, 0)) == NULL)
|
---|
716 | #endif
|
---|
717 | return (sp->fts_child = NULL);
|
---|
718 | sp->fts_child = fts_build(sp, instr);
|
---|
719 | #ifdef HAVE_FCHDIR
|
---|
720 | if (fchdir(fd)) {
|
---|
721 | (void)close(fd);
|
---|
722 | return (NULL);
|
---|
723 | }
|
---|
724 | (void)close(fd);
|
---|
725 | #else
|
---|
726 | rc = chdir(pszRoot);
|
---|
727 | free(pszRoot);
|
---|
728 | if (rc)
|
---|
729 | return (NULL);
|
---|
730 | #endif
|
---|
731 |
|
---|
732 | return (sp->fts_child);
|
---|
733 | }
|
---|
734 |
|
---|
735 | /*
|
---|
736 | * This is the tricky part -- do not casually change *anything* in here. The
|
---|
737 | * idea is to build the linked list of entries that are used by fts_children
|
---|
738 | * and fts_read. There are lots of special cases.
|
---|
739 | *
|
---|
740 | * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is
|
---|
741 | * set and it's a physical walk (so that symbolic links can't be directories),
|
---|
742 | * we can do things quickly. First, if it's a 4.4BSD file system, the type
|
---|
743 | * of the file is in the directory entry. Otherwise, we assume that the number
|
---|
744 | * of subdirectories in a node is equal to the number of links to the parent.
|
---|
745 | * The former skips all stat calls. The latter skips stat calls in any leaf
|
---|
746 | * directories and for any files after the subdirectories in the directory have
|
---|
747 | * been found, cutting the stat calls by about 2/3.
|
---|
748 | */
|
---|
749 | static FTSENT *
|
---|
750 | fts_build(sp, type)
|
---|
751 | FTS *sp;
|
---|
752 | int type;
|
---|
753 | {
|
---|
754 | struct dirent *dp;
|
---|
755 | FTSENT *p, *head;
|
---|
756 | size_t nitems;
|
---|
757 | FTSENT *cur, *tail;
|
---|
758 | DIR *dirp;
|
---|
759 | int adjust, cderrno, descend, len, level, nlinks, saved_errno, nostat;
|
---|
760 | size_t maxlen;
|
---|
761 | #ifdef FTS_WHITEOUT
|
---|
762 | int oflag;
|
---|
763 | #endif
|
---|
764 | char *cp = NULL; /* pacify gcc */
|
---|
765 |
|
---|
766 | _DIAGASSERT(sp != NULL);
|
---|
767 |
|
---|
768 | /* Set current node pointer. */
|
---|
769 | cur = sp->fts_cur;
|
---|
770 |
|
---|
771 | /*
|
---|
772 | * Open the directory for reading. If this fails, we're done.
|
---|
773 | * If being called from fts_read, set the fts_info field.
|
---|
774 | */
|
---|
775 | #ifdef FTS_WHITEOUT
|
---|
776 | if (ISSET(FTS_WHITEOUT))
|
---|
777 | oflag = DTF_NODUP|DTF_REWIND;
|
---|
778 | else
|
---|
779 | oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND;
|
---|
780 | #else
|
---|
781 | #define __opendir2(path, flag) opendir(path)
|
---|
782 | #endif
|
---|
783 | if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) {
|
---|
784 | if (type == BREAD) {
|
---|
785 | cur->fts_info = FTS_DNR;
|
---|
786 | cur->fts_errno = errno;
|
---|
787 | }
|
---|
788 | return (NULL);
|
---|
789 | }
|
---|
790 |
|
---|
791 | /*
|
---|
792 | * Nlinks is the number of possible entries of type directory in the
|
---|
793 | * directory if we're cheating on stat calls, 0 if we're not doing
|
---|
794 | * any stat calls at all, -1 if we're doing stats on everything.
|
---|
795 | */
|
---|
796 | if (type == BNAMES) {
|
---|
797 | nlinks = 0;
|
---|
798 | nostat = 1;
|
---|
799 | } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) {
|
---|
800 | nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2);
|
---|
801 | nostat = 1;
|
---|
802 | } else {
|
---|
803 | nlinks = -1;
|
---|
804 | nostat = 0;
|
---|
805 | }
|
---|
806 |
|
---|
807 | #ifdef notdef
|
---|
808 | (void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink);
|
---|
809 | (void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n",
|
---|
810 | ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT));
|
---|
811 | #endif
|
---|
812 | /*
|
---|
813 | * If we're going to need to stat anything or we want to descend
|
---|
814 | * and stay in the directory, chdir. If this fails we keep going,
|
---|
815 | * but set a flag so we don't chdir after the post-order visit.
|
---|
816 | * We won't be able to stat anything, but we can still return the
|
---|
817 | * names themselves. Note, that since fts_read won't be able to
|
---|
818 | * chdir into the directory, it will have to return different path
|
---|
819 | * names than before, i.e. "a/b" instead of "b". Since the node
|
---|
820 | * has already been visited in pre-order, have to wait until the
|
---|
821 | * post-order visit to return the error. There is a special case
|
---|
822 | * here, if there was nothing to stat then it's not an error to
|
---|
823 | * not be able to stat. This is all fairly nasty. If a program
|
---|
824 | * needed sorted entries or stat information, they had better be
|
---|
825 | * checking FTS_NS on the returned nodes.
|
---|
826 | */
|
---|
827 | cderrno = 0;
|
---|
828 | if (nlinks || type == BREAD) {
|
---|
829 | #ifdef HAVE_FCHDIR
|
---|
830 | if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) {
|
---|
831 | #else
|
---|
832 | if (fts_safe_changedir(sp, cur, dirfd(dirp), cur->fts_accpath)) {
|
---|
833 | #endif
|
---|
834 | if (nlinks && type == BREAD)
|
---|
835 | cur->fts_errno = errno;
|
---|
836 | cur->fts_flags |= FTS_DONTCHDIR;
|
---|
837 | descend = 0;
|
---|
838 | cderrno = errno;
|
---|
839 | } else
|
---|
840 | descend = 1;
|
---|
841 | } else
|
---|
842 | descend = 0;
|
---|
843 |
|
---|
844 | /*
|
---|
845 | * Figure out the max file name length that can be stored in the
|
---|
846 | * current path -- the inner loop allocates more path as necessary.
|
---|
847 | * We really wouldn't have to do the maxlen calculations here, we
|
---|
848 | * could do them in fts_read before returning the path, but it's a
|
---|
849 | * lot easier here since the length is part of the dirent structure.
|
---|
850 | *
|
---|
851 | * If not changing directories set a pointer so that can just append
|
---|
852 | * each new name into the path.
|
---|
853 | */
|
---|
854 | len = NAPPEND(cur);
|
---|
855 | if (ISSET(FTS_NOCHDIR)) {
|
---|
856 | cp = sp->fts_path + len;
|
---|
857 | *cp++ = '/';
|
---|
858 | }
|
---|
859 | len++;
|
---|
860 | maxlen = sp->fts_pathlen - len;
|
---|
861 |
|
---|
862 | level = cur->fts_level + 1;
|
---|
863 |
|
---|
864 | /* Read the directory, attaching each entry to the `link' pointer. */
|
---|
865 | adjust = 0;
|
---|
866 | for (head = tail = NULL, nitems = 0; (dp = readdir(dirp)) != NULL;) {
|
---|
867 | size_t dlen;
|
---|
868 |
|
---|
869 | if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
|
---|
870 | continue;
|
---|
871 |
|
---|
872 | #if HAVE_STRUCT_DIRENT_D_NAMLEN
|
---|
873 | dlen = dp->d_namlen;
|
---|
874 | #else
|
---|
875 | dlen = strlen(dp->d_name);
|
---|
876 | #endif
|
---|
877 | if ((p = fts_alloc(sp, dp->d_name, dlen)) == NULL)
|
---|
878 | goto mem1;
|
---|
879 | if (dlen >= maxlen) { /* include space for NUL */
|
---|
880 | if (fts_palloc(sp, len + dlen + 1)) {
|
---|
881 | /*
|
---|
882 | * No more memory for path or structures. Save
|
---|
883 | * errno, free up the current structure and the
|
---|
884 | * structures already allocated.
|
---|
885 | */
|
---|
886 | mem1: saved_errno = errno;
|
---|
887 | if (p)
|
---|
888 | free(p);
|
---|
889 | fts_lfree(head);
|
---|
890 | (void)closedir(dirp);
|
---|
891 | errno = saved_errno;
|
---|
892 | cur->fts_info = FTS_ERR;
|
---|
893 | SET(FTS_STOP);
|
---|
894 | return (NULL);
|
---|
895 | }
|
---|
896 | adjust = 1;
|
---|
897 | if (ISSET(FTS_NOCHDIR))
|
---|
898 | cp = sp->fts_path + len;
|
---|
899 | maxlen = sp->fts_pathlen - len;
|
---|
900 | }
|
---|
901 |
|
---|
902 | p->fts_pathlen = len + dlen;
|
---|
903 | p->fts_parent = sp->fts_cur;
|
---|
904 | p->fts_level = level;
|
---|
905 |
|
---|
906 | #ifdef FTS_WHITEOUT
|
---|
907 | if (dp->d_type == DT_WHT)
|
---|
908 | p->fts_flags |= FTS_ISW;
|
---|
909 | #endif
|
---|
910 |
|
---|
911 | if (cderrno) {
|
---|
912 | if (nlinks) {
|
---|
913 | p->fts_info = FTS_NS;
|
---|
914 | p->fts_errno = cderrno;
|
---|
915 | } else
|
---|
916 | p->fts_info = FTS_NSOK;
|
---|
917 | p->fts_accpath = cur->fts_accpath;
|
---|
918 | } else if (nlinks == 0
|
---|
919 | #ifdef DT_DIR
|
---|
920 | || (nostat &&
|
---|
921 | dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN)
|
---|
922 | #endif
|
---|
923 | ) {
|
---|
924 | p->fts_accpath =
|
---|
925 | ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
|
---|
926 | p->fts_info = FTS_NSOK;
|
---|
927 | } else {
|
---|
928 | /* Build a file name for fts_stat to stat. */
|
---|
929 | if (ISSET(FTS_NOCHDIR)) {
|
---|
930 | p->fts_accpath = p->fts_path;
|
---|
931 | memmove(cp, p->fts_name,
|
---|
932 | (size_t)(p->fts_namelen + 1));
|
---|
933 | } else
|
---|
934 | p->fts_accpath = p->fts_name;
|
---|
935 | /* Stat it. */
|
---|
936 | p->fts_info = fts_stat(sp, p, 0);
|
---|
937 |
|
---|
938 | /* Decrement link count if applicable. */
|
---|
939 | if (nlinks > 0 && (p->fts_info == FTS_D ||
|
---|
940 | p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
|
---|
941 | --nlinks;
|
---|
942 | }
|
---|
943 |
|
---|
944 | /* We walk in directory order so "ls -f" doesn't get upset. */
|
---|
945 | p->fts_link = NULL;
|
---|
946 | if (head == NULL)
|
---|
947 | head = tail = p;
|
---|
948 | else {
|
---|
949 | tail->fts_link = p;
|
---|
950 | tail = p;
|
---|
951 | }
|
---|
952 | ++nitems;
|
---|
953 | }
|
---|
954 | (void)closedir(dirp);
|
---|
955 |
|
---|
956 | /*
|
---|
957 | * If had to realloc the path, adjust the addresses for the rest
|
---|
958 | * of the tree.
|
---|
959 | */
|
---|
960 | if (adjust)
|
---|
961 | fts_padjust(sp, head);
|
---|
962 |
|
---|
963 | /*
|
---|
964 | * If not changing directories, reset the path back to original
|
---|
965 | * state.
|
---|
966 | */
|
---|
967 | if (ISSET(FTS_NOCHDIR)) {
|
---|
968 | if (cp - 1 > sp->fts_path)
|
---|
969 | --cp;
|
---|
970 | *cp = '\0';
|
---|
971 | }
|
---|
972 |
|
---|
973 | /*
|
---|
974 | * If descended after called from fts_children or after called from
|
---|
975 | * fts_read and nothing found, get back. At the root level we use
|
---|
976 | * the saved fd; if one of fts_open()'s arguments is a relative path
|
---|
977 | * to an empty directory, we wind up here with no other way back. If
|
---|
978 | * can't get back, we're done.
|
---|
979 | */
|
---|
980 | if (descend && (type == BCHILD || !nitems) &&
|
---|
981 | (cur->fts_level == FTS_ROOTLEVEL ?
|
---|
982 | #ifdef HAVE_FCHDIR
|
---|
983 | FCHDIR(sp, sp->fts_rfd) :
|
---|
984 | #else
|
---|
985 | CHDIR(sp, sp->fts_rdir) :
|
---|
986 | #endif
|
---|
987 | fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) {
|
---|
988 | cur->fts_info = FTS_ERR;
|
---|
989 | SET(FTS_STOP);
|
---|
990 | return (NULL);
|
---|
991 | }
|
---|
992 |
|
---|
993 | /* If didn't find anything, return NULL. */
|
---|
994 | if (!nitems) {
|
---|
995 | if (type == BREAD)
|
---|
996 | cur->fts_info = FTS_DP;
|
---|
997 | return (NULL);
|
---|
998 | }
|
---|
999 |
|
---|
1000 | /* Sort the entries. */
|
---|
1001 | if (sp->fts_compar && nitems > 1)
|
---|
1002 | head = fts_sort(sp, head, nitems);
|
---|
1003 | return (head);
|
---|
1004 | }
|
---|
1005 |
|
---|
1006 | static u_short
|
---|
1007 | fts_stat(sp, p, follow)
|
---|
1008 | FTS *sp;
|
---|
1009 | FTSENT *p;
|
---|
1010 | int follow;
|
---|
1011 | {
|
---|
1012 | FTSENT *t;
|
---|
1013 | dev_t dev;
|
---|
1014 | ino_t ino;
|
---|
1015 | struct STAT *sbp, sb;
|
---|
1016 | int saved_errno;
|
---|
1017 |
|
---|
1018 | _DIAGASSERT(sp != NULL);
|
---|
1019 | _DIAGASSERT(p != NULL);
|
---|
1020 |
|
---|
1021 | /* If user needs stat info, stat buffer already allocated. */
|
---|
1022 | sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
|
---|
1023 |
|
---|
1024 | #ifdef FTS_WHITEOUT
|
---|
1025 | /* check for whiteout */
|
---|
1026 | if (p->fts_flags & FTS_ISW) {
|
---|
1027 | if (sbp != &sb) {
|
---|
1028 | memset(sbp, '\0', sizeof (*sbp));
|
---|
1029 | sbp->st_mode = S_IFWHT;
|
---|
1030 | }
|
---|
1031 | return (FTS_W);
|
---|
1032 | }
|
---|
1033 | #endif
|
---|
1034 |
|
---|
1035 | /*
|
---|
1036 | * If doing a logical walk, or application requested FTS_FOLLOW, do
|
---|
1037 | * a stat(2). If that fails, check for a non-existent symlink. If
|
---|
1038 | * fail, set the errno from the stat call.
|
---|
1039 | */
|
---|
1040 | if (ISSET(FTS_LOGICAL) || follow) {
|
---|
1041 | if (stat(p->fts_accpath, sbp)) {
|
---|
1042 | saved_errno = errno;
|
---|
1043 | if (!lstat(p->fts_accpath, sbp)) {
|
---|
1044 | errno = 0;
|
---|
1045 | return (FTS_SLNONE);
|
---|
1046 | }
|
---|
1047 | p->fts_errno = saved_errno;
|
---|
1048 | goto err;
|
---|
1049 | }
|
---|
1050 | } else if (lstat(p->fts_accpath, sbp)) {
|
---|
1051 | p->fts_errno = errno;
|
---|
1052 | err: memset(sbp, 0, sizeof(struct STAT));
|
---|
1053 | return (FTS_NS);
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 | if (S_ISDIR(sbp->st_mode)) {
|
---|
1057 | /*
|
---|
1058 | * Set the device/inode. Used to find cycles and check for
|
---|
1059 | * crossing mount points. Also remember the link count, used
|
---|
1060 | * in fts_build to limit the number of stat calls. It is
|
---|
1061 | * understood that these fields are only referenced if fts_info
|
---|
1062 | * is set to FTS_D.
|
---|
1063 | */
|
---|
1064 | dev = p->fts_dev = sbp->st_dev;
|
---|
1065 | ino = p->fts_ino = sbp->st_ino;
|
---|
1066 | p->fts_nlink = sbp->st_nlink;
|
---|
1067 |
|
---|
1068 | if (ISDOT(p->fts_name))
|
---|
1069 | return (FTS_DOT);
|
---|
1070 |
|
---|
1071 | /*
|
---|
1072 | * Cycle detection is done by brute force when the directory
|
---|
1073 | * is first encountered. If the tree gets deep enough or the
|
---|
1074 | * number of symbolic links to directories is high enough,
|
---|
1075 | * something faster might be worthwhile.
|
---|
1076 | */
|
---|
1077 | for (t = p->fts_parent;
|
---|
1078 | t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
|
---|
1079 | if (ino == t->fts_ino && dev == t->fts_dev) {
|
---|
1080 | p->fts_cycle = t;
|
---|
1081 | return (FTS_DC);
|
---|
1082 | }
|
---|
1083 | return (FTS_D);
|
---|
1084 | }
|
---|
1085 | if (S_ISLNK(sbp->st_mode))
|
---|
1086 | return (FTS_SL);
|
---|
1087 | if (S_ISREG(sbp->st_mode))
|
---|
1088 | return (FTS_F);
|
---|
1089 | return (FTS_DEFAULT);
|
---|
1090 | }
|
---|
1091 |
|
---|
1092 | static FTSENT *
|
---|
1093 | fts_sort(sp, head, nitems)
|
---|
1094 | FTS *sp;
|
---|
1095 | FTSENT *head;
|
---|
1096 | size_t nitems;
|
---|
1097 | {
|
---|
1098 | FTSENT **ap, *p;
|
---|
1099 |
|
---|
1100 | _DIAGASSERT(sp != NULL);
|
---|
1101 | _DIAGASSERT(head != NULL);
|
---|
1102 |
|
---|
1103 | /*
|
---|
1104 | * Construct an array of pointers to the structures and call qsort(3).
|
---|
1105 | * Reassemble the array in the order returned by qsort. If unable to
|
---|
1106 | * sort for memory reasons, return the directory entries in their
|
---|
1107 | * current order. Allocate enough space for the current needs plus
|
---|
1108 | * 40 so don't realloc one entry at a time.
|
---|
1109 | */
|
---|
1110 | if (nitems > sp->fts_nitems) {
|
---|
1111 | FTSENT **new;
|
---|
1112 |
|
---|
1113 | new = realloc(sp->fts_array, sizeof(FTSENT *) * (nitems + 40));
|
---|
1114 | if (new == 0)
|
---|
1115 | return (head);
|
---|
1116 | sp->fts_array = new;
|
---|
1117 | sp->fts_nitems = nitems + 40;
|
---|
1118 | }
|
---|
1119 | for (ap = sp->fts_array, p = head; p; p = p->fts_link)
|
---|
1120 | *ap++ = p;
|
---|
1121 | qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *),
|
---|
1122 | (int (*)(const void *, const void *))sp->fts_compar);
|
---|
1123 | for (head = *(ap = sp->fts_array); --nitems; ++ap)
|
---|
1124 | ap[0]->fts_link = ap[1];
|
---|
1125 | ap[0]->fts_link = NULL;
|
---|
1126 | return (head);
|
---|
1127 | }
|
---|
1128 |
|
---|
1129 | static FTSENT *
|
---|
1130 | fts_alloc(sp, name, namelen)
|
---|
1131 | FTS *sp;
|
---|
1132 | const char *name;
|
---|
1133 | size_t namelen;
|
---|
1134 | {
|
---|
1135 | FTSENT *p;
|
---|
1136 | size_t len;
|
---|
1137 |
|
---|
1138 | _DIAGASSERT(sp != NULL);
|
---|
1139 | _DIAGASSERT(name != NULL);
|
---|
1140 |
|
---|
1141 | #if defined(ALIGNBYTES) && defined(ALIGN)
|
---|
1142 | /*
|
---|
1143 | * The file name is a variable length array and no stat structure is
|
---|
1144 | * necessary if the user has set the nostat bit. Allocate the FTSENT
|
---|
1145 | * structure, the file name and the stat structure in one chunk, but
|
---|
1146 | * be careful that the stat structure is reasonably aligned. Since the
|
---|
1147 | * fts_name field is declared to be of size 1, the fts_name pointer is
|
---|
1148 | * namelen + 2 before the first possible address of the stat structure.
|
---|
1149 | */
|
---|
1150 | len = sizeof(FTSENT) + namelen;
|
---|
1151 | if (!ISSET(FTS_NOSTAT))
|
---|
1152 | len += sizeof(struct STAT) + ALIGNBYTES;
|
---|
1153 | if ((p = malloc(len)) == NULL)
|
---|
1154 | return (NULL);
|
---|
1155 |
|
---|
1156 | if (!ISSET(FTS_NOSTAT))
|
---|
1157 | p->fts_statp =
|
---|
1158 | (struct STAT *)ALIGN((u_long)(p->fts_name + namelen + 2));
|
---|
1159 | #else
|
---|
1160 | (void)len;
|
---|
1161 | if ((p = malloc(sizeof(FTSENT) + namelen)) == NULL)
|
---|
1162 | return (NULL);
|
---|
1163 |
|
---|
1164 | if (!ISSET(FTS_NOSTAT))
|
---|
1165 | if ((p->fts_statp = malloc(sizeof(struct STAT))) == NULL) {
|
---|
1166 | free(p);
|
---|
1167 | return (NULL);
|
---|
1168 | }
|
---|
1169 | #endif
|
---|
1170 |
|
---|
1171 | /* Copy the name plus the trailing NULL. */
|
---|
1172 | memmove(p->fts_name, name, namelen + 1);
|
---|
1173 |
|
---|
1174 | p->fts_namelen = namelen;
|
---|
1175 | p->fts_path = sp->fts_path;
|
---|
1176 | p->fts_errno = 0;
|
---|
1177 | p->fts_flags = 0;
|
---|
1178 | p->fts_instr = FTS_NOINSTR;
|
---|
1179 | p->fts_number = 0;
|
---|
1180 | p->fts_pointer = NULL;
|
---|
1181 | return (p);
|
---|
1182 | }
|
---|
1183 |
|
---|
1184 | static void
|
---|
1185 | fts_lfree(head)
|
---|
1186 | FTSENT *head;
|
---|
1187 | {
|
---|
1188 | FTSENT *p;
|
---|
1189 |
|
---|
1190 | /* XXX: head may be NULL ? */
|
---|
1191 |
|
---|
1192 | /* Free a linked list of structures. */
|
---|
1193 | while ((p = head) != NULL) {
|
---|
1194 | head = head->fts_link;
|
---|
1195 |
|
---|
1196 | #if !defined(ALIGNBYTES) || !defined(ALIGN)
|
---|
1197 | if (p->fts_statp)
|
---|
1198 | free(p->fts_statp);
|
---|
1199 | #endif
|
---|
1200 | free(p);
|
---|
1201 | }
|
---|
1202 | }
|
---|
1203 |
|
---|
1204 | static size_t
|
---|
1205 | fts_pow2(x)
|
---|
1206 | size_t x;
|
---|
1207 | {
|
---|
1208 |
|
---|
1209 | x--;
|
---|
1210 | x |= x>>1;
|
---|
1211 | x |= x>>2;
|
---|
1212 | x |= x>>4;
|
---|
1213 | x |= x>>8;
|
---|
1214 | x |= x>>16;
|
---|
1215 | #if LONG_BIT > 32
|
---|
1216 | x |= x>>32;
|
---|
1217 | #endif
|
---|
1218 | #if LONG_BIT > 64
|
---|
1219 | x |= x>>64;
|
---|
1220 | #endif
|
---|
1221 | x++;
|
---|
1222 | return (x);
|
---|
1223 | }
|
---|
1224 |
|
---|
1225 | /*
|
---|
1226 | * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
|
---|
1227 | * Most systems will allow creation of paths much longer than MAXPATHLEN, even
|
---|
1228 | * though the kernel won't resolve them. Round up the new size to a power of 2,
|
---|
1229 | * so we don't realloc the path 2 bytes at a time.
|
---|
1230 | */
|
---|
1231 | static int
|
---|
1232 | fts_palloc(sp, size)
|
---|
1233 | FTS *sp;
|
---|
1234 | size_t size;
|
---|
1235 | {
|
---|
1236 | char *new;
|
---|
1237 |
|
---|
1238 | _DIAGASSERT(sp != NULL);
|
---|
1239 |
|
---|
1240 | #if 1
|
---|
1241 | /* Protect against fts_pathlen overflow. */
|
---|
1242 | if (size > USHRT_MAX + 1) {
|
---|
1243 | errno = ENOMEM;
|
---|
1244 | return (1);
|
---|
1245 | }
|
---|
1246 | #endif
|
---|
1247 | size = fts_pow2(size);
|
---|
1248 | new = realloc(sp->fts_path, size);
|
---|
1249 | if (new == 0)
|
---|
1250 | return (1);
|
---|
1251 | sp->fts_path = new;
|
---|
1252 | sp->fts_pathlen = size;
|
---|
1253 | return (0);
|
---|
1254 | }
|
---|
1255 |
|
---|
1256 | /*
|
---|
1257 | * When the path is realloc'd, have to fix all of the pointers in structures
|
---|
1258 | * already returned.
|
---|
1259 | */
|
---|
1260 | static void
|
---|
1261 | fts_padjust(sp, head)
|
---|
1262 | FTS *sp;
|
---|
1263 | FTSENT *head;
|
---|
1264 | {
|
---|
1265 | FTSENT *p;
|
---|
1266 | char *addr;
|
---|
1267 |
|
---|
1268 | _DIAGASSERT(sp != NULL);
|
---|
1269 |
|
---|
1270 | #define ADJUST(p) do { \
|
---|
1271 | if ((p)->fts_accpath != (p)->fts_name) \
|
---|
1272 | (p)->fts_accpath = \
|
---|
1273 | addr + ((p)->fts_accpath - (p)->fts_path); \
|
---|
1274 | (p)->fts_path = addr; \
|
---|
1275 | } while (/*CONSTCOND*/0)
|
---|
1276 |
|
---|
1277 | addr = sp->fts_path;
|
---|
1278 |
|
---|
1279 | /* Adjust the current set of children. */
|
---|
1280 | for (p = sp->fts_child; p; p = p->fts_link)
|
---|
1281 | ADJUST(p);
|
---|
1282 |
|
---|
1283 | /* Adjust the rest of the tree, including the current level. */
|
---|
1284 | for (p = head; p->fts_level >= FTS_ROOTLEVEL;) {
|
---|
1285 | ADJUST(p);
|
---|
1286 | p = p->fts_link ? p->fts_link : p->fts_parent;
|
---|
1287 | }
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 | static size_t
|
---|
1291 | fts_maxarglen(argv)
|
---|
1292 | char * const *argv;
|
---|
1293 | {
|
---|
1294 | size_t len, max;
|
---|
1295 |
|
---|
1296 | _DIAGASSERT(argv != NULL);
|
---|
1297 |
|
---|
1298 | for (max = 0; *argv; ++argv)
|
---|
1299 | if ((len = strlen(*argv)) > max)
|
---|
1300 | max = len;
|
---|
1301 | return (max + 1);
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | /*
|
---|
1305 | * Change to dir specified by fd or p->fts_accpath without getting
|
---|
1306 | * tricked by someone changing the world out from underneath us.
|
---|
1307 | * Assumes p->fts_dev and p->fts_ino are filled in.
|
---|
1308 | */
|
---|
1309 | static int
|
---|
1310 | fts_safe_changedir(sp, p, fd, path)
|
---|
1311 | const FTS *sp;
|
---|
1312 | const FTSENT *p;
|
---|
1313 | int fd;
|
---|
1314 | const char *path;
|
---|
1315 | {
|
---|
1316 | int oldfd = fd, ret = -1;
|
---|
1317 | struct STAT sb;
|
---|
1318 |
|
---|
1319 | if (ISSET(FTS_NOCHDIR))
|
---|
1320 | return 0;
|
---|
1321 |
|
---|
1322 | #ifdef HAVE_FCHDIR
|
---|
1323 | if (oldfd < 0 && (fd = open(path, O_RDONLY)) == -1)
|
---|
1324 | return -1;
|
---|
1325 |
|
---|
1326 | if (fstat(fd, &sb) == -1)
|
---|
1327 | goto bail;
|
---|
1328 | #else
|
---|
1329 | if (stat(path, &sb))
|
---|
1330 | goto bail;
|
---|
1331 | #endif
|
---|
1332 |
|
---|
1333 | if (sb.st_ino != p->fts_ino || sb.st_dev != p->fts_dev) {
|
---|
1334 | errno = ENOENT;
|
---|
1335 | goto bail;
|
---|
1336 | }
|
---|
1337 |
|
---|
1338 | #ifdef HAVE_FCHDIR
|
---|
1339 | ret = fchdir(fd);
|
---|
1340 | #else
|
---|
1341 | ret = chdir(path);
|
---|
1342 | #endif
|
---|
1343 |
|
---|
1344 | bail:
|
---|
1345 | #ifdef HAVE_FCHDIR
|
---|
1346 | if (oldfd < 0) {
|
---|
1347 | int save_errno = errno;
|
---|
1348 | (void)close(fd);
|
---|
1349 | errno = save_errno;
|
---|
1350 | }
|
---|
1351 | #endif
|
---|
1352 | return ret;
|
---|
1353 | }
|
---|