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