9 /* $Id: compat_fts.c,v 1.6 2014/12/11 18:20:07 schwarze Exp $ */
10 /* $OpenBSD: fts.c,v 1.49 2014/11/23 00:14:22 guenther 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])))
65 #define MAX(a,b) (((a)>(b))?(a):(b))
70 #define CLR(opt) (sp->fts_options &= ~(opt))
71 #define ISSET(opt) (sp->fts_options & (opt))
72 #define SET(opt) (sp->fts_options |= (opt))
74 #define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd))
77 fts_open(char * const *argv, int options, void *dummy)
86 if (options & ~FTS_OPTIONMASK) {
91 /* Allocate/initialize the stream */
92 if ((sp = calloc(1, sizeof(FTS))) == NULL)
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, MAX(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 = NULL, nitems = 0; *argv; ++argv, ++nitems) {
110 /* Don't allow zero-length paths. */
111 if ((len = strlen(*argv)) == 0) {
116 if ((p = fts_alloc(sp, *argv, len)) == NULL)
118 p->fts_level = FTS_ROOTLEVEL;
119 p->fts_parent = parent;
120 p->fts_accpath = p->fts_name;
121 p->fts_info = fts_stat(sp, p);
123 /* Command-line "." and ".." are real directories. */
124 if (p->fts_info == FTS_DOT)
137 * Allocate a dummy pointer and make fts_read think that we've just
138 * finished the node before the root(s); set p->fts_info to FTS_INIT
139 * so that everything about the "current" node is ignored.
141 if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
143 sp->fts_cur->fts_link = root;
144 sp->fts_cur->fts_info = FTS_INIT;
147 * If using chdir(2), grab a file descriptor pointing to dot to ensure
148 * that we can get back here; this could be avoided for some paths,
149 * but almost certainly not worth the effort. Slashes, symbolic links,
150 * and ".." are all fairly nasty problems. Note, if we can't get the
151 * descriptor we run anyway, just more slowly.
153 if (!ISSET(FTS_NOCHDIR) &&
154 (sp->fts_rfd = open(".", O_RDONLY | O_CLOEXEC)) < 0)
162 mem3: fts_lfree(root);
164 mem2: free(sp->fts_path);
170 fts_load(FTS *sp, FTSENT *p)
176 * Load the stream structure for the next traversal. Since we don't
177 * actually enter the directory until after the preorder visit, set
178 * the fts_accpath field specially so the chdir gets done to the right
179 * place and the user can access the first node. From fts_open it's
180 * known that the path will fit.
182 len = p->fts_pathlen = p->fts_namelen;
183 memmove(sp->fts_path, p->fts_name, len + 1);
184 if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
186 memmove(p->fts_name, cp, len + 1);
187 p->fts_namelen = len;
189 p->fts_accpath = p->fts_path = sp->fts_path;
190 sp->fts_dev = p->fts_dev;
200 * This still works if we haven't read anything -- the dummy structure
201 * points to the root list, so we step through to the end of the root
202 * list which has a valid parent pointer.
205 for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
207 p = p->fts_link ? p->fts_link : p->fts_parent;
213 /* Stash the original directory fd if needed. */
214 rfd = ISSET(FTS_NOCHDIR) ? -1 : sp->fts_rfd;
216 /* Free up child linked list, sort array, path buffer, stream ptr.*/
218 fts_lfree(sp->fts_child);
222 /* Return to original directory, checking for error. */
235 * Special case of "/" at the end of the path so that slashes aren't
236 * appended which would cause paths to be written as "....//foo".
239 (p->fts_path[p->fts_pathlen - 1] == '/' \
240 ? p->fts_pathlen - 1 : p->fts_pathlen)
249 /* If finished or unrecoverable error, return NULL. */
250 if (sp->fts_cur == NULL || ISSET(FTS_STOP))
253 /* Set current node pointer. */
256 /* Save and zero out user instructions. */
257 instr = p->fts_instr;
258 p->fts_instr = FTS_NOINSTR;
260 /* Directory in pre-order. */
261 if (p->fts_info == FTS_D) {
262 /* If skipped or crossed mount point, do post-order visit. */
263 if (instr == FTS_SKIP ||
264 (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
266 fts_lfree(sp->fts_child);
267 sp->fts_child = NULL;
269 p->fts_info = FTS_DP;
274 * Cd to the subdirectory.
276 * If have already read and now fail to chdir, whack the list
277 * to make the names come out right, and set the parent errno
278 * so the application will eventually get an error condition.
279 * Set the FTS_DONTCHDIR flag so that when we logically change
280 * directories back to the parent we don't do a chdir.
282 * If haven't read do so. If the read fails, fts_build sets
283 * FTS_STOP or the fts_info field of the node.
286 if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) {
287 p->fts_errno = errno;
288 p->fts_flags |= FTS_DONTCHDIR;
289 for (p = sp->fts_child; p; p = p->fts_link)
291 p->fts_parent->fts_accpath;
293 } else if ((sp->fts_child = fts_build(sp)) == NULL) {
299 sp->fts_child = NULL;
303 /* Move to the next node on this level. */
305 if ((p = p->fts_link)) {
309 * If reached the top, return to the original directory (or
310 * the root of the tree), and load the paths for the next root.
312 if (p->fts_level == FTS_ROOTLEVEL) {
313 if (FCHDIR(sp, sp->fts_rfd)) {
318 return (sp->fts_cur = p);
322 * User may have called fts_set on the node. If skipped,
323 * ignore. If followed, get a file descriptor so we can
324 * get back if necessary.
326 if (p->fts_instr == FTS_SKIP)
329 name: t = sp->fts_path + NAPPEND(p->fts_parent);
331 memmove(t, p->fts_name, p->fts_namelen + 1);
332 return (sp->fts_cur = p);
335 /* Move up to the parent node. */
339 if (p->fts_level == FTS_ROOTPARENTLEVEL) {
341 * Done; free everything up and set errno to 0 so the user
342 * can distinguish between error and EOF.
346 return (sp->fts_cur = NULL);
349 /* NUL terminate the pathname. */
350 sp->fts_path[p->fts_pathlen] = '\0';
353 * Return to the parent directory. If at a root node or came through
354 * a symlink, go back through the file descriptor. Otherwise, cd up
357 if (p->fts_level == FTS_ROOTLEVEL) {
358 if (FCHDIR(sp, sp->fts_rfd)) {
363 } else if (!(p->fts_flags & FTS_DONTCHDIR) &&
364 fts_safe_changedir(sp, p->fts_parent, -1, "..")) {
369 p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
370 return (sp->fts_cur = p);
374 * Fts_set takes the stream as an argument although it's not used in this
375 * implementation; it would be necessary if anyone wanted to add global
376 * semantics to fts using fts_set. An error return is allowed for similar
381 fts_set(FTS *sp, FTSENT *p, int instr)
383 if (instr && instr != FTS_NOINSTR && instr != FTS_SKIP) {
387 p->fts_instr = instr;
392 * This is the tricky part -- do not casually change *anything* in here. The
393 * idea is to build the linked list of entries that are used by fts_children
394 * and fts_read. There are lots of special cases.
396 * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is
397 * set and it's a physical walk (so that symbolic links can't be directories),
398 * we can do things quickly. First, if it's a 4.4BSD file system, the type
399 * of the file is in the directory entry. Otherwise, we assume that the number
400 * of subdirectories in a node is equal to the number of links to the parent.
401 * The former skips all stat calls. The latter skips stat calls in any leaf
402 * directories and for any files after the subdirectories in the directory have
403 * been found, cutting the stat calls by about 2/3.
413 size_t dlen, len, maxlen;
414 int nitems, cderrno, descend, level, doadjust;
418 /* Set current node pointer. */
422 * Open the directory for reading. If this fails, we're done.
423 * If being called from fts_read, set the fts_info field.
425 if ((dirp = opendir(cur->fts_accpath)) == NULL) {
426 cur->fts_info = FTS_DNR;
427 cur->fts_errno = errno;
432 * If we're going to need to stat anything or we want to descend
433 * and stay in the directory, chdir. If this fails we keep going,
434 * but set a flag so we don't chdir after the post-order visit.
435 * We won't be able to stat anything, but we can still return the
436 * names themselves. Note, that since fts_read won't be able to
437 * chdir into the directory, it will have to return different path
438 * names than before, i.e. "a/b" instead of "b". Since the node
439 * has already been visited in pre-order, have to wait until the
440 * post-order visit to return the error. There is a special case
441 * here, if there was nothing to stat then it's not an error to
442 * not be able to stat. This is all fairly nasty. If a program
443 * needed sorted entries or stat information, they had better be
444 * checking FTS_NS on the returned nodes.
447 if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) {
448 cur->fts_errno = errno;
449 cur->fts_flags |= FTS_DONTCHDIR;
452 (void)closedir(dirp);
458 * Figure out the max file name length that can be stored in the
459 * current path -- the inner loop allocates more path as necessary.
460 * We really wouldn't have to do the maxlen calculations here, we
461 * could do them in fts_read before returning the path, but it's a
462 * lot easier here since the length is part of the dirent structure.
464 * If not changing directories set a pointer so that can just append
465 * each new name into the path.
468 if (ISSET(FTS_NOCHDIR)) {
469 cp = sp->fts_path + len;
473 maxlen = sp->fts_pathlen - len;
476 * fts_level is signed so we must prevent it from wrapping
477 * around to FTS_ROOTLEVEL and FTS_ROOTPARENTLEVEL.
479 level = cur->fts_level;
480 if (level < FTS_MAXLEVEL)
483 /* Read the directory, attaching each entry to the `link' pointer. */
485 for (head = tail = NULL, nitems = 0; dirp && (dp = readdir(dirp));) {
486 if (ISDOT(dp->d_name))
489 #if HAVE_DIRENT_NAMLEN
492 dlen = strlen(dp->d_name);
495 if (!(p = fts_alloc(sp, dp->d_name, dlen)))
497 if (dlen >= maxlen) { /* include space for NUL */
498 oldaddr = sp->fts_path;
499 if (fts_palloc(sp, dlen + len + 1)) {
501 * No more memory for path or structures. Save
502 * errno, free up the current structure and the
503 * structures already allocated.
505 mem1: saved_errno = errno;
509 (void)closedir(dirp);
510 cur->fts_info = FTS_ERR;
515 /* Did realloc() change the pointer? */
516 if (oldaddr != sp->fts_path) {
518 if (ISSET(FTS_NOCHDIR))
519 cp = sp->fts_path + len;
521 maxlen = sp->fts_pathlen - len;
524 p->fts_level = level;
525 p->fts_parent = sp->fts_cur;
526 p->fts_pathlen = len + dlen;
527 if (p->fts_pathlen < len) {
529 * If we wrap, free up the current structure and
530 * the structures already allocated, then error
531 * out with ENAMETOOLONG.
535 (void)closedir(dirp);
536 cur->fts_info = FTS_ERR;
538 errno = ENAMETOOLONG;
543 p->fts_info = FTS_NS;
544 p->fts_errno = cderrno;
545 p->fts_accpath = cur->fts_accpath;
547 /* Build a file name for fts_stat to stat. */
548 if (ISSET(FTS_NOCHDIR)) {
549 p->fts_accpath = p->fts_path;
550 memmove(cp, p->fts_name, p->fts_namelen + 1);
552 p->fts_accpath = p->fts_name;
554 p->fts_info = fts_stat(sp, p);
557 /* We walk in directory order so "ls -f" doesn't get upset. */
568 (void)closedir(dirp);
571 * If realloc() changed the address of the path, adjust the
572 * addresses for the rest of the tree and the dir list.
575 fts_padjust(sp, head);
578 * If not changing directories, reset the path back to original
581 if (ISSET(FTS_NOCHDIR)) {
582 if (len == sp->fts_pathlen || nitems == 0)
588 * If descended after called from fts_children or after called from
589 * fts_read and nothing found, get back. At the root level we use
590 * the saved fd; if one of fts_open()'s arguments is a relative path
591 * to an empty directory, we wind up here with no other way back. If
592 * can't get back, we're done.
594 if (descend && !nitems &&
595 (cur->fts_level == FTS_ROOTLEVEL ? FCHDIR(sp, sp->fts_rfd) :
596 fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) {
597 cur->fts_info = FTS_ERR;
602 /* If didn't find anything, return NULL. */
604 cur->fts_info = FTS_DP;
610 static unsigned short
611 fts_stat(FTS *sp, FTSENT *p)
618 /* If user needs stat info, stat buffer already allocated. */
621 if (lstat(p->fts_accpath, sbp)) {
622 p->fts_errno = errno;
623 memset(sbp, 0, sizeof(struct stat));
627 if (S_ISDIR(sbp->st_mode)) {
629 * Set the device/inode. Used to find cycles and check for
630 * crossing mount points. Also remember the link count, used
631 * in fts_build to limit the number of stat calls. It is
632 * understood that these fields are only referenced if fts_info
635 dev = p->fts_dev = sbp->st_dev;
636 ino = p->fts_ino = sbp->st_ino;
637 p->fts_nlink = sbp->st_nlink;
639 if (ISDOT(p->fts_name))
643 * Cycle detection is done by brute force when the directory
644 * is first encountered. If the tree gets deep enough or the
645 * number of symbolic links to directories is high enough,
646 * something faster might be worthwhile.
648 for (t = p->fts_parent;
649 t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
650 if (ino == t->fts_ino && dev == t->fts_dev) {
656 if (S_ISLNK(sbp->st_mode))
658 if (S_ISREG(sbp->st_mode))
660 return (FTS_DEFAULT);
664 fts_alloc(FTS *sp, const char *name, size_t namelen)
669 len = sizeof(FTSENT) + namelen;
670 if ((p = calloc(1, len)) == NULL)
673 p->fts_path = sp->fts_path;
674 p->fts_namelen = namelen;
675 p->fts_instr = FTS_NOINSTR;
676 p->fts_statp = malloc(sizeof(struct stat));
677 if (p->fts_statp == NULL) {
681 memcpy(p->fts_name, name, namelen);
687 fts_lfree(FTSENT *head)
691 /* Free a linked list of structures. */
693 head = head->fts_link;
699 * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
700 * Most systems will allow creation of paths much longer than PATH_MAX, even
701 * though the kernel won't resolve them. Add the size (not just what's needed)
702 * plus 256 bytes so don't realloc the path 2 bytes at a time.
705 fts_palloc(FTS *sp, size_t more)
710 * Check for possible wraparound.
713 if (sp->fts_pathlen + more < sp->fts_pathlen) {
717 errno = ENAMETOOLONG;
720 sp->fts_pathlen += more;
721 p = realloc(sp->fts_path, sp->fts_pathlen);
733 * When the path is realloc'd, have to fix all of the pointers in structures
737 fts_padjust(FTS *sp, FTSENT *head)
740 char *addr = sp->fts_path;
742 #define ADJUST(p) { \
743 if ((p)->fts_accpath != (p)->fts_name) { \
745 (char *)addr + ((p)->fts_accpath - (p)->fts_path); \
747 (p)->fts_path = addr; \
749 /* Adjust the current set of children. */
750 for (p = sp->fts_child; p; p = p->fts_link)
753 /* Adjust the rest of the tree, including the current level. */
754 for (p = head; p->fts_level >= FTS_ROOTLEVEL;) {
756 p = p->fts_link ? p->fts_link : p->fts_parent;
761 fts_maxarglen(char * const *argv)
765 for (max = 0; *argv; ++argv)
766 if ((len = strlen(*argv)) > max)
772 * Change to dir specified by fd or p->fts_accpath without getting
773 * tricked by someone changing the world out from underneath us.
774 * Assumes p->fts_dev and p->fts_ino are filled in.
777 fts_safe_changedir(FTS *sp, FTSENT *p, int fd, const char *path)
779 int ret, oerrno, newfd;
783 if (ISSET(FTS_NOCHDIR))
785 if (fd < 0 && (newfd = open(path, O_RDONLY|O_DIRECTORY|O_CLOEXEC)) < 0)
787 if (fstat(newfd, &sb)) {
791 if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) {
792 errno = ENOENT; /* disinformation */