4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
32 * _xftw - file tree walk the uses expanded stat structure
34 * int _xftw(path, fn, depth) char *path; int (*fn)(); int depth;
36 * Given a path name, _xftw starts from the file given by that path
37 * name and visits each file and directory in the tree beneath
38 * that file. If a single file has multiple links within the
39 * structure, it will be visited once for each such link.
40 * For each object visited, fn is called with three arguments.
41 * (*fn) (pathname, statp, ftwflag)
42 * The first contains the path name of the object, the second
43 * contains a pointer to a stat buffer which will usually hold
44 * appropriate information for the object and the third will
45 * contain an integer value giving additional information about
47 * FTW_F The object is a file for which stat was
48 * successful. It does not guarantee that the
49 * file can actually be read.
51 * FTW_D The object is a directory for which stat and
52 * open for read were both successful.
54 * FTW_DNR The object is a directory for which stat
55 * succeeded, but which cannot be read. Because
56 * the directory cannot be read, fn will not be
57 * called for any descendants of this directory.
59 * FTW_NS Stat failed on the object because of lack of
60 * appropriate permission. This indication will
61 * be given for example for each file in a
62 * directory with read but no execute permission.
63 * Because stat failed, it is not possible to
64 * determine whether this object is a file or a
65 * directory. The stat buffer passed to fn will
66 * contain garbage. Stat failure for any reason
67 * other than lack of permission will be
68 * considered an error and will cause _xftw to stop
69 * and return -1 to its caller.
71 * If fn returns nonzero, _xftw stops and returns the same value
72 * to its caller. If _xftw gets into other trouble along the way,
73 * it returns -1 and leaves an indication of the cause in errno.
75 * The third argument to _xftw does not limit the depth to which
76 * _xftw will go. Rather, it limits the depth to which _xftw will
77 * go before it starts recycling file descriptors. In general,
78 * it is necessary to use a file descriptor for each level of the
79 * tree, but they can be recycled for deep trees by saving the
80 * position, closing, re-opening, and seeking. In order to descend
81 * to arbitrary depths, _xftw requires 2 file descriptors to be open
82 * during the call to openat(), therefore if the depth argument
83 * is less than 2 _xftw will not use openat(), and it will fail with
84 * ENAMETOOLONG if it descends to a directory that exceeds PATH_MAX.
88 * this interface uses the expanded stat structure and therefore
89 * must have EFT enabled.
96 #include <sys/types.h>
99 #include <sys/param.h>
112 static DIR *nocdopendir(const char *, struct Var
*);
113 static int nocdstat(const char *, struct stat
*, struct Var
*, int);
114 static const char *get_unrooted(const char *);
115 static int fwalk(const char *, int (*)(const char *, const struct stat
*, int),
120 _xftw(int ver
, const char *path
,
121 int (*fn
)(const char *, const struct stat
*, int), int depth
)
128 rc
= fwalk(path
, fn
, depth
, &var
);
133 * This is the recursive walker.
136 fwalk(const char *path
, int (*fn
)(const char *, const struct stat
*, int),
137 int depth
, struct Var
*vp
)
145 struct dirent
*direntp
;
150 * Try to get file status.
151 * If unsuccessful, errno will say why.
152 * It's ok to have a symbolic link that points to
153 * non-existing file. In this case, pass FTW_NS
154 * to a function instead of aborting fwalk() right away.
156 if (nocdstat(path
, &sb
, vp
, 0) < 0) {
159 if ((nocdstat(path
, &sb
, vp
, AT_SYMLINK_NOFOLLOW
) != -1) &&
160 ((sb
.st_mode
& S_IFMT
) == S_IFLNK
)) {
162 return (*fn
)(path
, &sb
, FTW_NS
);
167 return (errno
== EACCES
? (*fn
)(path
, &sb
, FTW_NS
): -1);
171 * The stat succeeded, so we know the object exists.
172 * If not a directory, call the user function and return.
174 if ((sb
.st_mode
& S_IFMT
) != S_IFDIR
)
175 return ((*fn
)(path
, &sb
, FTW_F
));
178 * The object was a directory.
180 * Open a file to read the directory
182 dirp
= nocdopendir(path
, vp
);
185 * Call the user function, telling it whether
186 * the directory can be read. If it can't be read
187 * call the user function or indicate an error,
188 * depending on the reason it couldn't be read.
191 return (errno
== EACCES
? (*fn
)(path
, &sb
, FTW_DNR
): -1);
193 /* We could read the directory. Call user function. */
194 rc
= (*fn
)(path
, &sb
, FTW_D
);
196 (void) closedir(dirp
);
201 * Read the directory one component at a time.
202 * We must ignore "." and "..", but other than that,
203 * just create a path name and call self to check it out.
205 while (direntp
= readdir(dirp
)) {
208 if (strcmp(direntp
->d_name
, ".") == 0 ||
209 strcmp(direntp
->d_name
, "..") == 0)
212 /* Create a prefix to which we will append component names */
214 subpath
= malloc(n
+ strlen(direntp
->d_name
) + 2);
216 (void) closedir(dirp
);
220 (void) strcpy(subpath
, path
);
221 if (subpath
[0] != '\0' && subpath
[n
-1] != '/')
224 /* Append component name to the working path */
225 (void) strlcpy(&subpath
[n
], direntp
->d_name
, MAXNAMELEN
);
228 * If we are about to exceed our depth,
229 * remember where we are and close a file.
232 here
= telldir(dirp
);
233 if (closedir(dirp
) < 0) {
240 * Do a recursive call to process the file.
241 * (watch this, sports fans)
243 rc
= fwalk(subpath
, fn
, depth
-1, vp
);
247 (void) closedir(dirp
);
252 * If we closed the file, try to reopen it.
255 dirp
= nocdopendir(path
, vp
);
264 (void) closedir(dirp
);
269 * Open a directory with an arbitrarily long path name. If the original
270 * depth arg >= 2, use openat() to make sure that it doesn't fail with
274 nocdopendir(const char *path
, struct Var
*vp
)
278 char *dirp
, *token
, *ptr
;
281 if ((vp
->odepth
> 1) && (fdd
== NULL
) && (errno
== ENAMETOOLONG
)) {
283 * Traverse the path using openat() to get the fd for
286 if ((dirp
= strdup(path
)) == NULL
) {
287 errno
= ENAMETOOLONG
;
290 if ((token
= strtok_r(dirp
, "/", &ptr
)) != NULL
) {
291 if ((fd
= openat(AT_FDCWD
, dirp
, O_RDONLY
)) < 0) {
293 errno
= ENAMETOOLONG
;
296 while ((token
= strtok_r(NULL
, "/", &ptr
)) != NULL
) {
297 if ((cfd
= openat(fd
, token
, O_RDONLY
)) < 0) {
300 errno
= ENAMETOOLONG
;
307 return (fdopendir(fd
));
310 errno
= ENAMETOOLONG
;
316 * Stat a file with an arbitrarily long path name. If we aren't doing a
317 * stat on the arg passed to _xftw() and if the original depth arg >= 2,
318 * use openat() to make sure that it doesn't fail with ENAMETOOLONG.
321 nocdstat(const char *path
, struct stat
*statp
, struct Var
*vp
, int sym
)
324 char *dirp
, *token
, *ptr
;
329 rc
= fstatat(AT_FDCWD
, path
, statp
, sym
);
330 if ((vp
->level
> 1) && (vp
->odepth
>= 2) && (rc
< 0) &&
331 (errno
== ENAMETOOLONG
)) {
332 /* Traverse path using openat() to get fd for fstatat(). */
333 if ((dirp
= strdup(path
)) == NULL
) {
334 errno
= ENAMETOOLONG
;
337 if ((token
= strtok_r(dirp
, "/", &ptr
)) != NULL
) {
338 if ((fd
= openat(AT_FDCWD
, dirp
, O_RDONLY
)) < 0) {
340 errno
= ENAMETOOLONG
;
343 unrootp
= get_unrooted(path
);
344 while (((token
= strtok_r(NULL
, "/", &ptr
)) != NULL
) &&
345 (strcmp(token
, unrootp
) != 0)) {
346 if ((cfd
= openat(fd
, token
, O_RDONLY
)) < 0) {
349 errno
= ENAMETOOLONG
;
356 rc
= fstatat(fd
, unrootp
, statp
, sym
);
363 errno
= ENAMETOOLONG
;
369 * Return pointer basename of path. This routine doesn't remove
370 * trailing slashes, but there won't be any.
373 get_unrooted(const char *path
)
380 ptr
= path
+ strlen(path
);
381 /* find last char in path before any trailing slashes */
382 while (ptr
!= path
&& *--ptr
== '/')
385 if (ptr
== path
) /* all slashes */