2N/A/*
2N/A * CDDL HEADER START
2N/A *
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 *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
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 *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 1989, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/* Copyright (c) 1988 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A/*
2N/A * _xftw - file tree walk the uses expanded stat structure
2N/A *
2N/A * int _xftw(path, fn, depth) char *path; int (*fn)(); int depth;
2N/A *
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 *
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 *
2N/A * FTW_D The object is a directory for which stat and
2N/A * open for read were both successful.
2N/A *
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 *
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 *
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 *
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 */
2N/A
2N/A/*
2N/A * this interface uses the expanded stat structure and therefore
2N/A * must have EFT enabled.
2N/A */
2N/A#ifdef _STYPES
2N/A#undef _STYPES
2N/A#endif
2N/A
2N/A#include "lint.h"
2N/A#include <sys/types.h>
2N/A#include <sys/stat.h>
2N/A#include <fcntl.h>
2N/A#include <sys/param.h>
2N/A#include <dirent.h>
2N/A#include <errno.h>
2N/A#include <ftw.h>
2N/A#include <string.h>
2N/A#include <stdlib.h>
2N/A#include <unistd.h>
2N/A
2N/A#if !defined(_LP64) && _FILE_OFFSET_BITS == 64
2N/A#define _xftw _xftw64
2N/A#define stat stat64
2N/A#define fstat fstat64
2N/A#define fstatat fstatat64
2N/A#define readdir readdir64
2N/A#define dirent dirent64
2N/A#endif /* !_LP64 && _FILE_OFFSET_BITS == 64 */
2N/A
2N/Astruct Var {
2N/A int level;
2N/A int odepth;
2N/A};
2N/A
2N/Astatic DIR *nocdopendir(const char *, struct Var *);
2N/Astatic int nocdstat(const char *, struct stat *, struct Var *, int);
2N/Astatic const char *get_unrooted(const char *);
2N/Astatic int fwalk(const char *, int (*)(const char *, const struct stat *, int),
2N/A int, struct Var *);
2N/A
2N/A/*ARGSUSED*/
2N/Aint
2N/A_xftw(int ver, const char *path,
2N/A int (*fn)(const char *, const struct stat *, int), int depth)
2N/A{
2N/A struct Var var;
2N/A int rc;
2N/A
2N/A var.level = 0;
2N/A var.odepth = depth;
2N/A rc = fwalk(path, fn, depth, &var);
2N/A return (rc);
2N/A}
2N/A
2N/A/*
2N/A * This is the recursive walker.
2N/A */
2N/Astatic int
2N/Afwalk(const char *path, int (*fn)(const char *, const struct stat *, int),
2N/A int depth, struct Var *vp)
2N/A{
2N/A size_t n;
2N/A int rc;
2N/A int save_errno;
2N/A DIR *dirp;
2N/A char *subpath;
2N/A struct stat sb;
2N/A struct dirent *direntp;
2N/A
2N/A vp->level++;
2N/A
2N/A /*
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 */
2N/A if (nocdstat(path, &sb, vp, 0) < 0) {
2N/A#ifdef S_IFLNK
2N/A save_errno = errno;
2N/A if ((nocdstat(path, &sb, vp, AT_SYMLINK_NOFOLLOW) != -1) &&
2N/A ((sb.st_mode & S_IFMT) == S_IFLNK)) {
2N/A errno = save_errno;
2N/A return (*fn)(path, &sb, FTW_NS);
2N/A } else {
2N/A errno = save_errno;
2N/A }
2N/A#endif
2N/A return (errno == EACCES? (*fn)(path, &sb, FTW_NS): -1);
2N/A }
2N/A
2N/A /*
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 */
2N/A if ((sb.st_mode & S_IFMT) != S_IFDIR)
2N/A return ((*fn)(path, &sb, FTW_F));
2N/A
2N/A /*
2N/A * The object was a directory.
2N/A *
2N/A * Open a file to read the directory
2N/A */
2N/A dirp = nocdopendir(path, vp);
2N/A
2N/A /*
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 */
2N/A if (dirp == NULL)
2N/A return (errno == EACCES? (*fn)(path, &sb, FTW_DNR): -1);
2N/A
2N/A /* We could read the directory. Call user function. */
2N/A rc = (*fn)(path, &sb, FTW_D);
2N/A if (rc != 0) {
2N/A (void) closedir(dirp);
2N/A return (rc);
2N/A }
2N/A
2N/A /*
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 */
2N/A while (direntp = readdir(dirp)) {
2N/A long here;
2N/A
2N/A if (strcmp(direntp->d_name, ".") == 0 ||
2N/A strcmp(direntp->d_name, "..") == 0)
2N/A continue;
2N/A
2N/A /* Create a prefix to which we will append component names */
2N/A n = strlen(path);
2N/A subpath = malloc(n + strlen(direntp->d_name) + 2);
2N/A if (subpath == 0) {
2N/A (void) closedir(dirp);
2N/A errno = ENOMEM;
2N/A return (-1);
2N/A }
2N/A (void) strcpy(subpath, path);
2N/A if (subpath[0] != '\0' && subpath[n-1] != '/')
2N/A subpath[n++] = '/';
2N/A
2N/A /* Append component name to the working path */
2N/A (void) strlcpy(&subpath[n], direntp->d_name, MAXNAMELEN);
2N/A
2N/A /*
2N/A * If we are about to exceed our depth,
2N/A * remember where we are and close a file.
2N/A */
2N/A if (depth <= 1) {
2N/A here = telldir(dirp);
2N/A if (closedir(dirp) < 0) {
2N/A free(subpath);
2N/A return (-1);
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * Do a recursive call to process the file.
2N/A * (watch this, sports fans)
2N/A */
2N/A rc = fwalk(subpath, fn, depth-1, vp);
2N/A if (rc != 0) {
2N/A free(subpath);
2N/A if (depth > 1)
2N/A (void) closedir(dirp);
2N/A return (rc);
2N/A }
2N/A
2N/A /*
2N/A * If we closed the file, try to reopen it.
2N/A */
2N/A if (depth <= 1) {
2N/A dirp = nocdopendir(path, vp);
2N/A if (dirp == NULL) {
2N/A free(subpath);
2N/A return (-1);
2N/A }
2N/A seekdir(dirp, here);
2N/A }
2N/A free(subpath);
2N/A }
2N/A (void) closedir(dirp);
2N/A return (0);
2N/A}
2N/A
2N/A/*
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 * ENAMETOOLONG.
2N/A */
2N/Astatic DIR *
2N/Anocdopendir(const char *path, struct Var *vp)
2N/A{
2N/A int fd, cfd;
2N/A DIR *fdd;
2N/A char *dirp, *token, *ptr;
2N/A
2N/A fdd = opendir(path);
2N/A if (vp->odepth > 1 && fdd == NULL && errno == ENAMETOOLONG) {
2N/A /*
2N/A * Traverse the path using openat() to get the fd for
2N/A * fdopendir().
2N/A */
2N/A if ((dirp = strdup(path)) == NULL) {
2N/A errno = ENAMETOOLONG;
2N/A return (NULL);
2N/A }
2N/A if ((token = strtok_r(dirp, "/", &ptr)) != NULL) {
2N/A if ((fd = openat(AT_FDCWD, dirp,
2N/A O_RDONLY | O_CLOEXEC)) < 0) {
2N/A (void) free(dirp);
2N/A errno = ENAMETOOLONG;
2N/A return (NULL);
2N/A }
2N/A while ((token = strtok_r(NULL, "/", &ptr)) != NULL) {
2N/A if ((cfd = openat(fd, token,
2N/A O_RDONLY | O_CLOEXEC)) < 0) {
2N/A (void) close(fd);
2N/A (void) free(dirp);
2N/A errno = ENAMETOOLONG;
2N/A return (NULL);
2N/A }
2N/A (void) close(fd);
2N/A fd = cfd;
2N/A }
2N/A (void) free(dirp);
2N/A return (fdopendir(fd));
2N/A }
2N/A (void) free(dirp);
2N/A errno = ENAMETOOLONG;
2N/A }
2N/A return (fdd);
2N/A}
2N/A
2N/A/*
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 */
2N/Astatic int
2N/Anocdstat(const char *path, struct stat *statp, struct Var *vp, int sym)
2N/A{
2N/A int fd, cfd;
2N/A char *dirp, *token, *ptr;
2N/A int rc;
2N/A const char *unrootp;
2N/A int save_err;
2N/A
2N/A rc = fstatat(AT_FDCWD, path, statp, sym);
2N/A if (vp->level > 1 && vp->odepth >= 2 && rc < 0 &&
2N/A errno == ENAMETOOLONG) {
2N/A /* Traverse path using openat() to get fd for fstatat(). */
2N/A if ((dirp = strdup(path)) == NULL) {
2N/A errno = ENAMETOOLONG;
2N/A return (-1);
2N/A }
2N/A if ((token = strtok_r(dirp, "/", &ptr)) != NULL) {
2N/A if ((fd = openat(AT_FDCWD, dirp, O_RDONLY)) < 0) {
2N/A (void) free(dirp);
2N/A errno = ENAMETOOLONG;
2N/A return (-1);
2N/A }
2N/A unrootp = get_unrooted(path);
2N/A while ((token = strtok_r(NULL, "/", &ptr)) != NULL &&
2N/A strcmp(token, unrootp) != 0) {
2N/A if ((cfd = openat(fd, token, O_RDONLY)) < 0) {
2N/A (void) close(fd);
2N/A (void) free(dirp);
2N/A errno = ENAMETOOLONG;
2N/A return (NULL);
2N/A }
2N/A (void) close(fd);
2N/A fd = cfd;
2N/A }
2N/A (void) free(dirp);
2N/A rc = fstatat(fd, unrootp, statp, sym);
2N/A save_err = errno;
2N/A (void) close(fd);
2N/A errno = save_err;
2N/A return (rc);
2N/A }
2N/A (void) free(dirp);
2N/A errno = ENAMETOOLONG;
2N/A }
2N/A return (rc);
2N/A}
2N/A
2N/A/*
2N/A * Return pointer basename of path. This routine doesn't remove
2N/A * trailing slashes, but there won't be any.
2N/A */
2N/Astatic const char *
2N/Aget_unrooted(const char *path)
2N/A{
2N/A const char *ptr;
2N/A
2N/A if (!path || !*path)
2N/A return (NULL);
2N/A
2N/A ptr = path + strlen(path);
2N/A /* find last char in path before any trailing slashes */
2N/A while (ptr != path && *--ptr == '/')
2N/A ;
2N/A
2N/A if (ptr == path) /* all slashes */
2N/A return (ptr);
2N/A
2N/A while (ptr != path)
2N/A if (*--ptr == '/')
2N/A return (++ptr);
2N/A
2N/A return (ptr);
2N/A}