11573Srgrimes/*- 21573Srgrimes * Copyright (c) 1990, 1993, 1994 31573Srgrimes * The Regents of the University of California. All rights reserved. 41573Srgrimes * 51573Srgrimes * Redistribution and use in source and binary forms, with or without 61573Srgrimes * modification, are permitted provided that the following conditions 71573Srgrimes * are met: 81573Srgrimes * 1. Redistributions of source code must retain the above copyright 91573Srgrimes * notice, this list of conditions and the following disclaimer. 101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111573Srgrimes * notice, this list of conditions and the following disclaimer in the 121573Srgrimes * documentation and/or other materials provided with the distribution. 131573Srgrimes * 4. Neither the name of the University nor the names of its contributors 141573Srgrimes * may be used to endorse or promote products derived from this software 151573Srgrimes * without specific prior written permission. 161573Srgrimes * 171573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271573Srgrimes * SUCH DAMAGE. 2854770Sgreen * 2954770Sgreen * $OpenBSD: fts.c,v 1.22 1999/10/03 19:22:22 millert Exp $ 301573Srgrimes */ 311573Srgrimes 32129184Sbde#if 0 331573Srgrimes#if defined(LIBC_SCCS) && !defined(lint) 3423668Speterstatic char sccsid[] = "@(#)fts.c 8.6 (Berkeley) 8/14/94"; 351573Srgrimes#endif /* LIBC_SCCS and not lint */ 36129184Sbde#endif 37129184Sbde 3890045Sobrien#include <sys/cdefs.h> 3990045Sobrien__FBSDID("$FreeBSD$"); 401573Srgrimes 4171579Sdeischen#include "namespace.h" 421573Srgrimes#include <sys/param.h> 43129161Speadar#include <sys/mount.h> 441573Srgrimes#include <sys/stat.h> 451573Srgrimes 461573Srgrimes#include <dirent.h> 471573Srgrimes#include <errno.h> 481573Srgrimes#include <fcntl.h> 491573Srgrimes#include <stdlib.h> 501573Srgrimes#include <string.h> 511573Srgrimes#include <unistd.h> 52175688Syar#include "fts-compat.h" 5371579Sdeischen#include "un-namespace.h" 541573Srgrimes 55235647Sgleb#include "gen-private.h" 56235647Sgleb 57175688SyarFTSENT *__fts_children_44bsd(FTS *, int); 58175688Syarint __fts_close_44bsd(FTS *); 59175688Syarvoid *__fts_get_clientptr_44bsd(FTS *); 60175688SyarFTS *__fts_get_stream_44bsd(FTSENT *); 61175688SyarFTS *__fts_open_44bsd(char * const *, int, 62175688Syar int (*)(const FTSENT * const *, const FTSENT * const *)); 63175688SyarFTSENT *__fts_read_44bsd(FTS *); 64175688Syarint __fts_set_44bsd(FTS *, FTSENT *, int); 65175688Syarvoid __fts_set_clientptr_44bsd(FTS *, void *); 66175688Syar 6790045Sobrienstatic FTSENT *fts_alloc(FTS *, char *, int); 6890045Sobrienstatic FTSENT *fts_build(FTS *, int); 6990045Sobrienstatic void fts_lfree(FTSENT *); 7090045Sobrienstatic void fts_load(FTS *, FTSENT *); 7190045Sobrienstatic size_t fts_maxarglen(char * const *); 7290045Sobrienstatic void fts_padjust(FTS *, FTSENT *); 7390045Sobrienstatic int fts_palloc(FTS *, size_t); 7490045Sobrienstatic FTSENT *fts_sort(FTS *, FTSENT *, int); 7590045Sobrienstatic u_short fts_stat(FTS *, FTSENT *, int); 7690045Sobrienstatic int fts_safe_changedir(FTS *, FTSENT *, int, char *); 77129161Speadarstatic int fts_ufslinks(FTS *, const FTSENT *); 781573Srgrimes 7928913Simp#define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2]))) 801573Srgrimes 8128913Simp#define CLR(opt) (sp->fts_options &= ~(opt)) 8228913Simp#define ISSET(opt) (sp->fts_options & (opt)) 8328913Simp#define SET(opt) (sp->fts_options |= (opt)) 841573Srgrimes 851573Srgrimes#define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd)) 861573Srgrimes 871573Srgrimes/* fts_build flags */ 881573Srgrimes#define BCHILD 1 /* fts_children */ 891573Srgrimes#define BNAMES 2 /* fts_children, names only */ 901573Srgrimes#define BREAD 3 /* fts_read */ 911573Srgrimes 92129052Speadar/* 93129161Speadar * Internal representation of an FTS, including extra implementation 94129161Speadar * details. The FTS returned from fts_open points to this structure's 95129161Speadar * ftsp_fts member (and can be cast to an _fts_private as required) 96129052Speadar */ 97129052Speadarstruct _fts_private { 98129161Speadar FTS ftsp_fts; 99129161Speadar struct statfs ftsp_statfs; 100129161Speadar dev_t ftsp_dev; 101129161Speadar int ftsp_linksreliable; 102129052Speadar}; 103129052Speadar 104129052Speadar/* 105129161Speadar * The "FTS_NOSTAT" option can avoid a lot of calls to stat(2) if it 106129161Speadar * knows that a directory could not possibly have subdirectories. This 107129161Speadar * is decided by looking at the link count: a subdirectory would 108129161Speadar * increment its parent's link count by virtue of its own ".." entry. 109129161Speadar * This assumption only holds for UFS-like filesystems that implement 110129161Speadar * links and directories this way, so we must punt for others. 111129052Speadar */ 112129052Speadar 113129052Speadarstatic const char *ufslike_filesystems[] = { 114129052Speadar "ufs", 115219696Spjd "zfs", 116129052Speadar "nfs", 117129052Speadar "nfs4", 118129052Speadar "ext2fs", 119129052Speadar 0 120129052Speadar}; 121129052Speadar 1221573SrgrimesFTS * 123175688Syar__fts_open_44bsd(argv, options, compar) 1241573Srgrimes char * const *argv; 12590045Sobrien int options; 126103726Swollman int (*compar)(const FTSENT * const *, const FTSENT * const *); 1271573Srgrimes{ 128129052Speadar struct _fts_private *priv; 12990045Sobrien FTS *sp; 13090045Sobrien FTSENT *p, *root; 13190045Sobrien int nitems; 1321573Srgrimes FTSENT *parent, *tmp; 1331573Srgrimes int len; 1341573Srgrimes 1351573Srgrimes /* Options check. */ 1361573Srgrimes if (options & ~FTS_OPTIONMASK) { 1371573Srgrimes errno = EINVAL; 1381573Srgrimes return (NULL); 1391573Srgrimes } 1401573Srgrimes 141129184Sbde /* Allocate/initialize the stream. */ 142129161Speadar if ((priv = malloc(sizeof(*priv))) == NULL) 1431573Srgrimes return (NULL); 144129161Speadar memset(priv, 0, sizeof(*priv)); 145129052Speadar sp = &priv->ftsp_fts; 1461573Srgrimes sp->fts_compar = compar; 1471573Srgrimes sp->fts_options = options; 1481573Srgrimes 14954770Sgreen /* Shush, GCC. */ 15054770Sgreen tmp = NULL; 15154770Sgreen 1521573Srgrimes /* Logical walks turn on NOCHDIR; symbolic links are too hard. */ 1531573Srgrimes if (ISSET(FTS_LOGICAL)) 1541573Srgrimes SET(FTS_NOCHDIR); 1551573Srgrimes 1561573Srgrimes /* 1571573Srgrimes * Start out with 1K of path space, and enough, in any case, 1581573Srgrimes * to hold the user's paths. 1591573Srgrimes */ 1601573Srgrimes if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN))) 1611573Srgrimes goto mem1; 1621573Srgrimes 1631573Srgrimes /* Allocate/initialize root's parent. */ 1641573Srgrimes if ((parent = fts_alloc(sp, "", 0)) == NULL) 1651573Srgrimes goto mem2; 1661573Srgrimes parent->fts_level = FTS_ROOTPARENTLEVEL; 1671573Srgrimes 1681573Srgrimes /* Allocate/initialize root(s). */ 16964740Sgreen for (root = NULL, nitems = 0; *argv != NULL; ++argv, ++nitems) { 1701573Srgrimes /* Don't allow zero-length paths. */ 1711573Srgrimes if ((len = strlen(*argv)) == 0) { 1721573Srgrimes errno = ENOENT; 1731573Srgrimes goto mem3; 1741573Srgrimes } 1751573Srgrimes 1761573Srgrimes p = fts_alloc(sp, *argv, len); 1771573Srgrimes p->fts_level = FTS_ROOTLEVEL; 1781573Srgrimes p->fts_parent = parent; 1791573Srgrimes p->fts_accpath = p->fts_name; 1801573Srgrimes p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW)); 1811573Srgrimes 1821573Srgrimes /* Command-line "." and ".." are real directories. */ 1831573Srgrimes if (p->fts_info == FTS_DOT) 1841573Srgrimes p->fts_info = FTS_D; 1851573Srgrimes 1861573Srgrimes /* 1871573Srgrimes * If comparison routine supplied, traverse in sorted 1881573Srgrimes * order; otherwise traverse in the order specified. 1891573Srgrimes */ 1901573Srgrimes if (compar) { 1911573Srgrimes p->fts_link = root; 1921573Srgrimes root = p; 1931573Srgrimes } else { 1941573Srgrimes p->fts_link = NULL; 1951573Srgrimes if (root == NULL) 1961573Srgrimes tmp = root = p; 1971573Srgrimes else { 1981573Srgrimes tmp->fts_link = p; 1991573Srgrimes tmp = p; 2001573Srgrimes } 2011573Srgrimes } 2021573Srgrimes } 2031573Srgrimes if (compar && nitems > 1) 2041573Srgrimes root = fts_sort(sp, root, nitems); 2051573Srgrimes 2061573Srgrimes /* 2071573Srgrimes * Allocate a dummy pointer and make fts_read think that we've just 2081573Srgrimes * finished the node before the root(s); set p->fts_info to FTS_INIT 2091573Srgrimes * so that everything about the "current" node is ignored. 2101573Srgrimes */ 2111573Srgrimes if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL) 2121573Srgrimes goto mem3; 2131573Srgrimes sp->fts_cur->fts_link = root; 2141573Srgrimes sp->fts_cur->fts_info = FTS_INIT; 2151573Srgrimes 2161573Srgrimes /* 21754770Sgreen * If using chdir(2), grab a file descriptor pointing to dot to ensure 2181573Srgrimes * that we can get back here; this could be avoided for some paths, 2191573Srgrimes * but almost certainly not worth the effort. Slashes, symbolic links, 2201573Srgrimes * and ".." are all fairly nasty problems. Note, if we can't get the 2211573Srgrimes * descriptor we run anyway, just more slowly. 2221573Srgrimes */ 223241010Sjilles if (!ISSET(FTS_NOCHDIR) && 224241010Sjilles (sp->fts_rfd = _open(".", O_RDONLY | O_CLOEXEC, 0)) < 0) 2251573Srgrimes SET(FTS_NOCHDIR); 2261573Srgrimes 2271573Srgrimes return (sp); 2281573Srgrimes 2291573Srgrimesmem3: fts_lfree(root); 2301573Srgrimes free(parent); 2311573Srgrimesmem2: free(sp->fts_path); 2321573Srgrimesmem1: free(sp); 2331573Srgrimes return (NULL); 2341573Srgrimes} 2351573Srgrimes 2361573Srgrimesstatic void 2371573Srgrimesfts_load(sp, p) 2381573Srgrimes FTS *sp; 23990045Sobrien FTSENT *p; 2401573Srgrimes{ 24190045Sobrien int len; 24290045Sobrien char *cp; 2431573Srgrimes 2441573Srgrimes /* 2451573Srgrimes * Load the stream structure for the next traversal. Since we don't 2461573Srgrimes * actually enter the directory until after the preorder visit, set 2471573Srgrimes * the fts_accpath field specially so the chdir gets done to the right 2481573Srgrimes * place and the user can access the first node. From fts_open it's 2491573Srgrimes * known that the path will fit. 2501573Srgrimes */ 2511573Srgrimes len = p->fts_pathlen = p->fts_namelen; 2521573Srgrimes memmove(sp->fts_path, p->fts_name, len + 1); 2531573Srgrimes if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) { 2541573Srgrimes len = strlen(++cp); 2551573Srgrimes memmove(p->fts_name, cp, len + 1); 2561573Srgrimes p->fts_namelen = len; 2571573Srgrimes } 2581573Srgrimes p->fts_accpath = p->fts_path = sp->fts_path; 2591573Srgrimes sp->fts_dev = p->fts_dev; 2601573Srgrimes} 2611573Srgrimes 2621573Srgrimesint 263175688Syar__fts_close_44bsd(sp) 2641573Srgrimes FTS *sp; 2651573Srgrimes{ 26690045Sobrien FTSENT *freep, *p; 2671573Srgrimes int saved_errno; 2681573Srgrimes 2691573Srgrimes /* 2701573Srgrimes * This still works if we haven't read anything -- the dummy structure 2711573Srgrimes * points to the root list, so we step through to the end of the root 2721573Srgrimes * list which has a valid parent pointer. 2731573Srgrimes */ 2741573Srgrimes if (sp->fts_cur) { 2751573Srgrimes for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { 2761573Srgrimes freep = p; 27764740Sgreen p = p->fts_link != NULL ? p->fts_link : p->fts_parent; 2781573Srgrimes free(freep); 2791573Srgrimes } 2801573Srgrimes free(p); 2811573Srgrimes } 2821573Srgrimes 2831573Srgrimes /* Free up child linked list, sort array, path buffer. */ 2841573Srgrimes if (sp->fts_child) 2851573Srgrimes fts_lfree(sp->fts_child); 2861573Srgrimes if (sp->fts_array) 2871573Srgrimes free(sp->fts_array); 2881573Srgrimes free(sp->fts_path); 2891573Srgrimes 2901573Srgrimes /* Return to original directory, save errno if necessary. */ 2911573Srgrimes if (!ISSET(FTS_NOCHDIR)) { 2921573Srgrimes saved_errno = fchdir(sp->fts_rfd) ? errno : 0; 29356698Sjasone (void)_close(sp->fts_rfd); 29454770Sgreen 29554770Sgreen /* Set errno and return. */ 29654770Sgreen if (saved_errno != 0) { 29754770Sgreen /* Free up the stream pointer. */ 29854770Sgreen free(sp); 29954770Sgreen errno = saved_errno; 30054770Sgreen return (-1); 30154770Sgreen } 3021573Srgrimes } 3031573Srgrimes 30437349Sphk /* Free up the stream pointer. */ 30537349Sphk free(sp); 3061573Srgrimes return (0); 3071573Srgrimes} 3081573Srgrimes 3091573Srgrimes/* 31028913Simp * Special case of "/" at the end of the path so that slashes aren't 31128913Simp * appended which would cause paths to be written as "....//foo". 3121573Srgrimes */ 3131573Srgrimes#define NAPPEND(p) \ 31428913Simp (p->fts_path[p->fts_pathlen - 1] == '/' \ 31528913Simp ? p->fts_pathlen - 1 : p->fts_pathlen) 3161573Srgrimes 3171573SrgrimesFTSENT * 318175688Syar__fts_read_44bsd(sp) 31990045Sobrien FTS *sp; 3201573Srgrimes{ 32190045Sobrien FTSENT *p, *tmp; 32290045Sobrien int instr; 32390045Sobrien char *t; 3241573Srgrimes int saved_errno; 3251573Srgrimes 3261573Srgrimes /* If finished or unrecoverable error, return NULL. */ 3271573Srgrimes if (sp->fts_cur == NULL || ISSET(FTS_STOP)) 3281573Srgrimes return (NULL); 3291573Srgrimes 3301573Srgrimes /* Set current node pointer. */ 3311573Srgrimes p = sp->fts_cur; 3321573Srgrimes 3331573Srgrimes /* Save and zero out user instructions. */ 3341573Srgrimes instr = p->fts_instr; 3351573Srgrimes p->fts_instr = FTS_NOINSTR; 3361573Srgrimes 3371573Srgrimes /* Any type of file may be re-visited; re-stat and re-turn. */ 3381573Srgrimes if (instr == FTS_AGAIN) { 3391573Srgrimes p->fts_info = fts_stat(sp, p, 0); 3401573Srgrimes return (p); 3411573Srgrimes } 3421573Srgrimes 3431573Srgrimes /* 3441573Srgrimes * Following a symlink -- SLNONE test allows application to see 3451573Srgrimes * SLNONE and recover. If indirecting through a symlink, have 3461573Srgrimes * keep a pointer to current location. If unable to get that 3471573Srgrimes * pointer, follow fails. 3481573Srgrimes */ 3491573Srgrimes if (instr == FTS_FOLLOW && 3501573Srgrimes (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) { 3511573Srgrimes p->fts_info = fts_stat(sp, p, 1); 35254770Sgreen if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { 353241010Sjilles if ((p->fts_symfd = _open(".", O_RDONLY | O_CLOEXEC, 354241010Sjilles 0)) < 0) { 3551573Srgrimes p->fts_errno = errno; 3561573Srgrimes p->fts_info = FTS_ERR; 3571573Srgrimes } else 3581573Srgrimes p->fts_flags |= FTS_SYMFOLLOW; 35954770Sgreen } 3601573Srgrimes return (p); 3611573Srgrimes } 3621573Srgrimes 3631573Srgrimes /* Directory in pre-order. */ 3641573Srgrimes if (p->fts_info == FTS_D) { 3651573Srgrimes /* If skipped or crossed mount point, do post-order visit. */ 3661573Srgrimes if (instr == FTS_SKIP || 36728913Simp (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) { 3681573Srgrimes if (p->fts_flags & FTS_SYMFOLLOW) 36956698Sjasone (void)_close(p->fts_symfd); 3701573Srgrimes if (sp->fts_child) { 3711573Srgrimes fts_lfree(sp->fts_child); 3721573Srgrimes sp->fts_child = NULL; 3731573Srgrimes } 3741573Srgrimes p->fts_info = FTS_DP; 3751573Srgrimes return (p); 3768870Srgrimes } 3771573Srgrimes 3781573Srgrimes /* Rebuild if only read the names and now traversing. */ 37964740Sgreen if (sp->fts_child != NULL && ISSET(FTS_NAMEONLY)) { 38028913Simp CLR(FTS_NAMEONLY); 3811573Srgrimes fts_lfree(sp->fts_child); 3821573Srgrimes sp->fts_child = NULL; 3831573Srgrimes } 3841573Srgrimes 3851573Srgrimes /* 3861573Srgrimes * Cd to the subdirectory. 3871573Srgrimes * 3881573Srgrimes * If have already read and now fail to chdir, whack the list 3891573Srgrimes * to make the names come out right, and set the parent errno 3901573Srgrimes * so the application will eventually get an error condition. 3911573Srgrimes * Set the FTS_DONTCHDIR flag so that when we logically change 3921573Srgrimes * directories back to the parent we don't do a chdir. 3931573Srgrimes * 3941573Srgrimes * If haven't read do so. If the read fails, fts_build sets 3951573Srgrimes * FTS_STOP or the fts_info field of the node. 3961573Srgrimes */ 39764740Sgreen if (sp->fts_child != NULL) { 39877599Skris if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) { 3991573Srgrimes p->fts_errno = errno; 4001573Srgrimes p->fts_flags |= FTS_DONTCHDIR; 401129184Sbde for (p = sp->fts_child; p != NULL; 40264740Sgreen p = p->fts_link) 4031573Srgrimes p->fts_accpath = 4041573Srgrimes p->fts_parent->fts_accpath; 4051573Srgrimes } 4061573Srgrimes } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) { 4071573Srgrimes if (ISSET(FTS_STOP)) 4081573Srgrimes return (NULL); 4091573Srgrimes return (p); 4101573Srgrimes } 4111573Srgrimes p = sp->fts_child; 4121573Srgrimes sp->fts_child = NULL; 4131573Srgrimes goto name; 4141573Srgrimes } 4151573Srgrimes 4161573Srgrimes /* Move to the next node on this level. */ 4171573Srgrimesnext: tmp = p; 41864740Sgreen if ((p = p->fts_link) != NULL) { 4191573Srgrimes free(tmp); 4201573Srgrimes 4211573Srgrimes /* 42254770Sgreen * If reached the top, return to the original directory (or 42354770Sgreen * the root of the tree), and load the paths for the next root. 4241573Srgrimes */ 4251573Srgrimes if (p->fts_level == FTS_ROOTLEVEL) { 42628913Simp if (FCHDIR(sp, sp->fts_rfd)) { 4271573Srgrimes SET(FTS_STOP); 4281573Srgrimes return (NULL); 4291573Srgrimes } 4301573Srgrimes fts_load(sp, p); 4311573Srgrimes return (sp->fts_cur = p); 4321573Srgrimes } 4331573Srgrimes 4341573Srgrimes /* 4351573Srgrimes * User may have called fts_set on the node. If skipped, 4361573Srgrimes * ignore. If followed, get a file descriptor so we can 4371573Srgrimes * get back if necessary. 4381573Srgrimes */ 4391573Srgrimes if (p->fts_instr == FTS_SKIP) 4401573Srgrimes goto next; 4411573Srgrimes if (p->fts_instr == FTS_FOLLOW) { 4421573Srgrimes p->fts_info = fts_stat(sp, p, 1); 44354770Sgreen if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { 4441573Srgrimes if ((p->fts_symfd = 445241010Sjilles _open(".", O_RDONLY | O_CLOEXEC, 0)) < 0) { 4461573Srgrimes p->fts_errno = errno; 4471573Srgrimes p->fts_info = FTS_ERR; 4481573Srgrimes } else 4491573Srgrimes p->fts_flags |= FTS_SYMFOLLOW; 45054770Sgreen } 4511573Srgrimes p->fts_instr = FTS_NOINSTR; 4521573Srgrimes } 4531573Srgrimes 4541573Srgrimesname: t = sp->fts_path + NAPPEND(p->fts_parent); 4551573Srgrimes *t++ = '/'; 4561573Srgrimes memmove(t, p->fts_name, p->fts_namelen + 1); 4571573Srgrimes return (sp->fts_cur = p); 4581573Srgrimes } 4591573Srgrimes 4601573Srgrimes /* Move up to the parent node. */ 4611573Srgrimes p = tmp->fts_parent; 4621573Srgrimes free(tmp); 4631573Srgrimes 4641573Srgrimes if (p->fts_level == FTS_ROOTPARENTLEVEL) { 4651573Srgrimes /* 4661573Srgrimes * Done; free everything up and set errno to 0 so the user 4671573Srgrimes * can distinguish between error and EOF. 4681573Srgrimes */ 4691573Srgrimes free(p); 4701573Srgrimes errno = 0; 4711573Srgrimes return (sp->fts_cur = NULL); 4721573Srgrimes } 4731573Srgrimes 47454770Sgreen /* NUL terminate the pathname. */ 4751573Srgrimes sp->fts_path[p->fts_pathlen] = '\0'; 4761573Srgrimes 4771573Srgrimes /* 4781573Srgrimes * Return to the parent directory. If at a root node or came through 4791573Srgrimes * a symlink, go back through the file descriptor. Otherwise, cd up 4801573Srgrimes * one directory. 4811573Srgrimes */ 4821573Srgrimes if (p->fts_level == FTS_ROOTLEVEL) { 48328913Simp if (FCHDIR(sp, sp->fts_rfd)) { 4841573Srgrimes SET(FTS_STOP); 4851573Srgrimes return (NULL); 4861573Srgrimes } 4871573Srgrimes } else if (p->fts_flags & FTS_SYMFOLLOW) { 4881573Srgrimes if (FCHDIR(sp, p->fts_symfd)) { 4891573Srgrimes saved_errno = errno; 49056698Sjasone (void)_close(p->fts_symfd); 4911573Srgrimes errno = saved_errno; 4921573Srgrimes SET(FTS_STOP); 4931573Srgrimes return (NULL); 4941573Srgrimes } 49556698Sjasone (void)_close(p->fts_symfd); 49677497Skris } else if (!(p->fts_flags & FTS_DONTCHDIR) && 497129184Sbde fts_safe_changedir(sp, p->fts_parent, -1, "..")) { 49877599Skris SET(FTS_STOP); 49977599Skris return (NULL); 5001573Srgrimes } 5011573Srgrimes p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP; 5021573Srgrimes return (sp->fts_cur = p); 5031573Srgrimes} 5041573Srgrimes 5051573Srgrimes/* 5061573Srgrimes * Fts_set takes the stream as an argument although it's not used in this 5071573Srgrimes * implementation; it would be necessary if anyone wanted to add global 5081573Srgrimes * semantics to fts using fts_set. An error return is allowed for similar 5091573Srgrimes * reasons. 5101573Srgrimes */ 5111573Srgrimes/* ARGSUSED */ 5121573Srgrimesint 513175688Syar__fts_set_44bsd(sp, p, instr) 5141573Srgrimes FTS *sp; 5151573Srgrimes FTSENT *p; 5161573Srgrimes int instr; 5171573Srgrimes{ 51864740Sgreen if (instr != 0 && instr != FTS_AGAIN && instr != FTS_FOLLOW && 5191573Srgrimes instr != FTS_NOINSTR && instr != FTS_SKIP) { 5201573Srgrimes errno = EINVAL; 5211573Srgrimes return (1); 5221573Srgrimes } 5231573Srgrimes p->fts_instr = instr; 5241573Srgrimes return (0); 5251573Srgrimes} 5261573Srgrimes 5271573SrgrimesFTSENT * 528175688Syar__fts_children_44bsd(sp, instr) 52990045Sobrien FTS *sp; 5301573Srgrimes int instr; 5311573Srgrimes{ 53290045Sobrien FTSENT *p; 5331573Srgrimes int fd; 5341573Srgrimes 53564740Sgreen if (instr != 0 && instr != FTS_NAMEONLY) { 5361573Srgrimes errno = EINVAL; 5371573Srgrimes return (NULL); 5381573Srgrimes } 5391573Srgrimes 5401573Srgrimes /* Set current node pointer. */ 5411573Srgrimes p = sp->fts_cur; 5421573Srgrimes 5431573Srgrimes /* 5441573Srgrimes * Errno set to 0 so user can distinguish empty directory from 5451573Srgrimes * an error. 5461573Srgrimes */ 5471573Srgrimes errno = 0; 5481573Srgrimes 5491573Srgrimes /* Fatal errors stop here. */ 5501573Srgrimes if (ISSET(FTS_STOP)) 5511573Srgrimes return (NULL); 5521573Srgrimes 5531573Srgrimes /* Return logical hierarchy of user's arguments. */ 5541573Srgrimes if (p->fts_info == FTS_INIT) 5551573Srgrimes return (p->fts_link); 5561573Srgrimes 5571573Srgrimes /* 5581573Srgrimes * If not a directory being visited in pre-order, stop here. Could 5591573Srgrimes * allow FTS_DNR, assuming the user has fixed the problem, but the 5601573Srgrimes * same effect is available with FTS_AGAIN. 5611573Srgrimes */ 5621573Srgrimes if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */) 5631573Srgrimes return (NULL); 5641573Srgrimes 5651573Srgrimes /* Free up any previous child list. */ 56664740Sgreen if (sp->fts_child != NULL) 5671573Srgrimes fts_lfree(sp->fts_child); 5681573Srgrimes 5691573Srgrimes if (instr == FTS_NAMEONLY) { 57028913Simp SET(FTS_NAMEONLY); 5711573Srgrimes instr = BNAMES; 5728870Srgrimes } else 5731573Srgrimes instr = BCHILD; 5741573Srgrimes 5751573Srgrimes /* 5761573Srgrimes * If using chdir on a relative path and called BEFORE fts_read does 5771573Srgrimes * its chdir to the root of a traversal, we can lose -- we need to 5781573Srgrimes * chdir into the subdirectory, and we don't know where the current 5791573Srgrimes * directory is, so we can't get back so that the upcoming chdir by 5801573Srgrimes * fts_read will work. 5811573Srgrimes */ 5821573Srgrimes if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' || 5831573Srgrimes ISSET(FTS_NOCHDIR)) 5841573Srgrimes return (sp->fts_child = fts_build(sp, instr)); 5851573Srgrimes 586241010Sjilles if ((fd = _open(".", O_RDONLY | O_CLOEXEC, 0)) < 0) 5871573Srgrimes return (NULL); 5881573Srgrimes sp->fts_child = fts_build(sp, instr); 5891573Srgrimes if (fchdir(fd)) 5901573Srgrimes return (NULL); 59156698Sjasone (void)_close(fd); 5921573Srgrimes return (sp->fts_child); 5931573Srgrimes} 5941573Srgrimes 595103726Swollman#ifndef fts_get_clientptr 596103726Swollman#error "fts_get_clientptr not defined" 597103726Swollman#endif 598103726Swollman 599103726Swollmanvoid * 600175688Syar(__fts_get_clientptr_44bsd)(FTS *sp) 601103726Swollman{ 602103726Swollman 603103726Swollman return (fts_get_clientptr(sp)); 604103726Swollman} 605103726Swollman 606103726Swollman#ifndef fts_get_stream 607103726Swollman#error "fts_get_stream not defined" 608103726Swollman#endif 609103726Swollman 610103726SwollmanFTS * 611175688Syar(__fts_get_stream_44bsd)(FTSENT *p) 612103726Swollman{ 613103726Swollman return (fts_get_stream(p)); 614103726Swollman} 615103726Swollman 616103726Swollmanvoid 617175688Syar__fts_set_clientptr_44bsd(FTS *sp, void *clientptr) 618103726Swollman{ 619103726Swollman 620103726Swollman sp->fts_clientptr = clientptr; 621103726Swollman} 622103726Swollman 6231573Srgrimes/* 6241573Srgrimes * This is the tricky part -- do not casually change *anything* in here. The 6251573Srgrimes * idea is to build the linked list of entries that are used by fts_children 6261573Srgrimes * and fts_read. There are lots of special cases. 6271573Srgrimes * 6281573Srgrimes * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is 6291573Srgrimes * set and it's a physical walk (so that symbolic links can't be directories), 6301573Srgrimes * we can do things quickly. First, if it's a 4.4BSD file system, the type 6311573Srgrimes * of the file is in the directory entry. Otherwise, we assume that the number 6321573Srgrimes * of subdirectories in a node is equal to the number of links to the parent. 6331573Srgrimes * The former skips all stat calls. The latter skips stat calls in any leaf 6341573Srgrimes * directories and for any files after the subdirectories in the directory have 6351573Srgrimes * been found, cutting the stat calls by about 2/3. 6361573Srgrimes */ 6371573Srgrimesstatic FTSENT * 6381573Srgrimesfts_build(sp, type) 63990045Sobrien FTS *sp; 6401573Srgrimes int type; 6411573Srgrimes{ 64290045Sobrien struct dirent *dp; 64390045Sobrien FTSENT *p, *head; 64490045Sobrien int nitems; 6451573Srgrimes FTSENT *cur, *tail; 6461573Srgrimes DIR *dirp; 64754770Sgreen void *oldaddr; 648128946Skientzle size_t dnamlen; 64954770Sgreen int cderrno, descend, len, level, maxlen, nlinks, oflag, saved_errno, 65054770Sgreen nostat, doadjust; 6511573Srgrimes char *cp; 6521573Srgrimes 6531573Srgrimes /* Set current node pointer. */ 6541573Srgrimes cur = sp->fts_cur; 6551573Srgrimes 6561573Srgrimes /* 6571573Srgrimes * Open the directory for reading. If this fails, we're done. 6581573Srgrimes * If being called from fts_read, set the fts_info field. 6591573Srgrimes */ 66023668Speter#ifdef FTS_WHITEOUT 66123668Speter if (ISSET(FTS_WHITEOUT)) 662129184Sbde oflag = DTF_NODUP | DTF_REWIND; 66323668Speter else 664129184Sbde oflag = DTF_HIDEW | DTF_NODUP | DTF_REWIND; 66523668Speter#else 66623668Speter#define __opendir2(path, flag) opendir(path) 66723668Speter#endif 66823668Speter if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) { 6691573Srgrimes if (type == BREAD) { 6701573Srgrimes cur->fts_info = FTS_DNR; 6711573Srgrimes cur->fts_errno = errno; 6721573Srgrimes } 6731573Srgrimes return (NULL); 6741573Srgrimes } 6751573Srgrimes 6761573Srgrimes /* 6771573Srgrimes * Nlinks is the number of possible entries of type directory in the 6781573Srgrimes * directory if we're cheating on stat calls, 0 if we're not doing 6791573Srgrimes * any stat calls at all, -1 if we're doing stats on everything. 6801573Srgrimes */ 68154770Sgreen if (type == BNAMES) { 6821573Srgrimes nlinks = 0; 68354770Sgreen /* Be quiet about nostat, GCC. */ 68454770Sgreen nostat = 0; 68554770Sgreen } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) { 686129052Speadar if (fts_ufslinks(sp, cur)) 687129052Speadar nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2); 688129052Speadar else 689129052Speadar nlinks = -1; 69054770Sgreen nostat = 1; 69154770Sgreen } else { 6921573Srgrimes nlinks = -1; 69354770Sgreen nostat = 0; 69454770Sgreen } 6951573Srgrimes 6961573Srgrimes#ifdef notdef 6971573Srgrimes (void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink); 6981573Srgrimes (void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n", 6991573Srgrimes ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT)); 7001573Srgrimes#endif 7011573Srgrimes /* 7021573Srgrimes * If we're going to need to stat anything or we want to descend 7031573Srgrimes * and stay in the directory, chdir. If this fails we keep going, 7041573Srgrimes * but set a flag so we don't chdir after the post-order visit. 7051573Srgrimes * We won't be able to stat anything, but we can still return the 7061573Srgrimes * names themselves. Note, that since fts_read won't be able to 7071573Srgrimes * chdir into the directory, it will have to return different path 7081573Srgrimes * names than before, i.e. "a/b" instead of "b". Since the node 7091573Srgrimes * has already been visited in pre-order, have to wait until the 7101573Srgrimes * post-order visit to return the error. There is a special case 7111573Srgrimes * here, if there was nothing to stat then it's not an error to 7121573Srgrimes * not be able to stat. This is all fairly nasty. If a program 7131573Srgrimes * needed sorted entries or stat information, they had better be 7141573Srgrimes * checking FTS_NS on the returned nodes. 7151573Srgrimes */ 7161573Srgrimes cderrno = 0; 71754770Sgreen if (nlinks || type == BREAD) { 718235647Sgleb if (fts_safe_changedir(sp, cur, _dirfd(dirp), NULL)) { 7191573Srgrimes if (nlinks && type == BREAD) 7201573Srgrimes cur->fts_errno = errno; 7211573Srgrimes cur->fts_flags |= FTS_DONTCHDIR; 7221573Srgrimes descend = 0; 7231573Srgrimes cderrno = errno; 7241573Srgrimes } else 7251573Srgrimes descend = 1; 72654770Sgreen } else 7271573Srgrimes descend = 0; 7281573Srgrimes 7291573Srgrimes /* 7301573Srgrimes * Figure out the max file name length that can be stored in the 7311573Srgrimes * current path -- the inner loop allocates more path as necessary. 7321573Srgrimes * We really wouldn't have to do the maxlen calculations here, we 7331573Srgrimes * could do them in fts_read before returning the path, but it's a 7341573Srgrimes * lot easier here since the length is part of the dirent structure. 7351573Srgrimes * 7361573Srgrimes * If not changing directories set a pointer so that can just append 7371573Srgrimes * each new name into the path. 7381573Srgrimes */ 7391573Srgrimes len = NAPPEND(cur); 7401573Srgrimes if (ISSET(FTS_NOCHDIR)) { 7411573Srgrimes cp = sp->fts_path + len; 7421573Srgrimes *cp++ = '/'; 74354770Sgreen } else { 74454770Sgreen /* GCC, you're too verbose. */ 74554770Sgreen cp = NULL; 7461573Srgrimes } 74754770Sgreen len++; 74854770Sgreen maxlen = sp->fts_pathlen - len; 7491573Srgrimes 7501573Srgrimes level = cur->fts_level + 1; 7511573Srgrimes 7521573Srgrimes /* Read the directory, attaching each entry to the `link' pointer. */ 75354770Sgreen doadjust = 0; 75428913Simp for (head = tail = NULL, nitems = 0; dirp && (dp = readdir(dirp));) { 755128946Skientzle dnamlen = dp->d_namlen; 7561573Srgrimes if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) 7571573Srgrimes continue; 7581573Srgrimes 759128946Skientzle if ((p = fts_alloc(sp, dp->d_name, (int)dnamlen)) == NULL) 7601573Srgrimes goto mem1; 761128946Skientzle if (dnamlen >= maxlen) { /* include space for NUL */ 76254770Sgreen oldaddr = sp->fts_path; 763128946Skientzle if (fts_palloc(sp, dnamlen + len + 1)) { 7641573Srgrimes /* 7651573Srgrimes * No more memory for path or structures. Save 7661573Srgrimes * errno, free up the current structure and the 7671573Srgrimes * structures already allocated. 7681573Srgrimes */ 7691573Srgrimesmem1: saved_errno = errno; 7701573Srgrimes if (p) 7711573Srgrimes free(p); 7721573Srgrimes fts_lfree(head); 7731573Srgrimes (void)closedir(dirp); 7741573Srgrimes cur->fts_info = FTS_ERR; 7751573Srgrimes SET(FTS_STOP); 77654770Sgreen errno = saved_errno; 7771573Srgrimes return (NULL); 7781573Srgrimes } 77954770Sgreen /* Did realloc() change the pointer? */ 78054770Sgreen if (oldaddr != sp->fts_path) { 78154770Sgreen doadjust = 1; 78254770Sgreen if (ISSET(FTS_NOCHDIR)) 78354770Sgreen cp = sp->fts_path + len; 78454770Sgreen } 78554770Sgreen maxlen = sp->fts_pathlen - len; 7861573Srgrimes } 7871573Srgrimes 788128946Skientzle if (len + dnamlen >= USHRT_MAX) { 78954770Sgreen /* 79054770Sgreen * In an FTSENT, fts_pathlen is a u_short so it is 79154770Sgreen * possible to wraparound here. If we do, free up 79254770Sgreen * the current structure and the structures already 79354770Sgreen * allocated, then error out with ENAMETOOLONG. 79454770Sgreen */ 79554770Sgreen free(p); 79654770Sgreen fts_lfree(head); 79754770Sgreen (void)closedir(dirp); 79854770Sgreen cur->fts_info = FTS_ERR; 79954770Sgreen SET(FTS_STOP); 80054770Sgreen errno = ENAMETOOLONG; 80154770Sgreen return (NULL); 80254770Sgreen } 80354770Sgreen p->fts_level = level; 8041573Srgrimes p->fts_parent = sp->fts_cur; 805128946Skientzle p->fts_pathlen = len + dnamlen; 8061573Srgrimes 80723668Speter#ifdef FTS_WHITEOUT 80823668Speter if (dp->d_type == DT_WHT) 80923668Speter p->fts_flags |= FTS_ISW; 81023668Speter#endif 81123668Speter 8121573Srgrimes if (cderrno) { 8131573Srgrimes if (nlinks) { 8141573Srgrimes p->fts_info = FTS_NS; 8151573Srgrimes p->fts_errno = cderrno; 8161573Srgrimes } else 8171573Srgrimes p->fts_info = FTS_NSOK; 8181573Srgrimes p->fts_accpath = cur->fts_accpath; 8191573Srgrimes } else if (nlinks == 0 8201573Srgrimes#ifdef DT_DIR 82154770Sgreen || (nostat && 82217141Sjkh dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN) 8231573Srgrimes#endif 8241573Srgrimes ) { 8251573Srgrimes p->fts_accpath = 8261573Srgrimes ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name; 8271573Srgrimes p->fts_info = FTS_NSOK; 8281573Srgrimes } else { 8291573Srgrimes /* Build a file name for fts_stat to stat. */ 8301573Srgrimes if (ISSET(FTS_NOCHDIR)) { 8311573Srgrimes p->fts_accpath = p->fts_path; 8321573Srgrimes memmove(cp, p->fts_name, p->fts_namelen + 1); 8331573Srgrimes } else 8341573Srgrimes p->fts_accpath = p->fts_name; 8351573Srgrimes /* Stat it. */ 8361573Srgrimes p->fts_info = fts_stat(sp, p, 0); 8371573Srgrimes 8381573Srgrimes /* Decrement link count if applicable. */ 8391573Srgrimes if (nlinks > 0 && (p->fts_info == FTS_D || 8401573Srgrimes p->fts_info == FTS_DC || p->fts_info == FTS_DOT)) 8411573Srgrimes --nlinks; 8421573Srgrimes } 8431573Srgrimes 8441573Srgrimes /* We walk in directory order so "ls -f" doesn't get upset. */ 8451573Srgrimes p->fts_link = NULL; 8461573Srgrimes if (head == NULL) 8471573Srgrimes head = tail = p; 8481573Srgrimes else { 8491573Srgrimes tail->fts_link = p; 8501573Srgrimes tail = p; 8511573Srgrimes } 8521573Srgrimes ++nitems; 8531573Srgrimes } 85428913Simp if (dirp) 85528913Simp (void)closedir(dirp); 8561573Srgrimes 8571573Srgrimes /* 85854770Sgreen * If realloc() changed the address of the path, adjust the 85954770Sgreen * addresses for the rest of the tree and the dir list. 8601573Srgrimes */ 86154770Sgreen if (doadjust) 86254770Sgreen fts_padjust(sp, head); 8631573Srgrimes 8641573Srgrimes /* 8651573Srgrimes * If not changing directories, reset the path back to original 8661573Srgrimes * state. 8671573Srgrimes */ 8681573Srgrimes if (ISSET(FTS_NOCHDIR)) { 86954770Sgreen if (len == sp->fts_pathlen || nitems == 0) 8701573Srgrimes --cp; 8711573Srgrimes *cp = '\0'; 8721573Srgrimes } 8731573Srgrimes 8741573Srgrimes /* 8751573Srgrimes * If descended after called from fts_children or after called from 8761573Srgrimes * fts_read and nothing found, get back. At the root level we use 8771573Srgrimes * the saved fd; if one of fts_open()'s arguments is a relative path 8781573Srgrimes * to an empty directory, we wind up here with no other way back. If 8791573Srgrimes * can't get back, we're done. 8801573Srgrimes */ 8811573Srgrimes if (descend && (type == BCHILD || !nitems) && 8821573Srgrimes (cur->fts_level == FTS_ROOTLEVEL ? 88377599Skris FCHDIR(sp, sp->fts_rfd) : 88477599Skris fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) { 8851573Srgrimes cur->fts_info = FTS_ERR; 8861573Srgrimes SET(FTS_STOP); 8871573Srgrimes return (NULL); 8881573Srgrimes } 8891573Srgrimes 8901573Srgrimes /* If didn't find anything, return NULL. */ 8911573Srgrimes if (!nitems) { 8921573Srgrimes if (type == BREAD) 8931573Srgrimes cur->fts_info = FTS_DP; 8941573Srgrimes return (NULL); 8951573Srgrimes } 8961573Srgrimes 8971573Srgrimes /* Sort the entries. */ 8981573Srgrimes if (sp->fts_compar && nitems > 1) 8991573Srgrimes head = fts_sort(sp, head, nitems); 9001573Srgrimes return (head); 9011573Srgrimes} 9021573Srgrimes 9031573Srgrimesstatic u_short 9041573Srgrimesfts_stat(sp, p, follow) 9051573Srgrimes FTS *sp; 90690045Sobrien FTSENT *p; 9071573Srgrimes int follow; 9081573Srgrimes{ 90990045Sobrien FTSENT *t; 91090045Sobrien dev_t dev; 91190045Sobrien ino_t ino; 9121573Srgrimes struct stat *sbp, sb; 9131573Srgrimes int saved_errno; 9141573Srgrimes 9151573Srgrimes /* If user needs stat info, stat buffer already allocated. */ 9161573Srgrimes sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp; 9178870Srgrimes 91823668Speter#ifdef FTS_WHITEOUT 919129184Sbde /* Check for whiteout. */ 92023668Speter if (p->fts_flags & FTS_ISW) { 92123668Speter if (sbp != &sb) { 922129184Sbde memset(sbp, '\0', sizeof(*sbp)); 92323668Speter sbp->st_mode = S_IFWHT; 92423668Speter } 92523668Speter return (FTS_W); 92623668Speter } 92723668Speter#endif 92823668Speter 9291573Srgrimes /* 9301573Srgrimes * If doing a logical walk, or application requested FTS_FOLLOW, do 9311573Srgrimes * a stat(2). If that fails, check for a non-existent symlink. If 9321573Srgrimes * fail, set the errno from the stat call. 9331573Srgrimes */ 9341573Srgrimes if (ISSET(FTS_LOGICAL) || follow) { 9351573Srgrimes if (stat(p->fts_accpath, sbp)) { 9361573Srgrimes saved_errno = errno; 9371573Srgrimes if (!lstat(p->fts_accpath, sbp)) { 9381573Srgrimes errno = 0; 9391573Srgrimes return (FTS_SLNONE); 9408870Srgrimes } 9411573Srgrimes p->fts_errno = saved_errno; 9421573Srgrimes goto err; 9431573Srgrimes } 9441573Srgrimes } else if (lstat(p->fts_accpath, sbp)) { 9451573Srgrimes p->fts_errno = errno; 9461573Srgrimeserr: memset(sbp, 0, sizeof(struct stat)); 9471573Srgrimes return (FTS_NS); 9481573Srgrimes } 9491573Srgrimes 9501573Srgrimes if (S_ISDIR(sbp->st_mode)) { 9511573Srgrimes /* 9521573Srgrimes * Set the device/inode. Used to find cycles and check for 9531573Srgrimes * crossing mount points. Also remember the link count, used 9541573Srgrimes * in fts_build to limit the number of stat calls. It is 9551573Srgrimes * understood that these fields are only referenced if fts_info 9561573Srgrimes * is set to FTS_D. 9571573Srgrimes */ 9581573Srgrimes dev = p->fts_dev = sbp->st_dev; 9591573Srgrimes ino = p->fts_ino = sbp->st_ino; 9601573Srgrimes p->fts_nlink = sbp->st_nlink; 9611573Srgrimes 9621573Srgrimes if (ISDOT(p->fts_name)) 9631573Srgrimes return (FTS_DOT); 9641573Srgrimes 9651573Srgrimes /* 9661573Srgrimes * Cycle detection is done by brute force when the directory 9671573Srgrimes * is first encountered. If the tree gets deep enough or the 9681573Srgrimes * number of symbolic links to directories is high enough, 9691573Srgrimes * something faster might be worthwhile. 9701573Srgrimes */ 9711573Srgrimes for (t = p->fts_parent; 9721573Srgrimes t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent) 9731573Srgrimes if (ino == t->fts_ino && dev == t->fts_dev) { 9741573Srgrimes p->fts_cycle = t; 9751573Srgrimes return (FTS_DC); 9761573Srgrimes } 9771573Srgrimes return (FTS_D); 9781573Srgrimes } 9791573Srgrimes if (S_ISLNK(sbp->st_mode)) 9801573Srgrimes return (FTS_SL); 9811573Srgrimes if (S_ISREG(sbp->st_mode)) 9821573Srgrimes return (FTS_F); 9831573Srgrimes return (FTS_DEFAULT); 9841573Srgrimes} 9851573Srgrimes 986103726Swollman/* 987103726Swollman * The comparison function takes pointers to pointers to FTSENT structures. 988103726Swollman * Qsort wants a comparison function that takes pointers to void. 989103726Swollman * (Both with appropriate levels of const-poisoning, of course!) 990103726Swollman * Use a trampoline function to deal with the difference. 991103726Swollman */ 992103726Swollmanstatic int 993103726Swollmanfts_compar(const void *a, const void *b) 994103726Swollman{ 995103726Swollman FTS *parent; 996103726Swollman 997103726Swollman parent = (*(const FTSENT * const *)a)->fts_fts; 998103726Swollman return (*parent->fts_compar)(a, b); 999103726Swollman} 1000103726Swollman 10011573Srgrimesstatic FTSENT * 10021573Srgrimesfts_sort(sp, head, nitems) 10031573Srgrimes FTS *sp; 10041573Srgrimes FTSENT *head; 100590045Sobrien int nitems; 10061573Srgrimes{ 100790045Sobrien FTSENT **ap, *p; 10081573Srgrimes 10091573Srgrimes /* 10101573Srgrimes * Construct an array of pointers to the structures and call qsort(3). 10111573Srgrimes * Reassemble the array in the order returned by qsort. If unable to 10121573Srgrimes * sort for memory reasons, return the directory entries in their 10131573Srgrimes * current order. Allocate enough space for the current needs plus 10141573Srgrimes * 40 so don't realloc one entry at a time. 10151573Srgrimes */ 10161573Srgrimes if (nitems > sp->fts_nitems) { 10171573Srgrimes sp->fts_nitems = nitems + 40; 101864740Sgreen if ((sp->fts_array = reallocf(sp->fts_array, 101954770Sgreen sp->fts_nitems * sizeof(FTSENT *))) == NULL) { 10201573Srgrimes sp->fts_nitems = 0; 10211573Srgrimes return (head); 10221573Srgrimes } 10231573Srgrimes } 10241573Srgrimes for (ap = sp->fts_array, p = head; p; p = p->fts_link) 10251573Srgrimes *ap++ = p; 1026103726Swollman qsort(sp->fts_array, nitems, sizeof(FTSENT *), fts_compar); 10271573Srgrimes for (head = *(ap = sp->fts_array); --nitems; ++ap) 10281573Srgrimes ap[0]->fts_link = ap[1]; 10291573Srgrimes ap[0]->fts_link = NULL; 10301573Srgrimes return (head); 10311573Srgrimes} 10321573Srgrimes 10331573Srgrimesstatic FTSENT * 10341573Srgrimesfts_alloc(sp, name, namelen) 10351573Srgrimes FTS *sp; 10361573Srgrimes char *name; 103790045Sobrien int namelen; 10381573Srgrimes{ 103990045Sobrien FTSENT *p; 10401573Srgrimes size_t len; 10411573Srgrimes 1042103726Swollman struct ftsent_withstat { 1043103726Swollman FTSENT ent; 1044103726Swollman struct stat statbuf; 1045103726Swollman }; 1046103726Swollman 10471573Srgrimes /* 10481573Srgrimes * The file name is a variable length array and no stat structure is 10491573Srgrimes * necessary if the user has set the nostat bit. Allocate the FTSENT 10501573Srgrimes * structure, the file name and the stat structure in one chunk, but 1051103726Swollman * be careful that the stat structure is reasonably aligned. 10521573Srgrimes */ 1053103726Swollman if (ISSET(FTS_NOSTAT)) 1054103726Swollman len = sizeof(FTSENT) + namelen + 1; 1055103726Swollman else 1056103726Swollman len = sizeof(struct ftsent_withstat) + namelen + 1; 1057103726Swollman 10581573Srgrimes if ((p = malloc(len)) == NULL) 10591573Srgrimes return (NULL); 10601573Srgrimes 1061103726Swollman if (ISSET(FTS_NOSTAT)) { 1062103726Swollman p->fts_name = (char *)(p + 1); 1063103726Swollman p->fts_statp = NULL; 1064103726Swollman } else { 1065103726Swollman p->fts_name = (char *)((struct ftsent_withstat *)p + 1); 1066103726Swollman p->fts_statp = &((struct ftsent_withstat *)p)->statbuf; 1067103726Swollman } 1068103726Swollman 106954770Sgreen /* Copy the name and guarantee NUL termination. */ 1070103726Swollman memcpy(p->fts_name, name, namelen); 107154770Sgreen p->fts_name[namelen] = '\0'; 10721573Srgrimes p->fts_namelen = namelen; 10731573Srgrimes p->fts_path = sp->fts_path; 10741573Srgrimes p->fts_errno = 0; 10751573Srgrimes p->fts_flags = 0; 10761573Srgrimes p->fts_instr = FTS_NOINSTR; 10771573Srgrimes p->fts_number = 0; 10781573Srgrimes p->fts_pointer = NULL; 1079103726Swollman p->fts_fts = sp; 10801573Srgrimes return (p); 10811573Srgrimes} 10821573Srgrimes 10831573Srgrimesstatic void 10841573Srgrimesfts_lfree(head) 108590045Sobrien FTSENT *head; 10861573Srgrimes{ 108790045Sobrien FTSENT *p; 10881573Srgrimes 10891573Srgrimes /* Free a linked list of structures. */ 109028913Simp while ((p = head)) { 10911573Srgrimes head = head->fts_link; 10921573Srgrimes free(p); 10931573Srgrimes } 10941573Srgrimes} 10951573Srgrimes 10961573Srgrimes/* 10971573Srgrimes * Allow essentially unlimited paths; find, rm, ls should all work on any tree. 10981573Srgrimes * Most systems will allow creation of paths much longer than MAXPATHLEN, even 10991573Srgrimes * though the kernel won't resolve them. Add the size (not just what's needed) 11008870Srgrimes * plus 256 bytes so don't realloc the path 2 bytes at a time. 11011573Srgrimes */ 11021573Srgrimesstatic int 11031573Srgrimesfts_palloc(sp, more) 11041573Srgrimes FTS *sp; 11051573Srgrimes size_t more; 11061573Srgrimes{ 110754770Sgreen 11081573Srgrimes sp->fts_pathlen += more + 256; 110954770Sgreen /* 111054770Sgreen * Check for possible wraparound. In an FTS, fts_pathlen is 111154770Sgreen * a signed int but in an FTSENT it is an unsigned short. 111254770Sgreen * We limit fts_pathlen to USHRT_MAX to be safe in both cases. 111354770Sgreen */ 111454770Sgreen if (sp->fts_pathlen < 0 || sp->fts_pathlen >= USHRT_MAX) { 111554770Sgreen if (sp->fts_path) 111654770Sgreen free(sp->fts_path); 111754770Sgreen sp->fts_path = NULL; 111854770Sgreen errno = ENAMETOOLONG; 111954770Sgreen return (1); 112050790Simp } 112164740Sgreen sp->fts_path = reallocf(sp->fts_path, sp->fts_pathlen); 112264740Sgreen return (sp->fts_path == NULL); 112350790Simp} 112450790Simp 11251573Srgrimes/* 11261573Srgrimes * When the path is realloc'd, have to fix all of the pointers in structures 11271573Srgrimes * already returned. 11281573Srgrimes */ 11291573Srgrimesstatic void 113054770Sgreenfts_padjust(sp, head) 11311573Srgrimes FTS *sp; 113254770Sgreen FTSENT *head; 11331573Srgrimes{ 11341573Srgrimes FTSENT *p; 113554770Sgreen char *addr = sp->fts_path; 11361573Srgrimes 113764740Sgreen#define ADJUST(p) do { \ 113854770Sgreen if ((p)->fts_accpath != (p)->fts_name) { \ 113954770Sgreen (p)->fts_accpath = \ 114054770Sgreen (char *)addr + ((p)->fts_accpath - (p)->fts_path); \ 114154770Sgreen } \ 11421573Srgrimes (p)->fts_path = addr; \ 114364740Sgreen} while (0) 11441573Srgrimes /* Adjust the current set of children. */ 11451573Srgrimes for (p = sp->fts_child; p; p = p->fts_link) 114654770Sgreen ADJUST(p); 11471573Srgrimes 114854770Sgreen /* Adjust the rest of the tree, including the current level. */ 114954770Sgreen for (p = head; p->fts_level >= FTS_ROOTLEVEL;) { 115054770Sgreen ADJUST(p); 11511573Srgrimes p = p->fts_link ? p->fts_link : p->fts_parent; 11521573Srgrimes } 11531573Srgrimes} 11541573Srgrimes 11551573Srgrimesstatic size_t 11561573Srgrimesfts_maxarglen(argv) 11571573Srgrimes char * const *argv; 11581573Srgrimes{ 11591573Srgrimes size_t len, max; 11601573Srgrimes 11611573Srgrimes for (max = 0; *argv; ++argv) 11621573Srgrimes if ((len = strlen(*argv)) > max) 11631573Srgrimes max = len; 116454770Sgreen return (max + 1); 11651573Srgrimes} 116628913Simp 116728913Simp/* 116828913Simp * Change to dir specified by fd or p->fts_accpath without getting 116928913Simp * tricked by someone changing the world out from underneath us. 117028913Simp * Assumes p->fts_dev and p->fts_ino are filled in. 117128913Simp */ 117228913Simpstatic int 117377599Skrisfts_safe_changedir(sp, p, fd, path) 117428913Simp FTS *sp; 117528913Simp FTSENT *p; 117628913Simp int fd; 117777599Skris char *path; 117828913Simp{ 117928913Simp int ret, oerrno, newfd; 118028913Simp struct stat sb; 118128913Simp 118228913Simp newfd = fd; 118328913Simp if (ISSET(FTS_NOCHDIR)) 118428913Simp return (0); 1185241010Sjilles if (fd < 0 && (newfd = _open(path, O_RDONLY | O_CLOEXEC, 0)) < 0) 118628913Simp return (-1); 118771579Sdeischen if (_fstat(newfd, &sb)) { 118828913Simp ret = -1; 118928913Simp goto bail; 119028913Simp } 119128913Simp if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) { 119228913Simp errno = ENOENT; /* disinformation */ 119328913Simp ret = -1; 119428913Simp goto bail; 119528913Simp } 119628913Simp ret = fchdir(newfd); 119728913Simpbail: 119828913Simp oerrno = errno; 119928913Simp if (fd < 0) 120056698Sjasone (void)_close(newfd); 120128913Simp errno = oerrno; 120228913Simp return (ret); 120328913Simp} 1204129052Speadar 1205129052Speadar/* 1206129052Speadar * Check if the filesystem for "ent" has UFS-style links. 1207129052Speadar */ 1208129052Speadarstatic int 1209129052Speadarfts_ufslinks(FTS *sp, const FTSENT *ent) 1210129052Speadar{ 1211129052Speadar struct _fts_private *priv; 1212129052Speadar const char **cpp; 1213129052Speadar 1214129161Speadar priv = (struct _fts_private *)sp; 1215129052Speadar /* 1216129052Speadar * If this node's device is different from the previous, grab 1217129052Speadar * the filesystem information, and decide on the reliability 1218129052Speadar * of the link information from this filesystem for stat(2) 1219129052Speadar * avoidance. 1220129052Speadar */ 1221129052Speadar if (priv->ftsp_dev != ent->fts_dev) { 1222129052Speadar if (statfs(ent->fts_path, &priv->ftsp_statfs) != -1) { 1223129052Speadar priv->ftsp_dev = ent->fts_dev; 1224129052Speadar priv->ftsp_linksreliable = 0; 1225129052Speadar for (cpp = ufslike_filesystems; *cpp; cpp++) { 1226129052Speadar if (strcmp(priv->ftsp_statfs.f_fstypename, 1227129052Speadar *cpp) == 0) { 1228129052Speadar priv->ftsp_linksreliable = 1; 1229129052Speadar break; 1230129052Speadar } 1231129052Speadar } 1232129052Speadar } else { 1233129052Speadar priv->ftsp_linksreliable = 0; 1234129052Speadar } 1235129052Speadar } 1236129161Speadar return (priv->ftsp_linksreliable); 1237129052Speadar} 1238175688Syar 1239175688Syar__sym_compat(fts_open, __fts_open_44bsd, FBSD_1.0); 1240175688Syar__sym_compat(fts_close, __fts_close_44bsd, FBSD_1.0); 1241175688Syar__sym_compat(fts_read, __fts_read_44bsd, FBSD_1.0); 1242175688Syar__sym_compat(fts_set, __fts_set_44bsd, FBSD_1.0); 1243175688Syar__sym_compat(fts_children, __fts_children_44bsd, FBSD_1.0); 1244175688Syar__sym_compat(fts_get_clientptr, __fts_get_clientptr_44bsd, FBSD_1.0); 1245175688Syar__sym_compat(fts_get_stream, __fts_get_stream_44bsd, FBSD_1.0); 1246175688Syar__sym_compat(fts_set_clientptr, __fts_set_clientptr_44bsd, FBSD_1.0); 1247