2 * Copyright (c) 1999, 2000, 2003, 2005, 2008, 2012, 2016 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. Neither the name of the University nor the names of its contributors
36 * may be used to endorse or promote products derived from this software
37 * without specific prior written permission.
39 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
40 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
43 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 /* $OpenBSD: fts.c,v 1.51 2015/09/12 13:32:24 guenther Exp $ */
54 #include <sys/param.h>
66 #include <sys/vnode.h>
68 #include <os/assumes.h>
72 #endif /* __BLOCKS__ */
73 #include <malloc_private.h>
75 #pragma clang diagnostic push
76 #pragma clang diagnostic ignored "-Wstrict-prototypes"
78 static FTSENT
*fts_alloc(FTS
*, char *, ssize_t
);
79 static FTSENT
*fts_build(FTS
*, int);
80 static void fts_lfree(FTSENT
*);
81 static void fts_load(FTS
*, FTSENT
*);
82 static size_t fts_maxarglen(char * const *);
83 static void fts_padjust(FTS
*, FTSENT
*);
84 static int fts_palloc(FTS
*, size_t);
85 static FTSENT
*fts_sort(FTS
*, FTSENT
*, int);
86 static u_short
fts_stat(FTS
*, FTSENT
*, int, int);
87 static u_short
fts_stat2(FTS
*, FTSENT
*, int, int, struct stat
*);
88 static int fts_safe_changedir(FTS
*, FTSENT
*, int, char *);
90 #define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
92 #define CLR(opt) (sp->fts_options &= ~(opt))
93 #define ISSET(opt) (sp->fts_options & (opt))
94 #define SET(opt) (sp->fts_options |= (opt))
96 #define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd))
99 #define BCHILD 1 /* fts_children */
100 #define BNAMES 2 /* fts_children, names only */
101 #define BREAD 3 /* fts_read */
104 * For directories containing > 64k subdirectories (or HFS+ with > 64k files
105 * and subdirectories), struct stat's st_nlink (16 bits) will overflow. This
106 * causes the case with FTS_NOSTAT and FTS_PHYSICAL set to prematurely stop
107 * recursing into subdirectories, because of an optimization that expects
108 * st_nlink to be the number of subdirectories (once that number has been
109 * encountered, no further calls to stat should be needed).
111 * However, on Mac OS X, another optimization largely nullifies the st_nlink
112 * optimization. struct dirent contains d_type, which can distinguish
113 * directories from files without initially calling stat. So stat is only
114 * called on known directories, rather than on other files. With this
115 * optimization, the difference in also using the st_nlink optimization is
116 * pretty minimal (tests show an improvement of a percent or two, probably
117 * due to additional if statement clauses that need to be evaluated).
119 * So removing the st_nlink optimization code will fix the > 64k subdirectories
120 * problem. And if we replace the multiple if clause logic with a single
121 * switch statement, we can recover the minimal performance lose. We can
122 * go even further and for the case of FTS_NOSTAT and FTS_LOGICAL set, we
123 * can use d_type to also distinguish symbolic links, and so we only need to
124 * call stat on directories and symlinks, not on all files. This provides
125 * a significant performance boost in that special case.
128 * The following macros defines values of the dostat variable, which is or-ed
129 * with the value of d_type, and the result used in a switch statement to
130 * determine whether to call stat or not. (We order the macros to minimize
131 * the size of any jump table that the compiler may generate.)
133 #define F_SHIFT 4 /* shift to leave space for d_type */
134 #define F_NOSTAT (0 << F_SHIFT) /* don't do any stat's */
135 #define F_STATDIRSYM (1 << F_SHIFT) /* only stat directories and symlinks (and unknowns) */
136 #define F_ALWAYSSTAT (2 << F_SHIFT) /* always stat */
137 #define F_STATDIR (3 << F_SHIFT) /* only stat directories (and unknowns) */
138 #define F_D_TYPE (4 << F_SHIFT) /* only stat directories but use d_type */
139 #define F_D_TYPESYM (5 << F_SHIFT) /* only stat directories and symlinks but use d_type */
142 __fts_open(char * const *argv
, FTS
*sp
)
146 FTSENT
*parent
, *tmp
;
149 /* Logical walks turn on NOCHDIR; symbolic links are too hard. */
150 if (ISSET(FTS_LOGICAL
))
154 * Start out with 1K of path space, and enough, in any case,
155 * to hold the user's paths.
157 if (fts_palloc(sp
, MAX(fts_maxarglen(argv
), PATH_MAX
)))
160 /* Allocate/initialize root's parent. */
161 if ((parent
= fts_alloc(sp
, "", 0)) == NULL
)
163 parent
->fts_level
= FTS_ROOTPARENTLEVEL
;
165 /* Allocate/initialize root(s). */
166 for (root
= NULL
, nitems
= 0; *argv
; ++argv
, ++nitems
) {
168 if ((p
= fts_alloc(sp
, *argv
, len
)) == NULL
)
170 p
->fts_level
= FTS_ROOTLEVEL
;
171 p
->fts_parent
= parent
;
172 p
->fts_accpath
= p
->fts_name
;
173 p
->fts_info
= fts_stat(sp
, p
, ISSET(FTS_COMFOLLOWDIR
) ? -1 : ISSET(FTS_COMFOLLOW
), -1);
175 /* Command-line "." and ".." are real directories. */
176 if (p
->fts_info
== FTS_DOT
)
180 * If comparison routine supplied, traverse in sorted
181 * order; otherwise traverse in the order specified.
183 if (sp
->fts_compar
) {
196 if (sp
->fts_compar
&& nitems
> 1)
197 root
= fts_sort(sp
, root
, nitems
);
200 * Allocate a dummy pointer and make fts_read think that we've just
201 * finished the node before the root(s); set p->fts_info to FTS_INIT
202 * so that everything about the "current" node is ignored.
204 if ((sp
->fts_cur
= fts_alloc(sp
, "", 0)) == NULL
)
206 sp
->fts_cur
->fts_link
= root
;
207 sp
->fts_cur
->fts_info
= FTS_INIT
;
210 * If using chdir(2), grab a file descriptor pointing to dot to ensure
211 * that we can get back here; this could be avoided for some paths,
212 * but almost certainly not worth the effort. Slashes, symbolic links,
213 * and ".." are all fairly nasty problems. Note, if we can't get the
214 * descriptor we run anyway, just more slowly.
216 if (!ISSET(FTS_NOCHDIR
) &&
217 (sp
->fts_rfd
= open(".", O_RDONLY
| O_CLOEXEC
)) < 0)
225 mem3
: fts_lfree(root
);
227 mem2
: free(sp
->fts_path
);
233 fts_open(char * const *argv
, int options
, int (*compar
)())
238 if (options
& ~FTS_OPTIONMASK
) {
242 if (options
& FTS_NOSTAT_TYPE
) options
|= FTS_NOSTAT
;
244 /* Allocate/initialize the stream */
245 if ((sp
= calloc(1, sizeof(FTS
))) == NULL
)
247 sp
->fts_compar
= compar
;
248 sp
->fts_options
= options
;
250 return __fts_open(argv
, sp
);
255 fts_open_b(char * const *argv
, int options
, 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
= calloc(1, sizeof(FTS
))) == NULL
)
269 sp
->fts_compar_b
= (int (^)())Block_copy(compar
);
270 sp
->fts_options
= options
| FTS_BLOCK_COMPAR
;
272 return __fts_open(argv
, sp
);
274 #endif /* __BLOCKS__ */
277 fts_load(FTS
*sp
, FTSENT
*p
)
283 * Load the stream structure for the next traversal. Since we don't
284 * actually enter the directory until after the preorder visit, set
285 * the fts_accpath field specially so the chdir gets done to the right
286 * place and the user can access the first node. From fts_open it's
287 * known that the path will fit.
289 len
= p
->fts_pathlen
= p
->fts_namelen
;
290 memmove(sp
->fts_path
, p
->fts_name
, len
+ 1);
291 if ((cp
= strrchr(p
->fts_name
, '/')) && (cp
!= p
->fts_name
|| cp
[1])) {
293 memmove(p
->fts_name
, cp
, len
+ 1);
294 p
->fts_namelen
= len
;
296 p
->fts_accpath
= p
->fts_path
= sp
->fts_path
;
297 sp
->fts_dev
= p
->fts_dev
;
307 * This still works if we haven't read anything -- the dummy structure
308 * points to the root list, so we step through to the end of the root
309 * list which has a valid parent pointer.
312 for (p
= sp
->fts_cur
; p
->fts_level
>= FTS_ROOTLEVEL
;) {
314 p
= p
->fts_link
? p
->fts_link
: p
->fts_parent
;
320 /* Stash the original directory fd if needed. */
321 rfd
= ISSET(FTS_NOCHDIR
) ? -1 : sp
->fts_rfd
;
323 /* Free up child linked list, sort array, path buffer, stream ptr.*/
325 fts_lfree(sp
->fts_child
);
327 free(sp
->fts_array
); sp
->fts_array
= NULL
;
328 free(sp
->fts_path
); sp
->fts_path
= NULL
;
331 /* Free up any block pointer. */
332 if (ISSET(FTS_BLOCK_COMPAR
) && sp
->fts_compar_b
!= NULL
)
333 Block_release(sp
->fts_compar_b
);
334 #endif /* __BLOCKS__ */
336 /* Free up the stream pointer. */
339 /* Return to original directory, checking for error. */
341 int saved_errno
= errno
;
342 if (fchdir(rfd
) != 0){
346 if (close(rfd
) != 0){
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)
372 /* If finished or unrecoverable error, return NULL. */
373 if (sp
->fts_cur
== NULL
|| ISSET(FTS_STOP
))
376 /* Set current node pointer. */
379 /* Save and zero out user instructions. */
380 instr
= p
->fts_instr
;
381 p
->fts_instr
= FTS_NOINSTR
;
383 /* Any type of file may be re-visited; re-stat and re-turn. */
384 if (instr
== FTS_AGAIN
) {
385 p
->fts_info
= fts_stat(sp
, p
, 0, -1);
390 * Following a symlink -- SLNONE test allows application to see
391 * SLNONE and recover. If indirecting through a symlink, have
392 * keep a pointer to current location. If unable to get that
393 * pointer, follow fails.
395 if (instr
== FTS_FOLLOW
&&
396 (p
->fts_info
== FTS_SL
|| p
->fts_info
== FTS_SLNONE
)) {
397 p
->fts_info
= fts_stat(sp
, p
, 1, -1);
398 if (p
->fts_info
== FTS_D
&& !ISSET(FTS_NOCHDIR
)) {
400 open(".", O_RDONLY
| O_CLOEXEC
)) < 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
&& ISSET(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 (fts_safe_changedir(sp
, p
, -1, 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
)) {
468 * If reached the top, return to the original directory (or
469 * the root of the tree), and load the paths for the next root.
471 if (p
->fts_level
== FTS_ROOTLEVEL
) {
472 if (FCHDIR(sp
, sp
->fts_rfd
)) {
477 return (sp
->fts_cur
= p
);
481 * User may have called fts_set on the node. If skipped,
482 * ignore. If followed, get a file descriptor so we can
483 * get back if necessary.
485 if (p
->fts_instr
== FTS_SKIP
) {
488 if (p
->fts_instr
== FTS_FOLLOW
) {
489 p
->fts_info
= fts_stat(sp
, p
, 1, -1);
490 if (p
->fts_info
== FTS_D
&& !ISSET(FTS_NOCHDIR
)) {
492 open(".", O_RDONLY
| O_CLOEXEC
, 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
;
502 name
: t
= sp
->fts_path
+ NAPPEND(p
->fts_parent
);
504 memmove(t
, p
->fts_name
, p
->fts_namelen
+ 1);
505 return (sp
->fts_cur
= p
);
508 /* 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.
519 return (sp
->fts_cur
= NULL
);
522 /* NUL terminate the pathname. */
523 sp
->fts_path
[p
->fts_pathlen
] = '\0';
526 * Return to the parent directory. If at a root node or came through
527 * a symlink, go back through the file descriptor. Otherwise, cd up
530 if (p
->fts_level
== FTS_ROOTLEVEL
) {
531 if (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
);
545 (void)close(p
->fts_symfd
);
546 } else if (!(p
->fts_flags
& FTS_DONTCHDIR
) &&
547 fts_safe_changedir(sp
, p
->fts_parent
, -1, "..")) {
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(FTS __unused
*sp
, FTSENT
*p
, int instr
)
566 if (instr
&& instr
!= FTS_AGAIN
&& instr
!= FTS_FOLLOW
&&
567 instr
!= FTS_NOINSTR
&& instr
!= FTS_SKIP
) {
571 p
->fts_instr
= instr
;
576 fts_children(FTS
*sp
, int instr
)
581 if (instr
&& instr
!= FTS_NAMEONLY
) {
586 /* Set current node pointer. */
590 * Errno set to 0 so user can distinguish empty directory from
595 /* Fatal errors stop here. */
599 /* Return logical hierarchy of user's arguments. */
600 if (p
->fts_info
== FTS_INIT
)
601 return (p
->fts_link
);
604 * If not a directory being visited in pre-order, stop here. Could
605 * allow FTS_DNR, assuming the user has fixed the problem, but the
606 * same effect is available with FTS_AGAIN.
608 if (p
->fts_info
!= FTS_D
/* && p->fts_info != FTS_DNR */)
611 /* Free up any previous child list. */
613 fts_lfree(sp
->fts_child
);
615 if (instr
== FTS_NAMEONLY
) {
622 * If using chdir on a relative path and called BEFORE fts_read does
623 * its chdir to the root of a traversal, we can lose -- we need to
624 * chdir into the subdirectory, and we don't know where the current
625 * directory is, so we can't get back so that the upcoming chdir by
626 * fts_read will work.
628 if (p
->fts_level
!= FTS_ROOTLEVEL
|| p
->fts_accpath
[0] == '/' ||
630 return (sp
->fts_child
= fts_build(sp
, instr
));
632 if ((fd
= open(".", O_RDONLY
| O_CLOEXEC
, 0)) < 0)
634 sp
->fts_child
= fts_build(sp
, instr
);
640 return (sp
->fts_child
);
643 typedef struct __attribute__((packed
)) __attribute__((__aligned__(4))) {
646 /* common attributes */
647 attribute_set_t attrset
;
648 attrreference_t name
;
650 fsobj_type_t objtype
;
651 struct timespec st_birthtimespec
;
652 struct timespec st_mtimespec
;
653 struct timespec st_ctimespec
;
654 struct timespec st_atimespec
;
661 /* non-directory attributes */
667 } attrListAttributes
;
669 typedef struct __attribute__((packed
)) {
672 /* common attributes */
673 attribute_set_t attrset
;
674 attrreference_t name
;
676 fsobj_type_t objtype
;
679 /* non-directory attributes */
682 } attrListAttributes_nostat
;
684 #define ATTR_BUF_SIZE (32*1024)
689 struct attrlist requested_attrs
;
692 attrListAttributes
*curattr
;
693 attrListAttributes_nostat
*curattr_nostat
;
714 advance_directory(dir_handle
*handle
)
716 if (handle
->done
) return true;
718 os_assert(handle
->dirfd
!= -1);
719 handle
->entry_count
= getattrlistbulk(handle
->dirfd
, &handle
->requested_attrs
,
720 handle
->attrbuf
, ATTR_BUF_SIZE
, FSOPT_PACK_INVAL_ATTRS
);
721 if (handle
->entry_count
== -1) {
723 } else if (handle
->entry_count
== 0) {
724 /* No more entries. */
727 handle
->cur_entry
= 0;
728 handle
->curattr
= handle
->attrbuf
;
732 int saved_errno
= errno
;
733 close(handle
->dirfd
);
741 open_directory(FTS
*sp
, dir_handle
*handle
, const char *path
)
743 memset(handle
, 0, sizeof(*handle
));
745 handle
->nostat
= ISSET(FTS_NOSTAT
);
746 handle
->needs_dot
= handle
->needs_dotdot
= ISSET(FTS_SEEDOT
);
748 handle
->dirfd
= open(path
, O_RDONLY
| O_NONBLOCK
| O_DIRECTORY
| O_CLOEXEC
);
749 if (handle
->dirfd
== -1) goto fallback
;
751 handle
->attrbuf
= malloc(ATTR_BUF_SIZE
);
752 if (!handle
->attrbuf
) goto fallback
;
754 handle
->requested_attrs
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
755 if (!handle
->nostat
) {
756 handle
->requested_attrs
.commonattr
=
757 ATTR_CMN_RETURNED_ATTRS
|
758 ATTR_CMN_NAME
|ATTR_CMN_DEVID
|ATTR_CMN_OBJTYPE
|
759 ATTR_CMN_CRTIME
|ATTR_CMN_MODTIME
|ATTR_CMN_CHGTIME
|ATTR_CMN_ACCTIME
|
760 ATTR_CMN_OWNERID
|ATTR_CMN_GRPID
|ATTR_CMN_ACCESSMASK
|ATTR_CMN_FLAGS
|
762 handle
->requested_attrs
.fileattr
= ATTR_FILE_LINKCOUNT
|ATTR_FILE_ALLOCSIZE
|
763 ATTR_FILE_IOBLOCKSIZE
|ATTR_FILE_DEVTYPE
|ATTR_FILE_DATALENGTH
;
765 handle
->requested_attrs
.commonattr
= ATTR_CMN_RETURNED_ATTRS
|
766 ATTR_CMN_NAME
|ATTR_CMN_DEVID
|ATTR_CMN_OBJTYPE
|
768 handle
->requested_attrs
.fileattr
= ATTR_FILE_LINKCOUNT
;
771 if (advance_directory(handle
)) {
773 * We successfully read the first attribute buffer,
774 * so we'll use getdirentriesattr/getattrlistbulk.
780 if (handle
->dirfd
!= -1) close(handle
->dirfd
);
782 free(handle
->attrbuf
);
783 handle
->attrbuf
= NULL
;
784 handle
->dirp
= opendir(path
);
785 return (handle
->dirp
!= NULL
);
789 read_dirent(dir_handle
*handle
, dir_entry
*entry
)
792 struct dirent
*de
= readdir(handle
->dirp
);
793 if (de
== NULL
) return false;
794 entry
->d_name
= de
->d_name
;
795 entry
->d_namlen
= de
->d_namlen
;
796 entry
->d_type
= de
->d_type
;
797 entry
->stat_valid
= false;
802 * There are three states that our dir_handle can be in:
803 * - real getattrlistbulk use (entry_count >= 0) - the rest of this function
804 * - fallback - handled above with dirp
805 * - closed fallback (dirp == NULL) - handled with this check
807 if (handle
->dirfd
== -1) {
811 if (handle
->needs_dot
) {
812 handle
->needs_dot
= false;
814 entry
->d_namlen
= strlen(".");
815 entry
->d_type
= DT_DIR
;
816 entry
->stat_valid
= false;
818 } else if (handle
->needs_dotdot
) {
819 handle
->needs_dotdot
= false;
820 entry
->d_name
= "..";
821 entry
->d_namlen
= strlen("..");
822 entry
->d_type
= DT_DIR
;
823 entry
->stat_valid
= false;
827 if (handle
->cur_entry
== handle
->entry_count
) {
828 if (handle
->done
) return false; /* Already done with the directory. */
829 if (!advance_directory(handle
)) return false; /* Reading the next buffer failed. */
830 if (handle
->done
) return false; /* We're now done with the directory. */
833 bzero(entry
, sizeof(*entry
));
835 attrListAttributes
*curattr_stat
= NULL
;
836 attrListAttributes_nostat
*curattr_nostat
= NULL
;
837 if (!handle
->nostat
) {
838 curattr_stat
= handle
->curattr
;
840 handle
->curattr
= (attrListAttributes
*)(((char*)curattr_stat
) + curattr_stat
->length
);
841 os_assert((handle
->cur_entry
== handle
->entry_count
) ||
842 (void*)handle
->curattr
+ handle
->curattr
->length
<= (void*)handle
->attrbuf
+ ATTR_BUF_SIZE
);
844 os_assert(curattr_stat
->name
.attr_length
> 0);
845 entry
->d_name
= ((char*)&curattr_stat
->name
) + curattr_stat
->name
.attr_dataoffset
;
846 /* attr_length includes the null terminator, but readdir's d_namlen doesn't. */
847 entry
->d_namlen
= curattr_stat
->name
.attr_length
- 1;
848 os_assert((void*)entry
->d_name
+ curattr_stat
->name
.attr_length
<= (void*)handle
->attrbuf
+ ATTR_BUF_SIZE
);
850 curattr_nostat
= handle
->curattr_nostat
;
852 handle
->curattr_nostat
= (attrListAttributes_nostat
*)(((char*)curattr_nostat
) + curattr_nostat
->length
);
853 os_assert((handle
->cur_entry
== handle
->entry_count
) ||
854 (void*)handle
->curattr
+ handle
->curattr
->length
<= (void*)handle
->attrbuf
+ ATTR_BUF_SIZE
);
856 os_assert(curattr_nostat
->name
.attr_length
> 0);
857 entry
->d_name
= ((char*)&curattr_nostat
->name
) + curattr_nostat
->name
.attr_dataoffset
;
858 /* attr_length includes the null terminator, but readdir's d_namlen doesn't. */
859 entry
->d_namlen
= curattr_nostat
->name
.attr_length
- 1;
860 os_assert((void*)entry
->d_name
+ curattr_nostat
->name
.attr_length
<= (void*)handle
->attrbuf
+ ATTR_BUF_SIZE
);
865 switch (handle
->nostat
? curattr_nostat
->objtype
: curattr_stat
->objtype
) {
867 entry
->d_type
= DT_REG
;
871 entry
->d_type
= DT_DIR
;
872 /* Force a stat call so we don't have to guess on st_size, st_blocks, etc. */
873 // stat_type = S_IFDIR;
876 entry
->d_type
= DT_BLK
;
880 entry
->d_type
= DT_CHR
;
884 entry
->d_type
= DT_LNK
;
888 entry
->d_type
= DT_SOCK
;
889 stat_type
= S_IFSOCK
;
892 entry
->d_type
= DT_FIFO
;
896 entry
->d_type
= DT_UNKNOWN
;
900 if (!handle
->nostat
&& stat_type
) {
901 entry
->stat_valid
= true;
904 * Make sure we got all the attributes we need to fill out a stat structure.
907 attrgroup_t requiredCommon
= handle
->requested_attrs
.commonattr
;
908 attrgroup_t requiredFile
= handle
->requested_attrs
.fileattr
;
910 if ((entry
->d_type
!= DT_BLK
) && (entry
->d_type
!= DT_CHR
)) {
911 /* It's okay for ATTR_FILE_DEVTYPE to be missing if the entry isn't a block or character device. */
912 curattr_stat
->st_rdev
= 0;
913 requiredFile
&= ~ATTR_FILE_DEVTYPE
;
916 if ((curattr_stat
->attrset
.commonattr
& ATTR_CMN_CRTIME
) == 0) {
917 /* Many (most?) file systems don't support create time (see vn_stat_noauth in xnu) */
918 curattr_stat
->st_birthtimespec
.tv_sec
= curattr_stat
->st_birthtimespec
.tv_nsec
= 0;
919 requiredCommon
&= ~ ATTR_CMN_CRTIME
;
922 if ((curattr_stat
->attrset
.commonattr
& requiredCommon
) != requiredCommon
||
923 (curattr_stat
->attrset
.fileattr
& requiredFile
) != requiredFile
) {
924 /* Some of our required attributes are missing. */
925 entry
->stat_valid
= false;
928 entry
->stat_valid
= false;
931 if (entry
->stat_valid
) {
933 #define COPY_FIELD(fld) entry->sb.fld = curattr_stat->fld
935 /* COPY_FIELD(st_mode); Handled below. */
936 COPY_FIELD(st_nlink
);
941 COPY_FIELD(st_atimespec
);
942 COPY_FIELD(st_mtimespec
);
943 COPY_FIELD(st_ctimespec
);
944 #if __DARWIN_64_BIT_INO_T
945 COPY_FIELD(st_birthtimespec
);
946 #endif /* __DARWIN_64_BIT_INO_T */
948 /* COPY_FIELD(st_blocks); Handled below. */
949 COPY_FIELD(st_blksize
);
950 COPY_FIELD(st_flags
);
953 /* We have to handle some fields specially. */
954 entry
->sb
.st_mode
= (curattr_stat
->accessmask
& ~S_IFMT
) | stat_type
;
955 entry
->sb
.st_blocks
= howmany(curattr_stat
->allocsize
, 512); /* Matches vn_stat implementation in xnu. */
961 dir_fd(dir_handle
*handle
)
963 return handle
->dirp
? dirfd(handle
->dirp
) : handle
->dirfd
;
967 close_directory(dir_handle
*handle
)
970 closedir(handle
->dirp
);
973 if (handle
->dirfd
!= -1) {
974 close(handle
->dirfd
);
977 free(handle
->attrbuf
);
978 handle
->attrbuf
= NULL
;
982 * This is the tricky part -- do not casually change *anything* in here. The
983 * idea is to build the linked list of entries that are used by fts_children
984 * and fts_read. There are lots of special cases.
986 * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is
987 * set, we can use d_type to determine if the entry is a directory (or for
988 * logical walks, a directory or symlink) and not call stat for other file
989 * types. This cuts the number of stat calls significantly.
992 fts_build(FTS
*sp
, int type
)
994 dir_entry de
= { 0 };
1002 int cderrno
, descend
, level
, dostat
, doadjust
;
1006 /* Set current node pointer. */
1010 * Open the directory for reading. If this fails, we're done.
1011 * If being called from fts_read, set the fts_info field.
1013 if (!open_directory(sp
, &dirp
, cur
->fts_accpath
)) {
1014 if (type
== BREAD
) {
1015 cur
->fts_info
= FTS_DNR
;
1016 cur
->fts_errno
= errno
;
1023 else if (ISSET(FTS_NOSTAT_TYPE
))
1024 dostat
= ISSET(FTS_PHYSICAL
) ? F_D_TYPE
: F_D_TYPESYM
;
1025 else if (ISSET(FTS_NOSTAT
))
1026 dostat
= ISSET(FTS_PHYSICAL
) ? F_STATDIR
: F_STATDIRSYM
;
1028 dostat
= F_ALWAYSSTAT
;
1031 (void)printf("dostat == %d\n", dostat
);
1032 (void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n",
1033 ISSET(FTS_NOSTAT
), ISSET(FTS_PHYSICAL
), ISSET(FTS_SEEDOT
));
1036 * If we're going to need to stat anything or we want to descend
1037 * and stay in the directory, chdir. If this fails we keep going,
1038 * but set a flag so we don't chdir after the post-order visit.
1039 * We won't be able to stat anything, but we can still return the
1040 * names themselves. Note, that since fts_read won't be able to
1041 * chdir into the directory, it will have to return different path
1042 * names than before, i.e. "a/b" instead of "b". Since the node
1043 * has already been visited in pre-order, have to wait until the
1044 * post-order visit to return the error. There is a special case
1045 * here, if there was nothing to stat then it's not an error to
1046 * not be able to stat. This is all fairly nasty. If a program
1047 * needed sorted entries or stat information, they had better be
1048 * checking FTS_NS on the returned nodes.
1051 if (dostat
|| type
== BREAD
) {
1052 if (fts_safe_changedir(sp
, cur
, dir_fd(&dirp
), NULL
)) {
1053 if (dostat
&& type
== BREAD
) {
1054 cur
->fts_errno
= errno
;
1056 cur
->fts_flags
|= FTS_DONTCHDIR
;
1059 close_directory(&dirp
);
1068 * Figure out the max file name length that can be stored in the
1069 * current path -- the inner loop allocates more path as necessary.
1070 * We really wouldn't have to do the maxlen calculations here, we
1071 * could do them in fts_read before returning the path, but it's a
1072 * lot easier here since the length is part of the dirent structure.
1074 * If not changing directories set a pointer so that can just append
1075 * each new name into the path.
1078 if (ISSET(FTS_NOCHDIR
)) {
1079 cp
= sp
->fts_path
+ len
;
1083 maxlen
= sp
->fts_pathlen
- len
;
1086 * fts_level is signed so we must prevent it from wrapping
1087 * around to FTS_ROOTLEVEL and FTS_ROOTPARENTLEVEL.
1089 level
= cur
->fts_level
;
1090 if (level
< FTS_MAXLEVEL
)
1093 /* Read the directory, attaching each entry to the `link' pointer. */
1095 for (head
= tail
= NULL
, nitems
= 0; read_dirent(&dirp
, dp
) ; ) {
1096 if (!ISSET(FTS_SEEDOT
) && ISDOT(dp
->d_name
))
1099 if ((p
= fts_alloc(sp
, dp
->d_name
, (int)dp
->d_namlen
)) == NULL
)
1101 if (dp
->d_namlen
>= (size_t)maxlen
) { /* include space for NUL */
1102 oldaddr
= sp
->fts_path
;
1103 if (fts_palloc(sp
, dp
->d_namlen
+len
+ 1)) {
1105 * No more memory for path or structures. Save
1106 * errno, free up the current structure and the
1107 * structures already allocated.
1109 mem1
: saved_errno
= errno
;
1112 close_directory(&dirp
);
1113 cur
->fts_info
= FTS_ERR
;
1115 errno
= saved_errno
;
1118 /* Did realloc() change the pointer? */
1119 if (oldaddr
!= sp
->fts_path
) {
1121 if (ISSET(FTS_NOCHDIR
))
1122 cp
= sp
->fts_path
+ len
;
1124 maxlen
= sp
->fts_pathlen
- len
;
1127 if (len
+ dp
->d_namlen
>= USHRT_MAX
) {
1129 * In an FTSENT, fts_pathlen is a u_short so it is
1130 * possible to wraparound here. If we do, free up
1131 * the current structure and the structures already
1132 * allocated, then error out with ENAMETOOLONG.
1136 close_directory(&dirp
);
1137 cur
->fts_info
= FTS_ERR
;
1139 errno
= ENAMETOOLONG
;
1142 p
->fts_level
= level
;
1143 p
->fts_parent
= sp
->fts_cur
;
1144 p
->fts_pathlen
= len
+ dp
->d_namlen
;
1148 p
->fts_info
= FTS_NS
;
1149 p
->fts_errno
= cderrno
;
1151 p
->fts_info
= FTS_NSOK
;
1152 p
->fts_accpath
= cur
->fts_accpath
;
1155 * We need to know all file types values that d_type may
1156 * be set to. So if that changes, the following needs
1157 * to be modified appropriately.
1159 switch(dostat
| dp
->d_type
) {
1160 case (F_STATDIR
| DT_UNKNOWN
):
1161 case (F_STATDIR
| DT_DIR
):
1162 case (F_STATDIRSYM
| DT_UNKNOWN
):
1163 case (F_STATDIRSYM
| DT_DIR
):
1164 case (F_STATDIRSYM
| DT_LNK
):
1165 case (F_ALWAYSSTAT
| DT_UNKNOWN
):
1166 case (F_ALWAYSSTAT
| DT_FIFO
):
1167 case (F_ALWAYSSTAT
| DT_CHR
):
1168 case (F_ALWAYSSTAT
| DT_DIR
):
1169 case (F_ALWAYSSTAT
| DT_BLK
):
1170 case (F_ALWAYSSTAT
| DT_REG
):
1171 case (F_ALWAYSSTAT
| DT_LNK
):
1172 case (F_ALWAYSSTAT
| DT_SOCK
):
1173 case (F_ALWAYSSTAT
| DT_WHT
):
1174 case (F_D_TYPE
| DT_UNKNOWN
):
1175 case (F_D_TYPE
| DT_DIR
):
1176 case (F_D_TYPESYM
| DT_UNKNOWN
):
1177 case (F_D_TYPESYM
| DT_DIR
):
1178 case (F_D_TYPESYM
| DT_LNK
):
1179 /* Build a file name for fts_stat to stat. */
1180 if (ISSET(FTS_NOCHDIR
)) {
1181 p
->fts_accpath
= p
->fts_path
;
1182 memmove(cp
, p
->fts_name
, p
->fts_namelen
+ 1);
1183 p
->fts_info
= fts_stat2(sp
, p
, 0, dir_fd(&dirp
), dp
->stat_valid
? &dp
->sb
: NULL
);
1185 p
->fts_accpath
= p
->fts_name
;
1186 p
->fts_info
= fts_stat2(sp
, p
, 0, -1, dp
->stat_valid
? &dp
->sb
: NULL
);
1189 case (F_D_TYPE
| DT_FIFO
):
1190 case (F_D_TYPE
| DT_CHR
):
1191 case (F_D_TYPE
| DT_BLK
):
1192 case (F_D_TYPE
| DT_SOCK
):
1193 case (F_D_TYPESYM
| DT_FIFO
):
1194 case (F_D_TYPESYM
| DT_CHR
):
1195 case (F_D_TYPESYM
| DT_BLK
):
1196 case (F_D_TYPESYM
| DT_SOCK
):
1197 p
->fts_info
= FTS_DEFAULT
;
1198 goto common_no_stat
;
1199 case (F_D_TYPE
| DT_REG
):
1200 case (F_D_TYPESYM
| DT_REG
):
1201 p
->fts_info
= FTS_F
;
1202 goto common_no_stat
;
1203 case (F_D_TYPE
| DT_LNK
):
1204 p
->fts_info
= FTS_SL
;
1205 goto common_no_stat
;
1206 case (F_D_TYPE
| DT_WHT
):
1207 case (F_D_TYPESYM
| DT_WHT
):
1208 p
->fts_info
= FTS_W
;
1209 goto common_no_stat
;
1211 /* No stat necessary */
1212 p
->fts_info
= FTS_NSOK
;
1215 ISSET(FTS_NOCHDIR
) ? p
->fts_path
: p
->fts_name
;
1220 /* We walk in directory order so "ls -f" doesn't get upset. */
1230 close_directory(&dirp
);
1233 * If realloc() changed the address of the path, adjust the
1234 * addresses for the rest of the tree and the dir list.
1237 fts_padjust(sp
, head
);
1240 * If not changing directories, reset the path back to original
1243 if (ISSET(FTS_NOCHDIR
)) {
1244 if (len
== sp
->fts_pathlen
|| nitems
== 0)
1250 * If descended after called from fts_children or after called from
1251 * fts_read and nothing found, get back. At the root level we use
1252 * the saved fd; if one of fts_open()'s arguments is a relative path
1253 * to an empty directory, we wind up here with no other way back. If
1254 * can't get back, we're done.
1256 if (descend
&& (type
== BCHILD
|| !nitems
) &&
1257 (cur
->fts_level
== FTS_ROOTLEVEL
? FCHDIR(sp
, sp
->fts_rfd
) :
1258 fts_safe_changedir(sp
, cur
->fts_parent
, -1, ".."))) {
1259 cur
->fts_info
= FTS_ERR
;
1264 /* If didn't find anything, return NULL. */
1267 cur
->fts_info
= FTS_DP
;
1271 /* Sort the entries. */
1272 if (sp
->fts_compar
&& nitems
> 1)
1273 head
= fts_sort(sp
, head
, nitems
);
1278 fts_stat(FTS
*sp
, FTSENT
*p
, int follow
, int dfd
)
1280 return fts_stat2(sp
, p
, follow
, dfd
, NULL
);
1284 fts_stat2(FTS
*sp
, FTSENT
*p
, int follow
, int dfd
, struct stat
*insb
)
1289 struct stat
*sbp
, sb
;
1294 path
= p
->fts_accpath
;
1300 /* If user needs stat info, stat buffer already allocated. */
1301 sbp
= ISSET(FTS_NOSTAT
) ? &sb
: p
->fts_statp
;
1304 * getattrlistbulk always returns information about the link, not its
1305 * destination, so if we are doing a logical walk we need to detect links
1306 * and do a stat anyway.
1310 } else if (insb
&& ISSET(FTS_LOGICAL
) && ((insb
->st_mode
& S_IFLNK
) == S_IFLNK
)) {
1315 memcpy(sbp
, insb
, sizeof(*insb
));
1316 } else if (ISSET(FTS_LOGICAL
) || follow
) {
1318 * If doing a logical walk, or application requested FTS_FOLLOW, do
1319 * a stat(2). If that fails, check for a non-existent symlink. If
1320 * fail, set the errno from the stat call.
1322 if (fstatat(dfd
, path
, sbp
, 0)) {
1323 saved_errno
= errno
;
1324 if (!fstatat(dfd
, path
, sbp
, AT_SYMLINK_NOFOLLOW
)) {
1325 if (saved_errno
== ELOOP
)
1326 p
->fts_errno
= ELOOP
;
1328 return (FTS_SLNONE
);
1330 p
->fts_errno
= saved_errno
;
1334 * For FTS_COMFOLLOWDIR, drop back to lstat unless we have
1337 if (follow
== -1 && !S_ISDIR(sbp
->st_mode
)) {
1338 if (fstatat(dfd
, path
, sbp
, AT_SYMLINK_NOFOLLOW
)) {
1339 p
->fts_errno
= errno
;
1343 } else if (fstatat(dfd
, path
, sbp
, AT_SYMLINK_NOFOLLOW
)) {
1344 p
->fts_errno
= errno
;
1345 err
: memset(sbp
, 0, sizeof(struct stat
));
1349 if (S_ISDIR(sbp
->st_mode
)) {
1351 * Set the device/inode. Used to find cycles and check for
1352 * crossing mount points. Also remember the link count, used
1353 * in fts_build to limit the number of stat calls. It is
1354 * understood that these fields are only referenced if fts_info
1357 dev
= p
->fts_dev
= sbp
->st_dev
;
1358 ino
= p
->fts_ino
= sbp
->st_ino
;
1359 p
->fts_nlink
= sbp
->st_nlink
;
1361 if (ISDOT(p
->fts_name
))
1365 * Cycle detection is done by brute force when the directory
1366 * is first encountered. If the tree gets deep enough or the
1367 * number of symbolic links to directories is high enough,
1368 * something faster might be worthwhile.
1370 for (t
= p
->fts_parent
;
1371 t
->fts_level
>= FTS_ROOTLEVEL
; t
= t
->fts_parent
)
1372 if (ino
== t
->fts_ino
&& dev
== t
->fts_dev
) {
1378 if (S_ISLNK(sbp
->st_mode
))
1380 if (S_ISREG(sbp
->st_mode
))
1382 return (FTS_DEFAULT
);
1386 fts_sort(FTS
*sp
, FTSENT
*head
, int nitems
)
1391 * Construct an array of pointers to the structures and call qsort(3).
1392 * Reassemble the array in the order returned by qsort. If unable to
1393 * sort for memory reasons, return the directory entries in their
1394 * current order. Allocate enough space for the current needs plus
1395 * 40 so don't realloc one entry at a time.
1397 if (nitems
> sp
->fts_nitems
) {
1400 sp
->fts_nitems
= nitems
+ 40;
1401 if ((a
= reallocarray(sp
->fts_array
,
1402 sp
->fts_nitems
, sizeof(FTSENT
*))) == NULL
) {
1403 free(sp
->fts_array
);
1404 sp
->fts_array
= NULL
;
1410 for (ap
= sp
->fts_array
, p
= head
; p
; p
= p
->fts_link
)
1413 if (ISSET(FTS_BLOCK_COMPAR
))
1414 qsort_b((void *)sp
->fts_array
, nitems
, sizeof(FTSENT
*), (int (^)(const void *, const void *))sp
->fts_compar_b
);
1416 #endif /* __BLOCKS__ */
1417 qsort((void *)sp
->fts_array
, nitems
, sizeof(FTSENT
*), sp
->fts_compar
);
1418 for (head
= *(ap
= sp
->fts_array
); --nitems
; ++ap
)
1419 ap
[0]->fts_link
= ap
[1];
1420 ap
[0]->fts_link
= NULL
;
1425 fts_alloc(FTS
*sp
, char *name
, ssize_t namelen
)
1431 * The file name is a variable length array and no stat structure is
1432 * necessary if the user has set the nostat bit. Allocate the FTSENT
1433 * structure, the file name and the stat structure in one chunk, but
1434 * be careful that the stat structure is reasonably aligned. Since the
1435 * fts_name field is declared to be of size 1, the fts_name pointer is
1436 * namelen + 2 before the first possible address of the stat structure.
1438 len
= sizeof(FTSENT
) + namelen
;
1439 if (!ISSET(FTS_NOSTAT
))
1440 len
+= sizeof(struct stat
) + ALIGNBYTES
;
1441 if ((p
= calloc(1, len
)) == NULL
)
1444 p
->fts_path
= sp
->fts_path
;
1445 p
->fts_namelen
= namelen
;
1446 p
->fts_instr
= FTS_NOINSTR
;
1447 if (!ISSET(FTS_NOSTAT
))
1448 p
->fts_statp
= (struct stat
*)ALIGN(p
->fts_name
+ namelen
+ 2);
1449 memcpy(p
->fts_name
, name
, namelen
);
1455 fts_lfree(FTSENT
*head
)
1459 /* Free a linked list of structures. */
1460 while ((p
= head
)) {
1461 head
= head
->fts_link
;
1467 * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
1468 * Most systems will allow creation of paths much longer than PATH_MAX, even
1469 * though the kernel won't resolve them. Add the size (not just what's needed)
1470 * plus 256 bytes so don't realloc the path 2 bytes at a time.
1473 fts_palloc(FTS
*sp
, size_t more
)
1477 sp
->fts_pathlen
+= more
+ 256;
1479 * Check for possible wraparound. In an FTS, fts_pathlen is
1480 * a signed int but in an FTSENT it is an unsigned short.
1481 * We limit fts_pathlen to USHRT_MAX to be safe in both cases.
1483 if (sp
->fts_pathlen
< 0 || sp
->fts_pathlen
>= USHRT_MAX
) {
1485 sp
->fts_path
= NULL
;
1486 errno
= ENAMETOOLONG
;
1489 p
= realloc(sp
->fts_path
, sp
->fts_pathlen
);
1492 sp
->fts_path
= NULL
;
1500 * When the path is realloc'd, have to fix all of the pointers in structures
1504 fts_padjust(FTS
*sp
, FTSENT
*head
)
1507 char *addr
= sp
->fts_path
;
1509 #define ADJUST(p) { \
1510 if ((p)->fts_accpath != (p)->fts_name) { \
1511 (p)->fts_accpath = \
1512 (char *)addr + ((p)->fts_accpath - (p)->fts_path); \
1514 (p)->fts_path = addr; \
1516 /* Adjust the current set of children. */
1517 for (p
= sp
->fts_child
; p
; p
= p
->fts_link
)
1520 /* Adjust the rest of the tree, including the current level. */
1521 for (p
= head
; p
->fts_level
>= FTS_ROOTLEVEL
;) {
1523 p
= p
->fts_link
? p
->fts_link
: p
->fts_parent
;
1528 fts_maxarglen(char * const *argv
)
1532 for (max
= 0; *argv
; ++argv
)
1533 if ((len
= strlen(*argv
)) > max
)
1539 * Change to dir specified by fd or p->fts_accpath without getting
1540 * tricked by someone changing the world out from underneath us.
1541 * Assumes p->fts_dev and p->fts_ino are filled in.
1544 fts_safe_changedir(FTS
*sp
, FTSENT
*p
, int fd
, char *path
)
1546 int ret
, oerrno
, newfd
;
1550 if (ISSET(FTS_NOCHDIR
))
1552 if (fd
< 0 && (newfd
= open(path
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
)) < 0)
1554 if (fstat(newfd
, &sb
)) {
1559 * On Darwin this check causes us to not correctly descend into automounts,
1560 * since the mount point and trigger will have a different devices and
1561 * inodes. It would be preferable to find a way to use
1562 * ATTR_DIR_MOUNTSTATUS and DIR_MNTSTATUS_TRIGGER to detect these cases,
1563 * but plumbing that through will be challenging. Instead, will just
1564 * disable it for now.
1567 if (p
->fts_dev
!= sb
.st_dev
|| p
->fts_ino
!= sb
.st_ino
) {
1568 errno
= ENOENT
; /* disinformation */
1574 ret
= fchdir(newfd
);
1582 #pragma clang diagnostic pop