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