1 /* $Id: compat_fts.c,v 1.17 2020/06/15 01:37:14 schwarze Exp $ */
2 /* $OpenBSD: fts.c,v 1.59 2019/06/28 13:32:41 deraadt Exp $ */
5 * Copyright (c) 1990, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/types.h>
44 #include "compat_fts.h"
46 #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
48 static FTSENT *fts_alloc(FTS *, const char *, size_t);
49 static FTSENT *fts_build(FTS *);
50 static void fts_lfree(FTSENT *);
51 static void fts_load(FTS *, FTSENT *);
52 static size_t fts_maxarglen(char * const *);
53 static void fts_padjust(FTS *, FTSENT *);
54 static int fts_palloc(FTS *, size_t);
55 static FTSENT *fts_sort(FTS *, FTSENT *, int);
56 static unsigned short fts_stat(FTS *, FTSENT *);
58 typedef int (*qsort_compar_proto)(const void *, const void *);
60 #define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
65 #define CLR(opt) (sp->fts_options &= ~(opt))
66 #define ISSET(opt) (sp->fts_options & (opt))
67 #define SET(opt) (sp->fts_options |= (opt))
70 fts_open(char * const *argv, int options,
71 int (*compar)(const FTSENT **, const FTSENT **))
76 FTSENT *parent, *prev;
79 if (options & ~FTS_OPTIONMASK) {
84 /* At least one path must be specified. */
90 /* Allocate/initialize the stream */
91 if ((sp = calloc(1, sizeof(FTS))) == NULL)
93 sp->fts_compar = compar;
94 sp->fts_options = options;
97 * Start out with 1K of path space, and enough, in any case,
98 * to hold the user's paths.
100 if (fts_palloc(sp, MAXIMUM(fts_maxarglen(argv), PATH_MAX)))
103 /* Allocate/initialize root's parent. */
104 if ((parent = fts_alloc(sp, "", 0)) == NULL)
106 parent->fts_level = FTS_ROOTPARENTLEVEL;
108 /* Allocate/initialize root(s). */
109 for (root = prev = NULL, nitems = 0; *argv; ++argv, ++nitems) {
110 if ((p = fts_alloc(sp, *argv, strlen(*argv))) == NULL)
112 p->fts_level = FTS_ROOTLEVEL;
113 p->fts_parent = parent;
114 p->fts_accpath = p->fts_name;
115 p->fts_info = fts_stat(sp, p);
117 /* Command-line "." and ".." are real directories. */
118 if (p->fts_info == FTS_DOT)
122 * If comparison routine supplied, traverse in sorted
123 * order; otherwise traverse in the order specified.
137 if (compar && nitems > 1)
138 root = fts_sort(sp, root, nitems);
141 * Allocate a dummy pointer and make fts_read think that we've just
142 * finished the node before the root(s); set p->fts_info to FTS_INIT
143 * so that everything about the "current" node is ignored.
145 if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
147 sp->fts_cur->fts_link = root;
148 sp->fts_cur->fts_info = FTS_INIT;
155 mem3: fts_lfree(root);
157 mem2: free(sp->fts_path);
163 fts_load(FTS *sp, FTSENT *p)
169 * Load the stream structure for the next traversal. Since we don't
170 * actually enter the directory until after the preorder visit, set
171 * the fts_accpath field specially so the chdir gets done to the right
172 * place and the user can access the first node. From fts_open it's
173 * known that the path will fit.
175 len = p->fts_pathlen = p->fts_namelen;
176 memmove(sp->fts_path, p->fts_name, len + 1);
177 if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
179 memmove(p->fts_name, cp, len + 1);
180 p->fts_namelen = len;
182 p->fts_accpath = p->fts_path = sp->fts_path;
183 sp->fts_dev = p->fts_dev;
192 * This still works if we haven't read anything -- the dummy structure
193 * points to the root list, so we step through to the end of the root
194 * list which has a valid parent pointer.
197 for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
199 p = p->fts_link ? p->fts_link : p->fts_parent;
205 /* Free up child linked list, sort array, path buffer, stream ptr.*/
207 fts_lfree(sp->fts_child);
216 * Special case of "/" at the end of the path so that slashes aren't
217 * appended which would cause paths to be written as "....//foo".
220 (p->fts_path[p->fts_pathlen - 1] == '/' \
221 ? p->fts_pathlen - 1 : p->fts_pathlen)
230 /* If finished or unrecoverable error, return NULL. */
231 if (sp->fts_cur == NULL || ISSET(FTS_STOP))
234 /* Set current node pointer. */
237 /* Save and zero out user instructions. */
238 instr = p->fts_instr;
239 p->fts_instr = FTS_NOINSTR;
241 /* Directory in pre-order. */
242 if (p->fts_info == FTS_D) {
243 /* If skipped or crossed mount point, do post-order visit. */
244 if (instr == FTS_SKIP ||
245 (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
247 fts_lfree(sp->fts_child);
248 sp->fts_child = NULL;
250 p->fts_info = FTS_DP;
255 * If haven't read do so. If the read fails, fts_build sets
256 * FTS_STOP or the fts_info field of the node.
260 } else if ((sp->fts_child = fts_build(sp)) == NULL) {
266 sp->fts_child = NULL;
270 /* Move to the next node on this level. */
272 if ((p = p->fts_link)) {
276 * If reached the top, return to the original directory (or
277 * the root of the tree), and load the paths for the next root.
279 if (p->fts_level == FTS_ROOTLEVEL) {
281 return (sp->fts_cur = p);
285 * User may have called fts_set on the node. If skipped,
286 * ignore. If followed, get a file descriptor so we can
287 * get back if necessary.
289 if (p->fts_instr == FTS_SKIP)
292 name: t = sp->fts_path + NAPPEND(p->fts_parent);
294 memmove(t, p->fts_name, p->fts_namelen + 1);
295 return (sp->fts_cur = p);
298 /* Move up to the parent node. */
302 if (p->fts_level == FTS_ROOTPARENTLEVEL) {
304 * Done; free everything up and set errno to 0 so the user
305 * can distinguish between error and EOF.
309 return (sp->fts_cur = NULL);
312 /* NUL terminate the pathname. */
313 sp->fts_path[p->fts_pathlen] = '\0';
315 p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
316 return (sp->fts_cur = p);
320 * Fts_set takes the stream as an argument although it's not used in this
321 * implementation; it would be necessary if anyone wanted to add global
322 * semantics to fts using fts_set. An error return is allowed for similar
326 fts_set(FTS *sp, FTSENT *p, int instr)
328 if (instr && instr != FTS_NOINSTR && instr != FTS_SKIP) {
332 p->fts_instr = instr;
337 * This is the tricky part -- do not casually change *anything* in here. The
338 * idea is to build the linked list of entries that are used by fts_children
339 * and fts_read. There are lots of special cases.
341 * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is
342 * set and it's a physical walk (so that symbolic links can't be directories),
343 * we can do things quickly. First, if it's a 4.4BSD file system, the type
344 * of the file is in the directory entry. Otherwise, we assume that the number
345 * of subdirectories in a node is equal to the number of links to the parent.
346 * The former skips all stat calls. The latter skips stat calls in any leaf
347 * directories and for any files after the subdirectories in the directory have
348 * been found, cutting the stat calls by about 2/3.
358 size_t dlen, len, maxlen;
359 int nitems, level, doadjust;
363 /* Set current node pointer. */
367 * Open the directory for reading. If this fails, we're done.
368 * If being called from fts_read, set the fts_info field.
370 if ((dirp = opendir(cur->fts_accpath)) == NULL) {
371 cur->fts_info = FTS_DNR;
372 cur->fts_errno = errno;
377 * Figure out the max file name length that can be stored in the
378 * current path -- the inner loop allocates more path as necessary.
379 * We really wouldn't have to do the maxlen calculations here, we
380 * could do them in fts_read before returning the path, but it's a
381 * lot easier here since the length is part of the dirent structure.
383 * If not changing directories set a pointer so that can just append
384 * each new name into the path.
387 cp = sp->fts_path + len;
390 maxlen = sp->fts_pathlen - len;
393 * fts_level is signed so we must prevent it from wrapping
394 * around to FTS_ROOTLEVEL and FTS_ROOTPARENTLEVEL.
396 level = cur->fts_level;
397 if (level < FTS_MAXLEVEL)
400 /* Read the directory, attaching each entry to the `link' pointer. */
402 for (head = tail = NULL, nitems = 0; dirp && (dp = readdir(dirp));) {
403 if (ISDOT(dp->d_name))
406 #if HAVE_DIRENT_NAMLEN
409 dlen = strlen(dp->d_name);
412 if (!(p = fts_alloc(sp, dp->d_name, dlen)))
414 if (dlen >= maxlen) { /* include space for NUL */
415 oldaddr = sp->fts_path;
416 if (fts_palloc(sp, dlen + len + 1)) {
418 * No more memory for path or structures. Save
419 * errno, free up the current structure and the
420 * structures already allocated.
422 mem1: saved_errno = errno;
425 (void)closedir(dirp);
426 cur->fts_info = FTS_ERR;
431 /* Did realloc() change the pointer? */
432 if (oldaddr != sp->fts_path) {
434 cp = sp->fts_path + len;
436 maxlen = sp->fts_pathlen - len;
439 p->fts_level = level;
440 p->fts_parent = sp->fts_cur;
441 p->fts_pathlen = len + dlen;
442 if (p->fts_pathlen < len) {
444 * If we wrap, free up the current structure and
445 * the structures already allocated, then error
446 * out with ENAMETOOLONG.
450 (void)closedir(dirp);
451 cur->fts_info = FTS_ERR;
453 errno = ENAMETOOLONG;
457 /* Build a file name for fts_stat to stat. */
458 p->fts_accpath = p->fts_path;
459 memmove(cp, p->fts_name, p->fts_namelen + 1);
461 p->fts_info = fts_stat(sp, p);
463 /* We walk in directory order so "ls -f" doesn't get upset. */
474 (void)closedir(dirp);
477 * If realloc() changed the address of the path, adjust the
478 * addresses for the rest of the tree and the dir list.
481 fts_padjust(sp, head);
484 * If not changing directories, reset the path back to original
487 if (len == sp->fts_pathlen || nitems == 0)
491 /* If didn't find anything, return NULL. */
493 cur->fts_info = FTS_DP;
497 /* Sort the entries. */
498 if (sp->fts_compar && nitems > 1)
499 head = fts_sort(sp, head, nitems);
503 static unsigned short
504 fts_stat(FTS *sp, FTSENT *p)
511 /* If user needs stat info, stat buffer already allocated. */
514 if (lstat(p->fts_accpath, sbp)) {
515 p->fts_errno = errno;
516 memset(sbp, 0, sizeof(struct stat));
520 if (S_ISDIR(sbp->st_mode)) {
522 * Set the device/inode. Used to find cycles and check for
523 * crossing mount points. Also remember the link count, used
524 * in fts_build to limit the number of stat calls. It is
525 * understood that these fields are only referenced if fts_info
528 dev = p->fts_dev = sbp->st_dev;
529 ino = p->fts_ino = sbp->st_ino;
530 p->fts_nlink = sbp->st_nlink;
532 if (ISDOT(p->fts_name))
536 * Cycle detection is done by brute force when the directory
537 * is first encountered. If the tree gets deep enough or the
538 * number of symbolic links to directories is high enough,
539 * something faster might be worthwhile.
541 for (t = p->fts_parent;
542 t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
543 if (ino == t->fts_ino && dev == t->fts_dev) {
549 if (S_ISLNK(sbp->st_mode))
551 if (S_ISREG(sbp->st_mode))
553 return (FTS_DEFAULT);
557 fts_sort(FTS *sp, FTSENT *head, int nitems)
562 * Construct an array of pointers to the structures and call qsort(3).
563 * Reassemble the array in the order returned by qsort. If unable to
564 * sort for memory reasons, return the directory entries in their
565 * current order. Allocate enough space for the current needs plus
566 * 40 so don't realloc one entry at a time.
568 if (nitems > sp->fts_nitems) {
571 if ((a = reallocarray(sp->fts_array,
572 nitems + 40, sizeof(FTSENT *))) == NULL) {
574 sp->fts_array = NULL;
578 sp->fts_nitems = nitems + 40;
581 for (ap = sp->fts_array, p = head; p; p = p->fts_link)
583 qsort(sp->fts_array, nitems, sizeof(FTSENT *),
584 (qsort_compar_proto)sp->fts_compar);
585 for (head = *(ap = sp->fts_array); --nitems; ++ap)
586 ap[0]->fts_link = ap[1];
587 ap[0]->fts_link = NULL;
592 fts_alloc(FTS *sp, const char *name, size_t namelen)
597 len = sizeof(FTSENT) + namelen;
598 if ((p = calloc(1, len)) == NULL)
601 p->fts_path = sp->fts_path;
602 p->fts_namelen = namelen;
603 p->fts_instr = FTS_NOINSTR;
604 p->fts_statp = malloc(sizeof(struct stat));
605 if (p->fts_statp == NULL) {
609 memcpy(p->fts_name, name, namelen);
615 fts_lfree(FTSENT *head)
619 /* Free a linked list of structures. */
621 head = head->fts_link;
627 * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
628 * Most systems will allow creation of paths much longer than PATH_MAX, even
629 * though the kernel won't resolve them. Add the size (not just what's needed)
630 * plus 256 bytes so don't realloc the path 2 bytes at a time.
633 fts_palloc(FTS *sp, size_t more)
638 * Check for possible wraparound.
641 if (sp->fts_pathlen + more < sp->fts_pathlen) {
644 errno = ENAMETOOLONG;
647 p = recallocarray(sp->fts_path, sp->fts_pathlen,
648 sp->fts_pathlen + more, 1);
654 sp->fts_pathlen += more;
660 * When the path is realloc'd, have to fix all of the pointers in structures
664 fts_padjust(FTS *sp, FTSENT *head)
667 char *addr = sp->fts_path;
669 #define ADJUST(p) { \
670 if ((p)->fts_accpath != (p)->fts_name) { \
672 (char *)addr + ((p)->fts_accpath - (p)->fts_path); \
674 (p)->fts_path = addr; \
676 /* Adjust the current set of children. */
677 for (p = sp->fts_child; p; p = p->fts_link)
680 /* Adjust the rest of the tree, including the current level. */
681 for (p = head; p->fts_level >= FTS_ROOTLEVEL;) {
683 p = p->fts_link ? p->fts_link : p->fts_parent;
688 fts_maxarglen(char * const *argv)
692 for (max = 0; *argv; ++argv)
693 if ((len = strlen(*argv)) > max)