1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #if defined(LIBC_SCCS) && !defined(lint) 9 static char sccsid[] = "@(#)fts.c 5.11 (Berkeley) 11/14/90"; 10 #endif /* LIBC_SCCS and not lint */ 11 12 #include <sys/param.h> 13 #include <sys/stat.h> 14 #include <fcntl.h> 15 #include <dirent.h> 16 #include <errno.h> 17 #include <fts.h> 18 #include <string.h> 19 #include <stdlib.h> 20 21 FTSENT *fts_alloc(), *fts_build(), *fts_cycle(), *fts_root(), *fts_sort(); 22 void fts_lfree(), fts_load(); 23 u_short fts_stat(); 24 25 /* 26 * Special case a root of "/" so that slashes aren't appended causing 27 * paths to be written as "//foo". 28 */ 29 #define NAPPEND(p) \ 30 (p->fts_level == ROOTLEVEL && p->fts_pathlen == 1 && \ 31 p->fts_path[0] == '/' ? 0 : p->fts_pathlen) 32 33 #define ISSET(opt) (sp->fts_options & opt) 34 #define SET(opt) (sp->fts_options |= opt) 35 36 #define CHDIR(sp, path) (!ISSET(FTS_NOCHDIR) && chdir(path)) 37 #define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd)) 38 39 /* fts_build flags */ 40 #define BCHILD 1 /* from fts_children */ 41 #define BREAD 2 /* from fts_read */ 42 43 /* fts_level values */ 44 #define ROOTLEVEL 0 45 #define ROOTPARENTLEVEL -1 46 47 FTS * 48 fts_open(argv, options, compar) 49 char *argv[]; 50 register int options; 51 int (*compar)(); 52 { 53 register FTS *sp; 54 register FTSENT *p, *root; 55 register int nitems, maxlen; 56 FTSENT *parent, *tmp; 57 char *fts_path(); 58 59 /* Allocate/initialize the stream */ 60 if (!(sp = (FTS *)malloc((u_int)sizeof(FTS)))) 61 return(NULL); 62 bzero(sp, sizeof(FTS)); 63 sp->fts_compar = compar; 64 sp->fts_options = options; 65 66 /* Logical walks turn on NOCHDIR; symbolic links are too hard. */ 67 if (ISSET(FTS_LOGICAL)) 68 SET(FTS_NOCHDIR); 69 70 /* Allocate/initialize root's parent. */ 71 if (!(parent = fts_alloc(sp, "", 0))) 72 goto mem1; 73 parent->fts_level = ROOTPARENTLEVEL; 74 75 /* Allocate/initialize root(s). */ 76 maxlen = -1; 77 for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) { 78 if (!(p = fts_root(sp, *argv))) 79 goto mem2; 80 if (maxlen < p->fts_namelen) 81 maxlen = p->fts_namelen; 82 /* 83 * If comparison routine supplied, traverse in sorted 84 * order; otherwise traverse in the order specified. 85 */ 86 if (compar) { 87 p->fts_link = root; 88 root = p; 89 p->fts_accpath = p->fts_name; 90 if (!(options & FTS_NOSTAT)) 91 p->fts_info = fts_stat(sp, p, 0); 92 } else { 93 p->fts_link = NULL; 94 if (!root) 95 tmp = root = p; 96 else { 97 tmp->fts_link = p; 98 tmp = p; 99 } 100 } 101 p->fts_level = ROOTLEVEL; 102 p->fts_parent = parent; 103 } 104 if (compar && nitems > 1) 105 root = fts_sort(sp, root, nitems); 106 107 /* 108 * Allocate a dummy pointer and make fts_read think that we've just 109 * finished the node before the root(s); set p->fts_info to FTS_NS 110 * so that everything about the "current" node is ignored. 111 */ 112 if (!(sp->fts_cur = fts_alloc(sp, "", 0))) 113 goto mem2; 114 sp->fts_cur->fts_link = root; 115 sp->fts_cur->fts_info = FTS_NS; 116 117 /* Start out with at least 1K+ of path space. */ 118 if (!fts_path(sp, MAX(maxlen, MAXPATHLEN))) 119 goto mem3; 120 121 /* 122 * If using chdir(2), grab a file descriptor pointing to dot to insure 123 * that we can get back here; this could be avoided for some paths, 124 * but almost certainly not worth the effort. Slashes, symbolic links, 125 * and ".." are all fairly nasty problems. Note, if we can't get the 126 * descriptor we run anyway, just more slowly. 127 */ 128 if (!ISSET(FTS_NOCHDIR) && (sp->fts_sd = open(".", O_RDONLY, 0)) < 0) 129 SET(FTS_NOCHDIR); 130 131 return(sp); 132 133 mem3: free((void *)sp->fts_cur); 134 mem2: fts_lfree(root); 135 free((void *)parent); 136 mem1: free((void *)sp); 137 return(NULL); 138 } 139 140 static void 141 fts_load(sp, p) 142 FTS *sp; 143 register FTSENT *p; 144 { 145 register int len; 146 register char *cp; 147 148 /* 149 * Load the stream structure for the next traversal; set the 150 * fts_accpath field specially so the chdir gets done to the 151 * right place and the user can access the first node. 152 */ 153 len = p->fts_pathlen = p->fts_namelen; 154 bcopy(p->fts_name, sp->fts_path, len + 1); 155 if ((cp = rindex(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) { 156 len = strlen(++cp); 157 bcopy(cp, p->fts_name, len + 1); 158 p->fts_namelen = len; 159 } 160 p->fts_accpath = p->fts_path = sp->fts_path; 161 } 162 163 fts_close(sp) 164 FTS *sp; 165 { 166 register FTSENT *freep, *p; 167 int saved_errno; 168 169 if (sp->fts_cur) { 170 /* 171 * This still works if we haven't read anything -- the dummy 172 * structure points to the root list, so we step through to 173 * the end of the root list which has a valid parent pointer. 174 */ 175 for (p = sp->fts_cur; p->fts_level > ROOTPARENTLEVEL;) { 176 freep = p; 177 p = p->fts_link ? p->fts_link : p->fts_parent; 178 free((void *)freep); 179 } 180 free((void *)p); 181 } 182 183 /* Free up child linked list, sort array, path buffer. */ 184 if (sp->fts_child) 185 fts_lfree(sp->fts_child); 186 if (sp->fts_array) 187 free((void *)sp->fts_array); 188 free((char *)sp->fts_path); 189 190 /* 191 * Return to original directory, save errno if necessary; free up 192 * the directory buffer. 193 */ 194 if (!ISSET(FTS_NOCHDIR)) { 195 saved_errno = fchdir(sp->fts_sd) ? errno : 0; 196 (void)close(sp->fts_sd); 197 } 198 199 /* Free up the stream pointer. */ 200 free((void *)sp); 201 202 /* Set errno and return. */ 203 if (!ISSET(FTS_NOCHDIR) && saved_errno) { 204 errno = saved_errno; 205 return(-1); 206 } 207 return(0); 208 } 209 210 FTSENT * 211 fts_read(sp) 212 register FTS *sp; 213 { 214 register FTSENT *p, *tmp; 215 register int instr; 216 register char *cp; 217 static int cd; 218 219 /* If finished or unrecoverable error, return NULL. */ 220 if (!sp->fts_cur || ISSET(FTS__STOP)) 221 return(NULL); 222 223 /* Set current node pointer. */ 224 p = sp->fts_cur; 225 226 /* Save and zero out user instructions. */ 227 instr = p->fts_instr; 228 p->fts_instr = FTS__NOINSTR; 229 230 /* If used fts_link pointer for cycle detection, restore it. */ 231 if (sp->fts_savelink) { 232 p->fts_link = sp->fts_savelink; 233 sp->fts_savelink = NULL; 234 } 235 236 /* Any type of file may be re-visited; re-stat and return. */ 237 if (instr == FTS_AGAIN) { 238 p->fts_info = fts_stat(sp, p, 0); 239 return(p); 240 } 241 242 /* Following a symbolic link. */ 243 if (p->fts_info == FTS_SL && instr == FTS_FOLLOW) { 244 p->fts_info = fts_stat(sp, p, 1); 245 return(p); 246 } 247 248 /* Directory in pre-order. */ 249 if (p->fts_info == FTS_D) { 250 /* If skipped or crossed mount point, do post-order visit. */ 251 if (instr == FTS_SKIP || ISSET(FTS_XDEV) && 252 p->fts_statb.st_dev != sp->sdev) { 253 if (sp->fts_child) { 254 fts_lfree(sp->fts_child); 255 sp->fts_child = NULL; 256 } 257 p->fts_info = FTS_DP; 258 return(p); 259 } 260 261 /* Read the directory if necessary, and return first entry. */ 262 if (sp->fts_child) 263 if (CHDIR(sp, p->fts_accpath)) { 264 fts_lfree(sp->fts_child); 265 p->fts_info = FTS_DNX; 266 } else { 267 p = sp->fts_child; 268 cp = sp->fts_path + NAPPEND(p->fts_parent); 269 *cp++ = '/'; 270 bcopy(p->fts_name, cp, p->fts_namelen + 1); 271 } 272 else { 273 if (!(sp->fts_child = fts_build(sp, BREAD))) 274 return(p); 275 p = sp->fts_child; 276 } 277 sp->fts_child = NULL; 278 return(sp->fts_cur = p); 279 } 280 281 /* Move to next node on this level. */ 282 next: tmp = p; 283 if (p = p->fts_link) { 284 /* 285 * If root level node, set up paths and return. If not the 286 * first time, and it's not an absolute pathname, get back 287 * to starting directory. If that fails, we're dead. 288 */ 289 if (p->fts_level == ROOTLEVEL) { 290 fts_load(sp, p); 291 free((void *)tmp); 292 if (cd && 293 p->fts_path[0] != '/' && FCHDIR(sp, sp->fts_sd)) { 294 /* Can't get back to start; we're dead. */ 295 p->fts_path = "starting directory"; 296 p->fts_info = FTS_ERR; 297 SET(FTS__STOP); 298 return(sp->fts_cur = p); 299 } 300 cd = 1; 301 p->fts_info = fts_stat(sp, p, 0); 302 sp->sdev = p->fts_statb.st_dev; 303 } else { 304 free((void *)tmp); 305 306 /* User may have called fts_set on node. */ 307 if (p->fts_instr == FTS_SKIP) 308 goto next; 309 if (p->fts_instr == FTS_FOLLOW) { 310 p->fts_info = fts_stat(sp, p, 1); 311 p->fts_instr = FTS__NOINSTR; 312 } 313 314 /* Fill in the paths. */ 315 cp = sp->fts_path + NAPPEND(p->fts_parent); 316 *cp++ = '/'; 317 bcopy(p->fts_name, cp, p->fts_namelen + 1); 318 319 /* Check for directory cycles. */ 320 if (p->fts_info == FTS_D && (tmp = fts_cycle(p))) { 321 sp->fts_savelink = p->fts_link; 322 p->fts_link = tmp; 323 p->fts_info = FTS_DC; 324 } 325 } 326 return(sp->fts_cur = p); 327 } 328 329 /* Move to parent. */ 330 p = tmp->fts_parent; 331 free((void *)tmp); 332 333 if (p->fts_level == ROOTPARENTLEVEL) { 334 /* 335 * Done; free everything up and set errno to 0 so the user 336 * can distinguish between error and EOF. 337 */ 338 free((void *)p); 339 errno = 0; 340 return(sp->fts_cur = NULL); 341 } 342 343 sp->fts_path[p->fts_pathlen] = '\0'; 344 if (CHDIR(sp, "..")) { 345 SET(FTS__STOP); 346 p->fts_info = FTS_ERR; 347 } else 348 p->fts_info = FTS_DP; 349 return(sp->fts_cur = p); 350 } 351 352 /* 353 * fts_set takes the stream as an argument although it's not used in this 354 * implementation; it would be necessary if anyone wanted to add global 355 * semantics to fts using fts_set. An error return is allowed for similar 356 * reasons. 357 */ 358 /* ARGSUSED */ 359 fts_set(sp, p, instr) 360 FTS *sp; 361 FTSENT *p; 362 int instr; 363 { 364 p->fts_instr = instr; 365 return(0); 366 } 367 368 FTSENT * 369 fts_children(sp) 370 register FTS *sp; 371 { 372 register FTSENT *p; 373 int fd; 374 375 /* Set current node pointer. */ 376 p = sp->fts_cur; 377 378 /* 379 * Set errno to 0 so that user can tell the difference between an 380 * error and a directory without entries. 381 */ 382 errno = 0; 383 if (ISSET(FTS__STOP) || 384 p->fts_info != FTS_D && p->fts_info != FTS_DNX && 385 p->fts_info != FTS_DNR) 386 return(NULL); 387 388 /* Free up any previous child list. */ 389 if (sp->fts_child) 390 fts_lfree(sp->fts_child); 391 392 /* 393 * If using chdir on a relative path and called BEFORE fts_read does 394 * its chdir to the root of a traversal, we can lose because we need 395 * to chdir into the subdirectory, and we don't know where the current 396 * directory is to get back so that the upcoming chdir by fts_read 397 * will work. 398 */ 399 if (p->fts_level != ROOTLEVEL || p->fts_accpath[0] == '/' || 400 ISSET(FTS_NOCHDIR)) 401 return(sp->fts_child = fts_build(sp, BCHILD)); 402 403 if ((fd = open(".", O_RDONLY, 0)) < 0) 404 return(NULL); 405 sp->fts_child = fts_build(sp, BCHILD); 406 if (fchdir(fd)) 407 return(NULL); 408 (void)close(fd); 409 return(sp->fts_child); 410 } 411 412 #define ISDOT(a) (a[0] == '.' && (!a[1] || a[1] == '.' && !a[2])) 413 414 FTSENT * 415 fts_build(sp, type) 416 register FTS *sp; 417 int type; 418 { 419 register struct dirent *dp; 420 register FTSENT *p, *head; 421 register int nitems; 422 DIR *dirp; 423 int descend, len, level, maxlen, nlinks, saved_errno; 424 char *cp; 425 426 /* Set current node pointer. */ 427 p = sp->fts_cur; 428 429 if (!(dirp = opendir(p->fts_accpath))) { 430 if (type == BREAD) { 431 p->fts_info = FTS_DNR; 432 errno = 0; 433 } 434 return(NULL); 435 } 436 437 /* 438 * The real slowdown in walking the tree is the stat calls. If 439 * FTS_NOSTAT is set and it's a physical walk (so that symbolic 440 * links can't be directories), fts assumes that the number of 441 * subdirectories in a node is equal to the number of links to 442 * the parent. This allows stat calls to be skipped in any leaf 443 * directories and for any nodes after the directories in the 444 * parent node have been found. This empirically cuts the stat 445 * calls by about 2/3. 446 */ 447 nlinks = 448 ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL) ? 449 p->fts_statb.st_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2) : -1; 450 451 /* If told to descend or found links and not told not to descend. */ 452 descend = 0; 453 if (nlinks || type == BREAD) 454 if (!FCHDIR(sp, dirfd(dirp))) 455 descend = 1; 456 /* 457 * Return all the information possible; fts_read doing a 458 * relative walk of the tree will have to descend, so it 459 * can't succeed. Fts_children or absolute walks of the 460 * tree can succeed, but no stat information will be available. 461 */ 462 else { 463 if (type == BREAD) { 464 (void)closedir(dirp); 465 p->fts_info = FTS_DNX; 466 errno = 0; 467 return(NULL); 468 } 469 nlinks = 0; 470 } 471 472 /* Get max file name length that can be stored in current path. */ 473 maxlen = sp->fts_pathlen - p->fts_pathlen - 1; 474 475 cp = sp->fts_path + (len = NAPPEND(p)); 476 *cp++ = '/'; 477 478 level = p->fts_level + 1; 479 480 /* Read the directory, attching each new entry to the `link' pointer. */ 481 for (head = NULL, nitems = 0; dp = readdir(dirp);) { 482 if (ISDOT(dp->d_name) && !ISSET(FTS_SEEDOT)) 483 continue; 484 485 if (!(p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen))) { 486 saved_errno = errno; 487 goto mem1; 488 } 489 if (dp->d_namlen > maxlen) { 490 if (!fts_path(sp, (int)dp->d_namlen)) { 491 /* Quit: this stream no longer has a path. */ 492 SET(FTS__STOP); 493 saved_errno = errno; 494 free((void *)p); 495 mem1: fts_lfree(head); 496 if (type == BREAD) 497 p->fts_info = FTS_ERR; 498 if (descend && CHDIR(sp, "..")) { 499 /* 500 * chdir error is more interesting 501 * than memory error, since it stops 502 * everything. 503 */ 504 saved_errno = errno; 505 SET(FTS__STOP); 506 } 507 errno = saved_errno; 508 (void)closedir(dirp); 509 return(NULL); 510 } 511 maxlen = sp->fts_pathlen - sp->fts_cur->fts_pathlen - 1; 512 } 513 514 p->fts_pathlen = len + dp->d_namlen + 1; 515 p->fts_accpath = ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name; 516 517 p->fts_parent = sp->fts_cur; 518 p->fts_level = level; 519 520 if (nlinks) { 521 /* Make sure fts_stat has a filename to stat. */ 522 if (ISSET(FTS_NOCHDIR)) 523 bcopy(p->fts_name, cp, p->fts_namelen + 1); 524 p->fts_info = fts_stat(sp, p, 0); 525 if (nlinks > 0 && (p->fts_info == FTS_D || 526 p->fts_info == FTS_DNR || p->fts_info == FTS_DNX)) 527 --nlinks; 528 } else 529 p->fts_info = FTS_NS; 530 531 p->fts_link = head; 532 head = p; 533 ++nitems; 534 } 535 (void)closedir(dirp); 536 537 /* Reset the path. */ 538 if (cp - 1 > sp->fts_path) 539 --cp; 540 *cp = '\0'; 541 542 /* 543 * If descended: if were called from fts_read and didn't find anything, 544 * or were called from fts_children, get back. 545 */ 546 if (descend && (!nitems || type == BCHILD) && CHDIR(sp, "..")) { 547 SET(FTS__STOP); 548 p->fts_info = FTS_ERR; 549 return(NULL); 550 } 551 552 if (!nitems) { 553 if (type == BREAD) 554 p->fts_info = FTS_DP; 555 return(NULL); 556 } 557 558 if (sp->fts_compar && nitems > 1) 559 head = fts_sort(sp, head, nitems); 560 561 if (type == BREAD) { 562 *cp = '/'; 563 bcopy(head->fts_name, cp + 1, head->fts_namelen + 1); 564 } 565 return(head); 566 } 567 568 static u_short 569 fts_stat(sp, p, follow) 570 FTS *sp; 571 register FTSENT *p; 572 int follow; 573 { 574 /* 575 * If doing a logical walk, or application requested FTS_FOLLOW, do 576 * a stat(2). If that fails, either fail or do an lstat(2) for a 577 * non-existent symlink. (The check has to be done, or we wouldn't 578 * detect a symlink being deleted.) 579 * 580 * Don't leave errno set for FTS_NS cases. XXX 581 */ 582 if (ISSET(FTS_LOGICAL) || follow) { 583 if (stat(p->fts_accpath, &p->fts_statb)) { 584 errno = 0; 585 if (follow && !lstat(p->fts_accpath, &p->fts_statb)) 586 return(FTS_SLNONE); 587 else { 588 errno = 0; 589 return(FTS_NS); 590 } 591 } 592 } else if (lstat(p->fts_accpath, &p->fts_statb)) { 593 errno = 0; 594 return(FTS_NS); 595 } 596 597 if (S_ISDIR(p->fts_statb.st_mode)) 598 return(FTS_D); 599 if (S_ISLNK(p->fts_statb.st_mode)) 600 return(FTS_SL); 601 if (S_ISREG(p->fts_statb.st_mode)) 602 return(FTS_F); 603 return(FTS_DEFAULT); 604 } 605 606 static FTSENT * 607 fts_cycle(p) 608 register FTSENT *p; 609 { 610 register dev_t dev; 611 register ino_t ino; 612 613 /* 614 * Cycle detection is brute force; if the tree gets deep enough or 615 * the number of symbolic links to directories is really high 616 * something faster might be worthwhile. 617 */ 618 dev = p->fts_statb.st_dev; 619 ino = p->fts_statb.st_ino; 620 for (p = p->fts_parent; p->fts_level > ROOTLEVEL; p = p->fts_parent) 621 if (ino == p->fts_statb.st_ino && dev == p->fts_statb.st_dev) 622 return(p); 623 return(NULL); 624 } 625 626 #define R(type, nelem, ptr) \ 627 (type *)realloc((void *)ptr, (u_int)((nelem) * sizeof(type))) 628 629 static FTSENT * 630 fts_sort(sp, head, nitems) 631 FTS *sp; 632 FTSENT *head; 633 register int nitems; 634 { 635 register FTSENT **ap, *p; 636 637 /* 638 * Construct an array of pointers to the structures and call qsort(3). 639 * Reassemble the array in the order returned by qsort. If unable to 640 * sort for memory reasons, return the directory entries in their 641 * current order. Allocate enough space for the current needs plus 642 * 40 so we don't realloc one entry at a time. 643 */ 644 if (nitems > sp->fts_nitems) { 645 sp->fts_nitems = nitems + 40; 646 if (!(sp->fts_array = 647 R(FTSENT *, sp->fts_nitems, sp->fts_array))) { 648 sp->fts_nitems = 0; 649 return(head); 650 } 651 } 652 for (ap = sp->fts_array, p = head; p; p = p->fts_link) 653 *ap++ = p; 654 qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar); 655 for (head = *(ap = sp->fts_array); --nitems; ++ap) 656 ap[0]->fts_link = ap[1]; 657 ap[0]->fts_link = NULL; 658 return(head); 659 } 660 661 static FTSENT * 662 fts_alloc(sp, name, len) 663 FTS *sp; 664 char *name; 665 register int len; 666 { 667 register FTSENT *p; 668 669 /* 670 * Variable sized structures; the name is the last element so 671 * allocate enough extra space after the structure to hold it. 672 */ 673 if (!(p = (FTSENT *)malloc((size_t)(sizeof(FTSENT) + len)))) 674 return(NULL); 675 bcopy(name, p->fts_name, len + 1); 676 p->fts_namelen = len; 677 p->fts_path = sp->fts_path; 678 p->fts_instr = FTS__NOINSTR; 679 p->fts_number = 0; 680 p->fts_pointer = NULL; 681 return(p); 682 } 683 684 static void 685 fts_lfree(head) 686 register FTSENT *head; 687 { 688 register FTSENT *p; 689 690 /* Free a linked list of structures. */ 691 while (p = head) { 692 head = head->fts_link; 693 free((void *)p); 694 } 695 } 696 697 /* 698 * Allow essentially unlimited paths; certain programs (find, rm, ls) need to 699 * work on any tree. Most systems will allow creation of paths much longer 700 * than MAXPATHLEN, even though the kernel won't resolve them. Add an extra 701 * 128 bytes to the requested size so that we don't realloc the path 2 bytes 702 * at a time. 703 */ 704 static char * 705 fts_path(sp, size) 706 FTS *sp; 707 int size; 708 { 709 sp->fts_pathlen += size + 128; 710 return(sp->fts_path = R(char, sp->fts_pathlen, sp->fts_path)); } 711 712 static FTSENT * 713 fts_root(sp, name) 714 FTS *sp; 715 register char *name; 716 { 717 register char *cp; 718 719 /* 720 * Rip trailing slashes; it's somewhat unclear in POSIX 1003.1 what 721 * /a/b/ really is, they don't talk about what a null path component 722 * resolves to. This hopefully does what the user intended. Don't 723 * allow null pathnames. 724 */ 725 for (cp = name; *cp; ++cp); 726 if (cp == name) { 727 errno = ENOENT; 728 return(NULL); 729 } 730 while (--cp > name && *cp == '/'); 731 *++cp = '\0'; 732 return(fts_alloc(sp, name, cp - name)); 733 } 734