1/* find -- search for files in a directory hierarchy 2 Copyright (C) 1990, 91, 92, 93, 94, 2000, 3 2003, 2004, 2005, 2007 Free Software Foundation, Inc. 4 5 This program is free software: you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation, either version 3 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17*/ 18/* GNU find was written by Eric Decker <cire@cisco.com>, 19 with enhancements by David MacKenzie <djm@gnu.org>, 20 Jay Plett <jay@silence.princeton.nj.us>, 21 and Tim Wood <axolotl!tim@toad.com>. 22 The idea for -print0 and xargs -0 came from 23 Dan Bernstein <brnstnd@kramden.acf.nyu.edu>. 24 Improvements have been made by James Youngman <jay@gnu.org>. 25*/ 26 27 28#include "defs.h" 29 30#define USE_SAFE_CHDIR 1 31#undef STAT_MOUNTPOINTS 32 33 34#include <errno.h> 35#include <assert.h> 36 37 38#ifdef HAVE_FCNTL_H 39#include <fcntl.h> 40#else 41#include <sys/file.h> 42#endif 43 44#ifdef HAVE_SYS_UTSNAME_H 45#include <sys/utsname.h> 46#endif 47 48#include "../gnulib/lib/xalloc.h" 49#include "../gnulib/lib/human.h" 50#include "../gnulib/lib/canonicalize.h" 51#include "closein.h" 52#include <modetype.h> 53#include "savedirinfo.h" 54#include "buildcmd.h" 55#include "dirname.h" 56#include "quote.h" 57#include "quotearg.h" 58 59#ifdef HAVE_LOCALE_H 60#include <locale.h> 61#endif 62 63#if ENABLE_NLS 64# include <libintl.h> 65# define _(Text) gettext (Text) 66#else 67# define _(Text) Text 68#define textdomain(Domain) 69#define bindtextdomain(Package, Directory) 70#endif 71#ifdef gettext_noop 72# define N_(String) gettext_noop (String) 73#else 74/* See locate.c for explanation as to why not use (String) */ 75# define N_(String) String 76#endif 77 78#define apply_predicate(pathname, stat_buf_ptr, node) \ 79 (*(node)->pred_func)((pathname), (stat_buf_ptr), (node)) 80 81#ifdef STAT_MOUNTPOINTS 82static void init_mounted_dev_list(int mandatory); 83#endif 84 85static void process_top_path PARAMS((char *pathname, mode_t mode)); 86static int process_path PARAMS((char *pathname, char *name, boolean leaf, char *parent, mode_t type)); 87static void process_dir PARAMS((char *pathname, char *name, int pathlen, struct stat *statp, char *parent)); 88 89static void complete_pending_execdirs(struct predicate *p); 90static void complete_pending_execs (struct predicate *p); 91 92 93 94static boolean default_prints PARAMS((struct predicate *pred)); 95 96/* Name this program was run with. */ 97char *program_name; 98 99/* All predicates for each path to process. */ 100struct predicate *predicates; 101 102/* The last predicate allocated. */ 103struct predicate *last_pred; 104 105/* The root of the evaluation tree. */ 106static struct predicate *eval_tree = NULL; 107 108 109struct options options; 110struct state state; 111 112/* The full path of the initial working directory, or "." if 113 STARTING_DESC is nonnegative. */ 114char const *starting_dir = "."; 115 116/* A file descriptor open to the initial working directory. 117 Doing it this way allows us to work when the i.w.d. has 118 unreadable parents. */ 119int starting_desc; 120 121/* The stat buffer of the initial working directory. */ 122struct stat starting_stat_buf; 123 124enum ChdirSymlinkHandling 125 { 126 SymlinkHandleDefault, /* Normally the right choice */ 127 SymlinkFollowOk /* see comment in process_top_path() */ 128 }; 129 130 131enum TraversalDirection 132 { 133 TraversingUp, 134 TraversingDown 135 }; 136 137enum WdSanityCheckFatality 138 { 139 FATAL_IF_SANITY_CHECK_FAILS, 140 RETRY_IF_SANITY_CHECK_FAILS, 141 NON_FATAL_IF_SANITY_CHECK_FAILS 142 }; 143 144 145int 146following_links(void) 147{ 148 switch (options.symlink_handling) 149 { 150 case SYMLINK_ALWAYS_DEREF: 151 return 1; 152 case SYMLINK_DEREF_ARGSONLY: 153 return (state.curdepth == 0); 154 case SYMLINK_NEVER_DEREF: 155 default: 156 return 0; 157 } 158} 159 160 161static int 162fallback_stat(const char *name, struct stat *p, int prev_rv) 163{ 164 /* Our original stat() call failed. Perhaps we can't follow a 165 * symbolic link. If that might be the problem, lstat() the link. 166 * Otherwise, admit defeat. 167 */ 168 switch (errno) 169 { 170 case ENOENT: 171 case ENOTDIR: 172#ifdef DEBUG_STAT 173 fprintf(stderr, "fallback_stat(): stat(%s) failed; falling back on lstat()\n", name); 174#endif 175 return lstat(name, p); 176 177 case EACCES: 178 case EIO: 179 case ELOOP: 180 case ENAMETOOLONG: 181#ifdef EOVERFLOW 182 case EOVERFLOW: /* EOVERFLOW is not #defined on UNICOS. */ 183#endif 184 default: 185 return prev_rv; 186 } 187} 188 189 190/* optionh_stat() implements the stat operation when the -H option is 191 * in effect. 192 * 193 * If the item to be examined is a command-line argument, we follow 194 * symbolic links. If the stat() call fails on the command-line item, 195 * we fall back on the properties of the symbolic link. 196 * 197 * If the item to be examined is not a command-line argument, we 198 * examine the link itself. 199 */ 200int 201optionh_stat(const char *name, struct stat *p) 202{ 203 if (0 == state.curdepth) 204 { 205 /* This file is from the command line; deference the link (if it 206 * is a link). 207 */ 208 int rv = stat(name, p); 209 if (0 == rv) 210 return 0; /* success */ 211 else 212 return fallback_stat(name, p, rv); 213 } 214 else 215 { 216 /* Not a file on the command line; do not dereference the link. 217 */ 218 return lstat(name, p); 219 } 220} 221 222/* optionl_stat() implements the stat operation when the -L option is 223 * in effect. That option makes us examine the thing the symbolic 224 * link points to, not the symbolic link itself. 225 */ 226int 227optionl_stat(const char *name, struct stat *p) 228{ 229 int rv = stat(name, p); 230 if (0 == rv) 231 return 0; /* normal case. */ 232 else 233 return fallback_stat(name, p, rv); 234} 235 236/* optionp_stat() implements the stat operation when the -P option is 237 * in effect (this is also the default). That option makes us examine 238 * the symbolic link itself, not the thing it points to. 239 */ 240int 241optionp_stat(const char *name, struct stat *p) 242{ 243 return lstat(name, p); 244} 245 246#ifdef DEBUG_STAT 247static uintmax_t stat_count = 0u; 248 249static int 250debug_stat (const char *file, struct stat *bufp) 251{ 252 ++stat_count; 253 fprintf (stderr, "debug_stat (%s)\n", file); 254 switch (options.symlink_handling) 255 { 256 case SYMLINK_ALWAYS_DEREF: 257 return optionl_stat(file, bufp); 258 case SYMLINK_DEREF_ARGSONLY: 259 return optionh_stat(file, bufp); 260 case SYMLINK_NEVER_DEREF: 261 return optionp_stat(file, bufp); 262 } 263} 264#endif /* DEBUG_STAT */ 265 266void 267set_follow_state(enum SymlinkOption opt) 268{ 269 switch (opt) 270 { 271 case SYMLINK_ALWAYS_DEREF: /* -L */ 272 options.xstat = optionl_stat; 273 options.no_leaf_check = true; 274 break; 275 276 case SYMLINK_NEVER_DEREF: /* -P (default) */ 277 options.xstat = optionp_stat; 278 /* Can't turn no_leaf_check off because the user might have specified 279 * -noleaf anyway 280 */ 281 break; 282 283 case SYMLINK_DEREF_ARGSONLY: /* -H */ 284 options.xstat = optionh_stat; 285 options.no_leaf_check = true; 286 } 287 288 options.symlink_handling = opt; 289 290 /* For DEBUG_STAT, the choice is made at runtime within debug_stat() 291 * by checking the contents of the symlink_handling variable. 292 */ 293#if defined(DEBUG_STAT) 294 options.xstat = debug_stat; 295#endif /* !DEBUG_STAT */ 296} 297 298 299/* Complete any outstanding commands. 300 */ 301void 302cleanup(void) 303{ 304 if (eval_tree) 305 { 306 complete_pending_execs(eval_tree); 307 complete_pending_execdirs(eval_tree); 308 } 309} 310 311/* Get the stat information for a file, if it is 312 * not already known. 313 */ 314int 315get_statinfo (const char *pathname, const char *name, struct stat *p) 316{ 317 if (!state.have_stat && (*options.xstat) (name, p) != 0) 318 { 319 if (!options.ignore_readdir_race || (errno != ENOENT) ) 320 { 321 error (0, errno, "%s", pathname); 322 state.exit_status = 1; 323 } 324 return -1; 325 } 326 state.have_stat = true; 327 state.have_type = true; 328 state.type = p->st_mode; 329 return 0; 330} 331 332/* Get the stat/type information for a file, if it is 333 * not already known. 334 */ 335int 336get_info (const char *pathname, 337 const char *name, 338 struct stat *p, 339 struct predicate *pred_ptr) 340{ 341 /* If we need the full stat info, or we need the type info but don't 342 * already have it, stat the file now. 343 */ 344 (void) name; 345 if (pred_ptr->need_stat) 346 { 347 return get_statinfo(pathname, state.rel_pathname, p); 348 } 349 if ((pred_ptr->need_type && (0 == state.have_type))) 350 { 351 return get_statinfo(pathname, state.rel_pathname, p); 352 } 353 return 0; 354} 355 356/* Determine if we can use O_NOFOLLOW. 357 */ 358#if defined(O_NOFOLLOW) 359static boolean 360check_nofollow(void) 361{ 362 struct utsname uts; 363 float release; 364 365 if (0 == uname(&uts)) 366 { 367 /* POSIX requires that atof() ignore "unrecognised suffixes". */ 368 release = atof(uts.release); 369 370 if (0 == strcmp("Linux", uts.sysname)) 371 { 372 /* Linux kernels 2.1.126 and earlier ignore the O_NOFOLLOW flag. */ 373 return release >= 2.2; /* close enough */ 374 } 375 else if (0 == strcmp("FreeBSD", uts.sysname)) 376 { 377 /* FreeBSD 3.0-CURRENT and later support it */ 378 return release >= 3.1; 379 } 380 } 381 382 /* Well, O_NOFOLLOW was defined, so we'll try to use it. */ 383 return true; 384} 385#endif 386 387int 388main (int argc, char **argv) 389{ 390 int i; 391 const struct parser_table *entry_close, *entry_print, *entry_open; 392 const struct parser_table *parse_entry; /* Pointer to the parsing table entry for this expression. */ 393 struct predicate *cur_pred; 394 char *predicate_name; /* Name of predicate being parsed. */ 395 int end_of_leading_options = 0; /* First arg after any -H/-L etc. */ 396 397 398 program_name = argv[0]; 399 400 /* We call check_nofollow() before setlocale() because the numbers 401 * for which we check (in the results of uname) definitiely have "." 402 * as the decimal point indicator even under locales for which that 403 * is not normally true. Hence atof() would do the wrong thing 404 * if we call it after setlocale(). 405 */ 406#ifdef O_NOFOLLOW 407 options.open_nofollow_available = check_nofollow(); 408#else 409 options.open_nofollow_available = false; 410#endif 411 412 options.regex_options = RE_SYNTAX_EMACS; 413 414#ifdef HAVE_SETLOCALE 415 setlocale (LC_ALL, ""); 416#endif 417 bindtextdomain (PACKAGE, LOCALEDIR); 418 textdomain (PACKAGE); 419 atexit (close_stdin); 420 421 422 if (isatty(0)) 423 { 424 options.warnings = true; 425 } 426 else 427 { 428 options.warnings = false; 429 } 430 431 432 predicates = NULL; 433 last_pred = NULL; 434 options.do_dir_first = true; 435 options.maxdepth = options.mindepth = -1; 436 options.start_time = time (NULL); 437 options.cur_day_start = options.start_time - DAYSECS; 438 options.full_days = false; 439 options.stay_on_filesystem = false; 440 options.ignore_readdir_race = false; 441 442 state.exit_status = 0; 443 444#if defined(DEBUG_STAT) 445 options.xstat = debug_stat; 446#endif /* !DEBUG_STAT */ 447 448 if (getenv("POSIXLY_CORRECT")) 449 options.output_block_size = 512; 450 else 451 options.output_block_size = 1024; 452 453 if (getenv("FIND_BLOCK_SIZE")) 454 { 455 error (1, 0, _("The environment variable FIND_BLOCK_SIZE is not supported, the only thing that affects the block size is the POSIXLY_CORRECT environment variable")); 456 } 457 458#if LEAF_OPTIMISATION 459 /* The leaf optimisation is enabled. */ 460 options.no_leaf_check = false; 461#else 462 /* The leaf optimisation is disabled. */ 463 options.no_leaf_check = true; 464#endif 465 466 set_follow_state(SYMLINK_NEVER_DEREF); /* The default is equivalent to -P. */ 467 468#ifdef DEBUG 469 fprintf (stderr, "cur_day_start = %s", ctime (&options.cur_day_start)); 470#endif /* DEBUG */ 471 472 /* Check for -P, -H or -L options. */ 473 for (i=1; (end_of_leading_options = i) < argc; ++i) 474 { 475 if (0 == strcmp("-H", argv[i])) 476 { 477 /* Meaning: dereference symbolic links on command line, but nowhere else. */ 478 set_follow_state(SYMLINK_DEREF_ARGSONLY); 479 } 480 else if (0 == strcmp("-L", argv[i])) 481 { 482 /* Meaning: dereference all symbolic links. */ 483 set_follow_state(SYMLINK_ALWAYS_DEREF); 484 } 485 else if (0 == strcmp("-P", argv[i])) 486 { 487 /* Meaning: never dereference symbolic links (default). */ 488 set_follow_state(SYMLINK_NEVER_DEREF); 489 } 490 else if (0 == strcmp("--", argv[i])) 491 { 492 /* -- signifies the end of options. */ 493 end_of_leading_options = i+1; /* Next time start with the next option */ 494 break; 495 } 496 else 497 { 498 /* Hmm, must be one of 499 * (a) A path name 500 * (b) A predicate 501 */ 502 end_of_leading_options = i; /* Next time start with this option */ 503 break; 504 } 505 } 506 507 /* We are now processing the part of the "find" command line 508 * after the -H/-L options (if any). 509 */ 510 511 /* fprintf(stderr, "rest: optind=%ld\n", (long)optind); */ 512 513 /* Find where in ARGV the predicates begin. */ 514 for (i = end_of_leading_options; i < argc && strchr ("-!(),", argv[i][0]) == NULL; i++) 515 { 516 /* fprintf(stderr, "Looks like %s is not a predicate\n", argv[i]); */ 517 /* Do nothing. */ ; 518 } 519 520 /* Enclose the expression in `( ... )' so a default -print will 521 apply to the whole expression. */ 522 entry_open = find_parser("("); 523 entry_close = find_parser(")"); 524 entry_print = find_parser("print"); 525 assert(entry_open != NULL); 526 assert(entry_close != NULL); 527 assert(entry_print != NULL); 528 529 parse_openparen (entry_open, argv, &argc); 530 parse_begin_user_args(argv, argc, last_pred, predicates); 531 pred_sanity_check(last_pred); 532 533 /* Build the input order list. */ 534 while (i < argc) 535 { 536 if (strchr ("-!(),", argv[i][0]) == NULL) 537 usage (_("paths must precede expression")); 538 predicate_name = argv[i]; 539 parse_entry = find_parser (predicate_name); 540 if (parse_entry == NULL) 541 { 542 /* Command line option not recognized */ 543 error (1, 0, _("invalid predicate `%s'"), predicate_name); 544 } 545 546 i++; 547 if (!(*(parse_entry->parser_func)) (parse_entry, argv, &i)) 548 { 549 if (argv[i] == NULL) 550 /* Command line option requires an argument */ 551 error (1, 0, _("missing argument to `%s'"), predicate_name); 552 else 553 error (1, 0, _("invalid argument `%s' to `%s'"), 554 argv[i], predicate_name); 555 } 556 557 pred_sanity_check(last_pred); 558 pred_sanity_check(predicates); /* XXX: expensive */ 559 } 560 parse_end_user_args(argv, argc, last_pred, predicates); 561 562 if (predicates->pred_next == NULL) 563 { 564 /* No predicates that do something other than set a global variable 565 were given; remove the unneeded initial `(' and add `-print'. */ 566 cur_pred = predicates; 567 predicates = last_pred = predicates->pred_next; 568 free ((char *) cur_pred); 569 parse_print (entry_print, argv, &argc); 570 pred_sanity_check(last_pred); 571 pred_sanity_check(predicates); /* XXX: expensive */ 572 } 573 else if (!default_prints (predicates->pred_next)) 574 { 575 /* One or more predicates that produce output were given; 576 remove the unneeded initial `('. */ 577 cur_pred = predicates; 578 predicates = predicates->pred_next; 579 pred_sanity_check(predicates); /* XXX: expensive */ 580 free ((char *) cur_pred); 581 } 582 else 583 { 584 /* `( user-supplied-expression ) -print'. */ 585 parse_closeparen (entry_close, argv, &argc); 586 pred_sanity_check(last_pred); 587 parse_print (entry_print, argv, &argc); 588 pred_sanity_check(last_pred); 589 pred_sanity_check(predicates); /* XXX: expensive */ 590 } 591 592#ifdef DEBUG 593 fprintf (stderr, "Predicate List:\n"); 594 print_list (stderr, predicates); 595#endif /* DEBUG */ 596 597 /* do a sanity check */ 598 pred_sanity_check(predicates); 599 600 /* Done parsing the predicates. Build the evaluation tree. */ 601 cur_pred = predicates; 602 eval_tree = get_expr (&cur_pred, NO_PREC); 603 604 /* Check if we have any left-over predicates (this fixes 605 * Debian bug #185202). 606 */ 607 if (cur_pred != NULL) 608 { 609 error (1, 0, _("unexpected extra predicate")); 610 } 611 612#ifdef DEBUG 613 fprintf (stderr, "Eval Tree:\n"); 614 print_tree (stderr, eval_tree, 0); 615#endif /* DEBUG */ 616 617 /* Rearrange the eval tree in optimal-predicate order. */ 618 opt_expr (&eval_tree); 619 620 /* Determine the point, if any, at which to stat the file. */ 621 mark_stat (eval_tree); 622 /* Determine the point, if any, at which to determine file type. */ 623 mark_type (eval_tree); 624 625#ifdef DEBUG 626 fprintf (stderr, "Optimized Eval Tree:\n"); 627 print_tree (stderr, eval_tree, 0); 628 fprintf (stderr, "Optimized command line:\n"); 629 print_optlist(stderr, eval_tree); 630 fprintf(stderr, "\n"); 631#endif /* DEBUG */ 632 633 /* safely_chdir() needs to check that it has ended up in the right place. 634 * To avoid bailing out when something gets automounted, it checks if 635 * the target directory appears to have had a directory mounted on it as 636 * we chdir()ed. The problem with this is that in order to notice that 637 * a filesystem was mounted, we would need to lstat() all the mount points. 638 * That strategy loses if our machine is a client of a dead NFS server. 639 * 640 * Hence if safely_chdir() and wd_sanity_check() can manage without needing 641 * to know the mounted device list, we do that. 642 */ 643 if (!options.open_nofollow_available) 644 { 645#ifdef STAT_MOUNTPOINTS 646 init_mounted_dev_list(0); 647#endif 648 } 649 650 651 starting_desc = open (".", O_RDONLY); 652 if (0 <= starting_desc && fchdir (starting_desc) != 0) 653 { 654 close (starting_desc); 655 starting_desc = -1; 656 } 657 if (starting_desc < 0) 658 { 659 starting_dir = xgetcwd (); 660 if (! starting_dir) 661 error (1, errno, _("cannot get current directory")); 662 } 663 if ((*options.xstat) (".", &starting_stat_buf) != 0) 664 error (1, errno, _("cannot get current directory")); 665 666 /* If no paths are given, default to ".". */ 667 for (i = end_of_leading_options; i < argc && strchr ("-!(),", argv[i][0]) == NULL; i++) 668 { 669 process_top_path (argv[i], 0); 670 } 671 672 /* If there were no path arguments, default to ".". */ 673 if (i == end_of_leading_options) 674 { 675 /* 676 * We use a temporary variable here because some actions modify 677 * the path temporarily. Hence if we use a string constant, 678 * we get a coredump. The best example of this is if we say 679 * "find -printf %H" (note, not "find . -printf %H"). 680 */ 681 char defaultpath[2] = "."; 682 process_top_path (defaultpath, 0); 683 } 684 685 /* If "-exec ... {} +" has been used, there may be some 686 * partially-full command lines which have been built, 687 * but which are not yet complete. Execute those now. 688 */ 689 cleanup(); 690 return state.exit_status; 691} 692 693 694static char * 695specific_dirname(const char *dir) 696{ 697 char dirbuf[1024]; 698 699 if (0 == strcmp(".", dir)) 700 { 701 /* OK, what's '.'? */ 702 if (NULL != getcwd(dirbuf, sizeof(dirbuf))) 703 { 704 return strdup(dirbuf); 705 } 706 else 707 { 708 return strdup(dir); 709 } 710 } 711 else 712 { 713 char *result = canonicalize_filename_mode(dir, CAN_EXISTING); 714 if (NULL == result) 715 return strdup(dir); 716 else 717 return result; 718 } 719} 720 721 722 723/* Return non-zero if FS is the name of a filesystem that is likely to 724 * be automounted 725 */ 726static int 727fs_likely_to_be_automounted(const char *fs) 728{ 729 return ( (0==strcmp(fs, "nfs")) || (0==strcmp(fs, "autofs")) || (0==strcmp(fs, "subfs"))); 730} 731 732 733 734#ifdef STAT_MOUNTPOINTS 735static dev_t *mounted_devices = NULL; 736static size_t num_mounted_devices = 0u; 737 738 739static void 740init_mounted_dev_list(int mandatory) 741{ 742 assert(NULL == mounted_devices); 743 assert(0 == num_mounted_devices); 744 mounted_devices = get_mounted_devices(&num_mounted_devices); 745 if (mandatory && (NULL == mounted_devices)) 746 { 747 error(1, 0, "Cannot read list of mounted devices."); 748 } 749} 750 751static void 752refresh_mounted_dev_list(void) 753{ 754 if (mounted_devices) 755 { 756 free(mounted_devices); 757 mounted_devices = 0; 758 } 759 num_mounted_devices = 0u; 760 init_mounted_dev_list(1); 761} 762 763 764/* Search for device DEV in the array LIST, which is of size N. */ 765static int 766dev_present(dev_t dev, const dev_t *list, size_t n) 767{ 768 if (list) 769 { 770 while (n-- > 0u) 771 { 772 if ( (*list++) == dev ) 773 return 1; 774 } 775 } 776 return 0; 777} 778 779enum MountPointStateChange 780 { 781 MountPointRecentlyMounted, 782 MountPointRecentlyUnmounted, 783 MountPointStateUnchanged 784 }; 785 786 787 788static enum MountPointStateChange 789get_mount_state(dev_t newdev) 790{ 791 int new_is_present, new_was_present; 792 793 new_was_present = dev_present(newdev, mounted_devices, num_mounted_devices); 794 refresh_mounted_dev_list(); 795 new_is_present = dev_present(newdev, mounted_devices, num_mounted_devices); 796 797 if (new_was_present == new_is_present) 798 return MountPointStateUnchanged; 799 else if (new_is_present) 800 return MountPointRecentlyMounted; 801 else 802 return MountPointRecentlyUnmounted; 803} 804 805 806 807/* We stat()ed a directory, chdir()ed into it (we know this 808 * since direction is TraversingDown), stat()ed it again, 809 * and noticed that the device numbers are different. Check 810 * if the filesystem was recently mounted. 811 * 812 * If it was, it looks like chdir()ing into the directory 813 * caused a filesystem to be mounted. Maybe automount is 814 * running. Anyway, that's probably OK - but it happens 815 * only when we are moving downward. 816 * 817 * We also allow for the possibility that a similar thing 818 * has happened with the unmounting of a filesystem. This 819 * is much rarer, as it relies on an automounter timeout 820 * occurring at exactly the wrong moment. 821 */ 822static enum WdSanityCheckFatality 823dirchange_is_fatal(const char *specific_what, 824 enum WdSanityCheckFatality isfatal, 825 int silent, 826 struct stat *newinfo) 827{ 828 enum MountPointStateChange transition = get_mount_state(newinfo->st_dev); 829 switch (transition) 830 { 831 case MountPointRecentlyUnmounted: 832 isfatal = NON_FATAL_IF_SANITY_CHECK_FAILS; 833 if (!silent) 834 { 835 error (0, 0, 836 _("Warning: filesystem %s has recently been unmounted."), 837 specific_what); 838 } 839 break; 840 841 case MountPointRecentlyMounted: 842 isfatal = NON_FATAL_IF_SANITY_CHECK_FAILS; 843 if (!silent) 844 { 845 error (0, 0, 846 _("Warning: filesystem %s has recently been mounted."), 847 specific_what); 848 } 849 break; 850 851 case MountPointStateUnchanged: 852 /* leave isfatal as it is */ 853 break; 854 } 855 856 return isfatal; 857} 858 859 860#endif 861 862 863 864/* Examine the results of the stat() of a directory from before we 865 * entered or left it, with the results of stat()ing it afterward. If 866 * these are different, the filesystem tree has been modified while we 867 * were traversing it. That might be an attempt to use a race 868 * condition to persuade find to do something it didn't intend 869 * (e.g. an attempt by an ordinary user to exploit the fact that root 870 * sometimes runs find on the whole filesystem). However, this can 871 * also happen if automount is running (certainly on Solaris). With 872 * automount, moving into a directory can cause a filesystem to be 873 * mounted there. 874 * 875 * To cope sensibly with this, we will raise an error if we see the 876 * device number change unless we are chdir()ing into a subdirectory, 877 * and the directory we moved into has been mounted or unmounted "recently". 878 * Here "recently" means since we started "find" or we last re-read 879 * the /etc/mnttab file. 880 * 881 * If the device number does not change but the inode does, that is a 882 * problem. 883 * 884 * If the device number and inode are both the same, we are happy. 885 * 886 * If a filesystem is (un)mounted as we chdir() into the directory, that 887 * may mean that we're now examining a section of the filesystem that might 888 * have been excluded from consideration (via -prune or -quit for example). 889 * Hence we print a warning message to indicate that the output of find 890 * might be inconsistent due to the change in the filesystem. 891 */ 892static boolean 893wd_sanity_check(const char *thing_to_stat, 894 const char *progname, 895 const char *what, 896 dev_t old_dev, 897 ino_t old_ino, 898 struct stat *newinfo, 899 int parent, 900 int line_no, 901 enum TraversalDirection direction, 902 enum WdSanityCheckFatality isfatal, 903 boolean *changed) /* output parameter */ 904{ 905 const char *fstype; 906 char *specific_what = NULL; 907 int silent = 0; 908 const char *current_dir = "."; 909 910 *changed = false; 911 912 if ((*options.xstat) (current_dir, newinfo) != 0) 913 error (1, errno, "%s", thing_to_stat); 914 915 if (old_dev != newinfo->st_dev) 916 { 917 *changed = true; 918 specific_what = specific_dirname(what); 919 fstype = filesystem_type(newinfo, current_dir); 920 silent = fs_likely_to_be_automounted(fstype); 921 922 /* This condition is rare, so once we are here it is 923 * reasonable to perform an expensive computation to 924 * determine if we should continue or fail. 925 */ 926 if (TraversingDown == direction) 927 { 928#ifdef STAT_MOUNTPOINTS 929 isfatal = dirchange_is_fatal(specific_what,isfatal,silent,newinfo); 930#else 931 isfatal = RETRY_IF_SANITY_CHECK_FAILS; 932#endif 933 } 934 935 switch (isfatal) 936 { 937 case FATAL_IF_SANITY_CHECK_FAILS: 938 { 939 fstype = filesystem_type(newinfo, current_dir); 940 error (1, 0, 941 _("%s%s changed during execution of %s (old device number %ld, new device number %ld, filesystem type is %s) [ref %ld]"), 942 specific_what, 943 parent ? "/.." : "", 944 progname, 945 (long) old_dev, 946 (long) newinfo->st_dev, 947 fstype, 948 line_no); 949 /*NOTREACHED*/ 950 return false; 951 } 952 953 case NON_FATAL_IF_SANITY_CHECK_FAILS: 954 { 955 /* Since the device has changed under us, the inode number 956 * will almost certainly also be different. However, we have 957 * already decided that this is not a problem. Hence we return 958 * without checking the inode number. 959 */ 960 free(specific_what); 961 return true; 962 } 963 964 case RETRY_IF_SANITY_CHECK_FAILS: 965 return false; 966 } 967 } 968 969 /* Device number was the same, check if the inode has changed. */ 970 if (old_ino != newinfo->st_ino) 971 { 972 *changed = true; 973 specific_what = specific_dirname(what); 974 fstype = filesystem_type(newinfo, current_dir); 975 976 error ((isfatal == FATAL_IF_SANITY_CHECK_FAILS) ? 1 : 0, 977 0, /* no relevant errno value */ 978 _("%s%s changed during execution of %s (old inode number %ld, new inode number %ld, filesystem type is %s) [ref %ld]"), 979 specific_what, 980 parent ? "/.." : "", 981 progname, 982 (long) old_ino, 983 (long) newinfo->st_ino, 984 fstype, 985 line_no); 986 free(specific_what); 987 return false; 988 } 989 990 return true; 991} 992 993enum SafeChdirStatus 994 { 995 SafeChdirOK, 996 SafeChdirFailSymlink, 997 SafeChdirFailNotDir, 998 SafeChdirFailStat, 999 SafeChdirFailWouldBeUnableToReturn, 1000 SafeChdirFailChdirFailed, 1001 SafeChdirFailNonexistent, 1002 SafeChdirFailDestUnreadable 1003 }; 1004 1005/* Safely perform a change in directory. We do this by calling 1006 * lstat() on the subdirectory, using chdir() to move into it, and 1007 * then lstat()ing ".". We compare the results of the two stat calls 1008 * to see if they are consistent. If not, we sound the alarm. 1009 * 1010 * If following_links() is true, we do follow symbolic links. 1011 */ 1012static enum SafeChdirStatus 1013safely_chdir_lstat(const char *dest, 1014 enum TraversalDirection direction, 1015 struct stat *statbuf_dest, 1016 enum ChdirSymlinkHandling symlink_follow_option, 1017 boolean *did_stat) 1018{ 1019 struct stat statbuf_arrived; 1020 int rv, dotfd=-1; 1021 int saved_errno; /* specific_dirname() changes errno. */ 1022 boolean rv_set = false; 1023 boolean statflag = false; 1024 int tries = 0; 1025 enum WdSanityCheckFatality isfatal = RETRY_IF_SANITY_CHECK_FAILS; 1026 1027 saved_errno = errno = 0; 1028 1029 dotfd = open(".", O_RDONLY); 1030 1031 /* We jump back to here if wd_sanity_check() 1032 * recoverably triggers an alert. 1033 */ 1034 retry: 1035 ++tries; 1036 1037 if (dotfd >= 0) 1038 { 1039 /* Stat the directory we're going to. */ 1040 if (0 == options.xstat(dest, statbuf_dest)) 1041 { 1042 statflag = true; 1043 1044#ifdef S_ISLNK 1045 /* symlink_follow_option might be set to SymlinkFollowOk, which 1046 * would allow us to chdir() into a symbolic link. This is 1047 * only useful for the case where the directory we're 1048 * chdir()ing into is the basename of a command line 1049 * argument, for example where "foo/bar/baz" is specified on 1050 * the command line. When -P is in effect (the default), 1051 * baz will not be followed if it is a symlink, but if bar 1052 * is a symlink, it _should_ be followed. Hence we need the 1053 * ability to override the policy set by following_links(). 1054 */ 1055 if (!following_links() && S_ISLNK(statbuf_dest->st_mode)) 1056 { 1057 /* We're not supposed to be following links, but this is 1058 * a link. Check symlink_follow_option to see if we should 1059 * make a special exception. 1060 */ 1061 if (symlink_follow_option == SymlinkFollowOk) 1062 { 1063 /* We need to re-stat() the file so that the 1064 * sanity check can pass. 1065 */ 1066 if (0 != stat(dest, statbuf_dest)) 1067 { 1068 rv = SafeChdirFailNonexistent; 1069 rv_set = true; 1070 saved_errno = errno; 1071 goto fail; 1072 } 1073 statflag = true; 1074 } 1075 else 1076 { 1077 /* Not following symlinks, so the attempt to 1078 * chdir() into a symlink should be prevented. 1079 */ 1080 rv = SafeChdirFailSymlink; 1081 rv_set = true; 1082 saved_errno = 0; /* silence the error message */ 1083 goto fail; 1084 } 1085 } 1086#endif 1087#ifdef S_ISDIR 1088 /* Although the immediately following chdir() would detect 1089 * the fact that this is not a directory for us, this would 1090 * result in an extra system call that fails. Anybody 1091 * examining the system-call trace should ideally not be 1092 * concerned that something is actually failing. 1093 */ 1094 if (!S_ISDIR(statbuf_dest->st_mode)) 1095 { 1096 rv = SafeChdirFailNotDir; 1097 rv_set = true; 1098 saved_errno = 0; /* silence the error message */ 1099 goto fail; 1100 } 1101#endif 1102#ifdef DEBUG_STAT 1103 fprintf(stderr, "safely_chdir(): chdir(\"%s\")\n", dest); 1104#endif 1105 if (0 == chdir(dest)) 1106 { 1107 /* check we ended up where we wanted to go */ 1108 boolean changed = false; 1109 if (!wd_sanity_check(".", program_name, ".", 1110 statbuf_dest->st_dev, 1111 statbuf_dest->st_ino, 1112 &statbuf_arrived, 1113 0, __LINE__, direction, 1114 isfatal, 1115 &changed)) 1116 { 1117 /* Only allow one failure. */ 1118 if (RETRY_IF_SANITY_CHECK_FAILS == isfatal) 1119 { 1120 if (0 == fchdir(dotfd)) 1121 { 1122 isfatal = FATAL_IF_SANITY_CHECK_FAILS; 1123 goto retry; 1124 } 1125 else 1126 { 1127 /* Failed to return to original directory, 1128 * but we know that the current working 1129 * directory is not the one that we intend 1130 * to be in. Since fchdir() failed, we 1131 * can't recover from this and so this error 1132 * is fatal. 1133 */ 1134 error(1, errno, 1135 "failed to return to parent directory"); 1136 } 1137 } 1138 else 1139 { 1140 /* XXX: not sure what to use as an excuse here. */ 1141 rv = SafeChdirFailNonexistent; 1142 rv_set = true; 1143 saved_errno = 0; 1144 goto fail; 1145 } 1146 } 1147 1148 close(dotfd); 1149 return SafeChdirOK; 1150 } 1151 else 1152 { 1153 saved_errno = errno; 1154 if (ENOENT == saved_errno) 1155 { 1156 rv = SafeChdirFailNonexistent; 1157 rv_set = true; 1158 if (options.ignore_readdir_race) 1159 errno = 0; /* don't issue err msg */ 1160 } 1161 else if (ENOTDIR == saved_errno) 1162 { 1163 /* This can happen if the we stat a directory, 1164 * and then filesystem activity changes it into 1165 * a non-directory. 1166 */ 1167 saved_errno = 0; /* don't issue err msg */ 1168 rv = SafeChdirFailNotDir; 1169 rv_set = true; 1170 } 1171 else 1172 { 1173 rv = SafeChdirFailChdirFailed; 1174 rv_set = true; 1175 } 1176 goto fail; 1177 } 1178 } 1179 else 1180 { 1181 saved_errno = errno; 1182 rv = SafeChdirFailStat; 1183 rv_set = true; 1184 1185 if ( (ENOENT == saved_errno) || (0 == state.curdepth)) 1186 saved_errno = 0; /* don't issue err msg */ 1187 goto fail; 1188 } 1189 } 1190 else 1191 { 1192 /* We do not have read permissions on "." */ 1193 rv = SafeChdirFailWouldBeUnableToReturn; 1194 rv_set = true; 1195 goto fail; 1196 } 1197 1198 /* This is the success path, so we clear errno. The caller probably 1199 * won't be calling error() anyway. 1200 */ 1201 saved_errno = 0; 1202 1203 /* We use the same exit path for success or failure. 1204 * which has occurred is recorded in RV. 1205 */ 1206 fail: 1207 /* We do not call error() as this would result in a duplicate error 1208 * message when the caller does the same thing. 1209 */ 1210 if (saved_errno) 1211 errno = saved_errno; 1212 1213 if (dotfd >= 0) 1214 { 1215 close(dotfd); 1216 dotfd = -1; 1217 } 1218 1219 *did_stat = statflag; 1220 assert(rv_set); 1221 return rv; 1222} 1223 1224#if defined(O_NOFOLLOW) 1225/* Safely change working directory to the specified subdirectory. If 1226 * we are not allowed to follow symbolic links, we use open() with 1227 * O_NOFOLLOW, followed by fchdir(). This ensures that we don't 1228 * follow symbolic links (of course, we do follow them if the -L 1229 * option is in effect). 1230 */ 1231static enum SafeChdirStatus 1232safely_chdir_nofollow(const char *dest, 1233 enum TraversalDirection direction, 1234 struct stat *statbuf_dest, 1235 enum ChdirSymlinkHandling symlink_follow_option, 1236 boolean *did_stat) 1237{ 1238 int extraflags, fd; 1239 extraflags = 0; 1240 1241 *did_stat = false; 1242 1243 switch (symlink_follow_option) 1244 { 1245 case SymlinkFollowOk: 1246 extraflags = 0; 1247 break; 1248 1249 case SymlinkHandleDefault: 1250 if (following_links()) 1251 extraflags = 0; 1252 else 1253 extraflags = O_NOFOLLOW; 1254 break; 1255 } 1256 1257 errno = 0; 1258 fd = open(dest, O_RDONLY|extraflags); 1259 if (fd < 0) 1260 { 1261 switch (errno) 1262 { 1263 case ELOOP: 1264 return SafeChdirFailSymlink; /* This is why we use O_NOFOLLOW */ 1265 case ENOENT: 1266 return SafeChdirFailNonexistent; 1267 default: 1268 return SafeChdirFailDestUnreadable; 1269 } 1270 } 1271 1272 errno = 0; 1273 if (0 == fchdir(fd)) 1274 { 1275 close(fd); 1276 return SafeChdirOK; 1277 } 1278 else 1279 { 1280 int saved_errno = errno; 1281 close(fd); 1282 errno = saved_errno; 1283 1284 switch (errno) 1285 { 1286 case ENOTDIR: 1287 return SafeChdirFailNotDir; 1288 1289 case EACCES: 1290 case EBADF: /* Shouldn't happen */ 1291 case EINTR: 1292 case EIO: 1293 default: 1294 return SafeChdirFailChdirFailed; 1295 } 1296 } 1297} 1298#endif 1299 1300static enum SafeChdirStatus 1301safely_chdir(const char *dest, 1302 enum TraversalDirection direction, 1303 struct stat *statbuf_dest, 1304 enum ChdirSymlinkHandling symlink_follow_option, 1305 boolean *did_stat) 1306{ 1307 /* We're about to leave a directory. If there are any -execdir 1308 * argument lists which have been built but have not yet been 1309 * processed, do them now because they must be done in the same 1310 * directory. 1311 */ 1312 complete_pending_execdirs(eval_tree); 1313 1314#if defined(O_NOFOLLOW) 1315 if (options.open_nofollow_available) 1316 { 1317 enum SafeChdirStatus result; 1318 result = safely_chdir_nofollow(dest, direction, statbuf_dest, 1319 symlink_follow_option, did_stat); 1320 if (SafeChdirFailDestUnreadable != result) 1321 { 1322 return result; 1323 } 1324 else 1325 { 1326 /* Savannah bug #15384: fall through to use safely_chdir_lstat 1327 * if the directory is not readable. 1328 */ 1329 /* Do nothing. */ 1330 } 1331 } 1332#endif 1333 /* Even if O_NOFOLLOW is available, we may need to use the alternative 1334 * method, since parent of the start point may be executable but not 1335 * readable. 1336 */ 1337 return safely_chdir_lstat(dest, direction, statbuf_dest, 1338 symlink_follow_option, did_stat); 1339} 1340 1341 1342 1343/* Safely go back to the starting directory. */ 1344static void 1345chdir_back (void) 1346{ 1347 struct stat stat_buf; 1348 boolean dummy; 1349 1350 if (starting_desc < 0) 1351 { 1352#ifdef DEBUG_STAT 1353 fprintf(stderr, "chdir_back(): chdir(\"%s\")\n", starting_dir); 1354#endif 1355 1356#ifdef STAT_MOUNTPOINTS 1357 /* We will need the mounted device list. Get it now if we don't 1358 * already have it. 1359 */ 1360 if (NULL == mounted_devices) 1361 init_mounted_dev_list(1); 1362#endif 1363 1364 if (chdir (starting_dir) != 0) 1365 error (1, errno, "%s", starting_dir); 1366 1367 wd_sanity_check(starting_dir, 1368 program_name, 1369 starting_dir, 1370 starting_stat_buf.st_dev, 1371 starting_stat_buf.st_ino, 1372 &stat_buf, 0, __LINE__, 1373 TraversingUp, 1374 FATAL_IF_SANITY_CHECK_FAILS, 1375 &dummy); 1376 } 1377 else 1378 { 1379#ifdef DEBUG_STAT 1380 fprintf(stderr, "chdir_back(): chdir(<starting-point>)\n"); 1381#endif 1382 if (fchdir (starting_desc) != 0) 1383 error (1, errno, "%s", starting_dir); 1384 } 1385} 1386 1387/* Move to the parent of a given directory and then call a function, 1388 * restoring the cwd. Don't bother changing directory if the 1389 * specified directory is a child of "." or is the root directory. 1390 */ 1391static void 1392at_top (char *pathname, 1393 mode_t mode, 1394 struct stat *pstat, 1395 void (*action)(char *pathname, 1396 char *basename, 1397 int mode, 1398 struct stat *pstat)) 1399{ 1400 int dirchange; 1401 char *parent_dir = dir_name (pathname); 1402 char *base = last_component (pathname); 1403 1404 state.curdepth = 0; 1405 state.path_length = strlen (pathname); 1406 1407 if (0 == *base 1408 || 0 == strcmp(parent_dir, ".")) 1409 { 1410 dirchange = 0; 1411 base = pathname; 1412 } 1413 else 1414 { 1415 enum TraversalDirection direction; 1416 enum SafeChdirStatus chdir_status; 1417 struct stat st; 1418 boolean did_stat = false; 1419 1420 dirchange = 1; 1421 if (0 == strcmp(base, "..")) 1422 direction = TraversingUp; 1423 else 1424 direction = TraversingDown; 1425 1426 /* We pass SymlinkFollowOk to safely_chdir(), which allows it to 1427 * chdir() into a symbolic link. This is only useful for the 1428 * case where the directory we're chdir()ing into is the 1429 * basename of a command line argument, for example where 1430 * "foo/bar/baz" is specified on the command line. When -P is 1431 * in effect (the default), baz will not be followed if it is a 1432 * symlink, but if bar is a symlink, it _should_ be followed. 1433 * Hence we need the ability to override the policy set by 1434 * following_links(). 1435 */ 1436 chdir_status = safely_chdir(parent_dir, direction, &st, SymlinkFollowOk, &did_stat); 1437 if (SafeChdirOK != chdir_status) 1438 { 1439 const char *what = (SafeChdirFailWouldBeUnableToReturn == chdir_status) ? "." : parent_dir; 1440 if (errno) 1441 error (0, errno, "%s", what); 1442 else 1443 error (0, 0, "Failed to safely change directory into `%s'", 1444 parent_dir); 1445 1446 /* We can't process this command-line argument. */ 1447 state.exit_status = 1; 1448 return; 1449 } 1450 } 1451 1452 free (parent_dir); 1453 parent_dir = NULL; 1454 1455 action(pathname, base, mode, pstat); 1456 1457 if (dirchange) 1458 { 1459 chdir_back(); 1460 } 1461} 1462 1463 1464static void do_process_top_dir(char *pathname, 1465 char *base, 1466 int mode, 1467 struct stat *pstat) 1468{ 1469 process_path (pathname, base, false, ".", mode); 1470 complete_pending_execdirs(eval_tree); 1471} 1472 1473static void do_process_predicate(char *pathname, 1474 char *base, 1475 int mode, 1476 struct stat *pstat) 1477{ 1478 state.rel_pathname = base; 1479 apply_predicate (pathname, pstat, eval_tree); 1480} 1481 1482 1483 1484 1485/* Descend PATHNAME, which is a command-line argument. 1486 1487 Actions like -execdir assume that we are in the 1488 parent directory of the file we're examining, 1489 and on entry to this function our working directory 1490 is whatever it was when find was invoked. Therefore 1491 If PATHNAME is "." we just leave things as they are. 1492 Otherwise, we figure out what the parent directory is, 1493 and move to that. 1494*/ 1495static void 1496process_top_path (char *pathname, mode_t mode) 1497{ 1498 at_top(pathname, mode, NULL, do_process_top_dir); 1499} 1500 1501 1502/* Info on each directory in the current tree branch, to avoid 1503 getting stuck in symbolic link loops. */ 1504static struct dir_id *dir_ids = NULL; 1505/* Entries allocated in `dir_ids'. */ 1506static int dir_alloc = 0; 1507/* Index in `dir_ids' of directory currently being searched. 1508 This is always the last valid entry. */ 1509static int dir_curr = -1; 1510/* (Arbitrary) number of entries to grow `dir_ids' by. */ 1511#define DIR_ALLOC_STEP 32 1512 1513 1514 1515/* We've detected a filesystem loop. This is caused by one of 1516 * two things: 1517 * 1518 * 1. Option -L is in effect and we've hit a symbolic link that 1519 * points to an ancestor. This is harmless. We won't traverse the 1520 * symbolic link. 1521 * 1522 * 2. We have hit a real cycle in the directory hierarchy. In this 1523 * case, we issue a diagnostic message (POSIX requires this) and we 1524 * skip that directory entry. 1525 */ 1526static void 1527issue_loop_warning(const char *name, const char *pathname, int level) 1528{ 1529 struct stat stbuf_link; 1530 if (lstat(name, &stbuf_link) != 0) 1531 stbuf_link.st_mode = S_IFREG; 1532 1533 if (S_ISLNK(stbuf_link.st_mode)) 1534 { 1535 error(0, 0, 1536 _("Symbolic link `%s' is part of a loop in the directory hierarchy; we have already visited the directory to which it points."), 1537 pathname); 1538 } 1539 else 1540 { 1541 int distance = 1 + (dir_curr-level); 1542 /* We have found an infinite loop. POSIX requires us to 1543 * issue a diagnostic. Usually we won't get to here 1544 * because when the leaf optimisation is on, it will cause 1545 * the subdirectory to be skipped. If /a/b/c/d is a hard 1546 * link to /a/b, then the link count of /a/b/c is 2, 1547 * because the ".." entry of /b/b/c/d points to /a, not 1548 * to /a/b/c. 1549 */ 1550 error(0, 0, 1551 _("Filesystem loop detected; `%s' has the same device number and inode as a directory which is %d %s."), 1552 pathname, 1553 distance, 1554 (distance == 1 ? 1555 _("level higher in the filesystem hierarchy") : 1556 _("levels higher in the filesystem hierarchy"))); 1557 } 1558} 1559 1560/* Take a "mode" indicator and fill in the files of 'state'. 1561 */ 1562static int 1563digest_mode(mode_t mode, 1564 const char *pathname, 1565 const char *name, 1566 struct stat *pstat, 1567 boolean leaf) 1568{ 1569 /* If we know the type of the directory entry, and it is not a 1570 * symbolic link, we may be able to avoid a stat() or lstat() call. 1571 */ 1572 if (mode) 1573 { 1574 if (S_ISLNK(mode) && following_links()) 1575 { 1576 /* mode is wrong because we should have followed the symlink. */ 1577 if (get_statinfo(pathname, name, pstat) != 0) 1578 return 0; 1579 mode = state.type = pstat->st_mode; 1580 state.have_type = true; 1581 } 1582 else 1583 { 1584 state.have_type = true; 1585 pstat->st_mode = state.type = mode; 1586 } 1587 } 1588 else 1589 { 1590 /* Mode is not yet known; may have to stat the file unless we 1591 * can deduce that it is not a directory (which is all we need to 1592 * know at this stage) 1593 */ 1594 if (leaf) 1595 { 1596 state.have_stat = false; 1597 state.have_type = false;; 1598 state.type = 0; 1599 } 1600 else 1601 { 1602 if (get_statinfo(pathname, name, pstat) != 0) 1603 return 0; 1604 1605 /* If -L is in effect and we are dealing with a symlink, 1606 * st_mode is the mode of the pointed-to file, while mode is 1607 * the mode of the directory entry (S_IFLNK). Hence now 1608 * that we have the stat information, override "mode". 1609 */ 1610 state.type = pstat->st_mode; 1611 state.have_type = true; 1612 } 1613 } 1614 1615 /* success. */ 1616 return 1; 1617} 1618 1619 1620 1621/* Recursively descend path PATHNAME, applying the predicates. 1622 LEAF is true if PATHNAME is known to be in a directory that has no 1623 more unexamined subdirectories, and therefore it is not a directory. 1624 Knowing this allows us to avoid calling stat as long as possible for 1625 leaf files. 1626 1627 NAME is PATHNAME relative to the current directory. We access NAME 1628 but print PATHNAME. 1629 1630 PARENT is the path of the parent of NAME, relative to find's 1631 starting directory. 1632 1633 Return nonzero iff PATHNAME is a directory. */ 1634 1635static int 1636process_path (char *pathname, char *name, boolean leaf, char *parent, 1637 mode_t mode) 1638{ 1639 struct stat stat_buf; 1640 static dev_t root_dev; /* Device ID of current argument pathname. */ 1641 int i; 1642 1643 /* Assume it is a non-directory initially. */ 1644 stat_buf.st_mode = 0; 1645 state.rel_pathname = name; 1646 state.type = 0; 1647 state.have_stat = false; 1648 state.have_type = false; 1649 1650 if (!digest_mode(mode, pathname, name, &stat_buf, leaf)) 1651 return 0; 1652 1653 if (!S_ISDIR (state.type)) 1654 { 1655 if (state.curdepth >= options.mindepth) 1656 apply_predicate (pathname, &stat_buf, eval_tree); 1657 return 0; 1658 } 1659 1660 /* From here on, we're working on a directory. */ 1661 1662 1663 /* Now we really need to stat the directory, even if we know the 1664 * type, because we need information like struct stat.st_rdev. 1665 */ 1666 if (get_statinfo(pathname, name, &stat_buf) != 0) 1667 return 0; 1668 1669 state.have_stat = true; 1670 mode = state.type = stat_buf.st_mode; /* use full info now that we have it. */ 1671 state.stop_at_current_level = 1672 options.maxdepth >= 0 1673 && state.curdepth >= options.maxdepth; 1674 1675 /* If we've already seen this directory on this branch, 1676 don't descend it again. */ 1677 for (i = 0; i <= dir_curr; i++) 1678 if (stat_buf.st_ino == dir_ids[i].ino && 1679 stat_buf.st_dev == dir_ids[i].dev) 1680 { 1681 state.stop_at_current_level = true; 1682 issue_loop_warning(name, pathname, i); 1683 } 1684 1685 if (dir_alloc <= ++dir_curr) 1686 { 1687 dir_alloc += DIR_ALLOC_STEP; 1688 dir_ids = (struct dir_id *) 1689 xrealloc ((char *) dir_ids, dir_alloc * sizeof (struct dir_id)); 1690 } 1691 dir_ids[dir_curr].ino = stat_buf.st_ino; 1692 dir_ids[dir_curr].dev = stat_buf.st_dev; 1693 1694 if (options.stay_on_filesystem) 1695 { 1696 if (state.curdepth == 0) 1697 root_dev = stat_buf.st_dev; 1698 else if (stat_buf.st_dev != root_dev) 1699 state.stop_at_current_level = true; 1700 } 1701 1702 if (options.do_dir_first && state.curdepth >= options.mindepth) 1703 apply_predicate (pathname, &stat_buf, eval_tree); 1704 1705#ifdef DEBUG 1706 fprintf(stderr, "pathname = %s, stop_at_current_level = %d\n", 1707 pathname, state.stop_at_current_level); 1708#endif /* DEBUG */ 1709 1710 if (state.stop_at_current_level == false) 1711 /* Scan directory on disk. */ 1712 process_dir (pathname, name, strlen (pathname), &stat_buf, parent); 1713 1714 if (options.do_dir_first == false && state.curdepth >= options.mindepth) 1715 { 1716 /* The fields in 'state' are now out of date. Correct them. 1717 */ 1718 if (!digest_mode(mode, pathname, name, &stat_buf, leaf)) 1719 return 0; 1720 1721 if (0 == dir_curr) 1722 { 1723 at_top(pathname, mode, &stat_buf, do_process_predicate); 1724 } 1725 else 1726 { 1727 do_process_predicate(pathname, name, mode, &stat_buf); 1728 } 1729 } 1730 1731 dir_curr--; 1732 1733 return 1; 1734} 1735 1736/* Examine the predicate list for instances of -execdir or -okdir 1737 * which have been terminated with '+' (build argument list) rather 1738 * than ';' (singles only). If there are any, run them (this will 1739 * have no effect if there are no arguments waiting). 1740 */ 1741static void 1742complete_pending_execdirs(struct predicate *p) 1743{ 1744#if defined(NEW_EXEC) 1745 if (NULL == p) 1746 return; 1747 1748 complete_pending_execdirs(p->pred_left); 1749 1750 if (p->pred_func == pred_execdir || p->pred_func == pred_okdir) 1751 { 1752 /* It's an exec-family predicate. p->args.exec_val is valid. */ 1753 if (p->args.exec_vec.multiple) 1754 { 1755 struct exec_val *execp = &p->args.exec_vec; 1756 1757 /* This one was terminated by '+' and so might have some 1758 * left... Run it if necessary. 1759 */ 1760 if (execp->state.todo) 1761 { 1762 /* There are not-yet-executed arguments. */ 1763 launch (&execp->ctl, &execp->state); 1764 } 1765 } 1766 } 1767 1768 complete_pending_execdirs(p->pred_right); 1769#else 1770 /* nothing to do. */ 1771 return; 1772#endif 1773} 1774 1775/* Examine the predicate list for instances of -exec which have been 1776 * terminated with '+' (build argument list) rather than ';' (singles 1777 * only). If there are any, run them (this will have no effect if 1778 * there are no arguments waiting). 1779 */ 1780static void 1781complete_pending_execs(struct predicate *p) 1782{ 1783#if defined(NEW_EXEC) 1784 if (NULL == p) 1785 return; 1786 1787 complete_pending_execs(p->pred_left); 1788 1789 /* It's an exec-family predicate then p->args.exec_val is valid 1790 * and we can check it. 1791 */ 1792 if (p->pred_func == pred_exec && p->args.exec_vec.multiple) 1793 { 1794 struct exec_val *execp = &p->args.exec_vec; 1795 1796 /* This one was terminated by '+' and so might have some 1797 * left... Run it if necessary. Set state.exit_status if 1798 * there are any problems. 1799 */ 1800 if (execp->state.todo) 1801 { 1802 /* There are not-yet-executed arguments. */ 1803 launch (&execp->ctl, &execp->state); 1804 } 1805 } 1806 1807 complete_pending_execs(p->pred_right); 1808#else 1809 /* nothing to do. */ 1810 return; 1811#endif 1812} 1813 1814 1815/* Scan directory PATHNAME and recurse through process_path for each entry. 1816 1817 PATHLEN is the length of PATHNAME. 1818 1819 NAME is PATHNAME relative to the current directory. 1820 1821 STATP is the results of *options.xstat on it. 1822 1823 PARENT is the path of the parent of NAME, relative to find's 1824 starting directory. */ 1825 1826static void 1827process_dir (char *pathname, char *name, int pathlen, struct stat *statp, char *parent) 1828{ 1829 int subdirs_left; /* Number of unexamined subdirs in PATHNAME. */ 1830 boolean subdirs_unreliable; /* if true, cannot use dir link count as subdir limif (if false, it may STILL be unreliable) */ 1831 int idx; /* Which entry are we on? */ 1832 struct stat stat_buf; 1833 1834 struct savedir_dirinfo *dirinfo; 1835 1836 if (statp->st_nlink < 2) 1837 { 1838 subdirs_unreliable = true; 1839 } 1840 else 1841 { 1842 subdirs_unreliable = false; /* not necessarily right */ 1843 subdirs_left = statp->st_nlink - 2; /* Account for name and ".". */ 1844 } 1845 1846 errno = 0; 1847 dirinfo = xsavedir(name, 0); 1848 1849 1850 if (dirinfo == NULL) 1851 { 1852 assert(errno != 0); 1853 error (0, errno, "%s", pathname); 1854 state.exit_status = 1; 1855 } 1856 else 1857 { 1858 register char *namep; /* Current point in `name_space'. */ 1859 char *cur_path; /* Full path of each file to process. */ 1860 char *cur_name; /* Base name of each file to process. */ 1861 unsigned cur_path_size; /* Bytes allocated for `cur_path'. */ 1862 register unsigned file_len; /* Length of each path to process. */ 1863 register unsigned pathname_len; /* PATHLEN plus trailing '/'. */ 1864 boolean did_stat = false; 1865 1866 if (pathname[pathlen - 1] == '/') 1867 pathname_len = pathlen + 1; /* For '\0'; already have '/'. */ 1868 else 1869 pathname_len = pathlen + 2; /* For '/' and '\0'. */ 1870 cur_path_size = 0; 1871 cur_path = NULL; 1872 1873 /* We're about to leave the directory. If there are any 1874 * -execdir argument lists which have been built but have not 1875 * yet been processed, do them now because they must be done in 1876 * the same directory. 1877 */ 1878 complete_pending_execdirs(eval_tree); 1879 1880 if (strcmp (name, ".")) 1881 { 1882 enum SafeChdirStatus status = safely_chdir (name, TraversingDown, &stat_buf, SymlinkHandleDefault, &did_stat); 1883 switch (status) 1884 { 1885 case SafeChdirOK: 1886 /* If there had been a change but wd_sanity_check() 1887 * accepted it, we need to accept that on the 1888 * way back up as well, so modify our record 1889 * of what we think we should see later. 1890 * If there was no change, the assignments are a no-op. 1891 * 1892 * However, before performing the assignment, we need to 1893 * check that we have the stat information. If O_NOFOLLOW 1894 * is available, safely_chdir() will not have needed to use 1895 * stat(), and so stat_buf will just contain random data. 1896 */ 1897 if (!did_stat) 1898 { 1899 /* If there is a link we need to follow it. Hence 1900 * the direct call to stat() not through (options.xstat) 1901 */ 1902 if (0 != stat(".", &stat_buf)) 1903 break; /* skip the assignment. */ 1904 } 1905 dir_ids[dir_curr].dev = stat_buf.st_dev; 1906 dir_ids[dir_curr].ino = stat_buf.st_ino; 1907 1908 break; 1909 1910 case SafeChdirFailWouldBeUnableToReturn: 1911 error (0, errno, "."); 1912 state.exit_status = 1; 1913 break; 1914 1915 case SafeChdirFailNonexistent: 1916 case SafeChdirFailDestUnreadable: 1917 case SafeChdirFailStat: 1918 case SafeChdirFailNotDir: 1919 case SafeChdirFailChdirFailed: 1920 error (0, errno, "%s", pathname); 1921 state.exit_status = 1; 1922 return; 1923 1924 case SafeChdirFailSymlink: 1925 error (0, 0, 1926 _("warning: not following the symbolic link %s"), 1927 pathname); 1928 state.exit_status = 1; 1929 return; 1930 } 1931 } 1932 1933 for (idx=0; idx < dirinfo->size; ++idx) 1934 { 1935 /* savedirinfo() may return dirinfo=NULL if extended information 1936 * is not available. 1937 */ 1938 mode_t mode = (dirinfo->entries[idx].flags & SavedirHaveFileType) ? 1939 dirinfo->entries[idx].type_info : 0; 1940 namep = dirinfo->entries[idx].name; 1941 1942 /* Append this directory entry's name to the path being searched. */ 1943 file_len = pathname_len + strlen (namep); 1944 if (file_len > cur_path_size) 1945 { 1946 while (file_len > cur_path_size) 1947 cur_path_size += 1024; 1948 if (cur_path) 1949 free (cur_path); 1950 cur_path = xmalloc (cur_path_size); 1951 strcpy (cur_path, pathname); 1952 cur_path[pathname_len - 2] = '/'; 1953 } 1954 cur_name = cur_path + pathname_len - 1; 1955 strcpy (cur_name, namep); 1956 1957 state.curdepth++; 1958 if (!options.no_leaf_check && !subdirs_unreliable) 1959 { 1960 if (mode && S_ISDIR(mode) && (subdirs_left == 0)) 1961 { 1962 /* This is a subdirectory, but the number of directories we 1963 * have found now exceeds the number we would expect given 1964 * the hard link count on the parent. This is likely to be 1965 * a bug in the filesystem driver (e.g. Linux's 1966 * /proc filesystem) or may just be a fact that the OS 1967 * doesn't really handle hard links with Unix semantics. 1968 * In the latter case, -noleaf should be used routinely. 1969 */ 1970 error(0, 0, _("WARNING: Hard link count is wrong for %s: this may be a bug in your filesystem driver. Automatically turning on find's -noleaf option. Earlier results may have failed to include directories that should have been searched."), 1971 pathname); 1972 state.exit_status = 1; /* We know the result is wrong, now */ 1973 options.no_leaf_check = true; /* Don't make same 1974 mistake again */ 1975 subdirs_left = 1; /* band-aid for this iteration. */ 1976 } 1977 1978 /* Normal case optimization. On normal Unix 1979 filesystems, a directory that has no subdirectories 1980 has two links: its name, and ".". Any additional 1981 links are to the ".." entries of its subdirectories. 1982 Once we have processed as many subdirectories as 1983 there are additional links, we know that the rest of 1984 the entries are non-directories -- in other words, 1985 leaf files. */ 1986 subdirs_left -= process_path (cur_path, cur_name, 1987 subdirs_left == 0, pathname, 1988 mode); 1989 } 1990 else 1991 { 1992 /* There might be weird (e.g., CD-ROM or MS-DOS) filesystems 1993 mounted, which don't have Unix-like directory link counts. */ 1994 process_path (cur_path, cur_name, false, pathname, mode); 1995 } 1996 1997 state.curdepth--; 1998 } 1999 2000 2001 /* We're about to leave the directory. If there are any 2002 * -execdir argument lists which have been built but have not 2003 * yet been processed, do them now because they must be done in 2004 * the same directory. 2005 */ 2006 complete_pending_execdirs(eval_tree); 2007 2008 2009 if (strcmp (name, ".")) 2010 { 2011 enum SafeChdirStatus status; 2012 struct dir_id did; 2013 boolean did_stat = false; 2014 2015 /* We could go back and do the next command-line arg 2016 instead, maybe using longjmp. */ 2017 char const *dir; 2018 boolean deref = following_links() ? true : false; 2019 2020 if ( (state.curdepth>0) && !deref) 2021 dir = ".."; 2022 else 2023 { 2024 chdir_back (); 2025 dir = parent; 2026 } 2027 2028 status = safely_chdir (dir, TraversingUp, &stat_buf, SymlinkHandleDefault, &did_stat); 2029 switch (status) 2030 { 2031 case SafeChdirOK: 2032 break; 2033 2034 case SafeChdirFailWouldBeUnableToReturn: 2035 error (1, errno, "."); 2036 return; 2037 2038 case SafeChdirFailNonexistent: 2039 case SafeChdirFailDestUnreadable: 2040 case SafeChdirFailStat: 2041 case SafeChdirFailSymlink: 2042 case SafeChdirFailNotDir: 2043 case SafeChdirFailChdirFailed: 2044 error (1, errno, "%s", pathname); 2045 return; 2046 } 2047 2048 if (dir_curr > 0) 2049 { 2050 did.dev = dir_ids[dir_curr-1].dev; 2051 did.ino = dir_ids[dir_curr-1].ino; 2052 } 2053 else 2054 { 2055 did.dev = starting_stat_buf.st_dev; 2056 did.ino = starting_stat_buf.st_ino; 2057 } 2058 } 2059 2060 if (cur_path) 2061 free (cur_path); 2062 free_dirinfo(dirinfo); 2063 } 2064} 2065 2066/* Return true if there are no predicates with no_default_print in 2067 predicate list PRED, false if there are any. 2068 Returns true if default print should be performed */ 2069 2070static boolean 2071default_prints (struct predicate *pred) 2072{ 2073 while (pred != NULL) 2074 { 2075 if (pred->no_default_print) 2076 return (false); 2077 pred = pred->pred_next; 2078 } 2079 return (true); 2080} 2081