xref: /illumos-gate/usr/src/lib/libc/port/gen/_xftw.c (revision 711890bc9379ceea66272dc8d4981812224ea86e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 /*
34  *	_xftw - file tree walk the uses expanded stat structure
35  *
36  *	int _xftw(path, fn, depth)  char *path; int (*fn)(); int depth;
37  *
38  *	Given a path name, _xftw starts from the file given by that path
39  *	name and visits each file and directory in the tree beneath
40  *	that file.  If a single file has multiple links within the
41  *	structure, it will be visited once for each such link.
42  *	For each object visited, fn is called with three arguments.
43  *		(*fn) (pathname, statp, ftwflag)
44  *	The first contains the path name of the object, the second
45  *	contains a pointer to a stat buffer which will usually hold
46  *	appropriate information for the object and the third will
47  *	contain an integer value giving additional information about
48  *
49  *		FTW_F	The object is a file for which stat was
50  *			successful.  It does not guarantee that the
51  *			file can actually be read.
52  *
53  *		FTW_D	The object is a directory for which stat and
54  *			open for read were both successful.
55  *
56  *		FTW_DNR	The object is a directory for which stat
57  *			succeeded, but which cannot be read.  Because
58  *			the directory cannot be read, fn will not be
59  *			called for any descendants of this directory.
60  *
61  *		FTW_NS	Stat failed on the object because of lack of
62  *			appropriate permission.  This indication will
63  *			be given for example for each file in a
64  *			directory with read but no execute permission.
65  *			Because stat failed, it is not possible to
66  *			determine whether this object is a file or a
67  *			directory.  The stat buffer passed to fn will
68  *			contain garbage.  Stat failure for any reason
69  *			other than lack of permission will be
70  *			considered an error and will cause _xftw to stop
71  *			and return -1 to its caller.
72  *
73  *	If fn returns nonzero, _xftw stops and returns the same value
74  *	to its caller.  If _xftw gets into other trouble along the way,
75  *	it returns -1 and leaves an indication of the cause in errno.
76  *
77  *	The third argument to _xftw does not limit the depth to which
78  *	_xftw will go.  Rather, it limits the depth to which _xftw will
79  *	go before it starts recycling file descriptors.  In general,
80  *	it is necessary to use a file descriptor for each level of the
81  *	tree, but they can be recycled for deep trees by saving the
82  *	position, closing, re-opening, and seeking.  It is possible
83  *	to start recycling file descriptors by sensing when we have
84  *	run out, but in general this will not be terribly useful if
85  *	fn expects to be able to open files.  We could also figure out
86  *	how many file descriptors are available and guarantee a certain
87  *	number to fn, but we would not know how many to guarantee,
88  *	and we do not want to impose the extra overhead on a caller who
89  *	knows how many are available without having to figure it out.
90  *
91  *	It is possible for _xftw to die with a memory fault in the event
92  *	of a file system so deeply nested that the stack overflows.
93  */
94 
95 /*
96  * this interface uses the expanded stat structure and therefore
97  * must have EFT enabled.
98  */
99 #ifdef _STYPES
100 #undef _STYPES
101 #endif
102 
103 #include <sys/feature_tests.h>
104 
105 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
106 #define	lstat64		_lstat64
107 #define	readdir64	_readdir64
108 #define	stat64		_stat64
109 #else
110 #define	lstat		_lstat
111 #define	readdir		_readdir
112 #define	stat		_stat
113 #endif /* !_LP64 && _FILE_OFFSET_BITS == 64 */
114 
115 #define	closedir	_closedir
116 #define	opendir		_opendir
117 #define	seekdir		_seekdir
118 #define	telldir		_telldir
119 
120 #include "lint.h"
121 #include <sys/types.h>
122 #include <sys/stat.h>
123 #include <sys/param.h>
124 #include <dirent.h>
125 #include <errno.h>
126 #include <ftw.h>
127 #include <string.h>
128 #include <stdlib.h>
129 #include <alloca.h>
130 
131 int
132 _xftw(int ver, const char *path,
133 	int (*fn)(const char *, const struct stat *, int), int depth)
134 {
135 	size_t	n;
136 	int rc;
137 	int save_errno;
138 	DIR *dirp;
139 	char *subpath;
140 	struct stat sb;
141 	struct dirent *direntp;
142 
143 
144 	/*
145 	 * Try to get file status.
146 	 * If unsuccessful, errno will say why.
147 	 * It's ok to have a symbolic link that points to
148 	 * non-existing file. In this case, pass FTW_NS
149 	 * to a function instead of aborting _xftw() right away.
150 	 */
151 	if (stat(path, &sb) < 0) {
152 #ifdef S_IFLNK
153 		save_errno = errno;
154 		if ((lstat(path, &sb) != -1) &&
155 				((sb.st_mode & S_IFMT) == S_IFLNK)) {
156 			errno = save_errno;
157 			return (*fn)(path, &sb, FTW_NS);
158 		} else  {
159 			errno = save_errno;
160 		}
161 #endif
162 		return (errno == EACCES? (*fn)(path, &sb, FTW_NS): -1);
163 	}
164 
165 	/*
166 	 *	The stat succeeded, so we know the object exists.
167 	 *	If not a directory, call the user function and return.
168 	 */
169 	if ((sb.st_mode & S_IFMT) != S_IFDIR)
170 		return ((*fn)(path, &sb, FTW_F));
171 
172 	/*
173 	 *	The object was a directory.
174 	 *
175 	 *	Open a file to read the directory
176 	 */
177 	dirp = opendir(path);
178 
179 	/*
180 	 *	Call the user function, telling it whether
181 	 *	the directory can be read.  If it can't be read
182 	 *	call the user function or indicate an error,
183 	 *	depending on the reason it couldn't be read.
184 	 */
185 	if (dirp == NULL)
186 		return (errno == EACCES? (*fn)(path, &sb, FTW_DNR): -1);
187 
188 	/* We could read the directory.  Call user function. */
189 	rc = (*fn)(path, &sb, FTW_D);
190 	if (rc != 0) {
191 		(void) closedir(dirp);
192 		return (rc);
193 	}
194 
195 	/* Create a prefix to which we will append component names */
196 	n = strlen(path);
197 	subpath = alloca(n + MAXNAMELEN + 2);
198 	(void) strcpy(subpath, path);
199 	if (subpath[0] != '\0' && subpath[n-1] != '/')
200 		subpath[n++] = '/';
201 
202 	/*
203 	 *	Read the directory one component at a time.
204 	 *	We must ignore "." and "..", but other than that,
205 	 *	just create a path name and call self to check it out.
206 	 */
207 	while (direntp = readdir(dirp)) {
208 		long here;
209 
210 		if (strcmp(direntp->d_name, ".") == 0 ||
211 		    strcmp(direntp->d_name, "..") == 0)
212 			continue;
213 
214 		/* Append component name to the working path */
215 		(void) strlcpy(&subpath[n], direntp->d_name, MAXNAMELEN);
216 
217 		/*
218 		 *	If we are about to exceed our depth,
219 		 *	remember where we are and close a file.
220 		 */
221 		if (depth <= 1) {
222 			here = telldir(dirp);
223 			if (closedir(dirp) < 0)
224 				return (-1);
225 		}
226 
227 		/*
228 		 *	Do a recursive call to process the file.
229 		 *	(watch this, sports fans)
230 		 */
231 		rc = _xftw(ver, subpath, fn, depth-1);
232 		if (rc != 0) {
233 			if (depth > 1)
234 				(void) closedir(dirp);
235 			return (rc);
236 		}
237 
238 		/*
239 		 *	If we closed the file, try to reopen it.
240 		 */
241 		if (depth <= 1) {
242 			dirp = opendir(path);
243 			if (dirp == NULL)
244 				return (-1);
245 			seekdir(dirp, here);
246 		}
247 	}
248 	(void) closedir(dirp);
249 	return (0);
250 }
251