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