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