10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*1053Smaheshvs * Common Development and Distribution License (the "License"). 6*1053Smaheshvs * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*1053Smaheshvs * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 23*1053Smaheshvs * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 270Sstevel@tonic-gate /* All Rights Reserved */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 300Sstevel@tonic-gate 310Sstevel@tonic-gate /* LINTLIBRARY */ 320Sstevel@tonic-gate /* 330Sstevel@tonic-gate * ftw - file tree walk 340Sstevel@tonic-gate * 350Sstevel@tonic-gate * int ftw (path, fn, depth) char *path; int (*fn)(); int depth; 360Sstevel@tonic-gate * 370Sstevel@tonic-gate * Given a path name, ftw starts from the file given by that path 380Sstevel@tonic-gate * name and visits each file and directory in the tree beneath 390Sstevel@tonic-gate * that file. If a single file has multiple links within the 400Sstevel@tonic-gate * structure, it will be visited once for each such link. 410Sstevel@tonic-gate * For each object visited, fn is called with three arguments. 420Sstevel@tonic-gate * The first contains the path name of the object, the second 430Sstevel@tonic-gate * contains a pointer to a stat buffer which will usually hold 440Sstevel@tonic-gate * appropriate information for the object and the third will 450Sstevel@tonic-gate * contain an integer value giving additional information: 460Sstevel@tonic-gate * 470Sstevel@tonic-gate * FTW_F The object is a file for which stat was 480Sstevel@tonic-gate * successful. It does not guarantee that the 490Sstevel@tonic-gate * file can actually be read. 500Sstevel@tonic-gate * 510Sstevel@tonic-gate * FTW_D The object is a directory for which stat and 520Sstevel@tonic-gate * open for read were both successful. 530Sstevel@tonic-gate * 540Sstevel@tonic-gate * FTW_DNR The object is a directory for which stat 550Sstevel@tonic-gate * succeeded, but which cannot be read. Because 560Sstevel@tonic-gate * the directory cannot be read, fn will not be 570Sstevel@tonic-gate * called for any descendants of this directory. 580Sstevel@tonic-gate * 590Sstevel@tonic-gate * FTW_NS Stat failed on the object because of lack of 600Sstevel@tonic-gate * appropriate permission. This indication will 610Sstevel@tonic-gate * be given, for example, for each file in a 620Sstevel@tonic-gate * directory with read but no execute permission. 630Sstevel@tonic-gate * Because stat failed, it is not possible to 640Sstevel@tonic-gate * determine whether this object is a file or a 650Sstevel@tonic-gate * directory. The stat buffer passed to fn will 660Sstevel@tonic-gate * contain garbage. Stat failure for any reason 670Sstevel@tonic-gate * other than lack of permission will be 680Sstevel@tonic-gate * considered an error and will cause ftw to stop 690Sstevel@tonic-gate * and return -1 to its caller. 700Sstevel@tonic-gate * 710Sstevel@tonic-gate * If fn returns nonzero, ftw stops and returns the same value 720Sstevel@tonic-gate * to its caller. If ftw gets into other trouble along the way, 730Sstevel@tonic-gate * it returns -1 and leaves an indication of the cause in errno. 740Sstevel@tonic-gate * 750Sstevel@tonic-gate * The third argument to ftw does not limit the depth to which 760Sstevel@tonic-gate * ftw will go. Rather, it limits the depth to which ftw will 770Sstevel@tonic-gate * go before it starts recycling file descriptors. In general, 780Sstevel@tonic-gate * it is necessary to use a file descriptor for each level of the 790Sstevel@tonic-gate * tree, but they can be recycled for deep trees by saving the 800Sstevel@tonic-gate * position, closing, re-opening, and seeking. It is possible 810Sstevel@tonic-gate * to start recycling file descriptors by sensing when we have 820Sstevel@tonic-gate * run out, but in general this will not be terribly useful if 830Sstevel@tonic-gate * fn expects to be able to open files. We could also figure out 840Sstevel@tonic-gate * how many file descriptors are available and guarantee a certain 850Sstevel@tonic-gate * number to fn, but we would not know how many to guarantee, 860Sstevel@tonic-gate * and we do not want to impose the extra overhead on a caller who 870Sstevel@tonic-gate * knows how many are available without having to figure it out. 880Sstevel@tonic-gate * 890Sstevel@tonic-gate * It is possible for ftw to die with a memory fault in the event 900Sstevel@tonic-gate * of a file system so deeply nested that the stack overflows. 910Sstevel@tonic-gate */ 920Sstevel@tonic-gate 930Sstevel@tonic-gate #include <sys/fs/ufs_inode.h> 940Sstevel@tonic-gate #include <sys/types.h> 950Sstevel@tonic-gate #include <sys/stat.h> 960Sstevel@tonic-gate #include <dirent.h> 970Sstevel@tonic-gate #include <errno.h> 980Sstevel@tonic-gate #include <malloc.h> 990Sstevel@tonic-gate #include <string.h> 1000Sstevel@tonic-gate #include <fcntl.h> 1010Sstevel@tonic-gate #include <unistd.h> 1020Sstevel@tonic-gate #include <ftw.h> 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate #define NULL 0 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate static int pwdfd; 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate static int lf_xftw( 1090Sstevel@tonic-gate const char *, 1100Sstevel@tonic-gate int (*)(const char *, const struct stat64 *, int), 1110Sstevel@tonic-gate int, 1120Sstevel@tonic-gate int (*)(const char *, struct stat64 *)); 1130Sstevel@tonic-gate 114*1053Smaheshvs int 1150Sstevel@tonic-gate lf_lftw( 1160Sstevel@tonic-gate const char *path, 1170Sstevel@tonic-gate int (*fn)(const char *, const struct stat64 *, int), 1180Sstevel@tonic-gate int depth) 1190Sstevel@tonic-gate { 1200Sstevel@tonic-gate int rc; 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate if ((pwdfd = open(".", O_RDONLY)) < 0) { 1230Sstevel@tonic-gate return (-1); 1240Sstevel@tonic-gate } else { 1250Sstevel@tonic-gate rc = (lf_xftw(path, fn, depth, lstat64)); 1260Sstevel@tonic-gate (void) close(pwdfd); 1270Sstevel@tonic-gate return (rc); 1280Sstevel@tonic-gate } 1290Sstevel@tonic-gate } 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate static int 1320Sstevel@tonic-gate #ifdef __STDC__ 1330Sstevel@tonic-gate lf_xftw( 1340Sstevel@tonic-gate const char *path, 1350Sstevel@tonic-gate int (*fn)(const char *, const struct stat64 *, int), 1360Sstevel@tonic-gate int depth, 1370Sstevel@tonic-gate int (*statfn)(const char *, struct stat64 *)) 1380Sstevel@tonic-gate #else 139*1053Smaheshvs lf_xftw(char *path, int (*fn)(), int depth, int (*statfn)()) 1400Sstevel@tonic-gate #endif 1410Sstevel@tonic-gate { 1420Sstevel@tonic-gate int n; 1430Sstevel@tonic-gate int rc, sublen, saverr, attrfd; 1440Sstevel@tonic-gate DIR *dirp; 1450Sstevel@tonic-gate char *subpath, *component; 1460Sstevel@tonic-gate struct stat64 sb; 1470Sstevel@tonic-gate struct dirent *dp; 1480Sstevel@tonic-gate extern dev_t partial_dev; 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate /* 1510Sstevel@tonic-gate * Try to get file status. 1520Sstevel@tonic-gate * If unsuccessful, errno will say why. 1530Sstevel@tonic-gate */ 1540Sstevel@tonic-gate if ((*statfn)(path, &sb) < 0) 1550Sstevel@tonic-gate return (errno == EACCES? (*fn)(path, &sb, FTW_NS): -1); 1560Sstevel@tonic-gate /* 1570Sstevel@tonic-gate * The stat succeeded, so we know the object exists. 1580Sstevel@tonic-gate * Make sure it is not a mount point for another filesystem. 1590Sstevel@tonic-gate * The following check must be made here because: 1600Sstevel@tonic-gate * 1610Sstevel@tonic-gate * + namefs can be mounted on anything, but a directory 1620Sstevel@tonic-gate * + all other filesystems must be mounted on a directory 1630Sstevel@tonic-gate */ 1640Sstevel@tonic-gate if (sb.st_dev != partial_dev) { 1650Sstevel@tonic-gate return (0); 1660Sstevel@tonic-gate } 1670Sstevel@tonic-gate /* 1680Sstevel@tonic-gate * Check for presence of attributes on file 1690Sstevel@tonic-gate */ 1700Sstevel@tonic-gate if (pathconf(path, _PC_XATTR_EXISTS) == 1) { 1710Sstevel@tonic-gate attrfd = attropen64(path, ".", O_RDONLY|O_NONBLOCK); 1720Sstevel@tonic-gate } else { 1730Sstevel@tonic-gate attrfd = -1; 1740Sstevel@tonic-gate } 1750Sstevel@tonic-gate /* 1760Sstevel@tonic-gate * If not a directory, call the user function and return. 1770Sstevel@tonic-gate */ 1780Sstevel@tonic-gate if ((sb.st_mode & S_IFMT) != S_IFDIR && 1790Sstevel@tonic-gate (sb.st_mode & IFMT) != IFATTRDIR) { 1800Sstevel@tonic-gate rc = (*fn)(path, &sb, FTW_F); 1810Sstevel@tonic-gate if (rc == 0 && attrfd != -1) { 1820Sstevel@tonic-gate (void) fchdir(attrfd); 1830Sstevel@tonic-gate rc = lf_xftw(".", fn, depth-1, statfn); 1840Sstevel@tonic-gate (void) fchdir(pwdfd); 1850Sstevel@tonic-gate (void) close(attrfd); 1860Sstevel@tonic-gate } 1870Sstevel@tonic-gate return (rc); 1880Sstevel@tonic-gate } 1890Sstevel@tonic-gate /* 1900Sstevel@tonic-gate * The object was a directory and not a mount point. 1910Sstevel@tonic-gate * 1920Sstevel@tonic-gate * Open a file to read the directory 1930Sstevel@tonic-gate */ 1940Sstevel@tonic-gate dirp = opendir(path); 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate /* 1970Sstevel@tonic-gate * Call the user function, telling it whether 1980Sstevel@tonic-gate * the directory can be read. If it can't be read 1990Sstevel@tonic-gate * call the user function or indicate an error, 2000Sstevel@tonic-gate * depending on the reason it couldn't be read. 2010Sstevel@tonic-gate */ 2020Sstevel@tonic-gate if (dirp == NULL) 2030Sstevel@tonic-gate rc = (errno == EACCES? (*fn)(path, &sb, FTW_DNR): -1); 2040Sstevel@tonic-gate else 2050Sstevel@tonic-gate rc = (*fn)(path, &sb, FTW_D); 2060Sstevel@tonic-gate /* 2070Sstevel@tonic-gate * If the directory has attributes, process the 2080Sstevel@tonic-gate * attributes before processing the directory contents. 2090Sstevel@tonic-gate */ 2100Sstevel@tonic-gate if (rc == 0 && attrfd != -1) { 2110Sstevel@tonic-gate (void) fchdir(attrfd); 2120Sstevel@tonic-gate rc = lf_xftw(".", fn, depth-1, statfn); 2130Sstevel@tonic-gate (void) fchdir(pwdfd); 2140Sstevel@tonic-gate (void) close(attrfd); 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate if (rc != 0 || dirp == NULL) 2170Sstevel@tonic-gate return (rc); 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate /* Allocate a buffer to hold generated pathnames. */ 2200Sstevel@tonic-gate /* LINTED: the length will fit into a signed integer */ 2210Sstevel@tonic-gate n = (int)strlen(path); 2220Sstevel@tonic-gate sublen = n + MAXNAMLEN + 1; /* +1 for appended / */ 2230Sstevel@tonic-gate subpath = malloc((unsigned)(sublen+1)); /* +1 for NUL */ 2240Sstevel@tonic-gate if (subpath == NULL) { 2250Sstevel@tonic-gate saverr = errno; 2260Sstevel@tonic-gate (void) closedir(dirp); 2270Sstevel@tonic-gate errno = saverr; 2280Sstevel@tonic-gate return (-1); 2290Sstevel@tonic-gate } 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate /* Create a prefix to which we will append component names */ 2320Sstevel@tonic-gate (void) strcpy(subpath, path); 2330Sstevel@tonic-gate if (subpath[0] != '\0' && subpath[n-1] != '/') 2340Sstevel@tonic-gate subpath[n++] = '/'; 2350Sstevel@tonic-gate component = &subpath[n]; 2360Sstevel@tonic-gate /* LINTED: result will fit into a 32-bit int */ 2370Sstevel@tonic-gate sublen -= component - subpath; 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate /* 2400Sstevel@tonic-gate * Read the directory one component at a time. 2410Sstevel@tonic-gate * We must ignore "." and "..", but other than that, 2420Sstevel@tonic-gate * just create a path name and call self to check it out. 2430Sstevel@tonic-gate */ 2440Sstevel@tonic-gate while ((dp = readdir(dirp)) != NULL) { 2450Sstevel@tonic-gate if (strcmp(dp->d_name, ".") != 0 && 2460Sstevel@tonic-gate strcmp(dp->d_name, "..") != 0) { 2470Sstevel@tonic-gate long here; 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate /* Append component name to the working path */ 2500Sstevel@tonic-gate (void) strncpy(component, dp->d_name, sublen); 2510Sstevel@tonic-gate component[sublen - 1] = '\0'; 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate /* 2540Sstevel@tonic-gate * If we are about to exceed our depth, 2550Sstevel@tonic-gate * remember where we are and close a file. 2560Sstevel@tonic-gate */ 2570Sstevel@tonic-gate if (depth <= 1) { 2580Sstevel@tonic-gate here = telldir(dirp); 2590Sstevel@tonic-gate (void) closedir(dirp); 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate /* 2630Sstevel@tonic-gate * Do a recursive call to process the file. 2640Sstevel@tonic-gate * (watch this, sports fans) 2650Sstevel@tonic-gate */ 2660Sstevel@tonic-gate rc = lf_xftw(subpath, fn, depth-1, statfn); 2670Sstevel@tonic-gate if (rc != 0) { 2680Sstevel@tonic-gate free(subpath); 2690Sstevel@tonic-gate if (depth > 1) 2700Sstevel@tonic-gate (void) closedir(dirp); 2710Sstevel@tonic-gate return (rc); 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate /* 2750Sstevel@tonic-gate * If we closed the file, try to reopen it. 2760Sstevel@tonic-gate */ 2770Sstevel@tonic-gate if (depth <= 1) { 2780Sstevel@tonic-gate dirp = opendir(path); 2790Sstevel@tonic-gate if (dirp == NULL) { 2800Sstevel@tonic-gate free(subpath); 2810Sstevel@tonic-gate return (-1); 2820Sstevel@tonic-gate } 2830Sstevel@tonic-gate seekdir(dirp, here); 2840Sstevel@tonic-gate } 2850Sstevel@tonic-gate } 2860Sstevel@tonic-gate } 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate /* 2890Sstevel@tonic-gate * We got out of the subdirectory loop. The return from 2900Sstevel@tonic-gate * the final readdir is in dp. Clean up. 2910Sstevel@tonic-gate */ 2920Sstevel@tonic-gate free(subpath); 2930Sstevel@tonic-gate (void) closedir(dirp); 2940Sstevel@tonic-gate return (0); 2950Sstevel@tonic-gate } 296