2N/A * The contents of this file are subject to the terms of the 2N/A * Common Development and Distribution License (the "License"). 2N/A * You may not use this file except in compliance with the License. 2N/A * See the License for the specific language governing permissions 2N/A * and limitations under the License. 2N/A * When distributing Covered Code, include this CDDL HEADER in each 2N/A * If applicable, add the following below this CDDL HEADER, with the 2N/A * fields enclosed by brackets "[]" replaced with your own identifying 2N/A * information: Portions Copyright [yyyy] [name of copyright owner] 2N/A * Copyright (c) 1989, 2011, Oracle and/or its affiliates. All rights reserved. 2N/A/* Copyright (c) 1988 AT&T */ 2N/A/* All Rights Reserved */ 2N/A * _xftw - file tree walk the uses expanded stat structure 2N/A * int _xftw(path, fn, depth) char *path; int (*fn)(); int depth; 2N/A * Given a path name, _xftw starts from the file given by that path 2N/A * name and visits each file and directory in the tree beneath 2N/A * that file. If a single file has multiple links within the 2N/A * structure, it will be visited once for each such link. 2N/A * For each object visited, fn is called with three arguments. 2N/A * (*fn) (pathname, statp, ftwflag) 2N/A * The first contains the path name of the object, the second 2N/A * contains a pointer to a stat buffer which will usually hold 2N/A * appropriate information for the object and the third will 2N/A * contain an integer value giving additional information about 2N/A * FTW_F The object is a file for which stat was 2N/A * successful. It does not guarantee that the 2N/A * file can actually be read. 2N/A * FTW_D The object is a directory for which stat and 2N/A * open for read were both successful. 2N/A * FTW_DNR The object is a directory for which stat 2N/A * succeeded, but which cannot be read. Because 2N/A * the directory cannot be read, fn will not be 2N/A * called for any descendants of this directory. 2N/A * FTW_NS Stat failed on the object because of lack of 2N/A * appropriate permission. This indication will 2N/A * be given for example for each file in a 2N/A * directory with read but no execute permission. 2N/A * Because stat failed, it is not possible to 2N/A * determine whether this object is a file or a 2N/A * directory. The stat buffer passed to fn will 2N/A * contain garbage. Stat failure for any reason 2N/A * other than lack of permission will be 2N/A * considered an error and will cause _xftw to stop 2N/A * and return -1 to its caller. 2N/A * If fn returns nonzero, _xftw stops and returns the same value 2N/A * to its caller. If _xftw gets into other trouble along the way, 2N/A * it returns -1 and leaves an indication of the cause in errno. 2N/A * The third argument to _xftw does not limit the depth to which 2N/A * _xftw will go. Rather, it limits the depth to which _xftw will 2N/A * go before it starts recycling file descriptors. In general, 2N/A * it is necessary to use a file descriptor for each level of the 2N/A * tree, but they can be recycled for deep trees by saving the 2N/A * position, closing, re-opening, and seeking. In order to descend 2N/A * to arbitrary depths, _xftw requires 2 file descriptors to be open 2N/A * during the call to openat(), therefore if the depth argument 2N/A * is less than 2 _xftw will not use openat(), and it will fail with 2N/A * ENAMETOOLONG if it descends to a directory that exceeds PATH_MAX. 2N/A * this interface uses the expanded stat structure and therefore 2N/A * must have EFT enabled. 2N/A#
endif /* !_LP64 && _FILE_OFFSET_BITS == 64 */ 2N/Astatic int fwalk(
const char *,
int (*)(
const char *,
const struct stat *,
int),
2N/A * This is the recursive walker. 2N/A * Try to get file status. 2N/A * If unsuccessful, errno will say why. 2N/A * It's ok to have a symbolic link that points to 2N/A * non-existing file. In this case, pass FTW_NS 2N/A * to a function instead of aborting fwalk() right away. 2N/A * The stat succeeded, so we know the object exists. 2N/A * If not a directory, call the user function and return. 2N/A * The object was a directory. 2N/A * Open a file to read the directory 2N/A * Call the user function, telling it whether 2N/A * the directory can be read. If it can't be read 2N/A * call the user function or indicate an error, 2N/A * depending on the reason it couldn't be read. 2N/A /* We could read the directory. Call user function. */ 2N/A * Read the directory one component at a time. 2N/A * We must ignore "." and "..", but other than that, 2N/A * just create a path name and call self to check it out. 2N/A /* Create a prefix to which we will append component names */ 2N/A /* Append component name to the working path */ 2N/A * If we are about to exceed our depth, 2N/A * remember where we are and close a file. 2N/A * Do a recursive call to process the file. 2N/A * (watch this, sports fans) 2N/A * If we closed the file, try to reopen it. 2N/A * Open a directory with an arbitrarily long path name. If the original 2N/A * depth arg >= 2, use openat() to make sure that it doesn't fail with 2N/A * Traverse the path using openat() to get the fd for 2N/A * Stat a file with an arbitrarily long path name. If we aren't doing a 2N/A * stat on the arg passed to _xftw() and if the original depth arg >= 2, 2N/A * use openat() to make sure that it doesn't fail with ENAMETOOLONG. 2N/A /* Traverse path using openat() to get fd for fstatat(). */ 2N/A * Return pointer basename of path. This routine doesn't remove 2N/A * trailing slashes, but there won't be any. 2N/A /* find last char in path before any trailing slashes */