1 /* $NetBSD: fts.c,v 1.40 2009/11/02 17:17:34 stacktic Exp $ */ 2 3 /*- 4 * Copyright (c) 1990, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #if HAVE_NBTOOL_CONFIG_H 33 #include "nbtool_config.h" 34 #endif 35 36 #include <sys/cdefs.h> 37 #if defined(LIBC_SCCS) && !defined(lint) 38 #if 0 39 static char sccsid[] = "@(#)fts.c 8.6 (Berkeley) 8/14/94"; 40 #else 41 __RCSID("$NetBSD: fts.c,v 1.40 2009/11/02 17:17:34 stacktic Exp $"); 42 #endif 43 #endif /* LIBC_SCCS and not lint */ 44 45 #include "namespace.h" 46 #include <sys/param.h> 47 #include <sys/stat.h> 48 49 #include <assert.h> 50 #include <dirent.h> 51 #include <errno.h> 52 #include <fcntl.h> 53 #include <fts.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <unistd.h> 57 58 #if ! HAVE_NBTOOL_CONFIG_H 59 #ifndef __minix 60 #define HAVE_STRUCT_DIRENT_D_NAMLEN 61 #endif 62 #endif 63 64 static FTSENT *fts_alloc(FTS *, const char *, size_t); 65 static FTSENT *fts_build(FTS *, int); 66 static void fts_free(FTSENT *); 67 static void fts_lfree(FTSENT *); 68 static void fts_load(FTS *, FTSENT *); 69 static size_t fts_maxarglen(char * const *); 70 static size_t fts_pow2(size_t); 71 static int fts_palloc(FTS *, size_t); 72 static void fts_padjust(FTS *, FTSENT *); 73 static FTSENT *fts_sort(FTS *, FTSENT *, size_t); 74 static unsigned short fts_stat(FTS *, FTSENT *, int); 75 static int fts_safe_changedir(const FTS *, const FTSENT *, int, 76 const char *); 77 78 #if defined(ALIGNBYTES) && defined(ALIGN) 79 #define FTS_ALLOC_ALIGNED 1 80 #else 81 #undef FTS_ALLOC_ALIGNED 82 #endif 83 84 #define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2]))) 85 86 #define CLR(opt) (sp->fts_options &= ~(opt)) 87 #define ISSET(opt) (sp->fts_options & (opt)) 88 #define SET(opt) (sp->fts_options |= (opt)) 89 90 #define CHDIR(sp, path) (!ISSET(FTS_NOCHDIR) && chdir(path)) 91 #define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd)) 92 93 /* fts_build flags */ 94 #define BCHILD 1 /* fts_children */ 95 #define BNAMES 2 /* fts_children, names only */ 96 #define BREAD 3 /* fts_read */ 97 98 #ifndef DTF_HIDEW 99 #undef FTS_WHITEOUT 100 #endif 101 102 FTS * 103 fts_open(char * const *argv, int options, 104 int (*compar)(const FTSENT **, const FTSENT **)) 105 { 106 FTS *sp; 107 FTSENT *p, *root; 108 size_t nitems; 109 FTSENT *parent, *tmp = NULL; /* pacify gcc */ 110 size_t len; 111 112 _DIAGASSERT(argv != NULL); 113 114 /* Options check. */ 115 if (options & ~FTS_OPTIONMASK) { 116 errno = EINVAL; 117 return (NULL); 118 } 119 120 /* Allocate/initialize the stream */ 121 if ((sp = malloc((unsigned int)sizeof(FTS))) == NULL) 122 return (NULL); 123 memset(sp, 0, sizeof(FTS)); 124 sp->fts_compar = compar; 125 sp->fts_options = options; 126 127 /* Logical walks turn on NOCHDIR; symbolic links are too hard. */ 128 if (ISSET(FTS_LOGICAL)) 129 SET(FTS_NOCHDIR); 130 131 /* 132 * Start out with 1K of path space, and enough, in any case, 133 * to hold the user's paths. 134 */ 135 if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN))) 136 goto mem1; 137 138 /* Allocate/initialize root's parent. */ 139 if ((parent = fts_alloc(sp, "", 0)) == NULL) 140 goto mem2; 141 parent->fts_level = FTS_ROOTPARENTLEVEL; 142 143 /* Allocate/initialize root(s). */ 144 for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) { 145 /* Don't allow zero-length paths. */ 146 if ((len = strlen(*argv)) == 0) { 147 errno = ENOENT; 148 goto mem3; 149 } 150 151 if ((p = fts_alloc(sp, *argv, len)) == NULL) 152 goto mem3; 153 p->fts_level = FTS_ROOTLEVEL; 154 p->fts_parent = parent; 155 p->fts_accpath = p->fts_name; 156 p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW)); 157 158 /* Command-line "." and ".." are real directories. */ 159 if (p->fts_info == FTS_DOT) 160 p->fts_info = FTS_D; 161 162 /* 163 * If comparison routine supplied, traverse in sorted 164 * order; otherwise traverse in the order specified. 165 */ 166 if (compar) { 167 p->fts_link = root; 168 root = p; 169 } else { 170 p->fts_link = NULL; 171 if (root == NULL) 172 tmp = root = p; 173 else { 174 tmp->fts_link = p; 175 tmp = p; 176 } 177 } 178 } 179 if (compar && nitems > 1) 180 root = fts_sort(sp, root, nitems); 181 182 /* 183 * Allocate a dummy pointer and make fts_read think that we've just 184 * finished the node before the root(s); set p->fts_info to FTS_INIT 185 * so that everything about the "current" node is ignored. 186 */ 187 if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL) 188 goto mem3; 189 sp->fts_cur->fts_link = root; 190 sp->fts_cur->fts_info = FTS_INIT; 191 192 /* 193 * If using chdir(2), grab a file descriptor pointing to dot to insure 194 * that we can get back here; this could be avoided for some paths, 195 * but almost certainly not worth the effort. Slashes, symbolic links, 196 * and ".." are all fairly nasty problems. Note, if we can't get the 197 * descriptor we run anyway, just more slowly. 198 */ 199 if (!ISSET(FTS_NOCHDIR)) { 200 if ((sp->fts_rfd = open(".", O_RDONLY, 0)) == -1) 201 SET(FTS_NOCHDIR); 202 else if (fcntl(sp->fts_rfd, F_SETFD, FD_CLOEXEC) == -1) { 203 close(sp->fts_rfd); 204 SET(FTS_NOCHDIR); 205 } 206 } 207 208 if (nitems == 0) 209 fts_free(parent); 210 211 return (sp); 212 213 mem3: fts_lfree(root); 214 fts_free(parent); 215 mem2: free(sp->fts_path); 216 mem1: free(sp); 217 return (NULL); 218 } 219 220 static void 221 fts_load(FTS *sp, FTSENT *p) 222 { 223 size_t len; 224 char *cp; 225 226 _DIAGASSERT(sp != NULL); 227 _DIAGASSERT(p != NULL); 228 229 /* 230 * Load the stream structure for the next traversal. Since we don't 231 * actually enter the directory until after the preorder visit, set 232 * the fts_accpath field specially so the chdir gets done to the right 233 * place and the user can access the first node. From fts_open it's 234 * known that the path will fit. 235 */ 236 len = p->fts_pathlen = p->fts_namelen; 237 memmove(sp->fts_path, p->fts_name, len + 1); 238 if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) { 239 len = strlen(++cp); 240 memmove(p->fts_name, cp, len + 1); 241 p->fts_namelen = len; 242 } 243 p->fts_accpath = p->fts_path = sp->fts_path; 244 sp->fts_dev = p->fts_dev; 245 } 246 247 int 248 fts_close(FTS *sp) 249 { 250 FTSENT *freep, *p; 251 int saved_errno = 0; 252 253 _DIAGASSERT(sp != NULL); 254 255 /* 256 * This still works if we haven't read anything -- the dummy structure 257 * points to the root list, so we step through to the end of the root 258 * list which has a valid parent pointer. 259 */ 260 if (sp->fts_cur) { 261 if (sp->fts_cur->fts_flags & FTS_SYMFOLLOW) 262 (void)close(sp->fts_cur->fts_symfd); 263 for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { 264 freep = p; 265 p = p->fts_link ? p->fts_link : p->fts_parent; 266 fts_free(freep); 267 } 268 fts_free(p); 269 } 270 271 /* Free up child linked list, sort array, path buffer. */ 272 if (sp->fts_child) 273 fts_lfree(sp->fts_child); 274 if (sp->fts_array) 275 free(sp->fts_array); 276 free(sp->fts_path); 277 278 /* Return to original directory, save errno if necessary. */ 279 if (!ISSET(FTS_NOCHDIR)) { 280 if (fchdir(sp->fts_rfd) == -1) 281 saved_errno = errno; 282 (void)close(sp->fts_rfd); 283 } 284 285 /* Free up the stream pointer. */ 286 free(sp); 287 if (saved_errno) { 288 errno = saved_errno; 289 return -1; 290 } 291 292 return 0; 293 } 294 295 #if !defined(__FTS_COMPAT_TAILINGSLASH) 296 297 /* 298 * Special case of "/" at the end of the path so that slashes aren't 299 * appended which would cause paths to be written as "....//foo". 300 */ 301 #define NAPPEND(p) \ 302 (p->fts_path[p->fts_pathlen - 1] == '/' \ 303 ? p->fts_pathlen - 1 : p->fts_pathlen) 304 305 #else /* !defined(__FTS_COMPAT_TAILINGSLASH) */ 306 307 /* 308 * compatibility with the old behaviour. 309 * 310 * Special case a root of "/" so that slashes aren't appended which would 311 * cause paths to be written as "//foo". 312 */ 313 314 #define NAPPEND(p) \ 315 (p->fts_level == FTS_ROOTLEVEL && p->fts_pathlen == 1 && \ 316 p->fts_path[0] == '/' ? 0 : p->fts_pathlen) 317 318 #endif /* !defined(__FTS_COMPAT_TAILINGSLASH) */ 319 320 FTSENT * 321 fts_read(FTS *sp) 322 { 323 FTSENT *p, *tmp; 324 int instr; 325 char *t; 326 int saved_errno; 327 328 _DIAGASSERT(sp != NULL); 329 330 /* If finished or unrecoverable error, return NULL. */ 331 if (sp->fts_cur == NULL || ISSET(FTS_STOP)) 332 return (NULL); 333 334 /* Set current node pointer. */ 335 p = sp->fts_cur; 336 337 /* Save and zero out user instructions. */ 338 instr = p->fts_instr; 339 p->fts_instr = FTS_NOINSTR; 340 341 /* Any type of file may be re-visited; re-stat and re-turn. */ 342 if (instr == FTS_AGAIN) { 343 p->fts_info = fts_stat(sp, p, 0); 344 return (p); 345 } 346 347 /* 348 * Following a symlink -- SLNONE test allows application to see 349 * SLNONE and recover. If indirecting through a symlink, have 350 * keep a pointer to current location. If unable to get that 351 * pointer, follow fails. 352 */ 353 if (instr == FTS_FOLLOW && 354 (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) { 355 p->fts_info = fts_stat(sp, p, 1); 356 if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { 357 if ((p->fts_symfd = open(".", O_RDONLY, 0)) == -1) { 358 p->fts_errno = errno; 359 p->fts_info = FTS_ERR; 360 } else if (fcntl(p->fts_symfd, F_SETFD, FD_CLOEXEC) == -1) { 361 p->fts_errno = errno; 362 p->fts_info = FTS_ERR; 363 close(p->fts_symfd); 364 } else 365 p->fts_flags |= FTS_SYMFOLLOW; 366 } 367 return (p); 368 } 369 370 /* Directory in pre-order. */ 371 if (p->fts_info == FTS_D) { 372 /* If skipped or crossed mount point, do post-order visit. */ 373 if (instr == FTS_SKIP || 374 (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) { 375 if (p->fts_flags & FTS_SYMFOLLOW) 376 (void)close(p->fts_symfd); 377 if (sp->fts_child) { 378 fts_lfree(sp->fts_child); 379 sp->fts_child = NULL; 380 } 381 p->fts_info = FTS_DP; 382 return (p); 383 } 384 385 /* Rebuild if only read the names and now traversing. */ 386 if (sp->fts_child && ISSET(FTS_NAMEONLY)) { 387 CLR(FTS_NAMEONLY); 388 fts_lfree(sp->fts_child); 389 sp->fts_child = NULL; 390 } 391 392 /* 393 * Cd to the subdirectory. 394 * 395 * If have already read and now fail to chdir, whack the list 396 * to make the names come out right, and set the parent errno 397 * so the application will eventually get an error condition. 398 * Set the FTS_DONTCHDIR flag so that when we logically change 399 * directories back to the parent we don't do a chdir. 400 * 401 * If haven't read do so. If the read fails, fts_build sets 402 * FTS_STOP or the fts_info field of the node. 403 */ 404 if (sp->fts_child) { 405 if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) { 406 p->fts_errno = errno; 407 p->fts_flags |= FTS_DONTCHDIR; 408 for (p = sp->fts_child; p; p = p->fts_link) 409 p->fts_accpath = 410 p->fts_parent->fts_accpath; 411 } 412 } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) { 413 if (ISSET(FTS_STOP)) 414 return (NULL); 415 return (p); 416 } 417 p = sp->fts_child; 418 sp->fts_child = NULL; 419 goto name; 420 } 421 422 /* Move to the next node on this level. */ 423 next: tmp = p; 424 if ((p = p->fts_link) != NULL) { 425 fts_free(tmp); 426 427 /* 428 * If reached the top, return to the original directory, and 429 * load the paths for the next root. 430 */ 431 if (p->fts_level == FTS_ROOTLEVEL) { 432 if (FCHDIR(sp, sp->fts_rfd)) { 433 SET(FTS_STOP); 434 return (NULL); 435 } 436 fts_load(sp, p); 437 return (sp->fts_cur = p); 438 } 439 440 /* 441 * User may have called fts_set on the node. If skipped, 442 * ignore. If followed, get a file descriptor so we can 443 * get back if necessary. 444 */ 445 if (p->fts_instr == FTS_SKIP) 446 goto next; 447 if (p->fts_instr == FTS_FOLLOW) { 448 p->fts_info = fts_stat(sp, p, 1); 449 if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { 450 if ((p->fts_symfd = 451 open(".", O_RDONLY, 0)) == -1) { 452 p->fts_errno = errno; 453 p->fts_info = FTS_ERR; 454 } else if (fcntl(p->fts_symfd, F_SETFD, FD_CLOEXEC) == -1) { 455 p->fts_errno = errno; 456 p->fts_info = FTS_ERR; 457 close(p->fts_symfd); 458 } else 459 p->fts_flags |= FTS_SYMFOLLOW; 460 } 461 p->fts_instr = FTS_NOINSTR; 462 } 463 464 name: t = sp->fts_path + NAPPEND(p->fts_parent); 465 *t++ = '/'; 466 memmove(t, p->fts_name, (size_t)(p->fts_namelen + 1)); 467 return (sp->fts_cur = p); 468 } 469 470 /* Move up to the parent node. */ 471 p = tmp->fts_parent; 472 fts_free(tmp); 473 474 if (p->fts_level == FTS_ROOTPARENTLEVEL) { 475 /* 476 * Done; free everything up and set errno to 0 so the user 477 * can distinguish between error and EOF. 478 */ 479 fts_free(p); 480 errno = 0; 481 return (sp->fts_cur = NULL); 482 } 483 484 /* Nul terminate the pathname. */ 485 sp->fts_path[p->fts_pathlen] = '\0'; 486 487 /* 488 * Return to the parent directory. If at a root node or came through 489 * a symlink, go back through the file descriptor. Otherwise, cd up 490 * one directory. 491 */ 492 if (p->fts_level == FTS_ROOTLEVEL) { 493 if (FCHDIR(sp, sp->fts_rfd)) { 494 SET(FTS_STOP); 495 return (NULL); 496 } 497 } else if (p->fts_flags & FTS_SYMFOLLOW) { 498 if (FCHDIR(sp, p->fts_symfd)) { 499 saved_errno = errno; 500 (void)close(p->fts_symfd); 501 errno = saved_errno; 502 SET(FTS_STOP); 503 return (NULL); 504 } 505 (void)close(p->fts_symfd); 506 } else if (!(p->fts_flags & FTS_DONTCHDIR) && 507 fts_safe_changedir(sp, p->fts_parent, -1, "..")) { 508 SET(FTS_STOP); 509 return (NULL); 510 } 511 p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP; 512 return (sp->fts_cur = p); 513 } 514 515 /* 516 * Fts_set takes the stream as an argument although it's not used in this 517 * implementation; it would be necessary if anyone wanted to add global 518 * semantics to fts using fts_set. An error return is allowed for similar 519 * reasons. 520 */ 521 /* ARGSUSED */ 522 int 523 fts_set(FTS *sp, FTSENT *p, int instr) 524 { 525 526 _DIAGASSERT(sp != NULL); 527 _DIAGASSERT(p != NULL); 528 529 if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW && 530 instr != FTS_NOINSTR && instr != FTS_SKIP) { 531 errno = EINVAL; 532 return (1); 533 } 534 p->fts_instr = instr; 535 return (0); 536 } 537 538 FTSENT * 539 fts_children(FTS *sp, int instr) 540 { 541 FTSENT *p; 542 int fd; 543 544 _DIAGASSERT(sp != NULL); 545 546 if (instr && instr != FTS_NAMEONLY) { 547 errno = EINVAL; 548 return (NULL); 549 } 550 551 /* Set current node pointer. */ 552 p = sp->fts_cur; 553 554 /* 555 * Errno set to 0 so user can distinguish empty directory from 556 * an error. 557 */ 558 errno = 0; 559 560 /* Fatal errors stop here. */ 561 if (ISSET(FTS_STOP)) 562 return (NULL); 563 564 /* Return logical hierarchy of user's arguments. */ 565 if (p->fts_info == FTS_INIT) 566 return (p->fts_link); 567 568 /* 569 * If not a directory being visited in pre-order, stop here. Could 570 * allow FTS_DNR, assuming the user has fixed the problem, but the 571 * same effect is available with FTS_AGAIN. 572 */ 573 if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */) 574 return (NULL); 575 576 /* Free up any previous child list. */ 577 if (sp->fts_child) 578 fts_lfree(sp->fts_child); 579 580 if (instr == FTS_NAMEONLY) { 581 SET(FTS_NAMEONLY); 582 instr = BNAMES; 583 } else 584 instr = BCHILD; 585 586 /* 587 * If using chdir on a relative path and called BEFORE fts_read does 588 * its chdir to the root of a traversal, we can lose -- we need to 589 * chdir into the subdirectory, and we don't know where the current 590 * directory is, so we can't get back so that the upcoming chdir by 591 * fts_read will work. 592 */ 593 if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' || 594 ISSET(FTS_NOCHDIR)) 595 return (sp->fts_child = fts_build(sp, instr)); 596 597 if ((fd = open(".", O_RDONLY, 0)) == -1) 598 return (sp->fts_child = NULL); 599 sp->fts_child = fts_build(sp, instr); 600 if (fchdir(fd)) { 601 (void)close(fd); 602 return (NULL); 603 } 604 (void)close(fd); 605 return (sp->fts_child); 606 } 607 608 /* 609 * This is the tricky part -- do not casually change *anything* in here. The 610 * idea is to build the linked list of entries that are used by fts_children 611 * and fts_read. There are lots of special cases. 612 * 613 * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is 614 * set and it's a physical walk (so that symbolic links can't be directories), 615 * we can do things quickly. First, if it's a 4.4BSD file system, the type 616 * of the file is in the directory entry. Otherwise, we assume that the number 617 * of subdirectories in a node is equal to the number of links to the parent. 618 * The former skips all stat calls. The latter skips stat calls in any leaf 619 * directories and for any files after the subdirectories in the directory have 620 * been found, cutting the stat calls by about 2/3. 621 */ 622 static FTSENT * 623 fts_build(FTS *sp, int type) 624 { 625 struct dirent *dp; 626 FTSENT *p, *head; 627 size_t nitems; 628 FTSENT *cur, *tail; 629 DIR *dirp; 630 void *oldaddr; 631 size_t dnamlen; 632 int cderrno, descend, level, nlinks, saved_errno, nostat, doadjust; 633 size_t len, maxlen; 634 #ifdef FTS_WHITEOUT 635 int oflag; 636 #endif 637 char *cp = NULL; /* pacify gcc */ 638 639 _DIAGASSERT(sp != NULL); 640 641 /* Set current node pointer. */ 642 cur = sp->fts_cur; 643 644 /* 645 * Open the directory for reading. If this fails, we're done. 646 * If being called from fts_read, set the fts_info field. 647 */ 648 #ifdef FTS_WHITEOUT 649 if (ISSET(FTS_WHITEOUT)) 650 oflag = DTF_NODUP|DTF_REWIND; 651 else 652 oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND; 653 #else 654 #define __opendir2(path, flag) opendir(path) 655 #endif 656 if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) { 657 if (type == BREAD) { 658 cur->fts_info = FTS_DNR; 659 cur->fts_errno = errno; 660 } 661 return (NULL); 662 } 663 664 /* 665 * Nlinks is the number of possible entries of type directory in the 666 * directory if we're cheating on stat calls, 0 if we're not doing 667 * any stat calls at all, -1 if we're doing stats on everything. 668 */ 669 if (type == BNAMES) { 670 nlinks = 0; 671 nostat = 1; 672 } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) { 673 nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2); 674 nostat = 1; 675 } else { 676 nlinks = -1; 677 nostat = 0; 678 } 679 680 #ifdef notdef 681 (void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink); 682 (void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n", 683 ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT)); 684 #endif 685 /* 686 * If we're going to need to stat anything or we want to descend 687 * and stay in the directory, chdir. If this fails we keep going, 688 * but set a flag so we don't chdir after the post-order visit. 689 * We won't be able to stat anything, but we can still return the 690 * names themselves. Note, that since fts_read won't be able to 691 * chdir into the directory, it will have to return different path 692 * names than before, i.e. "a/b" instead of "b". Since the node 693 * has already been visited in pre-order, have to wait until the 694 * post-order visit to return the error. There is a special case 695 * here, if there was nothing to stat then it's not an error to 696 * not be able to stat. This is all fairly nasty. If a program 697 * needed sorted entries or stat information, they had better be 698 * checking FTS_NS on the returned nodes. 699 */ 700 cderrno = 0; 701 if (nlinks || type == BREAD) { 702 if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) { 703 if (nlinks && type == BREAD) 704 cur->fts_errno = errno; 705 cur->fts_flags |= FTS_DONTCHDIR; 706 descend = 0; 707 cderrno = errno; 708 } else 709 descend = 1; 710 } else 711 descend = 0; 712 713 /* 714 * Figure out the max file name length that can be stored in the 715 * current path -- the inner loop allocates more path as necessary. 716 * We really wouldn't have to do the maxlen calculations here, we 717 * could do them in fts_read before returning the path, but it's a 718 * lot easier here since the length is part of the dirent structure. 719 * 720 * If not changing directories set a pointer so that can just append 721 * each new name into the path. 722 */ 723 len = NAPPEND(cur); 724 if (ISSET(FTS_NOCHDIR)) { 725 cp = sp->fts_path + len; 726 *cp++ = '/'; 727 } 728 len++; 729 maxlen = sp->fts_pathlen - len; 730 731 #if defined(__FTS_COMPAT_LEVEL) 732 if (cur->fts_level == SHRT_MAX) { 733 (void)closedir(dirp); 734 cur->fts_info = FTS_ERR; 735 SET(FTS_STOP); 736 errno = ENAMETOOLONG; 737 return (NULL); 738 } 739 #endif 740 741 level = cur->fts_level + 1; 742 743 /* Read the directory, attaching each entry to the `link' pointer. */ 744 doadjust = 0; 745 for (head = tail = NULL, nitems = 0; (dp = readdir(dirp)) != NULL;) { 746 747 if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) 748 continue; 749 750 #if defined(HAVE_STRUCT_DIRENT_D_NAMLEN) 751 dnamlen = dp->d_namlen; 752 #else 753 dnamlen = strlen(dp->d_name); 754 #endif 755 if ((p = fts_alloc(sp, dp->d_name, dnamlen)) == NULL) 756 goto mem1; 757 if (dnamlen >= maxlen) { /* include space for NUL */ 758 oldaddr = sp->fts_path; 759 if (fts_palloc(sp, dnamlen + len + 1)) { 760 /* 761 * No more memory for path or structures. Save 762 * errno, free up the current structure and the 763 * structures already allocated. 764 */ 765 mem1: saved_errno = errno; 766 if (p) 767 fts_free(p); 768 fts_lfree(head); 769 (void)closedir(dirp); 770 errno = saved_errno; 771 cur->fts_info = FTS_ERR; 772 SET(FTS_STOP); 773 return (NULL); 774 } 775 /* Did realloc() change the pointer? */ 776 if (oldaddr != sp->fts_path) { 777 doadjust = 1; 778 if (ISSET(FTS_NOCHDIR)) 779 cp = sp->fts_path + len; 780 } 781 maxlen = sp->fts_pathlen - len; 782 } 783 784 #if defined(__FTS_COMPAT_LENGTH) 785 if (len + dnamlen >= USHRT_MAX) { 786 /* 787 * In an FTSENT, fts_pathlen is an unsigned short 788 * so it is possible to wraparound here. 789 * If we do, free up the current structure and the 790 * structures already allocated, then error out 791 * with ENAMETOOLONG. 792 */ 793 fts_free(p); 794 fts_lfree(head); 795 (void)closedir(dirp); 796 cur->fts_info = FTS_ERR; 797 SET(FTS_STOP); 798 errno = ENAMETOOLONG; 799 return (NULL); 800 } 801 #endif 802 p->fts_level = level; 803 p->fts_pathlen = len + dnamlen; 804 p->fts_parent = sp->fts_cur; 805 806 #ifdef FTS_WHITEOUT 807 if (dp->d_type == DT_WHT) 808 p->fts_flags |= FTS_ISW; 809 #endif 810 811 if (cderrno) { 812 if (nlinks) { 813 p->fts_info = FTS_NS; 814 p->fts_errno = cderrno; 815 } else 816 p->fts_info = FTS_NSOK; 817 p->fts_accpath = cur->fts_accpath; 818 } else if (nlinks == 0 819 #ifdef DT_DIR 820 || (nostat && 821 dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN) 822 #endif 823 ) { 824 p->fts_accpath = 825 ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name; 826 p->fts_info = FTS_NSOK; 827 } else { 828 /* Build a file name for fts_stat to stat. */ 829 if (ISSET(FTS_NOCHDIR)) { 830 p->fts_accpath = p->fts_path; 831 memmove(cp, p->fts_name, 832 (size_t)(p->fts_namelen + 1)); 833 } else 834 p->fts_accpath = p->fts_name; 835 /* Stat it. */ 836 p->fts_info = fts_stat(sp, p, 0); 837 838 /* Decrement link count if applicable. */ 839 if (nlinks > 0 && (p->fts_info == FTS_D || 840 p->fts_info == FTS_DC || p->fts_info == FTS_DOT)) 841 --nlinks; 842 } 843 844 /* We walk in directory order so "ls -f" doesn't get upset. */ 845 p->fts_link = NULL; 846 if (head == NULL) 847 head = tail = p; 848 else { 849 tail->fts_link = p; 850 tail = p; 851 } 852 ++nitems; 853 } 854 (void)closedir(dirp); 855 856 /* 857 * If had to realloc the path, adjust the addresses for the rest 858 * of the tree. 859 */ 860 if (doadjust) 861 fts_padjust(sp, head); 862 863 /* 864 * If not changing directories, reset the path back to original 865 * state. 866 */ 867 if (ISSET(FTS_NOCHDIR)) { 868 if (len == sp->fts_pathlen || nitems == 0) 869 --cp; 870 *cp = '\0'; 871 } 872 873 /* 874 * If descended after called from fts_children or after called from 875 * fts_read and nothing found, get back. At the root level we use 876 * the saved fd; if one of fts_open()'s arguments is a relative path 877 * to an empty directory, we wind up here with no other way back. If 878 * can't get back, we're done. 879 */ 880 if (descend && (type == BCHILD || !nitems) && 881 (cur->fts_level == FTS_ROOTLEVEL ? 882 FCHDIR(sp, sp->fts_rfd) : 883 fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) { 884 cur->fts_info = FTS_ERR; 885 SET(FTS_STOP); 886 return (NULL); 887 } 888 889 /* If didn't find anything, return NULL. */ 890 if (!nitems) { 891 if (type == BREAD) 892 cur->fts_info = FTS_DP; 893 return (NULL); 894 } 895 896 /* Sort the entries. */ 897 if (sp->fts_compar && nitems > 1) 898 head = fts_sort(sp, head, nitems); 899 return (head); 900 } 901 902 static unsigned short 903 fts_stat(FTS *sp, FTSENT *p, int follow) 904 { 905 FTSENT *t; 906 dev_t dev; 907 __fts_ino_t ino; 908 __fts_stat_t *sbp, sb; 909 int saved_errno; 910 911 _DIAGASSERT(sp != NULL); 912 _DIAGASSERT(p != NULL); 913 914 /* If user needs stat info, stat buffer already allocated. */ 915 sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp; 916 917 #ifdef FTS_WHITEOUT 918 /* check for whiteout */ 919 if (p->fts_flags & FTS_ISW) { 920 if (sbp != &sb) { 921 memset(sbp, '\0', sizeof (*sbp)); 922 sbp->st_mode = S_IFWHT; 923 } 924 return (FTS_W); 925 } 926 #endif 927 928 /* 929 * If doing a logical walk, or application requested FTS_FOLLOW, do 930 * a stat(2). If that fails, check for a non-existent symlink. If 931 * fail, set the errno from the stat call. 932 */ 933 if (ISSET(FTS_LOGICAL) || follow) { 934 if (stat(p->fts_accpath, sbp)) { 935 saved_errno = errno; 936 if (!lstat(p->fts_accpath, sbp)) { 937 errno = 0; 938 return (FTS_SLNONE); 939 } 940 p->fts_errno = saved_errno; 941 goto err; 942 } 943 } else if (lstat(p->fts_accpath, sbp)) { 944 p->fts_errno = errno; 945 err: memset(sbp, 0, sizeof(*sbp)); 946 return (FTS_NS); 947 } 948 949 if (S_ISDIR(sbp->st_mode)) { 950 /* 951 * Set the device/inode. Used to find cycles and check for 952 * crossing mount points. Also remember the link count, used 953 * in fts_build to limit the number of stat calls. It is 954 * understood that these fields are only referenced if fts_info 955 * is set to FTS_D. 956 */ 957 dev = p->fts_dev = sbp->st_dev; 958 ino = p->fts_ino = sbp->st_ino; 959 p->fts_nlink = sbp->st_nlink; 960 961 if (ISDOT(p->fts_name)) 962 return (FTS_DOT); 963 964 /* 965 * Cycle detection is done by brute force when the directory 966 * is first encountered. If the tree gets deep enough or the 967 * number of symbolic links to directories is high enough, 968 * something faster might be worthwhile. 969 */ 970 for (t = p->fts_parent; 971 t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent) 972 if (ino == t->fts_ino && dev == t->fts_dev) { 973 p->fts_cycle = t; 974 return (FTS_DC); 975 } 976 return (FTS_D); 977 } 978 if (S_ISLNK(sbp->st_mode)) 979 return (FTS_SL); 980 if (S_ISREG(sbp->st_mode)) 981 return (FTS_F); 982 return (FTS_DEFAULT); 983 } 984 985 static FTSENT * 986 fts_sort(FTS *sp, FTSENT *head, size_t nitems) 987 { 988 FTSENT **ap, *p; 989 990 _DIAGASSERT(sp != NULL); 991 _DIAGASSERT(head != NULL); 992 993 /* 994 * Construct an array of pointers to the structures and call qsort(3). 995 * Reassemble the array in the order returned by qsort. If unable to 996 * sort for memory reasons, return the directory entries in their 997 * current order. Allocate enough space for the current needs plus 998 * 40 so don't realloc one entry at a time. 999 */ 1000 if (nitems > sp->fts_nitems) { 1001 FTSENT **new; 1002 1003 new = realloc(sp->fts_array, sizeof(FTSENT *) * (nitems + 40)); 1004 if (new == 0) 1005 return (head); 1006 sp->fts_array = new; 1007 sp->fts_nitems = nitems + 40; 1008 } 1009 for (ap = sp->fts_array, p = head; p; p = p->fts_link) 1010 *ap++ = p; 1011 qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), 1012 (int (*)(const void *, const void *))sp->fts_compar); 1013 for (head = *(ap = sp->fts_array); --nitems; ++ap) 1014 ap[0]->fts_link = ap[1]; 1015 ap[0]->fts_link = NULL; 1016 return (head); 1017 } 1018 1019 static FTSENT * 1020 fts_alloc(FTS *sp, const char *name, size_t namelen) 1021 { 1022 FTSENT *p; 1023 #if defined(FTS_ALLOC_ALIGNED) 1024 size_t len; 1025 #endif 1026 1027 _DIAGASSERT(sp != NULL); 1028 _DIAGASSERT(name != NULL); 1029 1030 #if defined(FTS_ALLOC_ALIGNED) 1031 /* 1032 * The file name is a variable length array and no stat structure is 1033 * necessary if the user has set the nostat bit. Allocate the FTSENT 1034 * structure, the file name and the stat structure in one chunk, but 1035 * be careful that the stat structure is reasonably aligned. Since the 1036 * fts_name field is declared to be of size 1, the fts_name pointer is 1037 * namelen + 2 before the first possible address of the stat structure. 1038 */ 1039 len = sizeof(FTSENT) + namelen; 1040 if (!ISSET(FTS_NOSTAT)) 1041 len += sizeof(*(p->fts_statp)) + ALIGNBYTES; 1042 if ((p = malloc(len)) == NULL) 1043 return (NULL); 1044 1045 if (!ISSET(FTS_NOSTAT)) 1046 p->fts_statp = (__fts_stat_t *)ALIGN( 1047 (unsigned long)(p->fts_name + namelen + 2)); 1048 #else 1049 if ((p = malloc(sizeof(FTSENT) + namelen)) == NULL) 1050 return (NULL); 1051 1052 if (!ISSET(FTS_NOSTAT)) 1053 if ((p->fts_statp = malloc(sizeof(*(p->fts_statp)))) == NULL) { 1054 free(p); 1055 return (NULL); 1056 } 1057 #endif 1058 1059 if (ISSET(FTS_NOSTAT)) 1060 p->fts_statp = NULL; 1061 1062 /* Copy the name plus the trailing NULL. */ 1063 memmove(p->fts_name, name, namelen + 1); 1064 1065 p->fts_namelen = namelen; 1066 p->fts_path = sp->fts_path; 1067 p->fts_errno = 0; 1068 p->fts_flags = 0; 1069 p->fts_instr = FTS_NOINSTR; 1070 p->fts_number = 0; 1071 p->fts_pointer = NULL; 1072 return (p); 1073 } 1074 1075 static void 1076 fts_free(FTSENT *p) 1077 { 1078 #if !defined(FTS_ALLOC_ALIGNED) 1079 if (p->fts_statp) 1080 free(p->fts_statp); 1081 #endif 1082 free(p); 1083 } 1084 1085 static void 1086 fts_lfree(FTSENT *head) 1087 { 1088 FTSENT *p; 1089 1090 /* XXX: head may be NULL ? */ 1091 1092 /* Free a linked list of structures. */ 1093 while ((p = head) != NULL) { 1094 head = head->fts_link; 1095 fts_free(p); 1096 } 1097 } 1098 1099 static size_t 1100 fts_pow2(size_t x) 1101 { 1102 1103 x--; 1104 x |= x>>1; 1105 x |= x>>2; 1106 x |= x>>4; 1107 x |= x>>8; 1108 x |= x>>16; 1109 #if LONG_BIT > 32 1110 x |= x>>32; 1111 #endif 1112 #if LONG_BIT > 64 1113 x |= x>>64; 1114 #endif 1115 x++; 1116 return (x); 1117 } 1118 1119 /* 1120 * Allow essentially unlimited paths; find, rm, ls should all work on any tree. 1121 * Most systems will allow creation of paths much longer than MAXPATHLEN, even 1122 * though the kernel won't resolve them. Round up the new size to a power of 2, 1123 * so we don't realloc the path 2 bytes at a time. 1124 */ 1125 static int 1126 fts_palloc(FTS *sp, size_t size) 1127 { 1128 char *new; 1129 1130 _DIAGASSERT(sp != NULL); 1131 1132 #ifdef __FTS_COMPAT_LENGTH 1133 /* Protect against fts_pathlen overflow. */ 1134 if (size > USHRT_MAX + 1) { 1135 errno = ENAMETOOLONG; 1136 return (1); 1137 } 1138 #endif 1139 size = fts_pow2(size); 1140 new = realloc(sp->fts_path, size); 1141 if (new == 0) 1142 return (1); 1143 sp->fts_path = new; 1144 sp->fts_pathlen = size; 1145 return (0); 1146 } 1147 1148 /* 1149 * When the path is realloc'd, have to fix all of the pointers in structures 1150 * already returned. 1151 */ 1152 static void 1153 fts_padjust(FTS *sp, FTSENT *head) 1154 { 1155 FTSENT *p; 1156 char *addr; 1157 1158 _DIAGASSERT(sp != NULL); 1159 1160 #define ADJUST(p) do { \ 1161 if ((p)->fts_accpath != (p)->fts_name) \ 1162 (p)->fts_accpath = \ 1163 addr + ((p)->fts_accpath - (p)->fts_path); \ 1164 (p)->fts_path = addr; \ 1165 } while (/*CONSTCOND*/0) 1166 1167 addr = sp->fts_path; 1168 1169 /* Adjust the current set of children. */ 1170 for (p = sp->fts_child; p; p = p->fts_link) 1171 ADJUST(p); 1172 1173 /* Adjust the rest of the tree, including the current level. */ 1174 for (p = head; p->fts_level >= FTS_ROOTLEVEL;) { 1175 ADJUST(p); 1176 p = p->fts_link ? p->fts_link : p->fts_parent; 1177 } 1178 } 1179 1180 static size_t 1181 fts_maxarglen(char * const *argv) 1182 { 1183 size_t len, max; 1184 1185 _DIAGASSERT(argv != NULL); 1186 1187 for (max = 0; *argv; ++argv) 1188 if ((len = strlen(*argv)) > max) 1189 max = len; 1190 return (max + 1); 1191 } 1192 1193 /* 1194 * Change to dir specified by fd or p->fts_accpath without getting 1195 * tricked by someone changing the world out from underneath us. 1196 * Assumes p->fts_dev and p->fts_ino are filled in. 1197 */ 1198 static int 1199 fts_safe_changedir(const FTS *sp, const FTSENT *p, int fd, const char *path) 1200 { 1201 int oldfd = fd, ret = -1; 1202 __fts_stat_t sb; 1203 1204 if (ISSET(FTS_NOCHDIR)) 1205 return 0; 1206 1207 if (oldfd < 0 && (fd = open(path, O_RDONLY)) == -1) 1208 return -1; 1209 1210 if (fstat(fd, &sb) == -1) 1211 goto bail; 1212 1213 if (sb.st_ino != p->fts_ino || sb.st_dev != p->fts_dev) { 1214 errno = ENOENT; 1215 goto bail; 1216 } 1217 1218 ret = fchdir(fd); 1219 1220 bail: 1221 if (oldfd < 0) { 1222 int save_errno = errno; 1223 (void)close(fd); 1224 errno = save_errno; 1225 } 1226 return ret; 1227 } 1228