17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
568a94dfcf * Common Development and Distribution License (the "License").
668a94dfcf * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
21e8031f0raf
227c478bdstevel@tonic-gate/*
237257d1braf * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate/*	Copyright (c) 1988 AT&T	*/
287c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
297c478bdstevel@tonic-gate
307257d1braf#pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bdstevel@tonic-gate
327c478bdstevel@tonic-gate/*
337c478bdstevel@tonic-gate *	nftw - new file tree walk
347c478bdstevel@tonic-gate *
357c478bdstevel@tonic-gate *	int nftw(char *path, int (*fn)(), int depth, int flags);
367c478bdstevel@tonic-gate *
377c478bdstevel@tonic-gate *	Derived from System V ftw() by David Korn
387c478bdstevel@tonic-gate *
397c478bdstevel@tonic-gate *	nftw visits each file and directory in the tree starting at
407c478bdstevel@tonic-gate *	path. It uses the generic directory reading library so it works
417c478bdstevel@tonic-gate *	for any file system type.  The flags field is used to specify:
4268a94dfcf *		FTW_PHYS  Physical walk, does not follow symbolic links
437c478bdstevel@tonic-gate *			  Otherwise, nftw will follow links but will not
447c478bdstevel@tonic-gate *			  walk down any path the crosses itself.
457c478bdstevel@tonic-gate *		FTW_MOUNT The walk will not cross a mount point.
467c478bdstevel@tonic-gate *		FTW_DEPTH All subdirectories will be visited before the
477c478bdstevel@tonic-gate *			  directory itself.
487c478bdstevel@tonic-gate *		FTW_CHDIR The walk will change to each directory before
497c478bdstevel@tonic-gate *			  reading it.  This is faster but core dumps
507c478bdstevel@tonic-gate *			  may not get generated.
517c478bdstevel@tonic-gate *
527c478bdstevel@tonic-gate *	The following flags are private, and are used by the find
537c478bdstevel@tonic-gate *	utility:
547c478bdstevel@tonic-gate *		FTW_ANYERR Call the callback function and return
557c478bdstevel@tonic-gate *			   FTW_NS on any stat failure, not just
567c478bdstevel@tonic-gate *			   lack of permission.
577c478bdstevel@tonic-gate *		FTW_HOPTION Use stat the first time the walk
587c478bdstevel@tonic-gate *			    function is called, regardless of
597c478bdstevel@tonic-gate *			    whether or not FTW_PHYS is specified.
6068a94dfcf *		FTW_NOLOOP Allow find utility to detect infinite loops created
6168a94dfcf *			   by both symbolic and hard linked directories.
627c478bdstevel@tonic-gate *
637c478bdstevel@tonic-gate *	fn is called with four arguments at each file and directory.
647c478bdstevel@tonic-gate *	The first argument is the pathname of the object, the second
657c478bdstevel@tonic-gate *	is a pointer to the stat buffer and the third is an integer
667c478bdstevel@tonic-gate *	giving additional information as follows:
677c478bdstevel@tonic-gate *
687c478bdstevel@tonic-gate *		FTW_F	The object is a file.
697c478bdstevel@tonic-gate *		FTW_D	The object is a directory.
707c478bdstevel@tonic-gate *		FTW_DP	The object is a directory and subdirectories
717c478bdstevel@tonic-gate *			have been visited.
727c478bdstevel@tonic-gate *		FTW_SL	The object is a symbolic link.
737c478bdstevel@tonic-gate *		FTW_SLN The object is a symbolic link pointing at a
747c478bdstevel@tonic-gate *		        non-existing file.
757c478bdstevel@tonic-gate *		FTW_DNR	The object is a directory that cannot be read.
767c478bdstevel@tonic-gate *			fn will not be called for any of its descendants.
777c478bdstevel@tonic-gate *		FTW_NS	Stat failed on the object because of lack of
787c478bdstevel@tonic-gate *			appropriate permission. The stat buffer passed to fn
797c478bdstevel@tonic-gate *			is undefined.  Stat failure for any reason is
807c478bdstevel@tonic-gate *			considered an error and nftw will return -1.
8168a94dfcf *	The following value is private, and is used by the find utility:
8268a94dfcf *		FTW_DL	An infinite loop has been detected.
837c478bdstevel@tonic-gate *	The fourth argument is a struct FTW* which contains the depth
847c478bdstevel@tonic-gate *	and the offset into pathname to the base name.
857c478bdstevel@tonic-gate *	If fn returns nonzero, nftw returns this value to its caller.
867c478bdstevel@tonic-gate *
877c478bdstevel@tonic-gate *	depth limits the number of open directories that ftw uses
887c478bdstevel@tonic-gate *	before it starts recycling file descriptors.  In general,
8968a94dfcf *	a file descriptor is used for each level.  When FTW_CHDIR isn't set,
9068a94dfcf *	in order to descend to arbitrary depths, nftw requires 2 file
9168a94dfcf *	descriptors to be open during the call to openat(), therefore if
9268a94dfcf *	the depth argument is less than 2 nftw will not use openat(), and
9368a94dfcf *	it will fail with ENAMETOOLONG if it descends to a directory that
9468a94dfcf *	exceeds PATH_MAX.
957c478bdstevel@tonic-gate *
967c478bdstevel@tonic-gate */
977c478bdstevel@tonic-gate
987c478bdstevel@tonic-gate#include "lint.h"
997c478bdstevel@tonic-gate#include <mtlib.h>
1007c478bdstevel@tonic-gate#include <sys/types.h>
1017c478bdstevel@tonic-gate#include <sys/stat.h>
1027c478bdstevel@tonic-gate#include <dirent.h>
1037c478bdstevel@tonic-gate#include <errno.h>
1047c478bdstevel@tonic-gate#include <limits.h>
1057c478bdstevel@tonic-gate#include <ftw.h>
1067c478bdstevel@tonic-gate#include <stdlib.h>
1077c478bdstevel@tonic-gate#include <string.h>
1087c478bdstevel@tonic-gate#include <unistd.h>
1097c478bdstevel@tonic-gate#include <thread.h>
1107c478bdstevel@tonic-gate#include <synch.h>
1117c478bdstevel@tonic-gate#include <stdio.h>
11268a94dfcf#include <strings.h>
11368a94dfcf#include <fcntl.h>
1147c478bdstevel@tonic-gate
1157257d1braf#if !defined(_LP64) && _FILE_OFFSET_BITS == 64
1167257d1braf#define	nftw	nftw64
1177257d1braf#define	stat	stat64
1187257d1braf#define	fstat	fstat64
1197257d1braf#define	fstatat	fstatat64
1207257d1braf#pragma weak _nftw64 = nftw64
1217257d1braf#else
1227257d1braf#pragma weak _nftw = nftw
1237257d1braf#endif /* !_LP64 && _FILE_OFFSET_BITS == 64 */
1247257d1braf
1257c478bdstevel@tonic-gate#ifndef PATH_MAX
1267c478bdstevel@tonic-gate#define	PATH_MAX	1023
1277c478bdstevel@tonic-gate#endif
1287c478bdstevel@tonic-gate
1297c478bdstevel@tonic-gate/*
1307c478bdstevel@tonic-gate * Local variables (used to be static local).
1317c478bdstevel@tonic-gate * Putting them into a structure that is passed
1327c478bdstevel@tonic-gate * around makes nftw() MT-safe with no locking required.
1337c478bdstevel@tonic-gate */
1347c478bdstevel@tonic-gatestruct Save {
1357c478bdstevel@tonic-gate	struct Save *last;
1367c478bdstevel@tonic-gate	DIR	*fd;
1377c478bdstevel@tonic-gate	char	*comp;
1387c478bdstevel@tonic-gate	long	here;
1397c478bdstevel@tonic-gate	dev_t	dev;
1407c478bdstevel@tonic-gate	ino_t	inode;
1417c478bdstevel@tonic-gate};
1427c478bdstevel@tonic-gate
14368a94dfcfstruct Var {
14468a94dfcf	char	*home;
14568a94dfcf	size_t	len;
14668a94dfcf	char	*fullpath;
14768a94dfcf	char	*tmppath;
14868a94dfcf	int	curflags;
14968a94dfcf	dev_t	cur_mount;
15068a94dfcf	struct FTW state;
15168a94dfcf	int	walklevel;
152b923897th	int	(*statf)(const char *, struct stat *, struct Save *, int flags);
153b923897th	int	(*savedstatf)(const char *, struct stat *, struct Save *,
154b923897th	    int flags);
15568a94dfcf	DIR	*(*opendirf)(const char *);
15668a94dfcf};
15768a94dfcf
1587c478bdstevel@tonic-gatestatic int oldclose(struct Save *);
159b923897thstatic int cdlstat(const char *, struct stat *, struct Save *, int flags);
160b923897thstatic int cdstat(const char *, struct stat *, struct Save *, int flags);
161b923897thstatic int nocdlstat(const char *, struct stat *, struct Save *, int flags);
162b923897thstatic int nocdstat(const char *, struct stat *, struct Save *, int flags);
16368a94dfcfstatic DIR *cdopendir(const char *);
16468a94dfcfstatic DIR *nocdopendir(const char *);
16568a94dfcfstatic const char *get_unrooted(const char *);
1667c478bdstevel@tonic-gate
1677c478bdstevel@tonic-gate/*
1687c478bdstevel@tonic-gate * This is the recursive walker.
1697c478bdstevel@tonic-gate */
1707c478bdstevel@tonic-gatestatic int
1717c478bdstevel@tonic-gatewalk(char *component,
1727c478bdstevel@tonic-gate    int (*fn)(const char *, const struct stat *, int, struct FTW *),
1737c478bdstevel@tonic-gate    int depth, struct Save *last, struct Var *vp)
1747c478bdstevel@tonic-gate{
1757c478bdstevel@tonic-gate	struct stat statb;
17668a94dfcf	char *p, *tmp;
1777c478bdstevel@tonic-gate	int type;
1787c478bdstevel@tonic-gate	char *comp;
1797c478bdstevel@tonic-gate	struct dirent *dir;
1807c478bdstevel@tonic-gate	char *q;
1817c478bdstevel@tonic-gate	int rc = 0;
1827c478bdstevel@tonic-gate	int val = -1;
1837c478bdstevel@tonic-gate	int cdval = -1;
1847c478bdstevel@tonic-gate	int oldbase;
1857c478bdstevel@tonic-gate	int skip;
1867c478bdstevel@tonic-gate	struct Save this;
18768a94dfcf	size_t base_comp, base_component, base_this_comp, base_last_comp;
18868a94dfcf	size_t base_fullpath, base_tmppath;
1897c478bdstevel@tonic-gate
1907c478bdstevel@tonic-gate	this.last = last;
1917c478bdstevel@tonic-gate	this.fd = 0;
1927c478bdstevel@tonic-gate	if ((vp->curflags & FTW_CHDIR) && last)
1937c478bdstevel@tonic-gate		comp = last->comp;
1947c478bdstevel@tonic-gate	else
1957c478bdstevel@tonic-gate		comp = vp->tmppath;
1967c478bdstevel@tonic-gate
1977c478bdstevel@tonic-gate	if (vp->savedstatf == NULL)
1987c478bdstevel@tonic-gate		vp->savedstatf = vp->statf;
1997c478bdstevel@tonic-gate
20068a94dfcf	if ((vp->walklevel++ == 0) && (vp->curflags & FTW_HOPTION)) {
20168a94dfcf		if (((vp->curflags & FTW_CHDIR) == 0) && (depth >= 2)) {
20268a94dfcf			vp->statf = nocdstat;
20368a94dfcf		} else {
20468a94dfcf			vp->statf = cdstat;
20568a94dfcf		}
20668a94dfcf	} else {
2077c478bdstevel@tonic-gate		vp->statf = vp->savedstatf;
20868a94dfcf	}
2097c478bdstevel@tonic-gate
2107c478bdstevel@tonic-gate	/*
2117c478bdstevel@tonic-gate	 * Determine the type of the component.
212b923897th	 *
213b923897th	 * Note that if the component is a trigger mount, this
214b923897th	 * will cause it to load.
2157c478bdstevel@tonic-gate	 */
216b923897th	if ((*vp->statf)(comp, &statb, last, _AT_TRIGGER) >= 0) {
2177c478bdstevel@tonic-gate		if ((statb.st_mode & S_IFMT) == S_IFDIR) {
2187c478bdstevel@tonic-gate			type = FTW_D;
2197c478bdstevel@tonic-gate			if (depth <= 1)
2207c478bdstevel@tonic-gate				(void) oldclose(last);
22168a94dfcf			if ((this.fd = (*vp->opendirf)(comp)) == 0) {
2227c478bdstevel@tonic-gate				if (errno == EMFILE && oldclose(last) &&
22368a94dfcf				    (this.fd = (*vp->opendirf)(comp)) != 0) {
22468a94dfcf					/*
22568a94dfcf					 * If opendirf fails because there
22668a94dfcf					 * are OPEN_MAX fd in the calling
22768a94dfcf					 * process, and we close the oldest
22868a94dfcf					 * fd, and another opendirf doesn't
22968a94dfcf					 * fail, depth is set to 1.
23068a94dfcf					 */
2317c478bdstevel@tonic-gate					depth = 1;
2327c478bdstevel@tonic-gate				} else {
2337c478bdstevel@tonic-gate					type = FTW_DNR;
2347c478bdstevel@tonic-gate					goto fail;
2357c478bdstevel@tonic-gate				}
2367c478bdstevel@tonic-gate			}
2377c478bdstevel@tonic-gate		} else if ((statb.st_mode & S_IFMT) == S_IFLNK) {
2387c478bdstevel@tonic-gate			type = FTW_SL;
2397c478bdstevel@tonic-gate		} else {
2407c478bdstevel@tonic-gate			type = FTW_F;
2417c478bdstevel@tonic-gate		}
2427c478bdstevel@tonic-gate	} else if ((vp->curflags & FTW_ANYERR) && errno != ENOENT) {
2437c478bdstevel@tonic-gate		/*
2447c478bdstevel@tonic-gate		 * If FTW_ANYERR is specified, then a stat error
2457c478bdstevel@tonic-gate		 * other than ENOENT automatically results in
2467c478bdstevel@tonic-gate		 * failure.  This allows the callback function
2477c478bdstevel@tonic-gate		 * to properly handle ENAMETOOLONG and ELOOP and
2487c478bdstevel@tonic-gate		 * things of that nature, that would be masked
2497c478bdstevel@tonic-gate		 * by calling lstat before failing.
2507c478bdstevel@tonic-gate		 */
2517c478bdstevel@tonic-gate		type = FTW_NS;
2527c478bdstevel@tonic-gate		goto fail;
2537c478bdstevel@tonic-gate	} else {
2547c478bdstevel@tonic-gate		/*
2557c478bdstevel@tonic-gate		 * Statf has failed. If stat was used instead of lstat,
2567c478bdstevel@tonic-gate		 * try using lstat. If lstat doesn't fail, "comp"
2577c478bdstevel@tonic-gate		 * must be a symbolic link pointing to a non-existent
2587c478bdstevel@tonic-gate		 * file. Such a symbolic link should be ignored.
2597c478bdstevel@tonic-gate		 * Also check the file type, if possible, for symbolic
2607c478bdstevel@tonic-gate		 * link.
2617c478bdstevel@tonic-gate		 */
26268a94dfcf		if (((vp->statf == cdstat) &&
263b923897th		    (cdlstat(comp, &statb, last, 0) >= 0) &&
26468a94dfcf		    ((statb.st_mode & S_IFMT) == S_IFLNK)) ||
26568a94dfcf		    ((vp->statf == nocdstat) &&
266b923897th		    (nocdlstat(comp, &statb, last, 0) >= 0) &&
26768a94dfcf		    ((statb.st_mode & S_IFMT) == S_IFLNK))) {
2687c478bdstevel@tonic-gate
2697c478bdstevel@tonic-gate			/*
2707c478bdstevel@tonic-gate			 * Ignore bad symbolic link, let "fn"
2717c478bdstevel@tonic-gate			 * report it.
2727c478bdstevel@tonic-gate			 */
2737c478bdstevel@tonic-gate
2747c478bdstevel@tonic-gate			errno = ENOENT;
2757c478bdstevel@tonic-gate			type = FTW_SLN;
2767c478bdstevel@tonic-gate		} else {
2777c478bdstevel@tonic-gate			type = FTW_NS;
2787c478bdstevel@tonic-gate	fail:
2797c478bdstevel@tonic-gate			/*
2807c478bdstevel@tonic-gate			 * if FTW_ANYERR is set in flags, we call
2817c478bdstevel@tonic-gate			 * the user function with FTW_NS set, regardless
2827c478bdstevel@tonic-gate			 * of the reason stat failed.
2837c478bdstevel@tonic-gate			 */
2847c478bdstevel@tonic-gate			if (!(vp->curflags & FTW_ANYERR))
2857c478bdstevel@tonic-gate				if (errno != EACCES)
2867c478bdstevel@tonic-gate					return (-1);
2877c478bdstevel@tonic-gate		}
2887c478bdstevel@tonic-gate	}
2897c478bdstevel@tonic-gate
2907c478bdstevel@tonic-gate	/*
2917c478bdstevel@tonic-gate	 * If the walk is not supposed to cross a mount point,
2927c478bdstevel@tonic-gate	 * and it did, get ready to return.
2937c478bdstevel@tonic-gate	 */
2947c478bdstevel@tonic-gate	if ((vp->curflags & FTW_MOUNT) && type != FTW_NS &&
2957c478bdstevel@tonic-gate	    statb.st_dev != vp->cur_mount)
2967c478bdstevel@tonic-gate		goto quit;
2977c478bdstevel@tonic-gate	vp->state.quit = 0;
2987c478bdstevel@tonic-gate
2997c478bdstevel@tonic-gate	/*
3007c478bdstevel@tonic-gate	 * If current component is not a directory, call user
3017c478bdstevel@tonic-gate	 * specified function and get ready to return.
3027c478bdstevel@tonic-gate	 */
3037c478bdstevel@tonic-gate	if (type != FTW_D || (vp->curflags & FTW_DEPTH) == 0)
3047c478bdstevel@tonic-gate		rc = (*fn)(vp->tmppath, &statb, type, &vp->state);
3057c478bdstevel@tonic-gate	if (rc > 0)
3067c478bdstevel@tonic-gate		val = rc;
3077c478bdstevel@tonic-gate	skip = (vp->state.quit & FTW_SKD);
3087c478bdstevel@tonic-gate	if (rc != 0 || type != FTW_D || (vp->state.quit & FTW_PRUNE))
3097c478bdstevel@tonic-gate		goto quit;
3107c478bdstevel@tonic-gate
3117c478bdstevel@tonic-gate	if (vp->tmppath[0] != '\0' && component[-1] != '/')
3127c478bdstevel@tonic-gate		*component++ = '/';
31368a94dfcf	*component = 0;
3147c478bdstevel@tonic-gate	if (vp->curflags & FTW_CHDIR) {
3157c478bdstevel@tonic-gate		struct stat statb2;
3167c478bdstevel@tonic-gate
3177c478bdstevel@tonic-gate		/*
3187c478bdstevel@tonic-gate		 * Security check (there is a window between
3197c478bdstevel@tonic-gate		 * (*vp->statf)() and opendir() above).
3207c478bdstevel@tonic-gate		 */
3217c478bdstevel@tonic-gate		if ((vp->curflags & FTW_PHYS) &&
3227c478bdstevel@tonic-gate		    (fstat(this.fd->dd_fd, &statb2) < 0 ||
3237c478bdstevel@tonic-gate		    statb2.st_ino != statb.st_ino ||
3247c478bdstevel@tonic-gate		    statb2.st_dev != statb.st_dev)) {
3257c478bdstevel@tonic-gate			errno = EAGAIN;
3267c478bdstevel@tonic-gate			rc = -1;
3277c478bdstevel@tonic-gate			goto quit;
3287c478bdstevel@tonic-gate		}
3297c478bdstevel@tonic-gate
3307c478bdstevel@tonic-gate		if ((cdval = fchdir(this.fd->dd_fd)) >= 0) {
3317c478bdstevel@tonic-gate			this.comp = component;
3327c478bdstevel@tonic-gate		} else {
3337c478bdstevel@tonic-gate			type = FTW_DNR;
3347c478bdstevel@tonic-gate			rc = (*fn)(vp->tmppath, &statb, type, &vp->state);
3357c478bdstevel@tonic-gate			goto quit;
3367c478bdstevel@tonic-gate		}
3377c478bdstevel@tonic-gate	}
3387c478bdstevel@tonic-gate
3397c478bdstevel@tonic-gate	/*
34068a94dfcf	 * If the walk has followed a symbolic link (FTW_PHYS is not set),
34168a94dfcf	 * traverse the walk back to make sure there is not a loop.
34268a94dfcf	 * The find utility (FTW_NOLOOP is set) detects infinite loops
34368a94dfcf	 * in both symbolic and hard linked directories.
3447c478bdstevel@tonic-gate	 */
34568a94dfcf	if ((vp->curflags & FTW_NOLOOP) ||
34668a94dfcf	    ((vp->curflags & FTW_PHYS) == 0)) {
3477c478bdstevel@tonic-gate		struct Save *sp = last;
3487c478bdstevel@tonic-gate		while (sp) {
3497c478bdstevel@tonic-gate			/*
3507c478bdstevel@tonic-gate			 * If the same node has already been visited, there
3517c478bdstevel@tonic-gate			 * is a loop. Get ready to return.
3527c478bdstevel@tonic-gate			 */
3537c478bdstevel@tonic-gate			if (sp->dev == statb.st_dev &&
35468a94dfcf			    sp->inode == statb.st_ino) {
35568a94dfcf				if (vp->curflags & FTW_NOLOOP) {
35668a94dfcf					/* private interface for find util */
35768a94dfcf					type = FTW_DL;
35868a94dfcf					goto fail;
35968a94dfcf				}
3607c478bdstevel@tonic-gate				goto quit;
36168a94dfcf			}
3627c478bdstevel@tonic-gate			sp = sp->last;
3637c478bdstevel@tonic-gate		}
3647c478bdstevel@tonic-gate	}
3657c478bdstevel@tonic-gate	this.dev = statb.st_dev;
3667c478bdstevel@tonic-gate	this.inode = statb.st_ino;
3677c478bdstevel@tonic-gate	oldbase = vp->state.base;
3687c478bdstevel@tonic-gate	vp->state.base = (int)(component - vp->tmppath);
3697c478bdstevel@tonic-gate	while (dir = readdir(this.fd)) {
3707c478bdstevel@tonic-gate		if (dir->d_ino == 0)
3717c478bdstevel@tonic-gate			continue;
3727c478bdstevel@tonic-gate		q = dir->d_name;
3737c478bdstevel@tonic-gate		if (*q == '.') {
3747c478bdstevel@tonic-gate			if (q[1] == 0)
3757c478bdstevel@tonic-gate				continue;
3767c478bdstevel@tonic-gate			else if (q[1] == '.' && q[2] == 0)
3777c478bdstevel@tonic-gate				continue;
3787c478bdstevel@tonic-gate		}
37968a94dfcf		if (last != NULL && last->comp != NULL) {
38068a94dfcf			base_last_comp = last->comp - vp->home;
38168a94dfcf		}
38268a94dfcf		base_comp = comp - vp->home;
38368a94dfcf		base_component = component - vp->home;
38468a94dfcf		if ((strlen(q) + strlen(vp->home) + 1) > vp->len) {
38568a94dfcf			/*
38668a94dfcf			 * When the space needed for vp->home has
38768a94dfcf			 * exceeded the amount of space that has
38868a94dfcf			 * been allocated, realloc() more space
38968a94dfcf			 * and adjust pointers to point to the
39068a94dfcf			 * (possibly moved) new block for vp->home
39168a94dfcf			 */
39268a94dfcf			base_this_comp = this.comp - vp->home;
39368a94dfcf			base_fullpath = vp->fullpath - vp->home;
39468a94dfcf			base_tmppath = vp->tmppath - vp->home;
39568a94dfcf			vp->len *= 2;
39668a94dfcf			tmp = (char *)realloc(vp->home, vp->len);
39768a94dfcf			if (tmp == NULL) {
39868a94dfcf				rc = -1;
39968a94dfcf				goto quit;
40068a94dfcf			}
40168a94dfcf			vp->home = tmp;
40268a94dfcf			comp = vp->home + base_comp;
40368a94dfcf			component = vp->home + base_component;
40468a94dfcf			this.comp = vp->home + base_this_comp;
40568a94dfcf			vp->fullpath = vp->home + base_fullpath;
40668a94dfcf			vp->tmppath = vp->home + base_tmppath;
40768a94dfcf			if (last != NULL && last->comp != NULL) {
40868a94dfcf				last->comp = vp->home + base_last_comp;
40968a94dfcf			}
41068a94dfcf		}
4117c478bdstevel@tonic-gate		p = component;
41268a94dfcf		while (*q != '\0')
4137c478bdstevel@tonic-gate			*p++ = *q++;
4147c478bdstevel@tonic-gate		*p = '\0';
4157c478bdstevel@tonic-gate		vp->state.level++;
4167c478bdstevel@tonic-gate
4177c478bdstevel@tonic-gate		/* Call walk() recursively.  */
4187c478bdstevel@tonic-gate		rc = walk(p, fn, depth-1, &this, vp);
41968a94dfcf		if (last != NULL && last->comp != NULL) {
42068a94dfcf			last->comp = vp->home + base_last_comp;
42168a94dfcf		}
42268a94dfcf		comp = vp->home + base_comp;
42368a94dfcf		component = vp->home + base_component;
4247c478bdstevel@tonic-gate		vp->state.level--;
4257c478bdstevel@tonic-gate		if (this.fd == 0) {
4267c478bdstevel@tonic-gate			*component = 0;
4277c478bdstevel@tonic-gate			if (vp->curflags & FTW_CHDIR) {
4287c478bdstevel@tonic-gate				this.fd = opendir(".");
4297c478bdstevel@tonic-gate			} else {
43068a94dfcf				this.fd = (*vp->opendirf)(comp);
4317c478bdstevel@tonic-gate			}
4327c478bdstevel@tonic-gate			if (this.fd == 0) {
4337c478bdstevel@tonic-gate				rc = -1;
4347c478bdstevel@tonic-gate				goto quit;
4357c478bdstevel@tonic-gate			}
4367c478bdstevel@tonic-gate			seekdir(this.fd, this.here);
4377c478bdstevel@tonic-gate		}
4387c478bdstevel@tonic-gate		if (rc != 0) {
4397c478bdstevel@tonic-gate			if (errno == ENOENT) {
4407c478bdstevel@tonic-gate				(void) fprintf(stderr, "cannot open %s: %s\n",
4417c478bdstevel@tonic-gate				    vp->tmppath, strerror(errno));
4427c478bdstevel@tonic-gate				val = rc;
4437c478bdstevel@tonic-gate				continue;
4447c478bdstevel@tonic-gate			}
4457c478bdstevel@tonic-gate			goto quit;	/* this seems extreme */
4467c478bdstevel@tonic-gate		}
4477c478bdstevel@tonic-gate	}
4487c478bdstevel@tonic-gate	vp->state.base = oldbase;
4497c478bdstevel@tonic-gate	*--component = 0;
4507c478bdstevel@tonic-gate	type = FTW_DP;
4517c478bdstevel@tonic-gate	if ((vp->tmppath[0] != '\0') && (vp->curflags & FTW_DEPTH) && !skip)
4527c478bdstevel@tonic-gate		rc = (*fn)(vp->tmppath, &statb, type, &vp->state);
4537c478bdstevel@tonic-gatequit:
4547c478bdstevel@tonic-gate	if (cdval >= 0 && last) {
4557c478bdstevel@tonic-gate		/* try to change back to previous directory */
4567c478bdstevel@tonic-gate		if (last->fd != NULL) {
4577c478bdstevel@tonic-gate			if (fchdir(last->fd->dd_fd) < 0) {
4587c478bdstevel@tonic-gate				rc = -1;
4597c478bdstevel@tonic-gate			}
4607c478bdstevel@tonic-gate		} else {
4617c478bdstevel@tonic-gate			if ((cdval = chdir("..")) >= 0) {
462b923897th				if ((*vp->statf)(".", &statb, last, 0) < 0 ||
4637c478bdstevel@tonic-gate				    statb.st_ino != last->inode ||
4647c478bdstevel@tonic-gate				    statb.st_dev != last->dev)
4657c478bdstevel@tonic-gate					cdval = -1;
4667c478bdstevel@tonic-gate			}
4677c478bdstevel@tonic-gate			*comp = 0;
4687c478bdstevel@tonic-gate			if (cdval < 0) {
4697c478bdstevel@tonic-gate				if (chdir(vp->fullpath) < 0) {
4707c478bdstevel@tonic-gate					rc = -1;
4717c478bdstevel@tonic-gate				} else {
472b923897th					/* Security check */
473b923897th					if ((vp->curflags & FTW_PHYS) &&
474b923897th					    ((*vp->statf)(".", &statb,
475b923897th					    last, 0) < 0 ||
476b923897th					    statb.st_ino != last->inode ||
477b923897th					    statb.st_dev != last->dev)) {
478b923897th						errno = EAGAIN;
479b923897th						rc = -1;
480b923897th					}
4817c478bdstevel@tonic-gate				}
4827c478bdstevel@tonic-gate			}
4837c478bdstevel@tonic-gate		}
4847c478bdstevel@tonic-gate	}
485b923897th
4867c478bdstevel@tonic-gate	if (this.fd)
4877c478bdstevel@tonic-gate		(void) closedir(this.fd);
4887c478bdstevel@tonic-gate	if (val > rc)
4897c478bdstevel@tonic-gate		return (val);
4907c478bdstevel@tonic-gate	else
4917c478bdstevel@tonic-gate		return (rc);
4927c478bdstevel@tonic-gate}
4937c478bdstevel@tonic-gate
4947c478bdstevel@tonic-gateint
4957257d1brafnftw(const char *path,
4967c478bdstevel@tonic-gate    int (*fn)(const char *, const struct stat *, int, struct FTW *),
4977c478bdstevel@tonic-gate    int depth, int flags)
4987c478bdstevel@tonic-gate{
4997c478bdstevel@tonic-gate	struct Var var;
5007c478bdstevel@tonic-gate	struct stat statb;
5017c478bdstevel@tonic-gate	int rc = -1;
5027c478bdstevel@tonic-gate	char *dp;
5037c478bdstevel@tonic-gate	char *base;
5047c478bdstevel@tonic-gate	char *endhome;
5057c478bdstevel@tonic-gate	const char *savepath = path;
5067c478bdstevel@tonic-gate	int save_errno;
5077c478bdstevel@tonic-gate
50868a94dfcf	var.walklevel = 0;
50968a94dfcf	var.len = 2*(PATH_MAX+1);
51068a94dfcf	var.home = (char *)malloc(var.len);
51168a94dfcf	if (var.home == NULL)
51268a94dfcf		return (-1);
51368a94dfcf
51468a94dfcf	var.home[0] = 0;
5157c478bdstevel@tonic-gate
5167c478bdstevel@tonic-gate	/*
5177c478bdstevel@tonic-gate	 * If the walk is going to change directory before
51868a94dfcf	 * reading it, save current working directory.
5197c478bdstevel@tonic-gate	 */
5207c478bdstevel@tonic-gate	if (flags & FTW_CHDIR) {
52168a94dfcf		if (getcwd(var.home, PATH_MAX+1) == 0) {
52268a94dfcf			free(var.home);
5237c478bdstevel@tonic-gate			return (-1);
52468a94dfcf		}
5257c478bdstevel@tonic-gate	}
52668a94dfcf	endhome = dp = var.home + strlen(var.home);
5277c478bdstevel@tonic-gate	if (*path == '/')
5287c478bdstevel@tonic-gate		var.fullpath = dp;
5297c478bdstevel@tonic-gate	else {
5307c478bdstevel@tonic-gate		*dp++ = '/';
53168a94dfcf		var.fullpath = var.home;
5327c478bdstevel@tonic-gate	}
5337c478bdstevel@tonic-gate	var.tmppath =  dp;
5347c478bdstevel@tonic-gate	base = dp-1;
53568a94dfcf	while (*path) {
5367c478bdstevel@tonic-gate		*dp = *path;
5377c478bdstevel@tonic-gate		if (*dp == '/')
5387c478bdstevel@tonic-gate			base = dp;
5397c478bdstevel@tonic-gate		dp++, path++;
5407c478bdstevel@tonic-gate	}
5417c478bdstevel@tonic-gate	*dp = 0;
5427c478bdstevel@tonic-gate	var.state.base = (int)(base + 1 - var.tmppath);
5437c478bdstevel@tonic-gate	if (*path) {
54468a94dfcf		free(var.home);
5457c478bdstevel@tonic-gate		errno = ENAMETOOLONG;
5467c478bdstevel@tonic-gate		return (-1);
5477c478bdstevel@tonic-gate	}
5487c478bdstevel@tonic-gate	var.curflags = flags;
5497c478bdstevel@tonic-gate
5507c478bdstevel@tonic-gate	/*
55168a94dfcf	 * If doing chdir()'s, set var.opendirf to cdopendir.
55268a94dfcf	 * If not doing chdir()'s and if nftw()'s depth arg >= 2,
55368a94dfcf	 * set var.opendirf to nocdopendir.  In order to
55468a94dfcf	 * descend to arbitrary depths without doing chdir()'s, nftw()
55568a94dfcf	 * requires a depth arg >= 2 so that nocdopendir() can use openat()
55668a94dfcf	 * to traverse the directories.  So when not doing
55768a94dfcf	 * chdir()'s if nftw()'s depth arg <= 1, set var.opendirf to
55868a94dfcf	 * cdopendir.
5597c478bdstevel@tonic-gate	 * If doing a physical walk (not following symbolic link), set
56068a94dfcf	 * var.statf to cdlstat() or nocdlstat(). Otherwise, set var.statf
56168a94dfcf	 * to cdstat() or nocdstat().
5627c478bdstevel@tonic-gate	 */
56368a94dfcf	if (((flags & FTW_CHDIR) == 0) && (depth >= 2)) {
56468a94dfcf		var.opendirf = nocdopendir;
56568a94dfcf		if (flags & FTW_PHYS)
56668a94dfcf			var.statf = nocdlstat;
56768a94dfcf		else
56868a94dfcf			var.statf = nocdstat;
56968a94dfcf	} else {
57068a94dfcf		var.opendirf = cdopendir;
57168a94dfcf		if (flags & FTW_PHYS)
57268a94dfcf			var.statf = cdlstat;
57368a94dfcf		else
57468a94dfcf			var.statf = cdstat;
57568a94dfcf	}
5767c478bdstevel@tonic-gate
5777c478bdstevel@tonic-gate	/*
5787c478bdstevel@tonic-gate	 * If walk is not going to cross a mount point,
5797c478bdstevel@tonic-gate	 * save the current mount point.
5807c478bdstevel@tonic-gate	 */
5817c478bdstevel@tonic-gate	if (flags & FTW_MOUNT) {
582b923897th		if ((*var.statf)(savepath, &statb, NULL, 0) >= 0)
5837c478bdstevel@tonic-gate			var.cur_mount = statb.st_dev;
5847c478bdstevel@tonic-gate		else
5857c478bdstevel@tonic-gate			goto done;
5867c478bdstevel@tonic-gate	}
5877c478bdstevel@tonic-gate	var.state.level = 0;
5887c478bdstevel@tonic-gate
5897c478bdstevel@tonic-gate	/*
5907c478bdstevel@tonic-gate	 * Call walk() which does most of the work.
5917c478bdstevel@tonic-gate	 * walk() uses errno in a rather obtuse way
5927c478bdstevel@tonic-gate	 * so we shield any incoming errno.
5937c478bdstevel@tonic-gate	 */
5947c478bdstevel@tonic-gate	save_errno = errno;
5957c478bdstevel@tonic-gate	errno = 0;
5967c478bdstevel@tonic-gate	var.savedstatf = NULL;
5977c478bdstevel@tonic-gate	rc = walk(dp, fn, depth, (struct Save *)0, &var);
5987c478bdstevel@tonic-gate	if (errno == 0)
5997c478bdstevel@tonic-gate		errno = save_errno;
6007c478bdstevel@tonic-gatedone:
6017c478bdstevel@tonic-gate	*endhome = 0;
6027c478bdstevel@tonic-gate	if (flags & FTW_CHDIR)
60368a94dfcf		(void) chdir(var.home);
60468a94dfcf	free(var.home);
6057c478bdstevel@tonic-gate	return (rc);
6067c478bdstevel@tonic-gate}
6077c478bdstevel@tonic-gate
6087c478bdstevel@tonic-gate/*
60968a94dfcf * Get stat info on path when FTW_CHDIR is set.
61068a94dfcf */
61168a94dfcf/*ARGSUSED1*/
61268a94dfcfstatic int
613b923897thcdstat(const char *path, struct stat *statp, struct Save *lp, int flags)
614b923897th{
615b923897th	return (fstatat(AT_FDCWD, path, statp, flags));
61668a94dfcf}
61768a94dfcf
61868a94dfcf/*
61968a94dfcf * Get lstat info on path when FTW_CHDIR is set.
62068a94dfcf */
62168a94dfcf/*ARGSUSED1*/
62268a94dfcfstatic int
623b923897thcdlstat(const char *path, struct stat *statp, struct Save *lp, int flags)
62468a94dfcf{
625b923897th	return (fstatat(AT_FDCWD, path, statp,
626b923897th	    flags | AT_SYMLINK_NOFOLLOW));
62768a94dfcf}
62868a94dfcf
62968a94dfcf/*
63068a94dfcf * Get stat info on path when FTW_CHDIR is not set.
63168a94dfcf */
63268a94dfcfstatic int
633b923897thnocdstat(const char *path, struct stat *statp, struct Save *lp, int flags)
63468a94dfcf{
635b923897th	int		fd;
636b923897th	const char	*basepath;
63768a94dfcf
63868a94dfcf	if (lp && lp->fd) {
63968a94dfcf		/* get basename of path */
640b923897th		basepath = get_unrooted(path);
641b923897th
642b923897th		fd = lp->fd->dd_fd;
64368a94dfcf	} else {
644b923897th		basepath = path;
645b923897th
646b923897th		fd = AT_FDCWD;
64768a94dfcf	}
648b923897th
649b923897th	return (fstatat(fd, basepath, statp, flags));
65068a94dfcf}
65168a94dfcf
65268a94dfcf/*
65368a94dfcf * Get lstat info on path when FTW_CHDIR is not set.
65468a94dfcf */
65568a94dfcfstatic int
656b923897thnocdlstat(const char *path, struct stat *statp, struct Save *lp, int flags)
65768a94dfcf{
658b923897th	int		fd;
659b923897th	const char	*basepath;
66068a94dfcf
66168a94dfcf	if (lp && lp->fd) {
66268a94dfcf		/* get basename of path */
663b923897th		basepath = get_unrooted(path);
664b923897th
665b923897th		fd = lp->fd->dd_fd;
66668a94dfcf	} else {
667b923897th		basepath = path;
668b923897th
669b923897th		fd = AT_FDCWD;
67068a94dfcf	}
671b923897th
672b923897th	return (fstatat(fd, basepath, statp, flags | AT_SYMLINK_NOFOLLOW));
67368a94dfcf}
67468a94dfcf
67568a94dfcf/*
67668a94dfcf * Open path directory when FTW_CHDIR is set.
67768a94dfcf *
67868a94dfcf */
67968a94dfcfstatic DIR *
68068a94dfcfcdopendir(const char *path)
68168a94dfcf{
68268a94dfcf	return (opendir(path));
68368a94dfcf}
68468a94dfcf
68568a94dfcf/*
68668a94dfcf * Open path directory when FTW_CHDIR is not set.
68768a94dfcf */
68868a94dfcfstatic DIR *
68968a94dfcfnocdopendir(const char *path)
69068a94dfcf{
69168a94dfcf	int fd, cfd;
69268a94dfcf	DIR *fdd;
69368a94dfcf	char *dirp, *token, *ptr;
69468a94dfcf
69568a94dfcf	if (((fdd = opendir(path)) == NULL) && (errno == ENAMETOOLONG)) {
69668a94dfcf		if ((dirp = strdup(path)) == NULL) {
69768a94dfcf			errno = ENAMETOOLONG;
69868a94dfcf			return (NULL);
69968a94dfcf		}
70068a94dfcf		if ((token = strtok_r(dirp, "/", &ptr)) != NULL) {
701b923897th			if ((fd = openat(AT_FDCWD, dirp, O_RDONLY)) < 0) {
702b923897th				(void) free(dirp);
703b923897th				errno = ENAMETOOLONG;
704b923897th				return (NULL);
705b923897th			}
706b923897th			while ((token = strtok_r(NULL, "/", &ptr)) != NULL) {
707b923897th				if ((cfd = openat(fd, token, O_RDONLY)) < 0) {
708b923897th					(void) close(fd);
709b923897th					(void) free(dirp);
710b923897th					errno = ENAMETOOLONG;
711b923897th					return (NULL);
712b923897th				}
713b923897th				(void) close(fd);
714b923897th				fd = cfd;
71568a94dfcf			}
716b923897th			(void) free(dirp);
717b923897th			return (fdopendir(fd));
71868a94dfcf		}
71968a94dfcf		(void) free(dirp);
72068a94dfcf		errno = ENAMETOOLONG;
72168a94dfcf	}
72268a94dfcf	return (fdd);
72368a94dfcf}
72468a94dfcf
725b923897th/*
726b923897th * return pointer basename of path, which may contain trailing slashes
727b923897th *
728b923897th * We do this when we do not chdir() on the input.
729b923897th */
73068a94dfcfstatic const char *
73168a94dfcfget_unrooted(const char *path)
73268a94dfcf{
73368a94dfcf	const char *ptr;
73468a94dfcf
73568a94dfcf	if (!path || !*path)
73668a94dfcf		return (NULL);
73768a94dfcf
73868a94dfcf	ptr = path + strlen(path);
73968a94dfcf	/* find last char in path before any trailing slashes */
74068a94dfcf	while (ptr != path && *--ptr == '/')
74168a94dfcf		;
74268a94dfcf
74368a94dfcf	if (ptr == path)	/* all slashes */
74468a94dfcf		return (ptr);
74568a94dfcf
74668a94dfcf	while (ptr != path)
74768a94dfcf		if (*--ptr == '/')
74868a94dfcf			return (++ptr);
74968a94dfcf
75068a94dfcf	return (ptr);
75168a94dfcf}
75268a94dfcf
75368a94dfcf/*
7547c478bdstevel@tonic-gate * close the oldest directory.  It saves the seek offset.
7557c478bdstevel@tonic-gate * return value is 0 unless it was unable to close any descriptor
7567c478bdstevel@tonic-gate */
7577c478bdstevel@tonic-gate
7587c478bdstevel@tonic-gatestatic int
7597c478bdstevel@tonic-gateoldclose(struct Save *sp)
7607c478bdstevel@tonic-gate{
7617c478bdstevel@tonic-gate	struct Save *spnext;
7627c478bdstevel@tonic-gate	while (sp) {
7637c478bdstevel@tonic-gate		spnext = sp->last;
7647c478bdstevel@tonic-gate		if (spnext == 0 || spnext->fd == 0)
7657c478bdstevel@tonic-gate			break;
7667c478bdstevel@tonic-gate		sp = spnext;
7677c478bdstevel@tonic-gate	}
7687c478bdstevel@tonic-gate	if (sp == 0 || sp->fd == 0)
7697c478bdstevel@tonic-gate		return (0);
7707c478bdstevel@tonic-gate	sp->here = telldir(sp->fd);
7717c478bdstevel@tonic-gate	(void) closedir(sp->fd);
7727c478bdstevel@tonic-gate	sp->fd = 0;
7737c478bdstevel@tonic-gate	return (1);
7747c478bdstevel@tonic-gate}
775