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