17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*fe0e7ec4Smaheshvs * Common Development and Distribution License (the "License"). 6*fe0e7ec4Smaheshvs * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*fe0e7ec4Smaheshvs * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 23*fe0e7ec4Smaheshvs * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* LINTLIBRARY */ 307c478bd9Sstevel@tonic-gate /* 317c478bd9Sstevel@tonic-gate * ftw - file tree walk 327c478bd9Sstevel@tonic-gate * 337c478bd9Sstevel@tonic-gate * int ftw (path, fn, depth) char *path; int (*fn)(); int depth; 347c478bd9Sstevel@tonic-gate * 357c478bd9Sstevel@tonic-gate * Given a path name, ftw starts from the file given by that path 367c478bd9Sstevel@tonic-gate * name and visits each file and directory in the tree beneath 377c478bd9Sstevel@tonic-gate * that file. If a single file has multiple links within the 387c478bd9Sstevel@tonic-gate * structure, it will be visited once for each such link. 397c478bd9Sstevel@tonic-gate * For each object visited, fn is called with three arguments. 407c478bd9Sstevel@tonic-gate * The first contains the path name of the object, the second 417c478bd9Sstevel@tonic-gate * contains a pointer to a stat buffer which will usually hold 427c478bd9Sstevel@tonic-gate * appropriate information for the object and the third will 437c478bd9Sstevel@tonic-gate * contain an integer value giving additional information: 447c478bd9Sstevel@tonic-gate * 457c478bd9Sstevel@tonic-gate * FTW_F The object is a file for which stat was 467c478bd9Sstevel@tonic-gate * successful. It does not guarantee that the 477c478bd9Sstevel@tonic-gate * file can actually be read. 487c478bd9Sstevel@tonic-gate * 497c478bd9Sstevel@tonic-gate * FTW_D The object is a directory for which stat and 507c478bd9Sstevel@tonic-gate * open for read were both successful. 517c478bd9Sstevel@tonic-gate * 527c478bd9Sstevel@tonic-gate * FTW_DNR The object is a directory for which stat 537c478bd9Sstevel@tonic-gate * succeeded, but which cannot be read. Because 547c478bd9Sstevel@tonic-gate * the directory cannot be read, fn will not be 557c478bd9Sstevel@tonic-gate * called for any descendants of this directory. 567c478bd9Sstevel@tonic-gate * 577c478bd9Sstevel@tonic-gate * FTW_NS Stat failed on the object because of lack of 587c478bd9Sstevel@tonic-gate * appropriate permission. This indication will 597c478bd9Sstevel@tonic-gate * be given, for example, for each file in a 607c478bd9Sstevel@tonic-gate * directory with read but no execute permission. 617c478bd9Sstevel@tonic-gate * Because stat failed, it is not possible to 627c478bd9Sstevel@tonic-gate * determine whether this object is a file or a 637c478bd9Sstevel@tonic-gate * directory. The stat buffer passed to fn will 647c478bd9Sstevel@tonic-gate * contain garbage. Stat failure for any reason 657c478bd9Sstevel@tonic-gate * other than lack of permission will be 667c478bd9Sstevel@tonic-gate * considered an error and will cause ftw to stop 677c478bd9Sstevel@tonic-gate * and return -1 to its caller. 687c478bd9Sstevel@tonic-gate * 697c478bd9Sstevel@tonic-gate * If fn returns nonzero, ftw stops and returns the same value 707c478bd9Sstevel@tonic-gate * to its caller. If ftw gets into other trouble along the way, 717c478bd9Sstevel@tonic-gate * it returns -1 and leaves an indication of the cause in errno. 727c478bd9Sstevel@tonic-gate * 737c478bd9Sstevel@tonic-gate * The third argument to ftw does not limit the depth to which 747c478bd9Sstevel@tonic-gate * ftw will go. Rather, it limits the depth to which ftw will 757c478bd9Sstevel@tonic-gate * go before it starts recycling file descriptors. In general, 767c478bd9Sstevel@tonic-gate * it is necessary to use a file descriptor for each level of the 777c478bd9Sstevel@tonic-gate * tree, but they can be recycled for deep trees by saving the 787c478bd9Sstevel@tonic-gate * position, closing, re-opening, and seeking. It is possible 797c478bd9Sstevel@tonic-gate * to start recycling file descriptors by sensing when we have 807c478bd9Sstevel@tonic-gate * run out, but in general this will not be terribly useful if 817c478bd9Sstevel@tonic-gate * fn expects to be able to open files. We could also figure out 827c478bd9Sstevel@tonic-gate * how many file descriptors are available and guarantee a certain 837c478bd9Sstevel@tonic-gate * number to fn, but we would not know how many to guarantee, 847c478bd9Sstevel@tonic-gate * and we do not want to impose the extra overhead on a caller who 857c478bd9Sstevel@tonic-gate * knows how many are available without having to figure it out. 867c478bd9Sstevel@tonic-gate * 877c478bd9Sstevel@tonic-gate * It is possible for ftw to die with a memory fault in the event 887c478bd9Sstevel@tonic-gate * of a file system so deeply nested that the stack overflows. 897c478bd9Sstevel@tonic-gate */ 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h> 927c478bd9Sstevel@tonic-gate #include <sys/types.h> 937c478bd9Sstevel@tonic-gate #include <sys/stat.h> 947c478bd9Sstevel@tonic-gate #include <dirent.h> 957c478bd9Sstevel@tonic-gate #include <errno.h> 967c478bd9Sstevel@tonic-gate #include <malloc.h> 977c478bd9Sstevel@tonic-gate #include <string.h> 987c478bd9Sstevel@tonic-gate #include <fcntl.h> 997c478bd9Sstevel@tonic-gate #include <unistd.h> 1007c478bd9Sstevel@tonic-gate #include <ftw.h> 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate static int pwdfd; 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate static int lf_xftw( 1057c478bd9Sstevel@tonic-gate const char *, 1067c478bd9Sstevel@tonic-gate int (*)(const char *, const struct stat64 *, int), 1077c478bd9Sstevel@tonic-gate int, 1087c478bd9Sstevel@tonic-gate int (*)(const char *, struct stat64 *)); 1097c478bd9Sstevel@tonic-gate 110*fe0e7ec4Smaheshvs int 1117c478bd9Sstevel@tonic-gate lf_lftw( 1127c478bd9Sstevel@tonic-gate const char *path, 1137c478bd9Sstevel@tonic-gate int (*fn)(const char *, const struct stat64 *, int), 1147c478bd9Sstevel@tonic-gate int depth) 1157c478bd9Sstevel@tonic-gate { 1167c478bd9Sstevel@tonic-gate int rc; 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate if ((pwdfd = open(".", O_RDONLY)) < 0) { 1197c478bd9Sstevel@tonic-gate return (-1); 1207c478bd9Sstevel@tonic-gate } else { 1217c478bd9Sstevel@tonic-gate rc = (lf_xftw(path, fn, depth, lstat64)); 1227c478bd9Sstevel@tonic-gate (void) close(pwdfd); 1237c478bd9Sstevel@tonic-gate return (rc); 1247c478bd9Sstevel@tonic-gate } 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate static int 1287c478bd9Sstevel@tonic-gate #ifdef __STDC__ 1297c478bd9Sstevel@tonic-gate lf_xftw( 1307c478bd9Sstevel@tonic-gate const char *path, 1317c478bd9Sstevel@tonic-gate int (*fn)(const char *, const struct stat64 *, int), 1327c478bd9Sstevel@tonic-gate int depth, 1337c478bd9Sstevel@tonic-gate int (*statfn)(const char *, struct stat64 *)) 1347c478bd9Sstevel@tonic-gate #else 135*fe0e7ec4Smaheshvs lf_xftw(char *path, int (*fn)(), int depth, int (*statfn)()) 1367c478bd9Sstevel@tonic-gate #endif 1377c478bd9Sstevel@tonic-gate { 1387c478bd9Sstevel@tonic-gate int n; 1397c478bd9Sstevel@tonic-gate int rc, sublen, saverr, attrfd; 1407c478bd9Sstevel@tonic-gate DIR *dirp; 1417c478bd9Sstevel@tonic-gate char *subpath, *component; 1427c478bd9Sstevel@tonic-gate struct stat64 sb; 1437c478bd9Sstevel@tonic-gate struct dirent *dp; 1447c478bd9Sstevel@tonic-gate extern dev_t partial_dev; 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate /* 1477c478bd9Sstevel@tonic-gate * Try to get file status. 1487c478bd9Sstevel@tonic-gate * If unsuccessful, errno will say why. 1497c478bd9Sstevel@tonic-gate */ 1507c478bd9Sstevel@tonic-gate if ((*statfn)(path, &sb) < 0) 1517c478bd9Sstevel@tonic-gate return (errno == EACCES? (*fn)(path, &sb, FTW_NS): -1); 1527c478bd9Sstevel@tonic-gate /* 1537c478bd9Sstevel@tonic-gate * The stat succeeded, so we know the object exists. 1547c478bd9Sstevel@tonic-gate * Make sure it is not a mount point for another filesystem. 1557c478bd9Sstevel@tonic-gate * The following check must be made here because: 1567c478bd9Sstevel@tonic-gate * 1577c478bd9Sstevel@tonic-gate * + namefs can be mounted on anything, but a directory 1587c478bd9Sstevel@tonic-gate * + all other filesystems must be mounted on a directory 1597c478bd9Sstevel@tonic-gate */ 1607c478bd9Sstevel@tonic-gate if (sb.st_dev != partial_dev) { 1617c478bd9Sstevel@tonic-gate return (0); 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate /* 1647c478bd9Sstevel@tonic-gate * Check for presence of attributes on file 1657c478bd9Sstevel@tonic-gate */ 1667c478bd9Sstevel@tonic-gate if (pathconf(path, _PC_XATTR_EXISTS) == 1) { 1677c478bd9Sstevel@tonic-gate attrfd = attropen64(path, ".", O_RDONLY|O_NONBLOCK); 1687c478bd9Sstevel@tonic-gate } else { 1697c478bd9Sstevel@tonic-gate attrfd = -1; 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate /* 1727c478bd9Sstevel@tonic-gate * If not a directory, call the user function and return. 1737c478bd9Sstevel@tonic-gate */ 1747c478bd9Sstevel@tonic-gate if ((sb.st_mode & S_IFMT) != S_IFDIR && 1757c478bd9Sstevel@tonic-gate (sb.st_mode & IFMT) != IFATTRDIR) { 1767c478bd9Sstevel@tonic-gate rc = (*fn)(path, &sb, FTW_F); 1777c478bd9Sstevel@tonic-gate if (rc == 0 && attrfd != -1) { 1787c478bd9Sstevel@tonic-gate (void) fchdir(attrfd); 1797c478bd9Sstevel@tonic-gate rc = lf_xftw(".", fn, depth-1, statfn); 1807c478bd9Sstevel@tonic-gate (void) fchdir(pwdfd); 1817c478bd9Sstevel@tonic-gate (void) close(attrfd); 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate return (rc); 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate /* 1867c478bd9Sstevel@tonic-gate * The object was a directory and not a mount point. 1877c478bd9Sstevel@tonic-gate * 1887c478bd9Sstevel@tonic-gate * Open a file to read the directory 1897c478bd9Sstevel@tonic-gate */ 1907c478bd9Sstevel@tonic-gate dirp = opendir(path); 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate /* 1937c478bd9Sstevel@tonic-gate * Call the user function, telling it whether 1947c478bd9Sstevel@tonic-gate * the directory can be read. If it can't be read 1957c478bd9Sstevel@tonic-gate * call the user function or indicate an error, 1967c478bd9Sstevel@tonic-gate * depending on the reason it couldn't be read. 1977c478bd9Sstevel@tonic-gate */ 1987c478bd9Sstevel@tonic-gate if (dirp == NULL) 1997c478bd9Sstevel@tonic-gate rc = (errno == EACCES? (*fn)(path, &sb, FTW_DNR): -1); 2007c478bd9Sstevel@tonic-gate else 2017c478bd9Sstevel@tonic-gate rc = (*fn)(path, &sb, FTW_D); 2027c478bd9Sstevel@tonic-gate /* 2037c478bd9Sstevel@tonic-gate * If the directory has attributes, process the 2047c478bd9Sstevel@tonic-gate * attributes before processing the directory contents. 2057c478bd9Sstevel@tonic-gate */ 2067c478bd9Sstevel@tonic-gate if (rc == 0 && attrfd != -1) { 2077c478bd9Sstevel@tonic-gate (void) fchdir(attrfd); 2087c478bd9Sstevel@tonic-gate rc = lf_xftw(".", fn, depth-1, statfn); 2097c478bd9Sstevel@tonic-gate (void) fchdir(pwdfd); 2107c478bd9Sstevel@tonic-gate (void) close(attrfd); 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate if (rc != 0 || dirp == NULL) 2137c478bd9Sstevel@tonic-gate return (rc); 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate /* Allocate a buffer to hold generated pathnames. */ 2167c478bd9Sstevel@tonic-gate /* LINTED: the length will fit into a signed integer */ 2177c478bd9Sstevel@tonic-gate n = (int)strlen(path); 2187c478bd9Sstevel@tonic-gate sublen = n + MAXNAMLEN + 1; /* +1 for appended / */ 2197c478bd9Sstevel@tonic-gate subpath = malloc((unsigned)(sublen+1)); /* +1 for NUL */ 2207c478bd9Sstevel@tonic-gate if (subpath == NULL) { 2217c478bd9Sstevel@tonic-gate saverr = errno; 2227c478bd9Sstevel@tonic-gate (void) closedir(dirp); 2237c478bd9Sstevel@tonic-gate errno = saverr; 2247c478bd9Sstevel@tonic-gate return (-1); 2257c478bd9Sstevel@tonic-gate } 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate /* Create a prefix to which we will append component names */ 2287c478bd9Sstevel@tonic-gate (void) strcpy(subpath, path); 2297c478bd9Sstevel@tonic-gate if (subpath[0] != '\0' && subpath[n-1] != '/') 2307c478bd9Sstevel@tonic-gate subpath[n++] = '/'; 2317c478bd9Sstevel@tonic-gate component = &subpath[n]; 2327c478bd9Sstevel@tonic-gate /* LINTED: result will fit into a 32-bit int */ 2337c478bd9Sstevel@tonic-gate sublen -= component - subpath; 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate /* 2367c478bd9Sstevel@tonic-gate * Read the directory one component at a time. 2377c478bd9Sstevel@tonic-gate * We must ignore "." and "..", but other than that, 2387c478bd9Sstevel@tonic-gate * just create a path name and call self to check it out. 2397c478bd9Sstevel@tonic-gate */ 2407c478bd9Sstevel@tonic-gate while ((dp = readdir(dirp)) != NULL) { 2417c478bd9Sstevel@tonic-gate if (strcmp(dp->d_name, ".") != 0 && 2427c478bd9Sstevel@tonic-gate strcmp(dp->d_name, "..") != 0) { 2437c478bd9Sstevel@tonic-gate long here; 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate /* Append component name to the working path */ 2467c478bd9Sstevel@tonic-gate (void) strncpy(component, dp->d_name, sublen); 2477c478bd9Sstevel@tonic-gate component[sublen - 1] = '\0'; 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate /* 2507c478bd9Sstevel@tonic-gate * If we are about to exceed our depth, 2517c478bd9Sstevel@tonic-gate * remember where we are and close a file. 2527c478bd9Sstevel@tonic-gate */ 2537c478bd9Sstevel@tonic-gate if (depth <= 1) { 2547c478bd9Sstevel@tonic-gate here = telldir(dirp); 2557c478bd9Sstevel@tonic-gate (void) closedir(dirp); 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate /* 2597c478bd9Sstevel@tonic-gate * Do a recursive call to process the file. 2607c478bd9Sstevel@tonic-gate * (watch this, sports fans) 2617c478bd9Sstevel@tonic-gate */ 2627c478bd9Sstevel@tonic-gate rc = lf_xftw(subpath, fn, depth-1, statfn); 2637c478bd9Sstevel@tonic-gate if (rc != 0) { 2647c478bd9Sstevel@tonic-gate free(subpath); 2657c478bd9Sstevel@tonic-gate if (depth > 1) 2667c478bd9Sstevel@tonic-gate (void) closedir(dirp); 2677c478bd9Sstevel@tonic-gate return (rc); 2687c478bd9Sstevel@tonic-gate } 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate /* 2717c478bd9Sstevel@tonic-gate * If we closed the file, try to reopen it. 2727c478bd9Sstevel@tonic-gate */ 2737c478bd9Sstevel@tonic-gate if (depth <= 1) { 2747c478bd9Sstevel@tonic-gate dirp = opendir(path); 2757c478bd9Sstevel@tonic-gate if (dirp == NULL) { 2767c478bd9Sstevel@tonic-gate free(subpath); 2777c478bd9Sstevel@tonic-gate return (-1); 2787c478bd9Sstevel@tonic-gate } 2797c478bd9Sstevel@tonic-gate seekdir(dirp, here); 2807c478bd9Sstevel@tonic-gate } 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate /* 2857c478bd9Sstevel@tonic-gate * We got out of the subdirectory loop. The return from 2867c478bd9Sstevel@tonic-gate * the final readdir is in dp. Clean up. 2877c478bd9Sstevel@tonic-gate */ 2887c478bd9Sstevel@tonic-gate free(subpath); 2897c478bd9Sstevel@tonic-gate (void) closedir(dirp); 2907c478bd9Sstevel@tonic-gate return (0); 2917c478bd9Sstevel@tonic-gate } 292