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