]>
git.saurik.com Git - apple/libc.git/blob - gen/fts.c
2 * Copyright (c) 1999, 2000, 2003, 2005, 2008, 2012 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1990, 1993, 1994
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 #include <sys/param.h>
69 #endif /* __BLOCKS__ */
71 static FTSENT
*fts_alloc(FTS
*, char *, int);
72 static FTSENT
*fts_build(FTS
*, int);
73 static void fts_lfree(FTSENT
*);
74 static void fts_load(FTS
*, FTSENT
*);
75 static size_t fts_maxarglen(char * const *);
76 static void fts_padjust(FTS
*, void *);
77 static int fts_palloc(FTS
*, size_t);
78 static FTSENT
*fts_sort(FTS
*, FTSENT
*, int);
79 static u_short
fts_stat(FTS
*, FTSENT
*, int);
81 #define ISDOT(a) (a[0] == '.' && (!a[1] || a[1] == '.' && !a[2]))
83 #define ISSET(opt) (sp->fts_options & opt)
84 #define SET(opt) (sp->fts_options |= opt)
86 #define CHDIR(sp, path) (!ISSET(FTS_NOCHDIR) && chdir(path))
87 #define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd))
90 #define BCHILD 1 /* fts_children */
91 #define BNAMES 2 /* fts_children, names only */
92 #define BREAD 3 /* fts_read */
95 * For directories containing > 64k subdirectories (or HFS+ with > 64k files
96 * and subdirectories), struct stat's st_nlink (16 bits) will overflow. This
97 * causes the case with FTS_NOSTAT and FTS_PHYSICAL set to prematurely stop
98 * recursing into subdirectories, because of an optimization that expects
99 * st_nlink to be the number of subdirectories (once that number has been
100 * encountered, no further calls to stat should be needed).
102 * However, on Mac OS X, another optimization largely nullifies the st_nlink
103 * optimization. struct dirent contains d_type, which can distinguish
104 * directories from files without initially calling stat. So stat is only
105 * called on known directories, rather than on other files. With this
106 * optimization, the difference in also using the st_nlink optimization is
107 * pretty minimal (tests show an improvement of a percent or two, probably
108 * due to additional if statement clauses that need to be evaluated).
110 * So removing the st_nlink optimization code will fix the > 64k subdirectories
111 * problem. And if we replace the multiple if clause logic with a single
112 * switch statement, we can recover the minimal performance lose. We can
113 * go even further and for the case of FTS_NOSTAT and FTS_LOGICAL set, we
114 * can use d_type to also distinguish symbolic links, and so we only need to
115 * call stat on directories and symlinks, not on all files. This provides
116 * a significant performance boost in that special case.
119 * The following macros defines values of the dostat variable, which is or-ed
120 * with the value of d_type, and the result used in a switch statement to
121 * determine whether to call stat or not. (We order the macros to minimize
122 * the size of any jump table that the compiler may generate.)
124 #define F_SHIFT 4 /* shift to leave space for d_type */
125 #define F_NOSTAT (0 << F_SHIFT) /* don't do any stat's */
126 #define F_STATDIRSYM (1 << F_SHIFT) /* only stat directories and symlinks (and unknowns) */
127 #define F_ALWAYSSTAT (2 << F_SHIFT) /* always stat */
128 #define F_STATDIR (3 << F_SHIFT) /* only stat directories (and unknowns) */
129 #define F_D_TYPE (4 << F_SHIFT) /* only stat directories but use d_type */
130 #define F_D_TYPESYM (5 << F_SHIFT) /* only stat directories and symlinks but use d_type */
137 register FTSENT
*p
, *root
;
139 FTSENT
*parent
, *tmp
= NULL
;
142 /* Logical walks turn on NOCHDIR; symbolic links are too hard. */
143 if (ISSET(FTS_LOGICAL
))
147 * Start out with 1K of path space, and enough, in any case,
148 * to hold the user's paths.
150 if (fts_palloc(sp
, MAX(fts_maxarglen(argv
), MAXPATHLEN
)))
153 /* Allocate/initialize root's parent. */
154 if ((parent
= fts_alloc(sp
, "", 0)) == NULL
)
156 parent
->fts_level
= FTS_ROOTPARENTLEVEL
;
158 /* Allocate/initialize root(s). */
159 for (root
= NULL
, nitems
= 0; *argv
; ++argv
, ++nitems
) {
160 /* Don't allow zero-length paths. */
161 if ((len
= strlen(*argv
)) == 0) {
166 p
= fts_alloc(sp
, *argv
, len
);
167 p
->fts_level
= FTS_ROOTLEVEL
;
168 p
->fts_parent
= parent
;
169 p
->fts_accpath
= p
->fts_name
;
170 p
->fts_info
= fts_stat(sp
, p
, ISSET(FTS_COMFOLLOWDIR
) ? -1 : ISSET(FTS_COMFOLLOW
));
172 /* Command-line "." and ".." are real directories. */
173 if (p
->fts_info
== FTS_DOT
)
177 * If comparison routine supplied, traverse in sorted
178 * order; otherwise traverse in the order specified.
180 if (sp
->fts_compar
) {
193 if (sp
->fts_compar
&& nitems
> 1)
194 root
= fts_sort(sp
, root
, nitems
);
197 * Allocate a dummy pointer and make fts_read think that we've just
198 * finished the node before the root(s); set p->fts_info to FTS_INIT
199 * so that everything about the "current" node is ignored.
201 if ((sp
->fts_cur
= fts_alloc(sp
, "", 0)) == NULL
)
203 sp
->fts_cur
->fts_link
= root
;
204 sp
->fts_cur
->fts_info
= FTS_INIT
;
207 * If using chdir(2), grab a file descriptor pointing to dot to insure
208 * that we can get back here; this could be avoided for some paths,
209 * but almost certainly not worth the effort. Slashes, symbolic links,
210 * and ".." are all fairly nasty problems. Note, if we can't get the
211 * descriptor we run anyway, just more slowly.
213 if (!ISSET(FTS_NOCHDIR
) && (sp
->fts_rfd
= open(".", O_RDONLY
, 0)) < 0)
218 mem3
: fts_lfree(root
);
220 mem2
: free(sp
->fts_path
);
226 fts_open(argv
, options
, compar
)
234 if (options
& ~FTS_OPTIONMASK
) {
238 if (options
& FTS_NOSTAT_TYPE
) options
|= FTS_NOSTAT
;
240 /* Allocate/initialize the stream */
241 if ((sp
= malloc((u_int
)sizeof(FTS
))) == NULL
)
243 memset(sp
, 0, sizeof(FTS
));
244 sp
->fts_compar
= compar
;
245 sp
->fts_options
= options
;
247 return __fts_open(argv
, sp
);
252 fts_open_b(argv
, options
, compar
)
255 int (^compar
)(const FTSENT
**, const FTSENT
**);
260 if (options
& ~FTS_OPTIONMASK
) {
264 if (options
& FTS_NOSTAT_TYPE
) options
|= FTS_NOSTAT
;
266 /* Allocate/initialize the stream */
267 if ((sp
= malloc((u_int
)sizeof(FTS
))) == NULL
)
269 memset(sp
, 0, sizeof(FTS
));
270 sp
->fts_compar_b
= (int (^)())Block_copy(compar
);
271 sp
->fts_options
= options
| FTS_BLOCK_COMPAR
;
273 return __fts_open(argv
, sp
);
275 #endif /* __BLOCKS__ */
286 * Load the stream structure for the next traversal. Since we don't
287 * actually enter the directory until after the preorder visit, set
288 * the fts_accpath field specially so the chdir gets done to the right
289 * place and the user can access the first node. From fts_open it's
290 * known that the path will fit.
292 len
= p
->fts_pathlen
= p
->fts_namelen
;
293 memmove(sp
->fts_path
, p
->fts_name
, len
+ 1);
294 if ((cp
= strrchr(p
->fts_name
, '/')) && (cp
!= p
->fts_name
|| cp
[1])) {
296 memmove(p
->fts_name
, cp
, len
+ 1);
297 p
->fts_namelen
= len
;
299 p
->fts_accpath
= p
->fts_path
= sp
->fts_path
;
300 sp
->fts_dev
= p
->fts_dev
;
307 register FTSENT
*freep
, *p
;
311 * This still works if we haven't read anything -- the dummy structure
312 * points to the root list, so we step through to the end of the root
313 * list which has a valid parent pointer.
316 for (p
= sp
->fts_cur
; p
->fts_level
>= FTS_ROOTLEVEL
;) {
318 p
= p
->fts_link
? p
->fts_link
: p
->fts_parent
;
324 /* Free up child linked list, sort array, path buffer. */
326 fts_lfree(sp
->fts_child
);
331 /* Return to original directory, save errno if necessary. */
332 if (!ISSET(FTS_NOCHDIR
)) {
333 if (fchdir(sp
->fts_rfd
)) {
336 (void)close(sp
->fts_rfd
);
340 /* Free up any block pointer. */
341 if (ISSET(FTS_BLOCK_COMPAR
) && sp
->fts_compar_b
!= NULL
)
342 Block_release(sp
->fts_compar_b
);
343 #endif /* __BLOCKS__ */
345 /* Free up the stream pointer. */
348 /* Set errno and return. */
357 * Special case a root of "/" so that slashes aren't appended which would
358 * cause paths to be written as "//foo".
361 (p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 && \
362 p->fts_path[0] == '/' ? 0 : p->fts_pathlen)
368 register FTSENT
*p
, *tmp
;
373 /* If finished or unrecoverable error, return NULL. */
374 if (sp
->fts_cur
== NULL
|| ISSET(FTS_STOP
))
377 /* Set current node pointer. */
380 /* Save and zero out user instructions. */
381 instr
= p
->fts_instr
;
382 p
->fts_instr
= FTS_NOINSTR
;
384 /* Any type of file may be re-visited; re-stat and re-turn. */
385 if (instr
== FTS_AGAIN
) {
386 p
->fts_info
= fts_stat(sp
, p
, 0);
391 * Following a symlink -- SLNONE test allows application to see
392 * SLNONE and recover. If indirecting through a symlink, have
393 * keep a pointer to current location. If unable to get that
394 * pointer, follow fails.
396 if (instr
== FTS_FOLLOW
&&
397 (p
->fts_info
== FTS_SL
|| p
->fts_info
== FTS_SLNONE
)) {
398 p
->fts_info
= fts_stat(sp
, p
, 1);
399 if (p
->fts_info
== FTS_D
&& !ISSET(FTS_NOCHDIR
)) {
400 if ((p
->fts_symfd
= open(".", O_RDONLY
, 0)) < 0) {
401 p
->fts_errno
= errno
;
402 p
->fts_info
= FTS_ERR
;
404 p
->fts_flags
|= FTS_SYMFOLLOW
;
410 /* Directory in pre-order. */
411 if (p
->fts_info
== FTS_D
) {
412 /* If skipped or crossed mount point, do post-order visit. */
413 if (instr
== FTS_SKIP
||
414 (ISSET(FTS_XDEV
) && p
->fts_dev
!= sp
->fts_dev
)) {
415 if (p
->fts_flags
& FTS_SYMFOLLOW
)
416 (void)close(p
->fts_symfd
);
418 fts_lfree(sp
->fts_child
);
419 sp
->fts_child
= NULL
;
421 p
->fts_info
= FTS_DP
;
425 /* Rebuild if only read the names and now traversing. */
426 if (sp
->fts_child
&& sp
->fts_options
& FTS_NAMEONLY
) {
427 sp
->fts_options
&= ~FTS_NAMEONLY
;
428 fts_lfree(sp
->fts_child
);
429 sp
->fts_child
= NULL
;
433 * Cd to the subdirectory.
435 * If have already read and now fail to chdir, whack the list
436 * to make the names come out right, and set the parent errno
437 * so the application will eventually get an error condition.
438 * Set the FTS_DONTCHDIR flag so that when we logically change
439 * directories back to the parent we don't do a chdir.
441 * If haven't read do so. If the read fails, fts_build sets
442 * FTS_STOP or the fts_info field of the node.
445 if (CHDIR(sp
, p
->fts_accpath
)) {
446 p
->fts_errno
= errno
;
447 p
->fts_flags
|= FTS_DONTCHDIR
;
448 for (p
= sp
->fts_child
; p
; p
= p
->fts_link
)
450 p
->fts_parent
->fts_accpath
;
452 } else if ((sp
->fts_child
= fts_build(sp
, BREAD
)) == NULL
) {
458 sp
->fts_child
= NULL
;
462 /* Move to the next node on this level. */
464 if ((p
= p
->fts_link
)) {
466 * If reached the top, return to the original directory, and
467 * load the paths for the next root.
469 if (p
->fts_level
== FTS_ROOTLEVEL
) {
470 if (!ISSET(FTS_NOCHDIR
) && FCHDIR(sp
, sp
->fts_rfd
)) {
476 return (sp
->fts_cur
= p
);
480 * User may have called fts_set on the node. If skipped,
481 * ignore. If followed, get a file descriptor so we can
482 * get back if necessary.
484 if (p
->fts_instr
== FTS_SKIP
) {
488 if (p
->fts_instr
== FTS_FOLLOW
) {
489 p
->fts_info
= fts_stat(sp
, p
, 1);
490 if (p
->fts_info
== FTS_D
&& !ISSET(FTS_NOCHDIR
)) {
492 open(".", O_RDONLY
, 0)) < 0) {
493 p
->fts_errno
= errno
;
494 p
->fts_info
= FTS_ERR
;
496 p
->fts_flags
|= FTS_SYMFOLLOW
;
499 p
->fts_instr
= FTS_NOINSTR
;
503 name
: t
= sp
->fts_path
+ NAPPEND(p
->fts_parent
);
505 memmove(t
, p
->fts_name
, p
->fts_namelen
+ 1);
506 return (sp
->fts_cur
= p
);
509 /* Move up to the parent node. */
512 if (p
->fts_level
== FTS_ROOTPARENTLEVEL
) {
514 * Done; free everything up and set errno to 0 so the user
515 * can distinguish between error and EOF.
520 return (sp
->fts_cur
= NULL
);
523 /* Nul terminate the pathname. */
524 sp
->fts_path
[p
->fts_pathlen
] = '\0';
527 * Return to the parent directory. If at a root node or came through
528 * a symlink, go back through the file descriptor. Otherwise, cd up
531 if (p
->fts_level
== FTS_ROOTLEVEL
) {
532 if (!ISSET(FTS_NOCHDIR
) && FCHDIR(sp
, sp
->fts_rfd
)) {
536 } else if (p
->fts_flags
& FTS_SYMFOLLOW
) {
537 if (FCHDIR(sp
, p
->fts_symfd
)) {
539 (void)close(p
->fts_symfd
);
544 (void)close(p
->fts_symfd
);
545 } else if (!(p
->fts_flags
& FTS_DONTCHDIR
)) {
546 if (CHDIR(sp
, "..")) {
552 p
->fts_info
= p
->fts_errno
? FTS_ERR
: FTS_DP
;
553 return (sp
->fts_cur
= p
);
557 * Fts_set takes the stream as an argument although it's not used in this
558 * implementation; it would be necessary if anyone wanted to add global
559 * semantics to fts using fts_set. An error return is allowed for similar
564 fts_set(sp
, p
, instr
)
569 if (instr
&& instr
!= FTS_AGAIN
&& instr
!= FTS_FOLLOW
&&
570 instr
!= FTS_NOINSTR
&& instr
!= FTS_SKIP
) {
574 p
->fts_instr
= instr
;
579 fts_children(sp
, instr
)
586 if (instr
&& instr
!= FTS_NAMEONLY
) {
591 /* Set current node pointer. */
595 * Errno set to 0 so user can distinguish empty directory from
600 /* Fatal errors stop here. */
604 /* Return logical hierarchy of user's arguments. */
605 if (p
->fts_info
== FTS_INIT
)
606 return (p
->fts_link
);
609 * If not a directory being visited in pre-order, stop here. Could
610 * allow FTS_DNR, assuming the user has fixed the problem, but the
611 * same effect is available with FTS_AGAIN.
613 if (p
->fts_info
!= FTS_D
/* && p->fts_info != FTS_DNR */)
616 /* Free up any previous child list. */
618 fts_lfree(sp
->fts_child
);
620 if (instr
== FTS_NAMEONLY
) {
621 sp
->fts_options
|= FTS_NAMEONLY
;
627 * If using chdir on a relative path and called BEFORE fts_read does
628 * its chdir to the root of a traversal, we can lose -- we need to
629 * chdir into the subdirectory, and we don't know where the current
630 * directory is, so we can't get back so that the upcoming chdir by
631 * fts_read will work.
633 if (p
->fts_level
!= FTS_ROOTLEVEL
|| p
->fts_accpath
[0] == '/' ||
635 return (sp
->fts_child
= fts_build(sp
, instr
));
637 if ((fd
= open(".", O_RDONLY
, 0)) < 0)
639 sp
->fts_child
= fts_build(sp
, instr
);
643 return (sp
->fts_child
);
647 * This is the tricky part -- do not casually change *anything* in here. The
648 * idea is to build the linked list of entries that are used by fts_children
649 * and fts_read. There are lots of special cases.
651 * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is
652 * set, we can use d_type to determine if the entry is a directory (or for
653 * logical walks, a directory or symlink) and not call stat for other file
654 * types. This cuts the number of stat calls significantly.
661 register struct dirent
*dp
;
662 register FTSENT
*p
, *head
;
667 int cderrno
, descend
, len
, level
, maxlen
, dostat
, oflag
, saved_errno
;
670 /* Set current node pointer. */
674 * Open the directory for reading. If this fails, we're done.
675 * If being called from fts_read, set the fts_info field.
678 if (ISSET(FTS_WHITEOUT
))
679 oflag
= DTF_NODUP
|DTF_REWIND
;
681 oflag
= DTF_HIDEW
|DTF_NODUP
|DTF_REWIND
;
683 #define __opendir2(path, flag) opendir(path)
685 if ((dirp
= __opendir2(cur
->fts_accpath
, oflag
)) == NULL
) {
687 cur
->fts_info
= FTS_DNR
;
688 cur
->fts_errno
= errno
;
695 else if (ISSET(FTS_NOSTAT_TYPE
))
696 dostat
= ISSET(FTS_PHYSICAL
) ? F_D_TYPE
: F_D_TYPESYM
;
697 else if (ISSET(FTS_NOSTAT
))
698 dostat
= ISSET(FTS_PHYSICAL
) ? F_STATDIR
: F_STATDIRSYM
;
700 dostat
= F_ALWAYSSTAT
;
703 (void)printf("dostat == %d\n", dostat
);
704 (void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n",
705 ISSET(FTS_NOSTAT
), ISSET(FTS_PHYSICAL
), ISSET(FTS_SEEDOT
));
708 * If we're going to need to stat anything or we want to descend
709 * and stay in the directory, chdir. If this fails we keep going,
710 * but set a flag so we don't chdir after the post-order visit.
711 * We won't be able to stat anything, but we can still return the
712 * names themselves. Note, that since fts_read won't be able to
713 * chdir into the directory, it will have to return different path
714 * names than before, i.e. "a/b" instead of "b". Since the node
715 * has already been visited in pre-order, have to wait until the
716 * post-order visit to return the error. There is a special case
717 * here, if there was nothing to stat then it's not an error to
718 * not be able to stat. This is all fairly nasty. If a program
719 * needed sorted entries or stat information, they had better be
720 * checking FTS_NS on the returned nodes.
723 if (dostat
|| type
== BREAD
)
724 if (FCHDIR(sp
, dirfd(dirp
))) {
725 if (dostat
&& type
== BREAD
)
726 cur
->fts_errno
= errno
;
727 cur
->fts_flags
|= FTS_DONTCHDIR
;
736 * Figure out the max file name length that can be stored in the
737 * current path -- the inner loop allocates more path as necessary.
738 * We really wouldn't have to do the maxlen calculations here, we
739 * could do them in fts_read before returning the path, but it's a
740 * lot easier here since the length is part of the dirent structure.
742 * If not changing directories set a pointer so that can just append
743 * each new name into the path.
746 if (ISSET(FTS_NOCHDIR
)) {
747 cp
= sp
->fts_path
+ len
;
751 maxlen
= sp
->fts_pathlen
- len
;
753 level
= cur
->fts_level
+ 1;
755 /* Read the directory, attaching each entry to the `link' pointer. */
757 for (head
= tail
= NULL
, nitems
= 0; (dp
= readdir(dirp
)) ; ) {
758 if (!ISSET(FTS_SEEDOT
) && ISDOT(dp
->d_name
))
761 if ((p
= fts_alloc(sp
, dp
->d_name
, (int)dp
->d_namlen
)) == NULL
)
763 if (dp
->d_namlen
>= maxlen
) { /* include space for NUL */
764 if (fts_palloc(sp
, (size_t)dp
->d_namlen
)) {
766 * No more memory for path or structures. Save
767 * errno, free up the current structure and the
768 * structures already allocated.
770 mem1
: saved_errno
= errno
;
774 (void)closedir(dirp
);
775 cur
->fts_info
= FTS_ERR
;
780 adjaddr
= sp
->fts_path
;
781 maxlen
= sp
->fts_pathlen
- sp
->fts_cur
->fts_pathlen
- 1;
784 p
->fts_level
= level
;
785 p
->fts_parent
= sp
->fts_cur
;
786 p
->fts_pathlen
= len
+ dp
->d_namlen
;
789 if (dp
->d_type
== DT_WHT
)
790 p
->fts_flags
|= FTS_ISW
;
795 p
->fts_info
= FTS_NS
;
796 p
->fts_errno
= cderrno
;
798 p
->fts_info
= FTS_NSOK
;
799 p
->fts_accpath
= cur
->fts_accpath
;
802 * We need to know all file types values that d_type may
803 * be set to. So if that changes, the following needs
804 * to be modified appropriately.
806 switch(dostat
| dp
->d_type
) {
807 case (F_STATDIR
| DT_UNKNOWN
):
808 case (F_STATDIR
| DT_DIR
):
809 case (F_STATDIRSYM
| DT_UNKNOWN
):
810 case (F_STATDIRSYM
| DT_DIR
):
811 case (F_STATDIRSYM
| DT_LNK
):
812 case (F_ALWAYSSTAT
| DT_UNKNOWN
):
813 case (F_ALWAYSSTAT
| DT_FIFO
):
814 case (F_ALWAYSSTAT
| DT_CHR
):
815 case (F_ALWAYSSTAT
| DT_DIR
):
816 case (F_ALWAYSSTAT
| DT_BLK
):
817 case (F_ALWAYSSTAT
| DT_REG
):
818 case (F_ALWAYSSTAT
| DT_LNK
):
819 case (F_ALWAYSSTAT
| DT_SOCK
):
820 case (F_ALWAYSSTAT
| DT_WHT
):
821 case (F_D_TYPE
| DT_UNKNOWN
):
822 case (F_D_TYPE
| DT_DIR
):
823 case (F_D_TYPESYM
| DT_UNKNOWN
):
824 case (F_D_TYPESYM
| DT_DIR
):
825 case (F_D_TYPESYM
| DT_LNK
):
826 /* Build a file name for fts_stat to stat. */
827 if (ISSET(FTS_NOCHDIR
)) {
828 p
->fts_accpath
= p
->fts_path
;
829 memmove(cp
, p
->fts_name
, p
->fts_namelen
+ 1);
831 p
->fts_accpath
= p
->fts_name
;
833 p
->fts_info
= fts_stat(sp
, p
, 0);
835 case (F_D_TYPE
| DT_FIFO
):
836 case (F_D_TYPE
| DT_CHR
):
837 case (F_D_TYPE
| DT_BLK
):
838 case (F_D_TYPE
| DT_SOCK
):
839 case (F_D_TYPESYM
| DT_FIFO
):
840 case (F_D_TYPESYM
| DT_CHR
):
841 case (F_D_TYPESYM
| DT_BLK
):
842 case (F_D_TYPESYM
| DT_SOCK
):
843 p
->fts_info
= FTS_DEFAULT
;
845 case (F_D_TYPE
| DT_REG
):
846 case (F_D_TYPESYM
| DT_REG
):
849 case (F_D_TYPE
| DT_LNK
):
850 p
->fts_info
= FTS_SL
;
852 case (F_D_TYPE
| DT_WHT
):
853 case (F_D_TYPESYM
| DT_WHT
):
857 /* No stat necessary */
858 p
->fts_info
= FTS_NSOK
;
861 ISSET(FTS_NOCHDIR
) ? p
->fts_path
: p
->fts_name
;
866 /* We walk in directory order so "ls -f" doesn't get upset. */
876 (void)closedir(dirp
);
879 * If had to realloc the path, adjust the addresses for the rest
883 fts_padjust(sp
, adjaddr
);
886 * If not changing directories, reset the path back to original
889 if (ISSET(FTS_NOCHDIR
)) {
890 if (cp
- 1 > sp
->fts_path
)
896 * If descended after called from fts_children or after called from
897 * fts_read and nothing found, get back. At the root level we use
898 * the saved fd; if one of fts_open()'s arguments is a relative path
899 * to an empty directory, we wind up here with no other way back. If
900 * can't get back, we're done.
902 if (descend
&& (type
== BCHILD
|| !nitems
) &&
903 (cur
->fts_level
== FTS_ROOTLEVEL
?
904 FCHDIR(sp
, sp
->fts_rfd
) : CHDIR(sp
, ".."))) {
905 cur
->fts_info
= FTS_ERR
;
910 /* If didn't find anything, return NULL. */
913 cur
->fts_info
= FTS_DP
;
917 /* Sort the entries. */
918 if (sp
->fts_compar
&& nitems
> 1)
919 head
= fts_sort(sp
, head
, nitems
);
924 fts_stat(sp
, p
, follow
)
932 struct stat
*sbp
, sb
;
935 /* If user needs stat info, stat buffer already allocated. */
936 sbp
= ISSET(FTS_NOSTAT
) ? &sb
: p
->fts_statp
;
939 /* check for whiteout */
940 if (p
->fts_flags
& FTS_ISW
) {
942 memset(sbp
, '\0', sizeof (*sbp
));
943 sbp
->st_mode
= S_IFWHT
;
950 * If doing a logical walk, or application requested FTS_FOLLOW, do
951 * a stat(2). If that fails, check for a non-existent symlink. If
952 * fail, set the errno from the stat call.
954 if (ISSET(FTS_LOGICAL
) || follow
) {
955 if (stat(p
->fts_accpath
, sbp
)) {
957 if (!lstat(p
->fts_accpath
, sbp
)) {
958 if (saved_errno
== ELOOP
)
959 p
->fts_errno
= ELOOP
;
963 p
->fts_errno
= saved_errno
;
967 * For FTS_COMFOLLOWDIR, drop back to lstat unless we have
970 if (follow
== -1 && !S_ISDIR(sbp
->st_mode
)) {
971 if (lstat(p
->fts_accpath
, sbp
)) {
972 p
->fts_errno
= errno
;
976 } else if (lstat(p
->fts_accpath
, sbp
)) {
977 p
->fts_errno
= errno
;
978 err
: memset(sbp
, 0, sizeof(struct stat
));
982 if (S_ISDIR(sbp
->st_mode
)) {
984 * Set the device/inode. Used to find cycles and check for
985 * crossing mount points. Also remember the link count, used
986 * in fts_build to limit the number of stat calls. It is
987 * understood that these fields are only referenced if fts_info
990 dev
= p
->fts_dev
= sbp
->st_dev
;
991 ino
= p
->fts_ino
= sbp
->st_ino
;
992 p
->fts_nlink
= sbp
->st_nlink
;
994 if (ISDOT(p
->fts_name
))
998 * Cycle detection is done by brute force when the directory
999 * is first encountered. If the tree gets deep enough or the
1000 * number of symbolic links to directories is high enough,
1001 * something faster might be worthwhile.
1003 for (t
= p
->fts_parent
;
1004 t
->fts_level
>= FTS_ROOTLEVEL
; t
= t
->fts_parent
)
1005 if (ino
== t
->fts_ino
&& dev
== t
->fts_dev
) {
1011 if (S_ISLNK(sbp
->st_mode
))
1013 if (S_ISREG(sbp
->st_mode
))
1015 return (FTS_DEFAULT
);
1019 fts_sort(sp
, head
, nitems
)
1022 register int nitems
;
1024 register FTSENT
**ap
, *p
;
1027 * Construct an array of pointers to the structures and call qsort(3).
1028 * Reassemble the array in the order returned by qsort. If unable to
1029 * sort for memory reasons, return the directory entries in their
1030 * current order. Allocate enough space for the current needs plus
1031 * 40 so don't realloc one entry at a time.
1033 if (nitems
> sp
->fts_nitems
) {
1034 sp
->fts_nitems
= nitems
+ 40;
1035 if ((sp
->fts_array
= realloc(sp
->fts_array
,
1036 (size_t)(sp
->fts_nitems
* sizeof(FTSENT
*)))) == NULL
) {
1041 for (ap
= sp
->fts_array
, p
= head
; p
; p
= p
->fts_link
)
1044 if (ISSET(FTS_BLOCK_COMPAR
))
1045 qsort_b((void *)sp
->fts_array
, nitems
, sizeof(FTSENT
*), (int (^)(const void *, const void *))sp
->fts_compar_b
);
1047 #endif /* __BLOCKS__ */
1048 qsort((void *)sp
->fts_array
, nitems
, sizeof(FTSENT
*), sp
->fts_compar
);
1049 for (head
= *(ap
= sp
->fts_array
); --nitems
; ++ap
)
1050 ap
[0]->fts_link
= ap
[1];
1051 ap
[0]->fts_link
= NULL
;
1056 fts_alloc(sp
, name
, namelen
)
1059 register int namelen
;
1065 * The file name is a variable length array and no stat structure is
1066 * necessary if the user has set the nostat bit. Allocate the FTSENT
1067 * structure, the file name and the stat structure in one chunk, but
1068 * be careful that the stat structure is reasonably aligned. Since the
1069 * fts_name field is declared to be of size 1, the fts_name pointer is
1070 * namelen + 2 before the first possible address of the stat structure.
1072 len
= sizeof(FTSENT
) + namelen
;
1073 if (!ISSET(FTS_NOSTAT
))
1074 len
+= sizeof(struct stat
) + ALIGNBYTES
;
1075 if ((p
= malloc(len
)) == NULL
)
1078 /* Copy the name plus the trailing NULL. */
1079 memmove(p
->fts_name
, name
, namelen
+ 1);
1081 if (!ISSET(FTS_NOSTAT
))
1082 p
->fts_statp
= (struct stat
*)ALIGN(p
->fts_name
+ namelen
+ 2);
1083 p
->fts_namelen
= namelen
;
1084 p
->fts_path
= sp
->fts_path
;
1087 p
->fts_instr
= FTS_NOINSTR
;
1089 p
->fts_pointer
= NULL
;
1095 register FTSENT
*head
;
1099 /* Free a linked list of structures. */
1100 while ((p
= head
)) {
1101 head
= head
->fts_link
;
1107 * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
1108 * Most systems will allow creation of paths much longer than MAXPATHLEN, even
1109 * though the kernel won't resolve them. Add the size (not just what's needed)
1110 * plus 256 bytes so don't realloc the path 2 bytes at a time.
1113 fts_palloc(sp
, more
)
1117 sp
->fts_pathlen
+= more
+ 256;
1118 sp
->fts_path
= realloc(sp
->fts_path
, (size_t)sp
->fts_pathlen
);
1119 return (sp
->fts_path
== NULL
);
1123 * When the path is realloc'd, have to fix all of the pointers in structures
1127 fts_padjust(FTS
*sp
, void *addr
)
1131 #define ADJUST(p) do { \
1132 if ((p)->fts_accpath != (p)->fts_name) { \
1133 (p)->fts_accpath = \
1134 (char *)addr + ((p)->fts_accpath - (p)->fts_path); \
1136 (p)->fts_path = addr; \
1138 /* Adjust the current set of children. */
1139 for (p
= sp
->fts_child
; p
; p
= p
->fts_link
)
1142 /* Adjust the rest of the tree. */
1143 for (p
= sp
->fts_cur
; p
->fts_level
>= FTS_ROOTLEVEL
;) {
1145 p
= p
->fts_link
? p
->fts_link
: p
->fts_parent
;
1155 for (max
= 0; *argv
; ++argv
)
1156 if ((len
= strlen(*argv
)) > max
)