]>
Commit | Line | Data |
---|---|---|
76b87c03 | 1 | /* File tree walker functions. |
04277e02 | 2 | Copyright (C) 1996-2019 Free Software Foundation, Inc. |
47707456 | 3 | This file is part of the GNU C Library. |
76b87c03 | 4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. |
28f540f4 | 5 | |
47707456 | 6 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
28f540f4 | 10 | |
47707456 UD |
11 | The GNU C Library is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
28f540f4 | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
28f540f4 | 19 | |
aff4519d UD |
20 | #ifdef HAVE_CONFIG_H |
21 | # include <config.h> | |
22 | #endif | |
23 | ||
ae9ecd08 UD |
24 | #if __GNUC__ |
25 | # define alloca __builtin_alloca | |
26 | #else | |
27 | # if HAVE_ALLOCA_H | |
28 | # include <alloca.h> | |
29 | # else | |
30 | # ifdef _AIX | |
31 | # pragma alloca | |
32 | # else | |
33 | char *alloca (); | |
34 | # endif | |
35 | # endif | |
36 | #endif | |
37 | ||
becac6c5 | 38 | #ifdef _LIBC |
ae9ecd08 UD |
39 | # include <dirent.h> |
40 | # define NAMLEN(dirent) _D_EXACT_NAMLEN (dirent) | |
41 | #else | |
42 | # if HAVE_DIRENT_H | |
43 | # include <dirent.h> | |
44 | # define NAMLEN(dirent) strlen ((dirent)->d_name) | |
45 | # else | |
46 | # define dirent direct | |
47 | # define NAMLEN(dirent) (dirent)->d_namlen | |
48 | # if HAVE_SYS_NDIR_H | |
49 | # include <sys/ndir.h> | |
50 | # endif | |
51 | # if HAVE_SYS_DIR_H | |
52 | # include <sys/dir.h> | |
53 | # endif | |
54 | # if HAVE_NDIR_H | |
55 | # include <ndir.h> | |
56 | # endif | |
57 | # endif | |
58 | #endif | |
59 | ||
28f540f4 | 60 | #include <errno.h> |
becac6c5 | 61 | #include <fcntl.h> |
76b87c03 | 62 | #include <ftw.h> |
5049f197 | 63 | #include <limits.h> |
76b87c03 | 64 | #include <search.h> |
28f540f4 RM |
65 | #include <stdlib.h> |
66 | #include <string.h> | |
76b87c03 | 67 | #include <unistd.h> |
d369ad76 | 68 | #include <not-cancel.h> |
12f2254b | 69 | #include <sys/param.h> |
aff4519d UD |
70 | #ifdef _LIBC |
71 | # include <include/sys/stat.h> | |
72 | #else | |
73 | # include <sys/stat.h> | |
74 | #endif | |
28f540f4 | 75 | |
ae9ecd08 UD |
76 | #if ! _LIBC && !HAVE_DECL_STPCPY && !defined stpcpy |
77 | char *stpcpy (); | |
78 | #endif | |
79 | ||
80 | #if ! _LIBC && ! defined HAVE_MEMPCPY && ! defined mempcpy | |
81 | /* Be CAREFUL that there are no side effects in N. */ | |
82 | # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N))) | |
83 | #endif | |
84 | ||
76b87c03 UD |
85 | /* #define NDEBUG 1 */ |
86 | #include <assert.h> | |
28f540f4 | 87 | |
aff4519d UD |
88 | #ifndef _LIBC |
89 | # undef __chdir | |
90 | # define __chdir chdir | |
91 | # undef __closedir | |
92 | # define __closedir closedir | |
93 | # undef __fchdir | |
94 | # define __fchdir fchdir | |
95 | # undef __getcwd | |
ae9ecd08 UD |
96 | # define __getcwd(P, N) xgetcwd () |
97 | extern char *xgetcwd (void); | |
98 | # undef __mempcpy | |
99 | # define __mempcpy mempcpy | |
aff4519d UD |
100 | # undef __opendir |
101 | # define __opendir opendir | |
102 | # undef __readdir64 | |
103 | # define __readdir64 readdir | |
ae9ecd08 UD |
104 | # undef __stpcpy |
105 | # define __stpcpy stpcpy | |
aff4519d UD |
106 | # undef __tdestroy |
107 | # define __tdestroy tdestroy | |
108 | # undef __tfind | |
109 | # define __tfind tfind | |
110 | # undef __tsearch | |
111 | # define __tsearch tsearch | |
aff4519d UD |
112 | # undef dirent64 |
113 | # define dirent64 dirent | |
114 | # undef MAX | |
115 | # define MAX(a, b) ((a) > (b) ? (a) : (b)) | |
116 | #endif | |
117 | ||
ae9ecd08 UD |
118 | /* Arrange to make lstat calls go through the wrapper function |
119 | on systems with an lstat function that does not dereference symlinks | |
120 | that are specified with a trailing slash. */ | |
121 | #if ! _LIBC && ! LSTAT_FOLLOWS_SLASHED_SYMLINK | |
122 | int rpl_lstat (const char *, struct stat *); | |
123 | # undef lstat | |
124 | # define lstat(Name, Stat_buf) rpl_lstat(Name, Stat_buf) | |
125 | #endif | |
126 | ||
aff4519d UD |
127 | #ifndef __set_errno |
128 | # define __set_errno(Val) errno = (Val) | |
129 | #endif | |
130 | ||
dfd2257a UD |
131 | /* Support for the LFS API version. */ |
132 | #ifndef FTW_NAME | |
133 | # define FTW_NAME ftw | |
134 | # define NFTW_NAME nftw | |
ca10f338 UD |
135 | # define NFTW_OLD_NAME __old_nftw |
136 | # define NFTW_NEW_NAME __new_nftw | |
dfd2257a UD |
137 | # define INO_T ino_t |
138 | # define STAT stat | |
ae9ecd08 UD |
139 | # ifdef _LIBC |
140 | # define LXSTAT __lxstat | |
141 | # define XSTAT __xstat | |
d369ad76 | 142 | # define FXSTATAT __fxstatat |
ae9ecd08 UD |
143 | # else |
144 | # define LXSTAT(V,f,sb) lstat (f,sb) | |
145 | # define XSTAT(V,f,sb) stat (f,sb) | |
d369ad76 | 146 | # define FXSTATAT(V,d,f,sb,m) fstatat (d, f, sb, m) |
ae9ecd08 | 147 | # endif |
dfd2257a UD |
148 | # define FTW_FUNC_T __ftw_func_t |
149 | # define NFTW_FUNC_T __nftw_func_t | |
150 | #endif | |
28f540f4 | 151 | |
5049f197 UD |
152 | /* We define PATH_MAX if the system does not provide a definition. |
153 | This does not artificially limit any operation. PATH_MAX is simply | |
154 | used as a guesstimate for the expected maximal path length. | |
155 | Buffers will be enlarged if necessary. */ | |
156 | #ifndef PATH_MAX | |
157 | # define PATH_MAX 1024 | |
158 | #endif | |
159 | ||
76b87c03 UD |
160 | struct dir_data |
161 | { | |
162 | DIR *stream; | |
d369ad76 | 163 | int streamfd; |
76b87c03 UD |
164 | char *content; |
165 | }; | |
28f540f4 | 166 | |
d951286f UD |
167 | struct known_object |
168 | { | |
169 | dev_t dev; | |
dfd2257a | 170 | INO_T ino; |
d951286f UD |
171 | }; |
172 | ||
76b87c03 | 173 | struct ftw_data |
28f540f4 | 174 | { |
d951286f | 175 | /* Array with pointers to open directory streams. */ |
76b87c03 UD |
176 | struct dir_data **dirstreams; |
177 | size_t actdir; | |
178 | size_t maxdir; | |
28f540f4 | 179 | |
d951286f | 180 | /* Buffer containing name of currently processed object. */ |
76b87c03 UD |
181 | char *dirbuf; |
182 | size_t dirbufsize; | |
d951286f UD |
183 | |
184 | /* Passed as fourth argument to `nftw' callback. The `base' member | |
185 | tracks the content of the `dirbuf'. */ | |
76b87c03 | 186 | struct FTW ftw; |
28f540f4 | 187 | |
d951286f | 188 | /* Flags passed to `nftw' function. 0 for `ftw'. */ |
76b87c03 | 189 | int flags; |
28f540f4 | 190 | |
d951286f | 191 | /* Conversion array for flag values. It is the identity mapping for |
ae9ecd08 | 192 | `nftw' calls, otherwise it maps the values to those known by |
d951286f | 193 | `ftw'. */ |
390500b1 | 194 | const int *cvt_arr; |
28f540f4 | 195 | |
d951286f | 196 | /* Callback function. We always use the `nftw' form. */ |
dfd2257a | 197 | NFTW_FUNC_T func; |
28f540f4 | 198 | |
d951286f | 199 | /* Device of starting point. Needed for FTW_MOUNT. */ |
76b87c03 | 200 | dev_t dev; |
d951286f UD |
201 | |
202 | /* Data structure for keeping fingerprints of already processed | |
203 | object. This is needed when not using FTW_PHYS. */ | |
204 | void *known_objects; | |
76b87c03 | 205 | }; |
28f540f4 | 206 | |
92777700 | 207 | |
ae9ecd08 UD |
208 | /* Internally we use the FTW_* constants used for `nftw'. When invoked |
209 | as `ftw', map each flag to the subset of values used by `ftw'. */ | |
390500b1 | 210 | static const int nftw_arr[] = |
76b87c03 UD |
211 | { |
212 | FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_SL, FTW_DP, FTW_SLN | |
213 | }; | |
28f540f4 | 214 | |
390500b1 | 215 | static const int ftw_arr[] = |
76b87c03 UD |
216 | { |
217 | FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_F, FTW_D, FTW_NS | |
218 | }; | |
28f540f4 | 219 | |
76b87c03 UD |
220 | |
221 | /* Forward declarations of local functions. */ | |
ca10f338 | 222 | static int ftw_dir (struct ftw_data *data, struct STAT *st, |
116ac301 | 223 | struct dir_data *old_dir); |
d951286f UD |
224 | |
225 | ||
226 | static int | |
227 | object_compare (const void *p1, const void *p2) | |
228 | { | |
229 | /* We don't need a sophisticated and useful comparison. We are only | |
0413b54c UD |
230 | interested in equality. However, we must be careful not to |
231 | accidentally compare `holes' in the structure. */ | |
232 | const struct known_object *kp1 = p1, *kp2 = p2; | |
233 | int cmp1; | |
78e88510 | 234 | cmp1 = (kp1->ino > kp2->ino) - (kp1->ino < kp2->ino); |
0413b54c UD |
235 | if (cmp1 != 0) |
236 | return cmp1; | |
78e88510 | 237 | return (kp1->dev > kp2->dev) - (kp1->dev < kp2->dev); |
d951286f UD |
238 | } |
239 | ||
240 | ||
300ea0ad | 241 | static int |
dfd2257a | 242 | add_object (struct ftw_data *data, struct STAT *st) |
d951286f UD |
243 | { |
244 | struct known_object *newp = malloc (sizeof (struct known_object)); | |
245 | if (newp == NULL) | |
246 | return -1; | |
247 | newp->dev = st->st_dev; | |
248 | newp->ino = st->st_ino; | |
249 | return __tsearch (newp, &data->known_objects, object_compare) ? 0 : -1; | |
250 | } | |
251 | ||
252 | ||
253 | static inline int | |
dfd2257a | 254 | find_object (struct ftw_data *data, struct STAT *st) |
d951286f | 255 | { |
ae9ecd08 UD |
256 | struct known_object obj; |
257 | obj.dev = st->st_dev; | |
258 | obj.ino = st->st_ino; | |
d951286f UD |
259 | return __tfind (&obj, &data->known_objects, object_compare) != NULL; |
260 | } | |
76b87c03 UD |
261 | |
262 | ||
263 | static inline int | |
dd9423a6 | 264 | __attribute ((always_inline)) |
d369ad76 | 265 | open_dir_stream (int *dfdp, struct ftw_data *data, struct dir_data *dirp) |
76b87c03 UD |
266 | { |
267 | int result = 0; | |
268 | ||
269 | if (data->dirstreams[data->actdir] != NULL) | |
270 | { | |
271 | /* Oh, oh. We must close this stream. Get all remaining | |
272 | entries and store them as a list in the `content' member of | |
273 | the `struct dir_data' variable. */ | |
274 | size_t bufsize = 1024; | |
275 | char *buf = malloc (bufsize); | |
276 | ||
277 | if (buf == NULL) | |
278 | result = -1; | |
279 | else | |
28f540f4 | 280 | { |
76b87c03 | 281 | DIR *st = data->dirstreams[data->actdir]->stream; |
2958e6cc | 282 | struct dirent64 *d; |
76b87c03 UD |
283 | size_t actsize = 0; |
284 | ||
2958e6cc | 285 | while ((d = __readdir64 (st)) != NULL) |
76b87c03 | 286 | { |
ae9ecd08 | 287 | size_t this_len = NAMLEN (d); |
76b87c03 UD |
288 | if (actsize + this_len + 2 >= bufsize) |
289 | { | |
290 | char *newp; | |
291 | bufsize += MAX (1024, 2 * this_len); | |
a5ce5fcf | 292 | newp = (char *) realloc (buf, bufsize); |
76b87c03 UD |
293 | if (newp == NULL) |
294 | { | |
295 | /* No more memory. */ | |
296 | int save_err = errno; | |
297 | free (buf); | |
298 | __set_errno (save_err); | |
400cc70a | 299 | return -1; |
76b87c03 UD |
300 | } |
301 | buf = newp; | |
302 | } | |
303 | ||
86187531 UD |
304 | *((char *) __mempcpy (buf + actsize, d->d_name, this_len)) |
305 | = '\0'; | |
306 | actsize += this_len + 1; | |
76b87c03 | 307 | } |
28f540f4 | 308 | |
76b87c03 UD |
309 | /* Terminate the list with an additional NUL byte. */ |
310 | buf[actsize++] = '\0'; | |
28f540f4 | 311 | |
76b87c03 UD |
312 | /* Shrink the buffer to what we actually need. */ |
313 | data->dirstreams[data->actdir]->content = realloc (buf, actsize); | |
314 | if (data->dirstreams[data->actdir]->content == NULL) | |
315 | { | |
316 | int save_err = errno; | |
317 | free (buf); | |
318 | __set_errno (save_err); | |
319 | result = -1; | |
320 | } | |
28f540f4 RM |
321 | else |
322 | { | |
50304ef0 | 323 | __closedir (st); |
76b87c03 | 324 | data->dirstreams[data->actdir]->stream = NULL; |
d369ad76 | 325 | data->dirstreams[data->actdir]->streamfd = -1; |
76b87c03 | 326 | data->dirstreams[data->actdir] = NULL; |
28f540f4 RM |
327 | } |
328 | } | |
76b87c03 UD |
329 | } |
330 | ||
331 | /* Open the new stream. */ | |
332 | if (result == 0) | |
333 | { | |
334 | assert (data->dirstreams[data->actdir] == NULL); | |
335 | ||
d369ad76 UD |
336 | if (dfdp != NULL && *dfdp != -1) |
337 | { | |
0bb2fabc AZ |
338 | int fd = __openat64_nocancel (*dfdp, data->dirbuf + data->ftw.base, |
339 | O_RDONLY | O_DIRECTORY | O_NDELAY); | |
d369ad76 UD |
340 | dirp->stream = NULL; |
341 | if (fd != -1 && (dirp->stream = __fdopendir (fd)) == NULL) | |
c181840c | 342 | __close_nocancel_nostatus (fd); |
d369ad76 UD |
343 | } |
344 | else | |
345 | { | |
63a2f305 UD |
346 | const char *name; |
347 | ||
348 | if (data->flags & FTW_CHDIR) | |
349 | { | |
350 | name = data->dirbuf + data->ftw.base; | |
351 | if (name[0] == '\0') | |
352 | name = "."; | |
353 | } | |
354 | else | |
355 | name = data->dirbuf; | |
356 | ||
d369ad76 UD |
357 | dirp->stream = __opendir (name); |
358 | } | |
359 | ||
76b87c03 UD |
360 | if (dirp->stream == NULL) |
361 | result = -1; | |
28f540f4 | 362 | else |
76b87c03 | 363 | { |
2c7bbfaf | 364 | dirp->streamfd = __dirfd (dirp->stream); |
76b87c03 UD |
365 | dirp->content = NULL; |
366 | data->dirstreams[data->actdir] = dirp; | |
367 | ||
368 | if (++data->actdir == data->maxdir) | |
369 | data->actdir = 0; | |
370 | } | |
371 | } | |
372 | ||
373 | return result; | |
374 | } | |
375 | ||
376 | ||
dd9423a6 | 377 | static int |
76b87c03 | 378 | process_entry (struct ftw_data *data, struct dir_data *dir, const char *name, |
d369ad76 | 379 | size_t namlen, int d_type) |
76b87c03 | 380 | { |
dfd2257a | 381 | struct STAT st; |
76b87c03 | 382 | int result = 0; |
256846bb | 383 | int flag = 0; |
5049f197 | 384 | size_t new_buflen; |
76b87c03 UD |
385 | |
386 | if (name[0] == '.' && (name[1] == '\0' | |
387 | || (name[1] == '.' && name[2] == '\0'))) | |
388 | /* Don't process the "." and ".." entries. */ | |
389 | return 0; | |
390 | ||
5049f197 UD |
391 | new_buflen = data->ftw.base + namlen + 2; |
392 | if (data->dirbufsize < new_buflen) | |
76b87c03 UD |
393 | { |
394 | /* Enlarge the buffer. */ | |
395 | char *newp; | |
396 | ||
5049f197 | 397 | data->dirbufsize = 2 * new_buflen; |
aff4519d | 398 | newp = (char *) realloc (data->dirbuf, data->dirbufsize); |
76b87c03 UD |
399 | if (newp == NULL) |
400 | return -1; | |
401 | data->dirbuf = newp; | |
402 | } | |
28f540f4 | 403 | |
86187531 | 404 | *((char *) __mempcpy (data->dirbuf + data->ftw.base, name, namlen)) = '\0'; |
28f540f4 | 405 | |
d369ad76 UD |
406 | int statres; |
407 | if (dir->streamfd != -1) | |
408 | statres = FXSTATAT (_STAT_VER, dir->streamfd, name, &st, | |
409 | (data->flags & FTW_PHYS) ? AT_SYMLINK_NOFOLLOW : 0); | |
410 | else | |
411 | { | |
412 | if ((data->flags & FTW_CHDIR) == 0) | |
413 | name = data->dirbuf; | |
414 | ||
415 | statres = ((data->flags & FTW_PHYS) | |
416 | ? LXSTAT (_STAT_VER, name, &st) | |
417 | : XSTAT (_STAT_VER, name, &st)); | |
418 | } | |
aff4519d | 419 | |
d369ad76 | 420 | if (statres < 0) |
76b87c03 UD |
421 | { |
422 | if (errno != EACCES && errno != ENOENT) | |
423 | result = -1; | |
bb549088 UD |
424 | else if (data->flags & FTW_PHYS) |
425 | flag = FTW_NS; | |
426 | else if (d_type == DT_LNK) | |
76b87c03 UD |
427 | flag = FTW_SLN; |
428 | else | |
bb549088 UD |
429 | { |
430 | if (dir->streamfd != -1) | |
431 | statres = FXSTATAT (_STAT_VER, dir->streamfd, name, &st, | |
432 | AT_SYMLINK_NOFOLLOW); | |
433 | else | |
434 | statres = LXSTAT (_STAT_VER, name, &st); | |
435 | if (statres == 0 && S_ISLNK (st.st_mode)) | |
436 | flag = FTW_SLN; | |
437 | else | |
438 | flag = FTW_NS; | |
439 | } | |
76b87c03 UD |
440 | } |
441 | else | |
442 | { | |
d951286f | 443 | if (S_ISDIR (st.st_mode)) |
76b87c03 | 444 | flag = FTW_D; |
d951286f | 445 | else if (S_ISLNK (st.st_mode)) |
76b87c03 UD |
446 | flag = FTW_SL; |
447 | else | |
448 | flag = FTW_F; | |
449 | } | |
450 | ||
451 | if (result == 0 | |
b13927da UD |
452 | && (flag == FTW_NS |
453 | || !(data->flags & FTW_MOUNT) || st.st_dev == data->dev)) | |
76b87c03 | 454 | { |
eb7c2001 | 455 | if (flag == FTW_D) |
28f540f4 | 456 | { |
eb7c2001 UD |
457 | if ((data->flags & FTW_PHYS) |
458 | || (!find_object (data, &st) | |
459 | /* Remember the object. */ | |
460 | && (result = add_object (data, &st)) == 0)) | |
ca10f338 | 461 | result = ftw_dir (data, &st, dir); |
28f540f4 | 462 | } |
eb7c2001 UD |
463 | else |
464 | result = (*data->func) (data->dirbuf, &st, data->cvt_arr[flag], | |
465 | &data->ftw); | |
76b87c03 UD |
466 | } |
467 | ||
ca10f338 UD |
468 | if ((data->flags & FTW_ACTIONRETVAL) && result == FTW_SKIP_SUBTREE) |
469 | result = 0; | |
470 | ||
76b87c03 UD |
471 | return result; |
472 | } | |
473 | ||
474 | ||
475 | static int | |
ca10f338 | 476 | __attribute ((noinline)) |
ca10f338 | 477 | ftw_dir (struct ftw_data *data, struct STAT *st, struct dir_data *old_dir) |
76b87c03 UD |
478 | { |
479 | struct dir_data dir; | |
2958e6cc | 480 | struct dirent64 *d; |
76b87c03 | 481 | int previous_base = data->ftw.base; |
d951286f | 482 | int result; |
76b87c03 UD |
483 | char *startp; |
484 | ||
d951286f UD |
485 | /* Open the stream for this directory. This might require that |
486 | another stream has to be closed. */ | |
d369ad76 UD |
487 | result = open_dir_stream (old_dir == NULL ? NULL : &old_dir->streamfd, |
488 | data, &dir); | |
d951286f UD |
489 | if (result != 0) |
490 | { | |
491 | if (errno == EACCES) | |
492 | /* We cannot read the directory. Signal this with a special flag. */ | |
493 | result = (*data->func) (data->dirbuf, st, FTW_DNR, &data->ftw); | |
494 | ||
495 | return result; | |
496 | } | |
497 | ||
76b87c03 UD |
498 | /* First, report the directory (if not depth-first). */ |
499 | if (!(data->flags & FTW_DEPTH)) | |
500 | { | |
d951286f | 501 | result = (*data->func) (data->dirbuf, st, FTW_D, &data->ftw); |
76b87c03 | 502 | if (result != 0) |
76b87c03 | 503 | { |
93787845 UD |
504 | int save_err; |
505 | fail: | |
506 | save_err = errno; | |
50304ef0 | 507 | __closedir (dir.stream); |
d369ad76 | 508 | dir.streamfd = -1; |
76b87c03 UD |
509 | __set_errno (save_err); |
510 | ||
511 | if (data->actdir-- == 0) | |
512 | data->actdir = data->maxdir - 1; | |
513 | data->dirstreams[data->actdir] = NULL; | |
93787845 UD |
514 | return result; |
515 | } | |
516 | } | |
76b87c03 | 517 | |
93787845 UD |
518 | /* If necessary, change to this directory. */ |
519 | if (data->flags & FTW_CHDIR) | |
520 | { | |
2c7bbfaf | 521 | if (__fchdir (__dirfd (dir.stream)) < 0) |
93787845 UD |
522 | { |
523 | result = -1; | |
524 | goto fail; | |
76b87c03 | 525 | } |
28f540f4 RM |
526 | } |
527 | ||
76b87c03 UD |
528 | /* Next, update the `struct FTW' information. */ |
529 | ++data->ftw.level; | |
e7c8359e | 530 | startp = __rawmemchr (data->dirbuf, '\0'); |
25f227b9 UD |
531 | /* There always must be a directory name. */ |
532 | assert (startp != data->dirbuf); | |
21a568e2 | 533 | if (startp[-1] != '/') |
25f227b9 | 534 | *startp++ = '/'; |
76b87c03 | 535 | data->ftw.base = startp - data->dirbuf; |
28f540f4 | 536 | |
2958e6cc | 537 | while (dir.stream != NULL && (d = __readdir64 (dir.stream)) != NULL) |
76b87c03 | 538 | { |
bea9b193 RM |
539 | int d_type = DT_UNKNOWN; |
540 | #ifdef _DIRENT_HAVE_D_TYPE | |
541 | d_type = d->d_type; | |
542 | #endif | |
543 | result = process_entry (data, &dir, d->d_name, NAMLEN (d), d_type); | |
76b87c03 UD |
544 | if (result != 0) |
545 | break; | |
546 | } | |
28f540f4 | 547 | |
76b87c03 | 548 | if (dir.stream != NULL) |
28f540f4 | 549 | { |
76b87c03 UD |
550 | /* The stream is still open. I.e., we did not need more |
551 | descriptors. Simply close the stream now. */ | |
552 | int save_err = errno; | |
553 | ||
554 | assert (dir.content == NULL); | |
555 | ||
50304ef0 | 556 | __closedir (dir.stream); |
d369ad76 | 557 | dir.streamfd = -1; |
76b87c03 UD |
558 | __set_errno (save_err); |
559 | ||
560 | if (data->actdir-- == 0) | |
561 | data->actdir = data->maxdir - 1; | |
562 | data->dirstreams[data->actdir] = NULL; | |
28f540f4 | 563 | } |
76b87c03 | 564 | else |
28f540f4 | 565 | { |
76b87c03 UD |
566 | int save_err; |
567 | char *runp = dir.content; | |
568 | ||
3d73829c | 569 | while (result == 0 && *runp != '\0') |
28f540f4 | 570 | { |
76b87c03 UD |
571 | char *endp = strchr (runp, '\0'); |
572 | ||
d369ad76 UD |
573 | // XXX Should store the d_type values as well?! |
574 | result = process_entry (data, &dir, runp, endp - runp, DT_UNKNOWN); | |
76b87c03 UD |
575 | |
576 | runp = endp + 1; | |
28f540f4 | 577 | } |
76b87c03 UD |
578 | |
579 | save_err = errno; | |
580 | free (dir.content); | |
581 | __set_errno (save_err); | |
28f540f4 | 582 | } |
28f540f4 | 583 | |
ca10f338 UD |
584 | if ((data->flags & FTW_ACTIONRETVAL) && result == FTW_SKIP_SIBLINGS) |
585 | result = 0; | |
586 | ||
76b87c03 | 587 | /* Prepare the return, revert the `struct FTW' information. */ |
d951286f | 588 | data->dirbuf[data->ftw.base - 1] = '\0'; |
76b87c03 UD |
589 | --data->ftw.level; |
590 | data->ftw.base = previous_base; | |
591 | ||
592 | /* Finally, if we process depth-first report the directory. */ | |
593 | if (result == 0 && (data->flags & FTW_DEPTH)) | |
d951286f | 594 | result = (*data->func) (data->dirbuf, st, FTW_DP, &data->ftw); |
28f540f4 | 595 | |
ca10f338 UD |
596 | if (old_dir |
597 | && (data->flags & FTW_CHDIR) | |
598 | && (result == 0 | |
599 | || ((data->flags & FTW_ACTIONRETVAL) | |
600 | && (result != -1 && result != FTW_STOP)))) | |
601 | { | |
602 | /* Change back to the parent directory. */ | |
603 | int done = 0; | |
604 | if (old_dir->stream != NULL) | |
2c7bbfaf | 605 | if (__fchdir (__dirfd (old_dir->stream)) == 0) |
ca10f338 UD |
606 | done = 1; |
607 | ||
608 | if (!done) | |
609 | { | |
610 | if (data->ftw.base == 1) | |
611 | { | |
612 | if (__chdir ("/") < 0) | |
613 | result = -1; | |
614 | } | |
615 | else | |
616 | if (__chdir ("..") < 0) | |
617 | result = -1; | |
618 | } | |
619 | } | |
620 | ||
76b87c03 UD |
621 | return result; |
622 | } | |
623 | ||
624 | ||
625 | static int | |
ca10f338 | 626 | __attribute ((noinline)) |
76b87c03 UD |
627 | ftw_startup (const char *dir, int is_nftw, void *func, int descriptors, |
628 | int flags) | |
629 | { | |
630 | struct ftw_data data; | |
dfd2257a | 631 | struct STAT st; |
76b87c03 UD |
632 | int result = 0; |
633 | int save_err; | |
becac6c5 | 634 | int cwdfd = -1; |
b576fca1 | 635 | char *cwd = NULL; |
76b87c03 UD |
636 | char *cp; |
637 | ||
638 | /* First make sure the parameters are reasonable. */ | |
639 | if (dir[0] == '\0') | |
640 | { | |
a7f775a5 | 641 | __set_errno (ENOENT); |
76b87c03 UD |
642 | return -1; |
643 | } | |
28f540f4 | 644 | |
76b87c03 UD |
645 | data.maxdir = descriptors < 1 ? 1 : descriptors; |
646 | data.actdir = 0; | |
647 | data.dirstreams = (struct dir_data **) alloca (data.maxdir | |
648 | * sizeof (struct dir_data *)); | |
649 | memset (data.dirstreams, '\0', data.maxdir * sizeof (struct dir_data *)); | |
650 | ||
5049f197 | 651 | /* PATH_MAX is always defined when we get here. */ |
76b87c03 UD |
652 | data.dirbufsize = MAX (2 * strlen (dir), PATH_MAX); |
653 | data.dirbuf = (char *) malloc (data.dirbufsize); | |
654 | if (data.dirbuf == NULL) | |
655 | return -1; | |
d951286f | 656 | cp = __stpcpy (data.dirbuf, dir); |
76b87c03 UD |
657 | /* Strip trailing slashes. */ |
658 | while (cp > data.dirbuf + 1 && cp[-1] == '/') | |
659 | --cp; | |
660 | *cp = '\0'; | |
661 | ||
662 | data.ftw.level = 0; | |
663 | ||
664 | /* Find basename. */ | |
665 | while (cp > data.dirbuf && cp[-1] != '/') | |
666 | --cp; | |
667 | data.ftw.base = cp - data.dirbuf; | |
668 | ||
669 | data.flags = flags; | |
670 | ||
671 | /* This assignment might seem to be strange but it is what we want. | |
672 | The trick is that the first three arguments to the `ftw' and | |
673 | `nftw' callback functions are equal. Therefore we can call in | |
674 | every case the callback using the format of the `nftw' version | |
675 | and get the correct result since the stack layout for a function | |
676 | call in C allows this. */ | |
dfd2257a | 677 | data.func = (NFTW_FUNC_T) func; |
76b87c03 UD |
678 | |
679 | /* Since we internally use the complete set of FTW_* values we need | |
680 | to reduce the value range before calling a `ftw' callback. */ | |
681 | data.cvt_arr = is_nftw ? nftw_arr : ftw_arr; | |
682 | ||
d951286f UD |
683 | /* No object known so far. */ |
684 | data.known_objects = NULL; | |
685 | ||
76b87c03 | 686 | /* Now go to the directory containing the initial file/directory. */ |
34c86f42 | 687 | if (flags & FTW_CHDIR) |
28f540f4 | 688 | { |
becac6c5 UD |
689 | /* We have to be able to go back to the current working |
690 | directory. The best way to do this is to use a file | |
691 | descriptor. */ | |
692 | cwdfd = __open (".", O_RDONLY | O_DIRECTORY); | |
693 | if (cwdfd == -1) | |
28f540f4 | 694 | { |
b576fca1 UD |
695 | /* Try getting the directory name. This can be needed if |
696 | the current directory is executable but not readable. */ | |
697 | if (errno == EACCES) | |
698 | /* GNU extension ahead. */ | |
699 | cwd = __getcwd (NULL, 0); | |
700 | ||
701 | if (cwd == NULL) | |
702 | goto out_fail; | |
703 | } | |
704 | else if (data.maxdir > 1) | |
705 | /* Account for the file descriptor we use here. */ | |
706 | --data.maxdir; | |
becac6c5 | 707 | |
b576fca1 UD |
708 | if (data.ftw.base > 0) |
709 | { | |
710 | /* Change to the directory the file is in. In data.dirbuf | |
711 | we have a writable copy of the file name. Just NUL | |
712 | terminate it for now and change the directory. */ | |
713 | if (data.ftw.base == 1) | |
714 | /* I.e., the file is in the root directory. */ | |
715 | result = __chdir ("/"); | |
716 | else | |
76b87c03 | 717 | { |
b576fca1 UD |
718 | char ch = data.dirbuf[data.ftw.base - 1]; |
719 | data.dirbuf[data.ftw.base - 1] = '\0'; | |
720 | result = __chdir (data.dirbuf); | |
721 | data.dirbuf[data.ftw.base - 1] = ch; | |
76b87c03 | 722 | } |
28f540f4 RM |
723 | } |
724 | } | |
725 | ||
76b87c03 UD |
726 | /* Get stat info for start directory. */ |
727 | if (result == 0) | |
6e4c40ba | 728 | { |
63a2f305 UD |
729 | const char *name; |
730 | ||
731 | if (data.flags & FTW_CHDIR) | |
732 | { | |
733 | name = data.dirbuf + data.ftw.base; | |
734 | if (name[0] == '\0') | |
735 | name = "."; | |
736 | } | |
737 | else | |
738 | name = data.dirbuf; | |
b2608c22 | 739 | |
6e4c40ba | 740 | if (((flags & FTW_PHYS) |
b2608c22 UD |
741 | ? LXSTAT (_STAT_VER, name, &st) |
742 | : XSTAT (_STAT_VER, name, &st)) < 0) | |
6e4c40ba | 743 | { |
0e24e73d UD |
744 | if (!(flags & FTW_PHYS) |
745 | && errno == ENOENT | |
40212ce0 | 746 | && LXSTAT (_STAT_VER, name, &st) == 0 |
0e24e73d | 747 | && S_ISLNK (st.st_mode)) |
6e4c40ba | 748 | result = (*data.func) (data.dirbuf, &st, data.cvt_arr[FTW_SLN], |
76b87c03 | 749 | &data.ftw); |
6e4c40ba UD |
750 | else |
751 | /* No need to call the callback since we cannot say anything | |
752 | about the object. */ | |
753 | result = -1; | |
754 | } | |
755 | else | |
756 | { | |
757 | if (S_ISDIR (st.st_mode)) | |
758 | { | |
759 | /* Remember the device of the initial directory in case | |
760 | FTW_MOUNT is given. */ | |
761 | data.dev = st.st_dev; | |
762 | ||
763 | /* We know this directory now. */ | |
764 | if (!(flags & FTW_PHYS)) | |
765 | result = add_object (&data, &st); | |
766 | ||
767 | if (result == 0) | |
ca10f338 | 768 | result = ftw_dir (&data, &st, NULL); |
6e4c40ba UD |
769 | } |
770 | else | |
771 | { | |
772 | int flag = S_ISLNK (st.st_mode) ? FTW_SL : FTW_F; | |
773 | ||
774 | result = (*data.func) (data.dirbuf, &st, data.cvt_arr[flag], | |
775 | &data.ftw); | |
776 | } | |
777 | } | |
ca10f338 UD |
778 | |
779 | if ((flags & FTW_ACTIONRETVAL) | |
780 | && (result == FTW_SKIP_SUBTREE || result == FTW_SKIP_SIBLINGS)) | |
781 | result = 0; | |
6e4c40ba | 782 | } |
76b87c03 UD |
783 | |
784 | /* Return to the start directory (if necessary). */ | |
becac6c5 | 785 | if (cwdfd != -1) |
76b87c03 UD |
786 | { |
787 | int save_err = errno; | |
becac6c5 | 788 | __fchdir (cwdfd); |
c181840c | 789 | __close_nocancel_nostatus (cwdfd); |
76b87c03 UD |
790 | __set_errno (save_err); |
791 | } | |
b576fca1 UD |
792 | else if (cwd != NULL) |
793 | { | |
794 | int save_err = errno; | |
795 | __chdir (cwd); | |
796 | free (cwd); | |
797 | __set_errno (save_err); | |
798 | } | |
76b87c03 UD |
799 | |
800 | /* Free all memory. */ | |
b576fca1 | 801 | out_fail: |
76b87c03 | 802 | save_err = errno; |
d951286f | 803 | __tdestroy (data.known_objects, free); |
76b87c03 UD |
804 | free (data.dirbuf); |
805 | __set_errno (save_err); | |
806 | ||
807 | return result; | |
808 | } | |
809 | ||
810 | ||
811 | ||
812 | /* Entry points. */ | |
813 | ||
814 | int | |
9dd346ff | 815 | FTW_NAME (const char *path, FTW_FUNC_T func, int descriptors) |
76b87c03 UD |
816 | { |
817 | return ftw_startup (path, 0, func, descriptors, 0); | |
818 | } | |
819 | ||
ca10f338 | 820 | #ifndef _LIBC |
76b87c03 | 821 | int |
9dd346ff | 822 | NFTW_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags) |
76b87c03 UD |
823 | { |
824 | return ftw_startup (path, 1, func, descriptors, flags); | |
28f540f4 | 825 | } |
ca10f338 UD |
826 | #else |
827 | ||
b576fca1 | 828 | # include <shlib-compat.h> |
ca10f338 | 829 | |
3c0fb574 UD |
830 | int NFTW_NEW_NAME (const char *, NFTW_FUNC_T, int, int); |
831 | ||
ca10f338 | 832 | int |
9dd346ff | 833 | NFTW_NEW_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags) |
ca10f338 UD |
834 | { |
835 | if (flags | |
836 | & ~(FTW_PHYS | FTW_MOUNT | FTW_CHDIR | FTW_DEPTH | FTW_ACTIONRETVAL)) | |
837 | { | |
838 | __set_errno (EINVAL); | |
839 | return -1; | |
840 | } | |
841 | return ftw_startup (path, 1, func, descriptors, flags); | |
842 | } | |
843 | ||
844 | versioned_symbol (libc, NFTW_NEW_NAME, NFTW_NAME, GLIBC_2_3_3); | |
845 | ||
b576fca1 | 846 | # if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_3_3) |
ca10f338 UD |
847 | |
848 | /* Older nftw* version just ignored all unknown flags. */ | |
849 | ||
3c0fb574 UD |
850 | int NFTW_OLD_NAME (const char *, NFTW_FUNC_T, int, int); |
851 | ||
ca10f338 | 852 | int |
4a381a81 | 853 | attribute_compat_text_section |
9dd346ff | 854 | NFTW_OLD_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags) |
ca10f338 UD |
855 | { |
856 | flags &= (FTW_PHYS | FTW_MOUNT | FTW_CHDIR | FTW_DEPTH); | |
857 | return ftw_startup (path, 1, func, descriptors, flags); | |
858 | } | |
859 | ||
860 | compat_symbol (libc, NFTW_OLD_NAME, NFTW_NAME, GLIBC_2_1); | |
b576fca1 | 861 | # endif |
ca10f338 | 862 | #endif |