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