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