author | Jon Tibble <meths@btinternet.com> |
Thu, 09 Dec 2010 22:32:39 +0100 | |
changeset 13255 | 4afa820d78b9 |
parent 6812 | febeba71273d |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* CDDL HEADER START |
|
3 |
* |
|
4 |
* The contents of this file are subject to the terms of the |
|
3523
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
5 |
* Common Development and Distribution License (the "License"). |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
6 |
* You may not use this file except in compliance with the License. |
0 | 7 |
* |
8 |
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 |
* or http://www.opensolaris.org/os/licensing. |
|
10 |
* See the License for the specific language governing permissions |
|
11 |
* and limitations under the License. |
|
12 |
* |
|
13 |
* When distributing Covered Code, include this CDDL HEADER in each |
|
14 |
* file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 |
* If applicable, add the following below this CDDL HEADER, with the |
|
16 |
* fields enclosed by brackets "[]" replaced with your own identifying |
|
17 |
* information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 |
* |
|
19 |
* CDDL HEADER END |
|
20 |
*/ |
|
6812 | 21 |
|
0 | 22 |
/* |
6812 | 23 |
* Copyright 2008 Sun Microsystems, Inc. All rights reserved. |
0 | 24 |
* Use is subject to license terms. |
25 |
*/ |
|
26 |
||
27 |
/* Copyright (c) 1988 AT&T */ |
|
28 |
/* All Rights Reserved */ |
|
29 |
||
6812 | 30 |
#pragma ident "%Z%%M% %I% %E% SMI" |
0 | 31 |
|
32 |
/* |
|
33 |
* _xftw - file tree walk the uses expanded stat structure |
|
34 |
* |
|
35 |
* int _xftw(path, fn, depth) char *path; int (*fn)(); int depth; |
|
36 |
* |
|
37 |
* Given a path name, _xftw starts from the file given by that path |
|
38 |
* name and visits each file and directory in the tree beneath |
|
39 |
* that file. If a single file has multiple links within the |
|
40 |
* structure, it will be visited once for each such link. |
|
41 |
* For each object visited, fn is called with three arguments. |
|
42 |
* (*fn) (pathname, statp, ftwflag) |
|
43 |
* The first contains the path name of the object, the second |
|
44 |
* contains a pointer to a stat buffer which will usually hold |
|
45 |
* appropriate information for the object and the third will |
|
46 |
* contain an integer value giving additional information about |
|
47 |
* |
|
48 |
* FTW_F The object is a file for which stat was |
|
49 |
* successful. It does not guarantee that the |
|
50 |
* file can actually be read. |
|
51 |
* |
|
52 |
* FTW_D The object is a directory for which stat and |
|
53 |
* open for read were both successful. |
|
54 |
* |
|
55 |
* FTW_DNR The object is a directory for which stat |
|
56 |
* succeeded, but which cannot be read. Because |
|
57 |
* the directory cannot be read, fn will not be |
|
58 |
* called for any descendants of this directory. |
|
59 |
* |
|
60 |
* FTW_NS Stat failed on the object because of lack of |
|
61 |
* appropriate permission. This indication will |
|
62 |
* be given for example for each file in a |
|
63 |
* directory with read but no execute permission. |
|
64 |
* Because stat failed, it is not possible to |
|
65 |
* determine whether this object is a file or a |
|
66 |
* directory. The stat buffer passed to fn will |
|
67 |
* contain garbage. Stat failure for any reason |
|
68 |
* other than lack of permission will be |
|
69 |
* considered an error and will cause _xftw to stop |
|
70 |
* and return -1 to its caller. |
|
71 |
* |
|
72 |
* If fn returns nonzero, _xftw stops and returns the same value |
|
73 |
* to its caller. If _xftw gets into other trouble along the way, |
|
74 |
* it returns -1 and leaves an indication of the cause in errno. |
|
75 |
* |
|
76 |
* The third argument to _xftw does not limit the depth to which |
|
77 |
* _xftw will go. Rather, it limits the depth to which _xftw will |
|
78 |
* go before it starts recycling file descriptors. In general, |
|
79 |
* it is necessary to use a file descriptor for each level of the |
|
80 |
* tree, but they can be recycled for deep trees by saving the |
|
3523
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
81 |
* position, closing, re-opening, and seeking. In order to descend |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
82 |
* to arbitrary depths, _xftw requires 2 file descriptors to be open |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
83 |
* during the call to openat(), therefore if the depth argument |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
84 |
* is less than 2 _xftw will not use openat(), and it will fail with |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
85 |
* ENAMETOOLONG if it descends to a directory that exceeds PATH_MAX. |
0 | 86 |
*/ |
87 |
||
88 |
/* |
|
89 |
* this interface uses the expanded stat structure and therefore |
|
90 |
* must have EFT enabled. |
|
91 |
*/ |
|
92 |
#ifdef _STYPES |
|
93 |
#undef _STYPES |
|
94 |
#endif |
|
95 |
||
96 |
#include "lint.h" |
|
97 |
#include <sys/types.h> |
|
98 |
#include <sys/stat.h> |
|
3523
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
99 |
#include <fcntl.h> |
0 | 100 |
#include <sys/param.h> |
101 |
#include <dirent.h> |
|
102 |
#include <errno.h> |
|
103 |
#include <ftw.h> |
|
104 |
#include <string.h> |
|
105 |
#include <stdlib.h> |
|
3523
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
106 |
#include <unistd.h> |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
107 |
|
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
108 |
struct Var { |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
109 |
int level; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
110 |
int odepth; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
111 |
}; |
0 | 112 |
|
3523
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
113 |
static DIR *nocdopendir(const char *, struct Var *); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
114 |
static int nocdstat(const char *, struct stat *, struct Var *, int); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
115 |
static const char *get_unrooted(const char *); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
116 |
static int fwalk(const char *, int (*)(const char *, const struct stat *, int), |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
117 |
int, struct Var *); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
118 |
|
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
119 |
/*ARGSUSED*/ |
0 | 120 |
int |
121 |
_xftw(int ver, const char *path, |
|
122 |
int (*fn)(const char *, const struct stat *, int), int depth) |
|
123 |
{ |
|
3523
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
124 |
struct Var var; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
125 |
int rc; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
126 |
|
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
127 |
var.level = 0; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
128 |
var.odepth = depth; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
129 |
rc = fwalk(path, fn, depth, &var); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
130 |
return (rc); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
131 |
} |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
132 |
|
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
133 |
/* |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
134 |
* This is the recursive walker. |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
135 |
*/ |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
136 |
static int |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
137 |
fwalk(const char *path, int (*fn)(const char *, const struct stat *, int), |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
138 |
int depth, struct Var *vp) |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
139 |
{ |
0 | 140 |
size_t n; |
141 |
int rc; |
|
142 |
int save_errno; |
|
143 |
DIR *dirp; |
|
144 |
char *subpath; |
|
145 |
struct stat sb; |
|
146 |
struct dirent *direntp; |
|
147 |
||
3523
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
148 |
vp->level++; |
0 | 149 |
|
150 |
/* |
|
151 |
* Try to get file status. |
|
152 |
* If unsuccessful, errno will say why. |
|
153 |
* It's ok to have a symbolic link that points to |
|
154 |
* non-existing file. In this case, pass FTW_NS |
|
3523
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
155 |
* to a function instead of aborting fwalk() right away. |
0 | 156 |
*/ |
3523
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
157 |
if (nocdstat(path, &sb, vp, 0) < 0) { |
0 | 158 |
#ifdef S_IFLNK |
159 |
save_errno = errno; |
|
3523
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
160 |
if ((nocdstat(path, &sb, vp, AT_SYMLINK_NOFOLLOW) != -1) && |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
161 |
((sb.st_mode & S_IFMT) == S_IFLNK)) { |
0 | 162 |
errno = save_errno; |
163 |
return (*fn)(path, &sb, FTW_NS); |
|
164 |
} else { |
|
165 |
errno = save_errno; |
|
166 |
} |
|
167 |
#endif |
|
168 |
return (errno == EACCES? (*fn)(path, &sb, FTW_NS): -1); |
|
169 |
} |
|
170 |
||
171 |
/* |
|
172 |
* The stat succeeded, so we know the object exists. |
|
173 |
* If not a directory, call the user function and return. |
|
174 |
*/ |
|
175 |
if ((sb.st_mode & S_IFMT) != S_IFDIR) |
|
176 |
return ((*fn)(path, &sb, FTW_F)); |
|
177 |
||
178 |
/* |
|
179 |
* The object was a directory. |
|
180 |
* |
|
181 |
* Open a file to read the directory |
|
182 |
*/ |
|
3523
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
183 |
dirp = nocdopendir(path, vp); |
0 | 184 |
|
185 |
/* |
|
186 |
* Call the user function, telling it whether |
|
187 |
* the directory can be read. If it can't be read |
|
188 |
* call the user function or indicate an error, |
|
189 |
* depending on the reason it couldn't be read. |
|
190 |
*/ |
|
191 |
if (dirp == NULL) |
|
192 |
return (errno == EACCES? (*fn)(path, &sb, FTW_DNR): -1); |
|
193 |
||
194 |
/* We could read the directory. Call user function. */ |
|
195 |
rc = (*fn)(path, &sb, FTW_D); |
|
196 |
if (rc != 0) { |
|
197 |
(void) closedir(dirp); |
|
198 |
return (rc); |
|
199 |
} |
|
200 |
||
201 |
/* |
|
202 |
* Read the directory one component at a time. |
|
203 |
* We must ignore "." and "..", but other than that, |
|
204 |
* just create a path name and call self to check it out. |
|
205 |
*/ |
|
206 |
while (direntp = readdir(dirp)) { |
|
207 |
long here; |
|
208 |
||
209 |
if (strcmp(direntp->d_name, ".") == 0 || |
|
210 |
strcmp(direntp->d_name, "..") == 0) |
|
211 |
continue; |
|
212 |
||
3523
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
213 |
/* Create a prefix to which we will append component names */ |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
214 |
n = strlen(path); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
215 |
subpath = malloc(n + strlen(direntp->d_name) + 2); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
216 |
if (subpath == 0) { |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
217 |
(void) closedir(dirp); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
218 |
errno = ENOMEM; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
219 |
return (-1); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
220 |
} |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
221 |
(void) strcpy(subpath, path); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
222 |
if (subpath[0] != '\0' && subpath[n-1] != '/') |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
223 |
subpath[n++] = '/'; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
224 |
|
0 | 225 |
/* Append component name to the working path */ |
226 |
(void) strlcpy(&subpath[n], direntp->d_name, MAXNAMELEN); |
|
227 |
||
228 |
/* |
|
229 |
* If we are about to exceed our depth, |
|
230 |
* remember where we are and close a file. |
|
231 |
*/ |
|
232 |
if (depth <= 1) { |
|
233 |
here = telldir(dirp); |
|
3523
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
234 |
if (closedir(dirp) < 0) { |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
235 |
free(subpath); |
0 | 236 |
return (-1); |
3523
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
237 |
} |
0 | 238 |
} |
239 |
||
240 |
/* |
|
241 |
* Do a recursive call to process the file. |
|
242 |
* (watch this, sports fans) |
|
243 |
*/ |
|
3523
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
244 |
rc = fwalk(subpath, fn, depth-1, vp); |
0 | 245 |
if (rc != 0) { |
3523
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
246 |
free(subpath); |
0 | 247 |
if (depth > 1) |
248 |
(void) closedir(dirp); |
|
249 |
return (rc); |
|
250 |
} |
|
251 |
||
252 |
/* |
|
253 |
* If we closed the file, try to reopen it. |
|
254 |
*/ |
|
255 |
if (depth <= 1) { |
|
3523
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
256 |
dirp = nocdopendir(path, vp); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
257 |
if (dirp == NULL) { |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
258 |
free(subpath); |
0 | 259 |
return (-1); |
3523
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
260 |
} |
0 | 261 |
seekdir(dirp, here); |
262 |
} |
|
3523
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
263 |
free(subpath); |
0 | 264 |
} |
265 |
(void) closedir(dirp); |
|
266 |
return (0); |
|
267 |
} |
|
3523
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
268 |
|
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
269 |
/* |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
270 |
* Open a directory with an arbitrarily long path name. If the original |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
271 |
* depth arg >= 2, use openat() to make sure that it doesn't fail with |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
272 |
* ENAMETOOLONG. |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
273 |
*/ |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
274 |
static DIR * |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
275 |
nocdopendir(const char *path, struct Var *vp) |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
276 |
{ |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
277 |
int fd, cfd; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
278 |
DIR *fdd; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
279 |
char *dirp, *token, *ptr; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
280 |
|
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
281 |
fdd = opendir(path); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
282 |
if ((vp->odepth > 1) && (fdd == NULL) && (errno == ENAMETOOLONG)) { |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
283 |
/* |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
284 |
* Traverse the path using openat() to get the fd for |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
285 |
* fdopendir(). |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
286 |
*/ |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
287 |
if ((dirp = strdup(path)) == NULL) { |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
288 |
errno = ENAMETOOLONG; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
289 |
return (NULL); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
290 |
} |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
291 |
if ((token = strtok_r(dirp, "/", &ptr)) != NULL) { |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
292 |
if ((fd = openat(AT_FDCWD, dirp, O_RDONLY)) < 0) { |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
293 |
(void) free(dirp); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
294 |
errno = ENAMETOOLONG; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
295 |
return (NULL); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
296 |
} |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
297 |
while ((token = strtok_r(NULL, "/", &ptr)) != NULL) { |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
298 |
if ((cfd = openat(fd, token, O_RDONLY)) < 0) { |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
299 |
(void) close(fd); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
300 |
(void) free(dirp); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
301 |
errno = ENAMETOOLONG; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
302 |
return (NULL); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
303 |
} |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
304 |
(void) close(fd); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
305 |
fd = cfd; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
306 |
} |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
307 |
(void) free(dirp); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
308 |
return (fdopendir(fd)); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
309 |
} |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
310 |
(void) free(dirp); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
311 |
errno = ENAMETOOLONG; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
312 |
} |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
313 |
return (fdd); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
314 |
} |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
315 |
|
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
316 |
/* |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
317 |
* Stat a file with an arbitrarily long path name. If we aren't doing a |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
318 |
* stat on the arg passed to _xftw() and if the original depth arg >= 2, |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
319 |
* use openat() to make sure that it doesn't fail with ENAMETOOLONG. |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
320 |
*/ |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
321 |
static int |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
322 |
nocdstat(const char *path, struct stat *statp, struct Var *vp, int sym) |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
323 |
{ |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
324 |
int fd, cfd; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
325 |
char *dirp, *token, *ptr; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
326 |
int rc; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
327 |
const char *unrootp; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
328 |
int save_err; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
329 |
|
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
330 |
rc = fstatat(AT_FDCWD, path, statp, sym); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
331 |
if ((vp->level > 1) && (vp->odepth >= 2) && (rc < 0) && |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
332 |
(errno == ENAMETOOLONG)) { |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
333 |
/* Traverse path using openat() to get fd for fstatat(). */ |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
334 |
if ((dirp = strdup(path)) == NULL) { |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
335 |
errno = ENAMETOOLONG; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
336 |
return (-1); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
337 |
} |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
338 |
if ((token = strtok_r(dirp, "/", &ptr)) != NULL) { |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
339 |
if ((fd = openat(AT_FDCWD, dirp, O_RDONLY)) < 0) { |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
340 |
(void) free(dirp); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
341 |
errno = ENAMETOOLONG; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
342 |
return (-1); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
343 |
} |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
344 |
unrootp = get_unrooted(path); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
345 |
while (((token = strtok_r(NULL, "/", &ptr)) != NULL) && |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
346 |
(strcmp(token, unrootp) != 0)) { |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
347 |
if ((cfd = openat(fd, token, O_RDONLY)) < 0) { |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
348 |
(void) close(fd); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
349 |
(void) free(dirp); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
350 |
errno = ENAMETOOLONG; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
351 |
return (NULL); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
352 |
} |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
353 |
(void) close(fd); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
354 |
fd = cfd; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
355 |
} |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
356 |
(void) free(dirp); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
357 |
rc = fstatat(fd, unrootp, statp, sym); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
358 |
save_err = errno; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
359 |
(void) close(fd); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
360 |
errno = save_err; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
361 |
return (rc); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
362 |
} |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
363 |
(void) free(dirp); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
364 |
errno = ENAMETOOLONG; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
365 |
} |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
366 |
return (rc); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
367 |
} |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
368 |
|
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
369 |
/* |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
370 |
* Return pointer basename of path. This routine doesn't remove |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
371 |
* trailing slashes, but there won't be any. |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
372 |
*/ |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
373 |
static const char * |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
374 |
get_unrooted(const char *path) |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
375 |
{ |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
376 |
const char *ptr; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
377 |
|
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
378 |
if (!path || !*path) |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
379 |
return (NULL); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
380 |
|
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
381 |
ptr = path + strlen(path); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
382 |
/* find last char in path before any trailing slashes */ |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
383 |
while (ptr != path && *--ptr == '/') |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
384 |
; |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
385 |
|
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
386 |
if (ptr == path) /* all slashes */ |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
387 |
return (ptr); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
388 |
|
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
389 |
while (ptr != path) |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
390 |
if (*--ptr == '/') |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
391 |
return (++ptr); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
392 |
|
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
393 |
return (ptr); |
b9f12aa2fb57
PSARC/2006/403 Add two new private interfaces to nftw() for 'find' command
cf46844
parents:
0
diff
changeset
|
394 |
} |