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