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