1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright (c) 1998,2001 by Sun Microsystems, Inc. 24*7c478bd9Sstevel@tonic-gate * All rights reserved. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 31*7c478bd9Sstevel@tonic-gate 32*7c478bd9Sstevel@tonic-gate /* LINTLIBRARY */ 33*7c478bd9Sstevel@tonic-gate /* 34*7c478bd9Sstevel@tonic-gate * ftw - file tree walk 35*7c478bd9Sstevel@tonic-gate * 36*7c478bd9Sstevel@tonic-gate * int ftw (path, fn, depth) char *path; int (*fn)(); int depth; 37*7c478bd9Sstevel@tonic-gate * 38*7c478bd9Sstevel@tonic-gate * Given a path name, ftw starts from the file given by that path 39*7c478bd9Sstevel@tonic-gate * name and visits each file and directory in the tree beneath 40*7c478bd9Sstevel@tonic-gate * that file. If a single file has multiple links within the 41*7c478bd9Sstevel@tonic-gate * structure, it will be visited once for each such link. 42*7c478bd9Sstevel@tonic-gate * For each object visited, fn is called with three arguments. 43*7c478bd9Sstevel@tonic-gate * The first contains the path name of the object, the second 44*7c478bd9Sstevel@tonic-gate * contains a pointer to a stat buffer which will usually hold 45*7c478bd9Sstevel@tonic-gate * appropriate information for the object and the third will 46*7c478bd9Sstevel@tonic-gate * contain an integer value giving additional information: 47*7c478bd9Sstevel@tonic-gate * 48*7c478bd9Sstevel@tonic-gate * FTW_F The object is a file for which stat was 49*7c478bd9Sstevel@tonic-gate * successful. It does not guarantee that the 50*7c478bd9Sstevel@tonic-gate * file can actually be read. 51*7c478bd9Sstevel@tonic-gate * 52*7c478bd9Sstevel@tonic-gate * FTW_D The object is a directory for which stat and 53*7c478bd9Sstevel@tonic-gate * open for read were both successful. 54*7c478bd9Sstevel@tonic-gate * 55*7c478bd9Sstevel@tonic-gate * FTW_DNR The object is a directory for which stat 56*7c478bd9Sstevel@tonic-gate * succeeded, but which cannot be read. Because 57*7c478bd9Sstevel@tonic-gate * the directory cannot be read, fn will not be 58*7c478bd9Sstevel@tonic-gate * called for any descendants of this directory. 59*7c478bd9Sstevel@tonic-gate * 60*7c478bd9Sstevel@tonic-gate * FTW_NS Stat failed on the object because of lack of 61*7c478bd9Sstevel@tonic-gate * appropriate permission. This indication will 62*7c478bd9Sstevel@tonic-gate * be given, for example, for each file in a 63*7c478bd9Sstevel@tonic-gate * directory with read but no execute permission. 64*7c478bd9Sstevel@tonic-gate * Because stat failed, it is not possible to 65*7c478bd9Sstevel@tonic-gate * determine whether this object is a file or a 66*7c478bd9Sstevel@tonic-gate * directory. The stat buffer passed to fn will 67*7c478bd9Sstevel@tonic-gate * contain garbage. Stat failure for any reason 68*7c478bd9Sstevel@tonic-gate * other than lack of permission will be 69*7c478bd9Sstevel@tonic-gate * considered an error and will cause ftw to stop 70*7c478bd9Sstevel@tonic-gate * and return -1 to its caller. 71*7c478bd9Sstevel@tonic-gate * 72*7c478bd9Sstevel@tonic-gate * If fn returns nonzero, ftw stops and returns the same value 73*7c478bd9Sstevel@tonic-gate * to its caller. If ftw gets into other trouble along the way, 74*7c478bd9Sstevel@tonic-gate * it returns -1 and leaves an indication of the cause in errno. 75*7c478bd9Sstevel@tonic-gate * 76*7c478bd9Sstevel@tonic-gate * The third argument to ftw does not limit the depth to which 77*7c478bd9Sstevel@tonic-gate * ftw will go. Rather, it limits the depth to which ftw will 78*7c478bd9Sstevel@tonic-gate * go before it starts recycling file descriptors. In general, 79*7c478bd9Sstevel@tonic-gate * it is necessary to use a file descriptor for each level of the 80*7c478bd9Sstevel@tonic-gate * tree, but they can be recycled for deep trees by saving the 81*7c478bd9Sstevel@tonic-gate * position, closing, re-opening, and seeking. It is possible 82*7c478bd9Sstevel@tonic-gate * to start recycling file descriptors by sensing when we have 83*7c478bd9Sstevel@tonic-gate * run out, but in general this will not be terribly useful if 84*7c478bd9Sstevel@tonic-gate * fn expects to be able to open files. We could also figure out 85*7c478bd9Sstevel@tonic-gate * how many file descriptors are available and guarantee a certain 86*7c478bd9Sstevel@tonic-gate * number to fn, but we would not know how many to guarantee, 87*7c478bd9Sstevel@tonic-gate * and we do not want to impose the extra overhead on a caller who 88*7c478bd9Sstevel@tonic-gate * knows how many are available without having to figure it out. 89*7c478bd9Sstevel@tonic-gate * 90*7c478bd9Sstevel@tonic-gate * It is possible for ftw to die with a memory fault in the event 91*7c478bd9Sstevel@tonic-gate * of a file system so deeply nested that the stack overflows. 92*7c478bd9Sstevel@tonic-gate */ 93*7c478bd9Sstevel@tonic-gate 94*7c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h> 95*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 96*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 97*7c478bd9Sstevel@tonic-gate #include <dirent.h> 98*7c478bd9Sstevel@tonic-gate #include <errno.h> 99*7c478bd9Sstevel@tonic-gate #include <malloc.h> 100*7c478bd9Sstevel@tonic-gate #include <string.h> 101*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 102*7c478bd9Sstevel@tonic-gate #include <unistd.h> 103*7c478bd9Sstevel@tonic-gate #include <ftw.h> 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate #define NULL 0 106*7c478bd9Sstevel@tonic-gate 107*7c478bd9Sstevel@tonic-gate static int pwdfd; 108*7c478bd9Sstevel@tonic-gate 109*7c478bd9Sstevel@tonic-gate #ifdef __STDC__ 110*7c478bd9Sstevel@tonic-gate static int lf_xftw( 111*7c478bd9Sstevel@tonic-gate const char *, 112*7c478bd9Sstevel@tonic-gate int (*)(const char *, const struct stat64 *, int), 113*7c478bd9Sstevel@tonic-gate int, 114*7c478bd9Sstevel@tonic-gate int (*)(const char *, struct stat64 *)); 115*7c478bd9Sstevel@tonic-gate #else 116*7c478bd9Sstevel@tonic-gate static int lf_xftw(); 117*7c478bd9Sstevel@tonic-gate #endif 118*7c478bd9Sstevel@tonic-gate 119*7c478bd9Sstevel@tonic-gate #ifdef __STDC__ 120*7c478bd9Sstevel@tonic-gate lf_lftw( 121*7c478bd9Sstevel@tonic-gate const char *path, 122*7c478bd9Sstevel@tonic-gate int (*fn)(const char *, const struct stat64 *, int), 123*7c478bd9Sstevel@tonic-gate int depth) 124*7c478bd9Sstevel@tonic-gate #else 125*7c478bd9Sstevel@tonic-gate lf_lftw(path, fn, depth) 126*7c478bd9Sstevel@tonic-gate char *path; 127*7c478bd9Sstevel@tonic-gate int (*fn)(); 128*7c478bd9Sstevel@tonic-gate int depth; 129*7c478bd9Sstevel@tonic-gate #endif 130*7c478bd9Sstevel@tonic-gate { 131*7c478bd9Sstevel@tonic-gate int rc; 132*7c478bd9Sstevel@tonic-gate 133*7c478bd9Sstevel@tonic-gate if ((pwdfd = open(".", O_RDONLY)) < 0) { 134*7c478bd9Sstevel@tonic-gate return (-1); 135*7c478bd9Sstevel@tonic-gate } else { 136*7c478bd9Sstevel@tonic-gate rc = (lf_xftw(path, fn, depth, lstat64)); 137*7c478bd9Sstevel@tonic-gate (void) close(pwdfd); 138*7c478bd9Sstevel@tonic-gate return (rc); 139*7c478bd9Sstevel@tonic-gate } 140*7c478bd9Sstevel@tonic-gate } 141*7c478bd9Sstevel@tonic-gate 142*7c478bd9Sstevel@tonic-gate static int 143*7c478bd9Sstevel@tonic-gate #ifdef __STDC__ 144*7c478bd9Sstevel@tonic-gate lf_xftw( 145*7c478bd9Sstevel@tonic-gate const char *path, 146*7c478bd9Sstevel@tonic-gate int (*fn)(const char *, const struct stat64 *, int), 147*7c478bd9Sstevel@tonic-gate int depth, 148*7c478bd9Sstevel@tonic-gate int (*statfn)(const char *, struct stat64 *)) 149*7c478bd9Sstevel@tonic-gate #else 150*7c478bd9Sstevel@tonic-gate lf_xftw(path, fn, depth, statfn) 151*7c478bd9Sstevel@tonic-gate char *path; 152*7c478bd9Sstevel@tonic-gate int (*fn)(); 153*7c478bd9Sstevel@tonic-gate int depth; 154*7c478bd9Sstevel@tonic-gate int (*statfn)(); 155*7c478bd9Sstevel@tonic-gate #endif 156*7c478bd9Sstevel@tonic-gate { 157*7c478bd9Sstevel@tonic-gate int n; 158*7c478bd9Sstevel@tonic-gate int rc, sublen, saverr, attrfd; 159*7c478bd9Sstevel@tonic-gate DIR *dirp; 160*7c478bd9Sstevel@tonic-gate char *subpath, *component; 161*7c478bd9Sstevel@tonic-gate struct stat64 sb; 162*7c478bd9Sstevel@tonic-gate struct dirent *dp; 163*7c478bd9Sstevel@tonic-gate extern dev_t partial_dev; 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate /* 166*7c478bd9Sstevel@tonic-gate * Try to get file status. 167*7c478bd9Sstevel@tonic-gate * If unsuccessful, errno will say why. 168*7c478bd9Sstevel@tonic-gate */ 169*7c478bd9Sstevel@tonic-gate if ((*statfn)(path, &sb) < 0) 170*7c478bd9Sstevel@tonic-gate return (errno == EACCES? (*fn)(path, &sb, FTW_NS): -1); 171*7c478bd9Sstevel@tonic-gate /* 172*7c478bd9Sstevel@tonic-gate * The stat succeeded, so we know the object exists. 173*7c478bd9Sstevel@tonic-gate * Make sure it is not a mount point for another filesystem. 174*7c478bd9Sstevel@tonic-gate * The following check must be made here because: 175*7c478bd9Sstevel@tonic-gate * 176*7c478bd9Sstevel@tonic-gate * + namefs can be mounted on anything, but a directory 177*7c478bd9Sstevel@tonic-gate * + all other filesystems must be mounted on a directory 178*7c478bd9Sstevel@tonic-gate */ 179*7c478bd9Sstevel@tonic-gate if (sb.st_dev != partial_dev) { 180*7c478bd9Sstevel@tonic-gate return (0); 181*7c478bd9Sstevel@tonic-gate } 182*7c478bd9Sstevel@tonic-gate /* 183*7c478bd9Sstevel@tonic-gate * Check for presence of attributes on file 184*7c478bd9Sstevel@tonic-gate */ 185*7c478bd9Sstevel@tonic-gate if (pathconf(path, _PC_XATTR_EXISTS) == 1) { 186*7c478bd9Sstevel@tonic-gate attrfd = attropen64(path, ".", O_RDONLY|O_NONBLOCK); 187*7c478bd9Sstevel@tonic-gate } else { 188*7c478bd9Sstevel@tonic-gate attrfd = -1; 189*7c478bd9Sstevel@tonic-gate } 190*7c478bd9Sstevel@tonic-gate /* 191*7c478bd9Sstevel@tonic-gate * If not a directory, call the user function and return. 192*7c478bd9Sstevel@tonic-gate */ 193*7c478bd9Sstevel@tonic-gate if ((sb.st_mode & S_IFMT) != S_IFDIR && 194*7c478bd9Sstevel@tonic-gate (sb.st_mode & IFMT) != IFATTRDIR) { 195*7c478bd9Sstevel@tonic-gate rc = (*fn)(path, &sb, FTW_F); 196*7c478bd9Sstevel@tonic-gate if (rc == 0 && attrfd != -1) { 197*7c478bd9Sstevel@tonic-gate (void) fchdir(attrfd); 198*7c478bd9Sstevel@tonic-gate rc = lf_xftw(".", fn, depth-1, statfn); 199*7c478bd9Sstevel@tonic-gate (void) fchdir(pwdfd); 200*7c478bd9Sstevel@tonic-gate (void) close(attrfd); 201*7c478bd9Sstevel@tonic-gate } 202*7c478bd9Sstevel@tonic-gate return (rc); 203*7c478bd9Sstevel@tonic-gate } 204*7c478bd9Sstevel@tonic-gate /* 205*7c478bd9Sstevel@tonic-gate * The object was a directory and not a mount point. 206*7c478bd9Sstevel@tonic-gate * 207*7c478bd9Sstevel@tonic-gate * Open a file to read the directory 208*7c478bd9Sstevel@tonic-gate */ 209*7c478bd9Sstevel@tonic-gate dirp = opendir(path); 210*7c478bd9Sstevel@tonic-gate 211*7c478bd9Sstevel@tonic-gate /* 212*7c478bd9Sstevel@tonic-gate * Call the user function, telling it whether 213*7c478bd9Sstevel@tonic-gate * the directory can be read. If it can't be read 214*7c478bd9Sstevel@tonic-gate * call the user function or indicate an error, 215*7c478bd9Sstevel@tonic-gate * depending on the reason it couldn't be read. 216*7c478bd9Sstevel@tonic-gate */ 217*7c478bd9Sstevel@tonic-gate if (dirp == NULL) 218*7c478bd9Sstevel@tonic-gate rc = (errno == EACCES? (*fn)(path, &sb, FTW_DNR): -1); 219*7c478bd9Sstevel@tonic-gate else 220*7c478bd9Sstevel@tonic-gate rc = (*fn)(path, &sb, FTW_D); 221*7c478bd9Sstevel@tonic-gate /* 222*7c478bd9Sstevel@tonic-gate * If the directory has attributes, process the 223*7c478bd9Sstevel@tonic-gate * attributes before processing the directory contents. 224*7c478bd9Sstevel@tonic-gate */ 225*7c478bd9Sstevel@tonic-gate if (rc == 0 && attrfd != -1) { 226*7c478bd9Sstevel@tonic-gate (void) fchdir(attrfd); 227*7c478bd9Sstevel@tonic-gate rc = lf_xftw(".", fn, depth-1, statfn); 228*7c478bd9Sstevel@tonic-gate (void) fchdir(pwdfd); 229*7c478bd9Sstevel@tonic-gate (void) close(attrfd); 230*7c478bd9Sstevel@tonic-gate } 231*7c478bd9Sstevel@tonic-gate if (rc != 0 || dirp == NULL) 232*7c478bd9Sstevel@tonic-gate return (rc); 233*7c478bd9Sstevel@tonic-gate 234*7c478bd9Sstevel@tonic-gate /* Allocate a buffer to hold generated pathnames. */ 235*7c478bd9Sstevel@tonic-gate /* LINTED: the length will fit into a signed integer */ 236*7c478bd9Sstevel@tonic-gate n = (int)strlen(path); 237*7c478bd9Sstevel@tonic-gate sublen = n + MAXNAMLEN + 1; /* +1 for appended / */ 238*7c478bd9Sstevel@tonic-gate subpath = malloc((unsigned)(sublen+1)); /* +1 for NUL */ 239*7c478bd9Sstevel@tonic-gate if (subpath == NULL) { 240*7c478bd9Sstevel@tonic-gate saverr = errno; 241*7c478bd9Sstevel@tonic-gate (void) closedir(dirp); 242*7c478bd9Sstevel@tonic-gate errno = saverr; 243*7c478bd9Sstevel@tonic-gate return (-1); 244*7c478bd9Sstevel@tonic-gate } 245*7c478bd9Sstevel@tonic-gate 246*7c478bd9Sstevel@tonic-gate /* Create a prefix to which we will append component names */ 247*7c478bd9Sstevel@tonic-gate (void) strcpy(subpath, path); 248*7c478bd9Sstevel@tonic-gate if (subpath[0] != '\0' && subpath[n-1] != '/') 249*7c478bd9Sstevel@tonic-gate subpath[n++] = '/'; 250*7c478bd9Sstevel@tonic-gate component = &subpath[n]; 251*7c478bd9Sstevel@tonic-gate /* LINTED: result will fit into a 32-bit int */ 252*7c478bd9Sstevel@tonic-gate sublen -= component - subpath; 253*7c478bd9Sstevel@tonic-gate 254*7c478bd9Sstevel@tonic-gate /* 255*7c478bd9Sstevel@tonic-gate * Read the directory one component at a time. 256*7c478bd9Sstevel@tonic-gate * We must ignore "." and "..", but other than that, 257*7c478bd9Sstevel@tonic-gate * just create a path name and call self to check it out. 258*7c478bd9Sstevel@tonic-gate */ 259*7c478bd9Sstevel@tonic-gate while ((dp = readdir(dirp)) != NULL) { 260*7c478bd9Sstevel@tonic-gate if (strcmp(dp->d_name, ".") != 0 && 261*7c478bd9Sstevel@tonic-gate strcmp(dp->d_name, "..") != 0) { 262*7c478bd9Sstevel@tonic-gate long here; 263*7c478bd9Sstevel@tonic-gate 264*7c478bd9Sstevel@tonic-gate /* Append component name to the working path */ 265*7c478bd9Sstevel@tonic-gate (void) strncpy(component, dp->d_name, sublen); 266*7c478bd9Sstevel@tonic-gate component[sublen - 1] = '\0'; 267*7c478bd9Sstevel@tonic-gate 268*7c478bd9Sstevel@tonic-gate /* 269*7c478bd9Sstevel@tonic-gate * If we are about to exceed our depth, 270*7c478bd9Sstevel@tonic-gate * remember where we are and close a file. 271*7c478bd9Sstevel@tonic-gate */ 272*7c478bd9Sstevel@tonic-gate if (depth <= 1) { 273*7c478bd9Sstevel@tonic-gate here = telldir(dirp); 274*7c478bd9Sstevel@tonic-gate (void) closedir(dirp); 275*7c478bd9Sstevel@tonic-gate } 276*7c478bd9Sstevel@tonic-gate 277*7c478bd9Sstevel@tonic-gate /* 278*7c478bd9Sstevel@tonic-gate * Do a recursive call to process the file. 279*7c478bd9Sstevel@tonic-gate * (watch this, sports fans) 280*7c478bd9Sstevel@tonic-gate */ 281*7c478bd9Sstevel@tonic-gate rc = lf_xftw(subpath, fn, depth-1, statfn); 282*7c478bd9Sstevel@tonic-gate if (rc != 0) { 283*7c478bd9Sstevel@tonic-gate free(subpath); 284*7c478bd9Sstevel@tonic-gate if (depth > 1) 285*7c478bd9Sstevel@tonic-gate (void) closedir(dirp); 286*7c478bd9Sstevel@tonic-gate return (rc); 287*7c478bd9Sstevel@tonic-gate } 288*7c478bd9Sstevel@tonic-gate 289*7c478bd9Sstevel@tonic-gate /* 290*7c478bd9Sstevel@tonic-gate * If we closed the file, try to reopen it. 291*7c478bd9Sstevel@tonic-gate */ 292*7c478bd9Sstevel@tonic-gate if (depth <= 1) { 293*7c478bd9Sstevel@tonic-gate dirp = opendir(path); 294*7c478bd9Sstevel@tonic-gate if (dirp == NULL) { 295*7c478bd9Sstevel@tonic-gate free(subpath); 296*7c478bd9Sstevel@tonic-gate return (-1); 297*7c478bd9Sstevel@tonic-gate } 298*7c478bd9Sstevel@tonic-gate seekdir(dirp, here); 299*7c478bd9Sstevel@tonic-gate } 300*7c478bd9Sstevel@tonic-gate } 301*7c478bd9Sstevel@tonic-gate } 302*7c478bd9Sstevel@tonic-gate 303*7c478bd9Sstevel@tonic-gate /* 304*7c478bd9Sstevel@tonic-gate * We got out of the subdirectory loop. The return from 305*7c478bd9Sstevel@tonic-gate * the final readdir is in dp. Clean up. 306*7c478bd9Sstevel@tonic-gate */ 307*7c478bd9Sstevel@tonic-gate free(subpath); 308*7c478bd9Sstevel@tonic-gate (void) closedir(dirp); 309*7c478bd9Sstevel@tonic-gate return (0); 310*7c478bd9Sstevel@tonic-gate } 311