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