9 /* $Id: compat_fts.c,v 1.4 2014/08/17 20:45:59 schwarze Exp $ */
10 /* $OpenBSD: fts.c,v 1.46 2014/05/25 17:47:04 tedu Exp $ */
13 * Copyright (c) 1990, 1993, 1994
14 * The Regents of the University of California. All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #include <sys/param.h>
43 #include <sys/types.h>
52 #include "compat_fts.h"
54 static FTSENT *fts_alloc(FTS *, const char *, size_t);
55 static FTSENT *fts_build(FTS *);
56 static void fts_lfree(FTSENT *);
57 static void fts_load(FTS *, FTSENT *);
58 static size_t fts_maxarglen(char * const *);
59 static void fts_padjust(FTS *, FTSENT *);
60 static int fts_palloc(FTS *, size_t);
61 static unsigned short fts_stat(FTS *, FTSENT *);
62 static int fts_safe_changedir(FTS *, FTSENT *, int, const char *);
64 #define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
66 #define CLR(opt) (sp->fts_options &= ~(opt))
67 #define ISSET(opt) (sp->fts_options & (opt))
68 #define SET(opt) (sp->fts_options |= (opt))
70 #define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd))
73 fts_open(char * const *argv, int options, void *dummy)
82 if (options & ~FTS_OPTIONMASK) {
87 /* Allocate/initialize the stream */
88 if ((sp = calloc(1, sizeof(FTS))) == NULL)
90 sp->fts_options = options;
93 * Start out with 1K of path space, and enough, in any case,
94 * to hold the user's paths.
96 if (fts_palloc(sp, MAX(fts_maxarglen(argv), PATH_MAX)))
99 /* Allocate/initialize root's parent. */
100 if ((parent = fts_alloc(sp, "", 0)) == NULL)
102 parent->fts_level = FTS_ROOTPARENTLEVEL;
104 /* Allocate/initialize root(s). */
105 for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) {
106 /* Don't allow zero-length paths. */
107 if ((len = strlen(*argv)) == 0) {
112 if ((p = fts_alloc(sp, *argv, len)) == NULL)
114 p->fts_level = FTS_ROOTLEVEL;
115 p->fts_parent = parent;
116 p->fts_accpath = p->fts_name;
117 p->fts_info = fts_stat(sp, p);
119 /* Command-line "." and ".." are real directories. */
120 if (p->fts_info == FTS_DOT)
133 * Allocate a dummy pointer and make fts_read think that we've just
134 * finished the node before the root(s); set p->fts_info to FTS_INIT
135 * so that everything about the "current" node is ignored.
137 if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
139 sp->fts_cur->fts_link = root;
140 sp->fts_cur->fts_info = FTS_INIT;
143 * If using chdir(2), grab a file descriptor pointing to dot to ensure
144 * that we can get back here; this could be avoided for some paths,
145 * but almost certainly not worth the effort. Slashes, symbolic links,
146 * and ".." are all fairly nasty problems. Note, if we can't get the
147 * descriptor we run anyway, just more slowly.
149 if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0)
157 mem3: fts_lfree(root);
159 mem2: free(sp->fts_path);
165 fts_load(FTS *sp, FTSENT *p)
171 * Load the stream structure for the next traversal. Since we don't
172 * actually enter the directory until after the preorder visit, set
173 * the fts_accpath field specially so the chdir gets done to the right
174 * place and the user can access the first node. From fts_open it's
175 * known that the path will fit.
177 len = p->fts_pathlen = p->fts_namelen;
178 memmove(sp->fts_path, p->fts_name, len + 1);
179 if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
181 memmove(p->fts_name, cp, len + 1);
182 p->fts_namelen = len;
184 p->fts_accpath = p->fts_path = sp->fts_path;
185 sp->fts_dev = p->fts_dev;
195 * This still works if we haven't read anything -- the dummy structure
196 * points to the root list, so we step through to the end of the root
197 * list which has a valid parent pointer.
200 for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
202 p = p->fts_link ? p->fts_link : p->fts_parent;
208 /* Stash the original directory fd if needed. */
209 rfd = ISSET(FTS_NOCHDIR) ? -1 : sp->fts_rfd;
211 /* Free up child linked list, sort array, path buffer, stream ptr.*/
213 fts_lfree(sp->fts_child);
217 /* Return to original directory, checking for error. */
230 * Special case of "/" at the end of the path so that slashes aren't
231 * appended which would cause paths to be written as "....//foo".
234 (p->fts_path[p->fts_pathlen - 1] == '/' \
235 ? p->fts_pathlen - 1 : p->fts_pathlen)
244 /* If finished or unrecoverable error, return NULL. */
245 if (sp->fts_cur == NULL || ISSET(FTS_STOP))
248 /* Set current node pointer. */
251 /* Save and zero out user instructions. */
252 instr = p->fts_instr;
253 p->fts_instr = FTS_NOINSTR;
255 /* Directory in pre-order. */
256 if (p->fts_info == FTS_D) {
257 /* If skipped or crossed mount point, do post-order visit. */
258 if (instr == FTS_SKIP ||
259 (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
261 fts_lfree(sp->fts_child);
262 sp->fts_child = NULL;
264 p->fts_info = FTS_DP;
269 * Cd to the subdirectory.
271 * If have already read and now fail to chdir, whack the list
272 * to make the names come out right, and set the parent errno
273 * so the application will eventually get an error condition.
274 * Set the FTS_DONTCHDIR flag so that when we logically change
275 * directories back to the parent we don't do a chdir.
277 * If haven't read do so. If the read fails, fts_build sets
278 * FTS_STOP or the fts_info field of the node.
281 if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) {
282 p->fts_errno = errno;
283 p->fts_flags |= FTS_DONTCHDIR;
284 for (p = sp->fts_child; p; p = p->fts_link)
286 p->fts_parent->fts_accpath;
288 } else if ((sp->fts_child = fts_build(sp)) == NULL) {
294 sp->fts_child = NULL;
298 /* Move to the next node on this level. */
300 if ((p = p->fts_link)) {
304 * If reached the top, return to the original directory (or
305 * the root of the tree), and load the paths for the next root.
307 if (p->fts_level == FTS_ROOTLEVEL) {
308 if (FCHDIR(sp, sp->fts_rfd)) {
313 return (sp->fts_cur = p);
317 * User may have called fts_set on the node. If skipped,
318 * ignore. If followed, get a file descriptor so we can
319 * get back if necessary.
321 if (p->fts_instr == FTS_SKIP)
324 name: t = sp->fts_path + NAPPEND(p->fts_parent);
326 memmove(t, p->fts_name, p->fts_namelen + 1);
327 return (sp->fts_cur = p);
330 /* Move up to the parent node. */
334 if (p->fts_level == FTS_ROOTPARENTLEVEL) {
336 * Done; free everything up and set errno to 0 so the user
337 * can distinguish between error and EOF.
341 return (sp->fts_cur = NULL);
344 /* NUL terminate the pathname. */
345 sp->fts_path[p->fts_pathlen] = '\0';
348 * Return to the parent directory. If at a root node or came through
349 * a symlink, go back through the file descriptor. Otherwise, cd up
352 if (p->fts_level == FTS_ROOTLEVEL) {
353 if (FCHDIR(sp, sp->fts_rfd)) {
358 } else if (!(p->fts_flags & FTS_DONTCHDIR) &&
359 fts_safe_changedir(sp, p->fts_parent, -1, "..")) {
364 p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
365 return (sp->fts_cur = p);
369 * Fts_set takes the stream as an argument although it's not used in this
370 * implementation; it would be necessary if anyone wanted to add global
371 * semantics to fts using fts_set. An error return is allowed for similar
376 fts_set(FTS *sp, FTSENT *p, int instr)
378 if (instr && instr != FTS_NOINSTR && instr != FTS_SKIP) {
382 p->fts_instr = instr;
387 * This is the tricky part -- do not casually change *anything* in here. The
388 * idea is to build the linked list of entries that are used by fts_children
389 * and fts_read. There are lots of special cases.
391 * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is
392 * set and it's a physical walk (so that symbolic links can't be directories),
393 * we can do things quickly. First, if it's a 4.4BSD file system, the type
394 * of the file is in the directory entry. Otherwise, we assume that the number
395 * of subdirectories in a node is equal to the number of links to the parent.
396 * The former skips all stat calls. The latter skips stat calls in any leaf
397 * directories and for any files after the subdirectories in the directory have
398 * been found, cutting the stat calls by about 2/3.
408 size_t dlen, len, maxlen;
409 int nitems, cderrno, descend, level, nlinks, nostat, doadjust;
413 /* Set current node pointer. */
417 * Open the directory for reading. If this fails, we're done.
418 * If being called from fts_read, set the fts_info field.
420 if ((dirp = opendir(cur->fts_accpath)) == NULL) {
421 cur->fts_info = FTS_DNR;
422 cur->fts_errno = errno;
427 * Nlinks is the number of possible entries of type directory in the
428 * directory if we're cheating on stat calls, 0 if we're not doing
429 * any stat calls at all, -1 if we're doing stats on everything.
435 * If we're going to need to stat anything or we want to descend
436 * and stay in the directory, chdir. If this fails we keep going,
437 * but set a flag so we don't chdir after the post-order visit.
438 * We won't be able to stat anything, but we can still return the
439 * names themselves. Note, that since fts_read won't be able to
440 * chdir into the directory, it will have to return different path
441 * names than before, i.e. "a/b" instead of "b". Since the node
442 * has already been visited in pre-order, have to wait until the
443 * post-order visit to return the error. There is a special case
444 * here, if there was nothing to stat then it's not an error to
445 * not be able to stat. This is all fairly nasty. If a program
446 * needed sorted entries or stat information, they had better be
447 * checking FTS_NS on the returned nodes.
450 if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) {
452 cur->fts_errno = errno;
453 cur->fts_flags |= FTS_DONTCHDIR;
456 (void)closedir(dirp);
462 * Figure out the max file name length that can be stored in the
463 * current path -- the inner loop allocates more path as necessary.
464 * We really wouldn't have to do the maxlen calculations here, we
465 * could do them in fts_read before returning the path, but it's a
466 * lot easier here since the length is part of the dirent structure.
468 * If not changing directories set a pointer so that can just append
469 * each new name into the path.
472 if (ISSET(FTS_NOCHDIR)) {
473 cp = sp->fts_path + len;
477 maxlen = sp->fts_pathlen - len;
480 * fts_level is signed so we must prevent it from wrapping
481 * around to FTS_ROOTLEVEL and FTS_ROOTPARENTLEVEL.
483 level = cur->fts_level;
484 if (level < FTS_MAXLEVEL)
487 /* Read the directory, attaching each entry to the `link' pointer. */
489 for (head = tail = NULL, nitems = 0; dirp && (dp = readdir(dirp));) {
490 if (ISDOT(dp->d_name))
493 #if HAVE_DIRENT_NAMLEN
496 dlen = strlen(dp->d_name);
499 if (!(p = fts_alloc(sp, dp->d_name, dlen)))
501 if (dlen >= maxlen) { /* include space for NUL */
502 oldaddr = sp->fts_path;
503 if (fts_palloc(sp, dlen + len + 1)) {
505 * No more memory for path or structures. Save
506 * errno, free up the current structure and the
507 * structures already allocated.
509 mem1: saved_errno = errno;
513 (void)closedir(dirp);
514 cur->fts_info = FTS_ERR;
519 /* Did realloc() change the pointer? */
520 if (oldaddr != sp->fts_path) {
522 if (ISSET(FTS_NOCHDIR))
523 cp = sp->fts_path + len;
525 maxlen = sp->fts_pathlen - len;
528 p->fts_level = level;
529 p->fts_parent = sp->fts_cur;
530 p->fts_pathlen = len + dlen;
531 if (p->fts_pathlen < len) {
533 * If we wrap, free up the current structure and
534 * the structures already allocated, then error
535 * out with ENAMETOOLONG.
539 (void)closedir(dirp);
540 cur->fts_info = FTS_ERR;
542 errno = ENAMETOOLONG;
548 p->fts_info = FTS_NS;
549 p->fts_errno = cderrno;
551 p->fts_info = FTS_NSOK;
552 p->fts_accpath = cur->fts_accpath;
553 } else if (nlinks == 0
556 dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN)
560 ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
561 p->fts_info = FTS_NSOK;
563 /* Build a file name for fts_stat to stat. */
564 if (ISSET(FTS_NOCHDIR)) {
565 p->fts_accpath = p->fts_path;
566 memmove(cp, p->fts_name, p->fts_namelen + 1);
568 p->fts_accpath = p->fts_name;
570 p->fts_info = fts_stat(sp, p);
572 /* Decrement link count if applicable. */
573 if (nlinks > 0 && (p->fts_info == FTS_D ||
574 p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
578 /* We walk in directory order so "ls -f" doesn't get upset. */
589 (void)closedir(dirp);
592 * If realloc() changed the address of the path, adjust the
593 * addresses for the rest of the tree and the dir list.
596 fts_padjust(sp, head);
599 * If not changing directories, reset the path back to original
602 if (ISSET(FTS_NOCHDIR)) {
603 if (len == sp->fts_pathlen || nitems == 0)
609 * If descended after called from fts_children or after called from
610 * fts_read and nothing found, get back. At the root level we use
611 * the saved fd; if one of fts_open()'s arguments is a relative path
612 * to an empty directory, we wind up here with no other way back. If
613 * can't get back, we're done.
615 if (descend && !nitems &&
616 (cur->fts_level == FTS_ROOTLEVEL ? FCHDIR(sp, sp->fts_rfd) :
617 fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) {
618 cur->fts_info = FTS_ERR;
623 /* If didn't find anything, return NULL. */
625 cur->fts_info = FTS_DP;
631 static unsigned short
632 fts_stat(FTS *sp, FTSENT *p)
639 /* If user needs stat info, stat buffer already allocated. */
642 if (lstat(p->fts_accpath, sbp)) {
643 p->fts_errno = errno;
644 memset(sbp, 0, sizeof(struct stat));
648 if (S_ISDIR(sbp->st_mode)) {
650 * Set the device/inode. Used to find cycles and check for
651 * crossing mount points. Also remember the link count, used
652 * in fts_build to limit the number of stat calls. It is
653 * understood that these fields are only referenced if fts_info
656 dev = p->fts_dev = sbp->st_dev;
657 ino = p->fts_ino = sbp->st_ino;
658 p->fts_nlink = sbp->st_nlink;
660 if (ISDOT(p->fts_name))
664 * Cycle detection is done by brute force when the directory
665 * is first encountered. If the tree gets deep enough or the
666 * number of symbolic links to directories is high enough,
667 * something faster might be worthwhile.
669 for (t = p->fts_parent;
670 t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
671 if (ino == t->fts_ino && dev == t->fts_dev) {
677 if (S_ISLNK(sbp->st_mode))
679 if (S_ISREG(sbp->st_mode))
681 return (FTS_DEFAULT);
685 fts_alloc(FTS *sp, const char *name, size_t namelen)
690 len = sizeof(FTSENT) + namelen;
691 if ((p = calloc(1, len)) == NULL)
694 p->fts_path = sp->fts_path;
695 p->fts_namelen = namelen;
696 p->fts_instr = FTS_NOINSTR;
697 p->fts_statp = malloc(sizeof(struct stat));
698 if (p->fts_statp == NULL) {
702 memcpy(p->fts_name, name, namelen);
708 fts_lfree(FTSENT *head)
712 /* Free a linked list of structures. */
714 head = head->fts_link;
720 * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
721 * Most systems will allow creation of paths much longer than PATH_MAX, even
722 * though the kernel won't resolve them. Add the size (not just what's needed)
723 * plus 256 bytes so don't realloc the path 2 bytes at a time.
726 fts_palloc(FTS *sp, size_t more)
731 * Check for possible wraparound.
734 if (sp->fts_pathlen + more < sp->fts_pathlen) {
738 errno = ENAMETOOLONG;
741 sp->fts_pathlen += more;
742 p = realloc(sp->fts_path, sp->fts_pathlen);
754 * When the path is realloc'd, have to fix all of the pointers in structures
758 fts_padjust(FTS *sp, FTSENT *head)
761 char *addr = sp->fts_path;
763 #define ADJUST(p) { \
764 if ((p)->fts_accpath != (p)->fts_name) { \
766 (char *)addr + ((p)->fts_accpath - (p)->fts_path); \
768 (p)->fts_path = addr; \
770 /* Adjust the current set of children. */
771 for (p = sp->fts_child; p; p = p->fts_link)
774 /* Adjust the rest of the tree, including the current level. */
775 for (p = head; p->fts_level >= FTS_ROOTLEVEL;) {
777 p = p->fts_link ? p->fts_link : p->fts_parent;
782 fts_maxarglen(char * const *argv)
786 for (max = 0; *argv; ++argv)
787 if ((len = strlen(*argv)) > max)
793 * Change to dir specified by fd or p->fts_accpath without getting
794 * tricked by someone changing the world out from underneath us.
795 * Assumes p->fts_dev and p->fts_ino are filled in.
798 fts_safe_changedir(FTS *sp, FTSENT *p, int fd, const char *path)
800 int ret, oerrno, newfd;
804 if (ISSET(FTS_NOCHDIR))
806 if (fd < 0 && (newfd = open(path, O_RDONLY, 0)) < 0)
808 if (fstat(newfd, &sb)) {
812 if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) {
813 errno = ENOENT; /* disinformation */