VirtualBox

source: kBuild/trunk/src/kmk/kmkbuiltin/fts.c@ 2134

Last change on this file since 2134 was 2121, checked in by bird, 16 years ago

kmk: make sure alloca.h gets included (Solaris again).

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette