Findutils 4.3.x defaults to using the the FTS implementation of find.
[findutils.git] / find / find.c
blob36b789e2aa75e38deaf4bd4dbb3a993b276dfb24
1 /* find -- search for files in a directory hierarchy
2 Copyright (C) 1990, 91, 92, 93, 94, 2000,
3 2003, 2004, 2005 Free Software Foundation, Inc.
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 2, or (at your option)
8 any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18 USA.*/
20 /* GNU find was written by Eric Decker <cire@cisco.com>,
21 with enhancements by David MacKenzie <djm@gnu.org>,
22 Jay Plett <jay@silence.princeton.nj.us>,
23 and Tim Wood <axolotl!tim@toad.com>.
24 The idea for -print0 and xargs -0 came from
25 Dan Bernstein <brnstnd@kramden.acf.nyu.edu>.
26 Improvements have been made by James Youngman <jay@gnu.org>.
30 #include "defs.h"
32 #define USE_SAFE_CHDIR 1
33 #undef STAT_MOUNTPOINTS
36 #include <errno.h>
37 #include <assert.h>
40 #ifdef HAVE_FCNTL_H
41 #include <fcntl.h>
42 #else
43 #include <sys/file.h>
44 #endif
46 #include "../gnulib/lib/xalloc.h"
47 #include "../gnulib/lib/human.h"
48 #include "../gnulib/lib/canonicalize.h"
49 #include "closeout.h"
50 #include <modetype.h>
51 #include "savedirinfo.h"
52 #include "buildcmd.h"
53 #include "dirname.h"
54 #include "quote.h"
55 #include "quotearg.h"
57 #ifdef HAVE_LOCALE_H
58 #include <locale.h>
59 #endif
61 #if ENABLE_NLS
62 # include <libintl.h>
63 # define _(Text) gettext (Text)
64 #else
65 # define _(Text) Text
66 #define textdomain(Domain)
67 #define bindtextdomain(Package, Directory)
68 #endif
69 #ifdef gettext_noop
70 # define N_(String) gettext_noop (String)
71 #else
72 /* See locate.c for explanation as to why not use (String) */
73 # define N_(String) String
74 #endif
76 #define apply_predicate(pathname, stat_buf_ptr, node) \
77 (*(node)->pred_func)((pathname), (stat_buf_ptr), (node))
79 #ifdef STAT_MOUNTPOINTS
80 static void init_mounted_dev_list(int mandatory);
81 #endif
83 static void process_top_path PARAMS((char *pathname, mode_t mode));
84 static int process_path PARAMS((char *pathname, char *name, boolean leaf, char *parent, mode_t type));
85 static void process_dir PARAMS((char *pathname, char *name, int pathlen, struct stat *statp, char *parent));
89 /* Name this program was run with. */
90 char *program_name;
92 /* All predicates for each path to process. */
93 struct predicate *predicates;
95 /* The last predicate allocated. */
96 struct predicate *last_pred;
98 /* A file descriptor open to the initial working directory.
99 Doing it this way allows us to work when the i.w.d. has
100 unreadable parents. */
101 int starting_desc;
103 /* The stat buffer of the initial working directory. */
104 static struct stat starting_stat_buf;
106 enum ChdirSymlinkHandling
108 SymlinkHandleDefault, /* Normally the right choice */
109 SymlinkFollowOk /* see comment in process_top_path() */
113 enum TraversalDirection
115 TraversingUp,
116 TraversingDown
119 enum WdSanityCheckFatality
121 FATAL_IF_SANITY_CHECK_FAILS,
122 RETRY_IF_SANITY_CHECK_FAILS,
123 NON_FATAL_IF_SANITY_CHECK_FAILS
128 main (int argc, char **argv)
130 int i;
131 const struct parser_table *entry_close, *entry_print, *entry_open;
132 const struct parser_table *parse_entry; /* Pointer to the parsing table entry for this expression. */
133 struct predicate *cur_pred;
134 char *predicate_name; /* Name of predicate being parsed. */
135 int end_of_leading_options = 0; /* First arg after any -H/-L etc. */
138 program_name = argv[0];
140 /* We call check_nofollow() before setlocale() because the numbers
141 * for which we check (in the results of uname) definitiely have "."
142 * as the decimal point indicator even under locales for which that
143 * is not normally true. Hence atof() would do the wrong thing
144 * if we call it after setlocale().
146 #ifdef O_NOFOLLOW
147 options.open_nofollow_available = check_nofollow();
148 #else
149 options.open_nofollow_available = false;
150 #endif
152 options.regex_options = RE_SYNTAX_EMACS;
154 #ifdef HAVE_SETLOCALE
155 setlocale (LC_ALL, "");
156 #endif
157 bindtextdomain (PACKAGE, LOCALEDIR);
158 textdomain (PACKAGE);
159 atexit (close_stdout);
162 if (isatty(0))
164 options.warnings = true;
166 else
168 options.warnings = false;
172 predicates = NULL;
173 last_pred = NULL;
174 options.do_dir_first = true;
175 options.maxdepth = options.mindepth = -1;
176 options.start_time = time (NULL);
177 options.cur_day_start = options.start_time - DAYSECS;
178 options.full_days = false;
179 options.stay_on_filesystem = false;
180 options.ignore_readdir_race = false;
182 state.exit_status = 0;
184 #if defined(DEBUG_STAT)
185 options.xstat = debug_stat;
186 #endif /* !DEBUG_STAT */
188 if (getenv("POSIXLY_CORRECT"))
189 options.output_block_size = 512;
190 else
191 options.output_block_size = 1024;
193 if (getenv("FIND_BLOCK_SIZE"))
195 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"));
198 #if LEAF_OPTIMISATION
199 /* The leaf optimisation is enabled. */
200 options.no_leaf_check = false;
201 #else
202 /* The leaf optimisation is disabled. */
203 options.no_leaf_check = true;
204 #endif
206 set_follow_state(SYMLINK_NEVER_DEREF); /* The default is equivalent to -P. */
208 #ifdef DEBUG
209 fprintf (stderr, "cur_day_start = %s", ctime (&options.cur_day_start));
210 #endif /* DEBUG */
212 /* Check for -P, -H or -L options. */
213 for (i=1; (end_of_leading_options = i) < argc; ++i)
215 if (0 == strcmp("-H", argv[i]))
217 /* Meaning: dereference symbolic links on command line, but nowhere else. */
218 set_follow_state(SYMLINK_DEREF_ARGSONLY);
220 else if (0 == strcmp("-L", argv[i]))
222 /* Meaning: dereference all symbolic links. */
223 set_follow_state(SYMLINK_ALWAYS_DEREF);
225 else if (0 == strcmp("-P", argv[i]))
227 /* Meaning: never dereference symbolic links (default). */
228 set_follow_state(SYMLINK_NEVER_DEREF);
230 else if (0 == strcmp("--", argv[i]))
232 /* -- signifies the end of options. */
233 end_of_leading_options = i+1; /* Next time start with the next option */
234 break;
236 else
238 /* Hmm, must be one of
239 * (a) A path name
240 * (b) A predicate
242 end_of_leading_options = i; /* Next time start with this option */
243 break;
247 /* We are now processing the part of the "find" command line
248 * after the -H/-L options (if any).
251 /* fprintf(stderr, "rest: optind=%ld\n", (long)optind); */
253 /* Find where in ARGV the predicates begin. */
254 for (i = end_of_leading_options; i < argc && strchr ("-!(),", argv[i][0]) == NULL; i++)
256 /* fprintf(stderr, "Looks like %s is not a predicate\n", argv[i]); */
257 /* Do nothing. */ ;
260 /* Enclose the expression in `( ... )' so a default -print will
261 apply to the whole expression. */
262 entry_open = find_parser("(");
263 entry_close = find_parser(")");
264 entry_print = find_parser("print");
265 assert(entry_open != NULL);
266 assert(entry_close != NULL);
267 assert(entry_print != NULL);
269 parse_open (entry_open, argv, &argc);
270 parse_begin_user_args(argv, argc, last_pred, predicates);
271 pred_sanity_check(last_pred);
273 /* Build the input order list. */
274 while (i < argc)
276 if (strchr ("-!(),", argv[i][0]) == NULL)
277 usage (_("paths must precede expression"));
278 predicate_name = argv[i];
279 parse_entry = find_parser (predicate_name);
280 if (parse_entry == NULL)
282 /* Command line option not recognized */
283 error (1, 0, _("invalid predicate `%s'"), predicate_name);
286 i++;
287 if (!(*(parse_entry->parser_func)) (parse_entry, argv, &i))
289 if (argv[i] == NULL)
290 /* Command line option requires an argument */
291 error (1, 0, _("missing argument to `%s'"), predicate_name);
292 else
293 error (1, 0, _("invalid argument `%s' to `%s'"),
294 argv[i], predicate_name);
297 pred_sanity_check(last_pred);
298 pred_sanity_check(predicates); /* XXX: expensive */
300 parse_end_user_args(argv, argc, last_pred, predicates);
302 if (predicates->pred_next == NULL)
304 /* No predicates that do something other than set a global variable
305 were given; remove the unneeded initial `(' and add `-print'. */
306 cur_pred = predicates;
307 predicates = last_pred = predicates->pred_next;
308 free ((char *) cur_pred);
309 parse_print (entry_print, argv, &argc);
310 pred_sanity_check(last_pred);
311 pred_sanity_check(predicates); /* XXX: expensive */
313 else if (!default_prints (predicates->pred_next))
315 /* One or more predicates that produce output were given;
316 remove the unneeded initial `('. */
317 cur_pred = predicates;
318 predicates = predicates->pred_next;
319 pred_sanity_check(predicates); /* XXX: expensive */
320 free ((char *) cur_pred);
322 else
324 /* `( user-supplied-expression ) -print'. */
325 parse_close (entry_close, argv, &argc);
326 pred_sanity_check(last_pred);
327 parse_print (entry_print, argv, &argc);
328 pred_sanity_check(last_pred);
329 pred_sanity_check(predicates); /* XXX: expensive */
332 #ifdef DEBUG
333 fprintf (stderr, "Predicate List:\n");
334 print_list (stderr, predicates);
335 #endif /* DEBUG */
337 /* do a sanity check */
338 pred_sanity_check(predicates);
340 /* Done parsing the predicates. Build the evaluation tree. */
341 cur_pred = predicates;
342 eval_tree = get_expr (&cur_pred, NO_PREC);
344 /* Check if we have any left-over predicates (this fixes
345 * Debian bug #185202).
347 if (cur_pred != NULL)
349 error (1, 0, _("unexpected extra predicate"));
352 #ifdef DEBUG
353 fprintf (stderr, "Eval Tree:\n");
354 print_tree (stderr, eval_tree, 0);
355 #endif /* DEBUG */
357 /* Rearrange the eval tree in optimal-predicate order. */
358 opt_expr (&eval_tree);
360 /* Determine the point, if any, at which to stat the file. */
361 mark_stat (eval_tree);
362 /* Determine the point, if any, at which to determine file type. */
363 mark_type (eval_tree);
365 #ifdef DEBUG
366 fprintf (stderr, "Optimized Eval Tree:\n");
367 print_tree (stderr, eval_tree, 0);
368 fprintf (stderr, "Optimized command line:\n");
369 print_optlist(stderr, eval_tree);
370 fprintf(stderr, "\n");
371 #endif /* DEBUG */
373 /* safely_chdir() needs to check that it has ended up in the right place.
374 * To avoid bailing out when something gets automounted, it checks if
375 * the target directory appears to have had a directory mounted on it as
376 * we chdir()ed. The problem with this is that in order to notice that
377 * a filesystem was mounted, we would need to lstat() all the mount points.
378 * That strategy loses if our machine is a client of a dead NFS server.
380 * Hence if safely_chdir() and wd_sanity_check() can manage without needing
381 * to know the mounted device list, we do that.
383 if (!options.open_nofollow_available)
385 #ifdef STAT_MOUNTPOINTS
386 init_mounted_dev_list(0);
387 #endif
391 starting_desc = open (".", O_RDONLY);
392 if (0 <= starting_desc && fchdir (starting_desc) != 0)
394 close (starting_desc);
395 starting_desc = -1;
397 if (starting_desc < 0)
399 starting_dir = xgetcwd ();
400 if (! starting_dir)
401 error (1, errno, _("cannot get current directory"));
403 if ((*options.xstat) (".", &starting_stat_buf) != 0)
404 error (1, errno, _("cannot get current directory"));
406 /* If no paths are given, default to ".". */
407 for (i = end_of_leading_options; i < argc && strchr ("-!(),", argv[i][0]) == NULL; i++)
409 process_top_path (argv[i], 0);
412 /* If there were no path arguments, default to ".". */
413 if (i == end_of_leading_options)
416 * We use a temporary variable here because some actions modify
417 * the path temporarily. Hence if we use a string constant,
418 * we get a coredump. The best example of this is if we say
419 * "find -printf %H" (note, not "find . -printf %H").
421 char defaultpath[2] = ".";
422 process_top_path (defaultpath, 0);
425 /* If "-exec ... {} +" has been used, there may be some
426 * partially-full command lines which have been built,
427 * but which are not yet complete. Execute those now.
429 cleanup();
430 return state.exit_status;
433 boolean is_fts_enabled()
435 /* this version of find (i.e. this main()) does not use fts. */
436 return false;
440 static char *
441 specific_dirname(const char *dir)
443 char dirbuf[1024];
445 if (0 == strcmp(".", dir))
447 /* OK, what's '.'? */
448 if (NULL != getcwd(dirbuf, sizeof(dirbuf)))
450 return strdup(dirbuf);
452 else
454 return strdup(dir);
457 else
459 char *result = canonicalize_filename_mode(dir, CAN_EXISTING);
460 if (NULL == result)
461 return strdup(dir);
462 else
463 return result;
469 /* Return non-zero if FS is the name of a filesystem that is likely to
470 * be automounted
472 static int
473 fs_likely_to_be_automounted(const char *fs)
475 return ( (0==strcmp(fs, "nfs")) || (0==strcmp(fs, "autofs")) || (0==strcmp(fs, "subfs")));
480 #ifdef STAT_MOUNTPOINTS
481 static dev_t *mounted_devices = NULL;
482 static size_t num_mounted_devices = 0u;
485 static void
486 init_mounted_dev_list(int mandatory)
488 assert(NULL == mounted_devices);
489 assert(0 == num_mounted_devices);
490 mounted_devices = get_mounted_devices(&num_mounted_devices);
491 if (mandatory && (NULL == mounted_devices))
493 error(1, 0, "Cannot read list of mounted devices.");
497 static void
498 refresh_mounted_dev_list(void)
500 if (mounted_devices)
502 free(mounted_devices);
503 mounted_devices = 0;
505 num_mounted_devices = 0u;
506 init_mounted_dev_list(1);
510 /* Search for device DEV in the array LIST, which is of size N. */
511 static int
512 dev_present(dev_t dev, const dev_t *list, size_t n)
514 if (list)
516 while (n-- > 0u)
518 if ( (*list++) == dev )
519 return 1;
522 return 0;
525 enum MountPointStateChange
527 MountPointRecentlyMounted,
528 MountPointRecentlyUnmounted,
529 MountPointStateUnchanged
534 static enum MountPointStateChange
535 get_mount_state(dev_t newdev)
537 int new_is_present, new_was_present;
539 new_was_present = dev_present(newdev, mounted_devices, num_mounted_devices);
540 refresh_mounted_dev_list();
541 new_is_present = dev_present(newdev, mounted_devices, num_mounted_devices);
543 if (new_was_present == new_is_present)
544 return MountPointStateUnchanged;
545 else if (new_is_present)
546 return MountPointRecentlyMounted;
547 else
548 return MountPointRecentlyUnmounted;
553 /* We stat()ed a directory, chdir()ed into it (we know this
554 * since direction is TraversingDown), stat()ed it again,
555 * and noticed that the device numbers are different. Check
556 * if the filesystem was recently mounted.
558 * If it was, it looks like chdir()ing into the directory
559 * caused a filesystem to be mounted. Maybe automount is
560 * running. Anyway, that's probably OK - but it happens
561 * only when we are moving downward.
563 * We also allow for the possibility that a similar thing
564 * has happened with the unmounting of a filesystem. This
565 * is much rarer, as it relies on an automounter timeout
566 * occurring at exactly the wrong moment.
568 static enum WdSanityCheckFatality
569 dirchange_is_fatal(const char *specific_what,
570 enum WdSanityCheckFatality isfatal,
571 int silent,
572 struct stat *newinfo)
574 enum MountPointStateChange transition = get_mount_state(newinfo->st_dev);
575 switch (transition)
577 case MountPointRecentlyUnmounted:
578 isfatal = NON_FATAL_IF_SANITY_CHECK_FAILS;
579 if (!silent)
581 error (0, 0,
582 _("Warning: filesystem %s has recently been unmounted."),
583 specific_what);
585 break;
587 case MountPointRecentlyMounted:
588 isfatal = NON_FATAL_IF_SANITY_CHECK_FAILS;
589 if (!silent)
591 error (0, 0,
592 _("Warning: filesystem %s has recently been mounted."),
593 specific_what);
595 break;
597 case MountPointStateUnchanged:
598 /* leave isfatal as it is */
599 break;
602 return isfatal;
606 #endif
610 /* Examine the results of the stat() of a directory from before we
611 * entered or left it, with the results of stat()ing it afterward. If
612 * these are different, the filesystem tree has been modified while we
613 * were traversing it. That might be an attempt to use a race
614 * condition to persuade find to do something it didn't intend
615 * (e.g. an attempt by an ordinary user to exploit the fact that root
616 * sometimes runs find on the whole filesystem). However, this can
617 * also happen if automount is running (certainly on Solaris). With
618 * automount, moving into a directory can cause a filesystem to be
619 * mounted there.
621 * To cope sensibly with this, we will raise an error if we see the
622 * device number change unless we are chdir()ing into a subdirectory,
623 * and the directory we moved into has been mounted or unmounted "recently".
624 * Here "recently" means since we started "find" or we last re-read
625 * the /etc/mnttab file.
627 * If the device number does not change but the inode does, that is a
628 * problem.
630 * If the device number and inode are both the same, we are happy.
632 * If a filesystem is (un)mounted as we chdir() into the directory, that
633 * may mean that we're now examining a section of the filesystem that might
634 * have been excluded from consideration (via -prune or -quit for example).
635 * Hence we print a warning message to indicate that the output of find
636 * might be inconsistent due to the change in the filesystem.
638 static boolean
639 wd_sanity_check(const char *thing_to_stat,
640 const char *progname,
641 const char *what,
642 dev_t old_dev,
643 ino_t old_ino,
644 struct stat *newinfo,
645 int parent,
646 int line_no,
647 enum TraversalDirection direction,
648 enum WdSanityCheckFatality isfatal,
649 boolean *changed) /* output parameter */
651 const char *fstype;
652 char *specific_what = NULL;
653 int silent = 0;
654 const char *current_dir = ".";
656 *changed = false;
658 if ((*options.xstat) (current_dir, newinfo) != 0)
659 error (1, errno, "%s", thing_to_stat);
661 if (old_dev != newinfo->st_dev)
663 *changed = true;
664 specific_what = specific_dirname(what);
665 fstype = filesystem_type(newinfo, current_dir);
666 silent = fs_likely_to_be_automounted(fstype);
668 /* This condition is rare, so once we are here it is
669 * reasonable to perform an expensive computation to
670 * determine if we should continue or fail.
672 if (TraversingDown == direction)
674 #ifdef STAT_MOUNTPOINTS
675 isfatal = dirchange_is_fatal(specific_what,isfatal,silent,newinfo);
676 #else
677 isfatal = RETRY_IF_SANITY_CHECK_FAILS;
678 #endif
681 switch (isfatal)
683 case FATAL_IF_SANITY_CHECK_FAILS:
685 fstype = filesystem_type(newinfo, current_dir);
686 error (1, 0,
687 _("%s%s changed during execution of %s (old device number %ld, new device number %ld, filesystem type is %s) [ref %ld]"),
688 specific_what,
689 parent ? "/.." : "",
690 progname,
691 (long) old_dev,
692 (long) newinfo->st_dev,
693 fstype,
694 line_no);
695 /*NOTREACHED*/
696 return false;
699 case NON_FATAL_IF_SANITY_CHECK_FAILS:
701 /* Since the device has changed under us, the inode number
702 * will almost certainly also be different. However, we have
703 * already decided that this is not a problem. Hence we return
704 * without checking the inode number.
706 free(specific_what);
707 return true;
710 case RETRY_IF_SANITY_CHECK_FAILS:
711 return false;
715 /* Device number was the same, check if the inode has changed. */
716 if (old_ino != newinfo->st_ino)
718 *changed = true;
719 specific_what = specific_dirname(what);
720 fstype = filesystem_type(newinfo, current_dir);
722 error ((isfatal == FATAL_IF_SANITY_CHECK_FAILS) ? 1 : 0,
723 0, /* no relevant errno value */
724 _("%s%s changed during execution of %s (old inode number %ld, new inode number %ld, filesystem type is %s) [ref %ld]"),
725 specific_what,
726 parent ? "/.." : "",
727 progname,
728 (long) old_ino,
729 (long) newinfo->st_ino,
730 fstype,
731 line_no);
732 free(specific_what);
733 return false;
736 return true;
739 enum SafeChdirStatus
741 SafeChdirOK,
742 SafeChdirFailSymlink,
743 SafeChdirFailNotDir,
744 SafeChdirFailStat,
745 SafeChdirFailWouldBeUnableToReturn,
746 SafeChdirFailChdirFailed,
747 SafeChdirFailNonexistent
750 /* Safely perform a change in directory. We do this by calling
751 * lstat() on the subdirectory, using chdir() to move into it, and
752 * then lstat()ing ".". We compare the results of the two stat calls
753 * to see if they are consistent. If not, we sound the alarm.
755 * If following_links() is true, we do follow symbolic links.
757 static enum SafeChdirStatus
758 safely_chdir_lstat(const char *dest,
759 enum TraversalDirection direction,
760 struct stat *statbuf_dest,
761 enum ChdirSymlinkHandling symlink_follow_option,
762 boolean *did_stat)
764 struct stat statbuf_arrived;
765 int rv, dotfd=-1;
766 int saved_errno; /* specific_dirname() changes errno. */
767 boolean rv_set = false;
768 boolean statflag = false;
769 int tries = 0;
770 enum WdSanityCheckFatality isfatal = RETRY_IF_SANITY_CHECK_FAILS;
772 saved_errno = errno = 0;
774 dotfd = open(".", O_RDONLY);
776 /* We jump back to here if wd_sanity_check()
777 * recoverably triggers an alert.
779 retry:
780 ++tries;
782 if (dotfd >= 0)
784 /* Stat the directory we're going to. */
785 if (0 == options.xstat(dest, statbuf_dest))
787 statflag = true;
789 #ifdef S_ISLNK
790 /* symlink_follow_option might be set to SymlinkFollowOk, which
791 * would allow us to chdir() into a symbolic link. This is
792 * only useful for the case where the directory we're
793 * chdir()ing into is the basename of a command line
794 * argument, for example where "foo/bar/baz" is specified on
795 * the command line. When -P is in effect (the default),
796 * baz will not be followed if it is a symlink, but if bar
797 * is a symlink, it _should_ be followed. Hence we need the
798 * ability to override the policy set by following_links().
800 if (!following_links() && S_ISLNK(statbuf_dest->st_mode))
802 /* We're not supposed to be following links, but this is
803 * a link. Check symlink_follow_option to see if we should
804 * make a special exception.
806 if (symlink_follow_option == SymlinkFollowOk)
808 /* We need to re-stat() the file so that the
809 * sanity check can pass.
811 if (0 != stat(dest, statbuf_dest))
813 rv = SafeChdirFailNonexistent;
814 rv_set = true;
815 saved_errno = errno;
816 goto fail;
818 statflag = true;
820 else
822 /* Not following symlinks, so the attempt to
823 * chdir() into a symlink should be prevented.
825 rv = SafeChdirFailSymlink;
826 rv_set = true;
827 saved_errno = 0; /* silence the error message */
828 goto fail;
831 #endif
832 #ifdef S_ISDIR
833 /* Although the immediately following chdir() would detect
834 * the fact that this is not a directory for us, this would
835 * result in an extra system call that fails. Anybody
836 * examining the system-call trace should ideally not be
837 * concerned that something is actually failing.
839 if (!S_ISDIR(statbuf_dest->st_mode))
841 rv = SafeChdirFailNotDir;
842 rv_set = true;
843 saved_errno = 0; /* silence the error message */
844 goto fail;
846 #endif
847 #ifdef DEBUG_STAT
848 fprintf(stderr, "safely_chdir(): chdir(\"%s\")\n", dest);
849 #endif
850 if (0 == chdir(dest))
852 /* check we ended up where we wanted to go */
853 boolean changed = false;
854 if (!wd_sanity_check(".", program_name, ".",
855 statbuf_dest->st_dev,
856 statbuf_dest->st_ino,
857 &statbuf_arrived,
858 0, __LINE__, direction,
859 isfatal,
860 &changed))
862 /* Only allow one failure. */
863 if (RETRY_IF_SANITY_CHECK_FAILS == isfatal)
865 if (0 == fchdir(dotfd))
867 isfatal = FATAL_IF_SANITY_CHECK_FAILS;
868 goto retry;
870 else
872 /* Failed to return to original directory,
873 * but we know that the current working
874 * directory is not the one that we intend
875 * to be in. Since fchdir() failed, we
876 * can't recover from this and so this error
877 * is fatal.
879 error(1, errno,
880 "failed to return to parent directory");
883 else
885 /* XXX: not sure what to use as an excuse here. */
886 rv = SafeChdirFailNonexistent;
887 rv_set = true;
888 saved_errno = 0;
889 goto fail;
893 close(dotfd);
894 return SafeChdirOK;
896 else
898 saved_errno = errno;
899 if (ENOENT == saved_errno)
901 rv = SafeChdirFailNonexistent;
902 rv_set = true;
903 if (options.ignore_readdir_race)
904 errno = 0; /* don't issue err msg */
906 else if (ENOTDIR == saved_errno)
908 /* This can happen if the we stat a directory,
909 * and then filesystem activity changes it into
910 * a non-directory.
912 saved_errno = 0; /* don't issue err msg */
913 rv = SafeChdirFailNotDir;
914 rv_set = true;
916 else
918 rv = SafeChdirFailChdirFailed;
919 rv_set = true;
921 goto fail;
924 else
926 saved_errno = errno;
927 rv = SafeChdirFailStat;
928 rv_set = true;
930 if ( (ENOENT == saved_errno) || (0 == state.curdepth))
931 saved_errno = 0; /* don't issue err msg */
932 goto fail;
935 else
937 /* We do not have read permissions on "." */
938 rv = SafeChdirFailWouldBeUnableToReturn;
939 rv_set = true;
940 goto fail;
943 /* This is the success path, so we clear errno. The caller probably
944 * won't be calling error() anyway.
946 saved_errno = 0;
948 /* We use the same exit path for success or failure.
949 * which has occurred is recorded in RV.
951 fail:
952 /* We do not call error() as this would result in a duplicate error
953 * message when the caller does the same thing.
955 if (saved_errno)
956 errno = saved_errno;
958 if (dotfd >= 0)
960 close(dotfd);
961 dotfd = -1;
964 *did_stat = statflag;
965 assert(rv_set);
966 return rv;
969 #if defined(O_NOFOLLOW)
970 /* Safely change working directory to the specified subdirectory. If
971 * we are not allowed to follow symbolic links, we use open() with
972 * O_NOFOLLOW, followed by fchdir(). This ensures that we don't
973 * follow symbolic links (of course, we do follow them if the -L
974 * option is in effect).
976 static enum SafeChdirStatus
977 safely_chdir_nofollow(const char *dest,
978 enum TraversalDirection direction,
979 struct stat *statbuf_dest,
980 enum ChdirSymlinkHandling symlink_follow_option,
981 boolean *did_stat)
983 int extraflags, fd;
984 extraflags = 0;
986 *did_stat = false;
988 switch (symlink_follow_option)
990 case SymlinkFollowOk:
991 extraflags = 0;
992 break;
994 case SymlinkHandleDefault:
995 if (following_links())
996 extraflags = 0;
997 else
998 extraflags = O_NOFOLLOW;
999 break;
1002 errno = 0;
1003 fd = open(dest, O_RDONLY|extraflags);
1004 if (fd < 0)
1006 switch (errno)
1008 case ELOOP:
1009 return SafeChdirFailSymlink; /* This is why we use O_NOFOLLOW */
1010 case ENOENT:
1011 return SafeChdirFailNonexistent;
1012 default:
1013 return SafeChdirFailChdirFailed;
1017 errno = 0;
1018 if (0 == fchdir(fd))
1020 close(fd);
1021 return SafeChdirOK;
1023 else
1025 int saved_errno = errno;
1026 close(fd);
1027 errno = saved_errno;
1029 switch (errno)
1031 case ENOTDIR:
1032 return SafeChdirFailNotDir;
1034 case EACCES:
1035 case EBADF: /* Shouldn't happen */
1036 case EINTR:
1037 case EIO:
1038 default:
1039 return SafeChdirFailChdirFailed;
1043 #endif
1045 static enum SafeChdirStatus
1046 safely_chdir(const char *dest,
1047 enum TraversalDirection direction,
1048 struct stat *statbuf_dest,
1049 enum ChdirSymlinkHandling symlink_follow_option,
1050 boolean *did_stat)
1052 /* We're about to leave a directory. If there are any -execdir
1053 * argument lists which have been built but have not yet been
1054 * processed, do them now because they must be done in the same
1055 * directory.
1057 complete_pending_execdirs(eval_tree);
1059 #if defined(O_NOFOLLOW)
1060 if (options.open_nofollow_available)
1061 return safely_chdir_nofollow(dest, direction, statbuf_dest, symlink_follow_option, did_stat);
1062 #endif
1063 return safely_chdir_lstat(dest, direction, statbuf_dest, symlink_follow_option, did_stat);
1068 /* Safely go back to the starting directory. */
1069 static void
1070 chdir_back (void)
1072 struct stat stat_buf;
1073 boolean dummy;
1075 if (starting_desc < 0)
1077 #ifdef DEBUG_STAT
1078 fprintf(stderr, "chdir_back(): chdir(\"%s\")\n", starting_dir);
1079 #endif
1081 #ifdef STAT_MOUNTPOINTS
1082 /* We will need the mounted device list. Get it now if we don't
1083 * already have it.
1085 if (NULL == mounted_devices)
1086 init_mounted_dev_list(1);
1087 #endif
1089 if (chdir (starting_dir) != 0)
1090 error (1, errno, "%s", starting_dir);
1092 wd_sanity_check(starting_dir,
1093 program_name,
1094 starting_dir,
1095 starting_stat_buf.st_dev,
1096 starting_stat_buf.st_ino,
1097 &stat_buf, 0, __LINE__,
1098 TraversingUp,
1099 FATAL_IF_SANITY_CHECK_FAILS,
1100 &dummy);
1102 else
1104 #ifdef DEBUG_STAT
1105 fprintf(stderr, "chdir_back(): chdir(<starting-point>)\n");
1106 #endif
1107 if (fchdir (starting_desc) != 0)
1108 error (1, errno, "%s", starting_dir);
1112 /* Move to the parent of a given directory and then call a function,
1113 * restoring the cwd. Don't bother changing directory if the
1114 * specified directory is a child of "." or is the root directory.
1116 static void
1117 at_top (char *pathname,
1118 mode_t mode,
1119 struct stat *pstat,
1120 void (*action)(char *pathname,
1121 char *basename,
1122 int mode,
1123 struct stat *pstat))
1125 int dirchange;
1126 char *parent_dir = dir_name(pathname);
1127 char *base = base_name(pathname);
1129 state.curdepth = 0;
1130 state.starting_path_length = strlen (pathname);
1132 if (0 == strcmp(pathname, parent_dir)
1133 || 0 == strcmp(parent_dir, "."))
1135 dirchange = 0;
1136 base = pathname;
1138 else
1140 enum TraversalDirection direction;
1141 enum SafeChdirStatus chdir_status;
1142 struct stat st;
1143 boolean did_stat = false;
1145 dirchange = 1;
1146 if (0 == strcmp(base, ".."))
1147 direction = TraversingUp;
1148 else
1149 direction = TraversingDown;
1151 /* We pass SymlinkFollowOk to safely_chdir(), which allows it to
1152 * chdir() into a symbolic link. This is only useful for the
1153 * case where the directory we're chdir()ing into is the
1154 * basename of a command line argument, for example where
1155 * "foo/bar/baz" is specified on the command line. When -P is
1156 * in effect (the default), baz will not be followed if it is a
1157 * symlink, but if bar is a symlink, it _should_ be followed.
1158 * Hence we need the ability to override the policy set by
1159 * following_links().
1161 chdir_status = safely_chdir(parent_dir, direction, &st, SymlinkFollowOk, &did_stat);
1162 if (SafeChdirOK != chdir_status)
1164 const char *what = (SafeChdirFailWouldBeUnableToReturn == chdir_status) ? "." : parent_dir;
1165 if (errno)
1166 error (0, errno, "%s", what);
1167 else
1168 error (0, 0, "Failed to safely change directory into `%s'",
1169 parent_dir);
1171 /* We can't process this command-line argument. */
1172 state.exit_status = 1;
1173 return;
1177 free (parent_dir);
1178 parent_dir = NULL;
1180 action(pathname, base, mode, pstat);
1182 if (dirchange)
1184 chdir_back();
1189 static void do_process_top_dir(char *pathname,
1190 char *base,
1191 int mode,
1192 struct stat *pstat)
1194 process_path (pathname, base, false, ".", mode);
1195 complete_pending_execdirs(eval_tree);
1198 static void do_process_predicate(char *pathname,
1199 char *base,
1200 int mode,
1201 struct stat *pstat)
1203 state.rel_pathname = base;
1204 apply_predicate (pathname, pstat, eval_tree);
1210 /* Descend PATHNAME, which is a command-line argument.
1212 Actions like -execdir assume that we are in the
1213 parent directory of the file we're examining,
1214 and on entry to this function our working directory
1215 is whatever it was when find was invoked. Therefore
1216 If PATHNAME is "." we just leave things as they are.
1217 Otherwise, we figure out what the parent directory is,
1218 and move to that.
1220 static void
1221 process_top_path (char *pathname, mode_t mode)
1223 at_top(pathname, mode, NULL, do_process_top_dir);
1227 /* Info on each directory in the current tree branch, to avoid
1228 getting stuck in symbolic link loops. */
1229 static struct dir_id *dir_ids = NULL;
1230 /* Entries allocated in `dir_ids'. */
1231 static int dir_alloc = 0;
1232 /* Index in `dir_ids' of directory currently being searched.
1233 This is always the last valid entry. */
1234 static int dir_curr = -1;
1235 /* (Arbitrary) number of entries to grow `dir_ids' by. */
1236 #define DIR_ALLOC_STEP 32
1240 /* We've detected a filesystem loop. This is caused by one of
1241 * two things:
1243 * 1. Option -L is in effect and we've hit a symbolic link that
1244 * points to an ancestor. This is harmless. We won't traverse the
1245 * symbolic link.
1247 * 2. We have hit a real cycle in the directory hierarchy. In this
1248 * case, we issue a diagnostic message (POSIX requires this) and we
1249 * skip that directory entry.
1251 static void
1252 issue_loop_warning(const char *name, const char *pathname, int level)
1254 struct stat stbuf_link;
1255 if (lstat(name, &stbuf_link) != 0)
1256 stbuf_link.st_mode = S_IFREG;
1258 if (S_ISLNK(stbuf_link.st_mode))
1260 error(0, 0,
1261 _("Symbolic link `%s' is part of a loop in the directory hierarchy; we have already visited the directory to which it points."),
1262 pathname);
1264 else
1266 int distance = 1 + (dir_curr-level);
1267 /* We have found an infinite loop. POSIX requires us to
1268 * issue a diagnostic. Usually we won't get to here
1269 * because when the leaf optimisation is on, it will cause
1270 * the subdirectory to be skipped. If /a/b/c/d is a hard
1271 * link to /a/b, then the link count of /a/b/c is 2,
1272 * because the ".." entry of /b/b/c/d points to /a, not
1273 * to /a/b/c.
1275 error(0, 0,
1276 _("Filesystem loop detected; `%s' has the same device number and inode as a directory which is %d %s."),
1277 pathname,
1278 distance,
1279 (distance == 1 ?
1280 _("level higher in the filesystem hierarchy") :
1281 _("levels higher in the filesystem hierarchy")));
1287 /* Recursively descend path PATHNAME, applying the predicates.
1288 LEAF is true if PATHNAME is known to be in a directory that has no
1289 more unexamined subdirectories, and therefore it is not a directory.
1290 Knowing this allows us to avoid calling stat as long as possible for
1291 leaf files.
1293 NAME is PATHNAME relative to the current directory. We access NAME
1294 but print PATHNAME.
1296 PARENT is the path of the parent of NAME, relative to find's
1297 starting directory.
1299 Return nonzero iff PATHNAME is a directory. */
1301 static int
1302 process_path (char *pathname, char *name, boolean leaf, char *parent,
1303 mode_t mode)
1305 struct stat stat_buf;
1306 static dev_t root_dev; /* Device ID of current argument pathname. */
1307 int i;
1309 /* Assume it is a non-directory initially. */
1310 stat_buf.st_mode = 0;
1311 state.rel_pathname = name;
1312 state.type = 0;
1313 state.have_stat = false;
1314 state.have_type = false;
1316 if (!digest_mode(mode, pathname, name, &stat_buf, leaf))
1317 return 0;
1319 if (!S_ISDIR (state.type))
1321 if (state.curdepth >= options.mindepth)
1322 apply_predicate (pathname, &stat_buf, eval_tree);
1323 return 0;
1326 /* From here on, we're working on a directory. */
1329 /* Now we really need to stat the directory, even if we know the
1330 * type, because we need information like struct stat.st_rdev.
1332 if (get_statinfo(pathname, name, &stat_buf) != 0)
1333 return 0;
1335 state.have_stat = true;
1336 mode = state.type = stat_buf.st_mode; /* use full info now that we have it. */
1337 state.stop_at_current_level =
1338 options.maxdepth >= 0
1339 && state.curdepth >= options.maxdepth;
1341 /* If we've already seen this directory on this branch,
1342 don't descend it again. */
1343 for (i = 0; i <= dir_curr; i++)
1344 if (stat_buf.st_ino == dir_ids[i].ino &&
1345 stat_buf.st_dev == dir_ids[i].dev)
1347 state.stop_at_current_level = true;
1348 issue_loop_warning(name, pathname, i);
1351 if (dir_alloc <= ++dir_curr)
1353 dir_alloc += DIR_ALLOC_STEP;
1354 dir_ids = (struct dir_id *)
1355 xrealloc ((char *) dir_ids, dir_alloc * sizeof (struct dir_id));
1357 dir_ids[dir_curr].ino = stat_buf.st_ino;
1358 dir_ids[dir_curr].dev = stat_buf.st_dev;
1360 if (options.stay_on_filesystem)
1362 if (state.curdepth == 0)
1363 root_dev = stat_buf.st_dev;
1364 else if (stat_buf.st_dev != root_dev)
1365 state.stop_at_current_level = true;
1368 if (options.do_dir_first && state.curdepth >= options.mindepth)
1369 apply_predicate (pathname, &stat_buf, eval_tree);
1371 #ifdef DEBUG
1372 fprintf(stderr, "pathname = %s, stop_at_current_level = %d\n",
1373 pathname, state.stop_at_current_level);
1374 #endif /* DEBUG */
1376 if (state.stop_at_current_level == false)
1377 /* Scan directory on disk. */
1378 process_dir (pathname, name, strlen (pathname), &stat_buf, parent);
1380 if (options.do_dir_first == false && state.curdepth >= options.mindepth)
1382 /* The fields in 'state' are now out of date. Correct them.
1384 if (!digest_mode(mode, pathname, name, &stat_buf, leaf))
1385 return 0;
1387 if (0 == dir_curr)
1389 at_top(pathname, mode, &stat_buf, do_process_predicate);
1391 else
1393 do_process_predicate(pathname, name, mode, &stat_buf);
1397 dir_curr--;
1399 return 1;
1403 /* Scan directory PATHNAME and recurse through process_path for each entry.
1405 PATHLEN is the length of PATHNAME.
1407 NAME is PATHNAME relative to the current directory.
1409 STATP is the results of *options.xstat on it.
1411 PARENT is the path of the parent of NAME, relative to find's
1412 starting directory. */
1414 static void
1415 process_dir (char *pathname, char *name, int pathlen, struct stat *statp, char *parent)
1417 int subdirs_left; /* Number of unexamined subdirs in PATHNAME. */
1418 boolean subdirs_unreliable; /* if true, cannot use dir link count as subdir limif (if false, it may STILL be unreliable) */
1419 int idx; /* Which entry are we on? */
1420 struct stat stat_buf;
1422 struct savedir_dirinfo *dirinfo;
1424 if (statp->st_nlink < 2)
1426 subdirs_unreliable = true;
1428 else
1430 subdirs_unreliable = false; /* not necessarily right */
1431 subdirs_left = statp->st_nlink - 2; /* Account for name and ".". */
1434 errno = 0;
1435 dirinfo = xsavedir(name, 0);
1438 if (dirinfo == NULL)
1440 assert(errno != 0);
1441 error (0, errno, "%s", pathname);
1442 state.exit_status = 1;
1444 else
1446 register char *namep; /* Current point in `name_space'. */
1447 char *cur_path; /* Full path of each file to process. */
1448 char *cur_name; /* Base name of each file to process. */
1449 unsigned cur_path_size; /* Bytes allocated for `cur_path'. */
1450 register unsigned file_len; /* Length of each path to process. */
1451 register unsigned pathname_len; /* PATHLEN plus trailing '/'. */
1452 boolean did_stat = false;
1454 if (pathname[pathlen - 1] == '/')
1455 pathname_len = pathlen + 1; /* For '\0'; already have '/'. */
1456 else
1457 pathname_len = pathlen + 2; /* For '/' and '\0'. */
1458 cur_path_size = 0;
1459 cur_path = NULL;
1461 /* We're about to leave the directory. If there are any
1462 * -execdir argument lists which have been built but have not
1463 * yet been processed, do them now because they must be done in
1464 * the same directory.
1466 complete_pending_execdirs(eval_tree);
1468 if (strcmp (name, "."))
1470 enum SafeChdirStatus status = safely_chdir (name, TraversingDown, &stat_buf, SymlinkHandleDefault, &did_stat);
1471 switch (status)
1473 case SafeChdirOK:
1474 /* If there had been a change but wd_sanity_check()
1475 * accepted it, we need to accept that on the
1476 * way back up as well, so modify our record
1477 * of what we think we should see later.
1478 * If there was no change, the assignments are a no-op.
1480 * However, before performing the assignment, we need to
1481 * check that we have the stat information. If O_NOFOLLOW
1482 * is available, safely_chdir() will not have needed to use
1483 * stat(), and so stat_buf will just contain random data.
1485 if (!did_stat)
1487 /* If there is a link we need to follow it. Hence
1488 * the direct call to stat() not through (options.xstat)
1490 if (0 != stat(".", &stat_buf))
1491 break; /* skip the assignment. */
1493 dir_ids[dir_curr].dev = stat_buf.st_dev;
1494 dir_ids[dir_curr].ino = stat_buf.st_ino;
1496 break;
1498 case SafeChdirFailWouldBeUnableToReturn:
1499 error (0, errno, ".");
1500 state.exit_status = 1;
1501 break;
1503 case SafeChdirFailNonexistent:
1504 case SafeChdirFailStat:
1505 case SafeChdirFailNotDir:
1506 case SafeChdirFailChdirFailed:
1507 error (0, errno, "%s", pathname);
1508 state.exit_status = 1;
1509 return;
1511 case SafeChdirFailSymlink:
1512 error (0, 0,
1513 _("warning: not following the symbolic link %s"),
1514 pathname);
1515 state.exit_status = 1;
1516 return;
1520 for (idx=0; idx < dirinfo->size; ++idx)
1522 /* savedirinfo() may return dirinfo=NULL if extended information
1523 * is not available.
1525 mode_t mode = (dirinfo->entries[idx].flags & SavedirHaveFileType) ?
1526 dirinfo->entries[idx].type_info : 0;
1527 namep = dirinfo->entries[idx].name;
1529 /* Append this directory entry's name to the path being searched. */
1530 file_len = pathname_len + strlen (namep);
1531 if (file_len > cur_path_size)
1533 while (file_len > cur_path_size)
1534 cur_path_size += 1024;
1535 if (cur_path)
1536 free (cur_path);
1537 cur_path = xmalloc (cur_path_size);
1538 strcpy (cur_path, pathname);
1539 cur_path[pathname_len - 2] = '/';
1541 cur_name = cur_path + pathname_len - 1;
1542 strcpy (cur_name, namep);
1544 state.curdepth++;
1545 if (!options.no_leaf_check && !subdirs_unreliable)
1547 if (mode && S_ISDIR(mode) && (subdirs_left == 0))
1549 /* This is a subdirectory, but the number of directories we
1550 * have found now exceeds the number we would expect given
1551 * the hard link count on the parent. This is likely to be
1552 * a bug in the filesystem driver (e.g. Linux's
1553 * /proc filesystem) or may just be a fact that the OS
1554 * doesn't really handle hard links with Unix semantics.
1555 * In the latter case, -noleaf should be used routinely.
1557 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."),
1558 parent);
1559 state.exit_status = 1; /* We know the result is wrong, now */
1560 options.no_leaf_check = true; /* Don't make same
1561 mistake again */
1562 subdirs_left = 1; /* band-aid for this iteration. */
1565 /* Normal case optimization. On normal Unix
1566 filesystems, a directory that has no subdirectories
1567 has two links: its name, and ".". Any additional
1568 links are to the ".." entries of its subdirectories.
1569 Once we have processed as many subdirectories as
1570 there are additional links, we know that the rest of
1571 the entries are non-directories -- in other words,
1572 leaf files. */
1573 subdirs_left -= process_path (cur_path, cur_name,
1574 subdirs_left == 0, pathname,
1575 mode);
1577 else
1579 /* There might be weird (e.g., CD-ROM or MS-DOS) filesystems
1580 mounted, which don't have Unix-like directory link counts. */
1581 process_path (cur_path, cur_name, false, pathname, mode);
1584 state.curdepth--;
1588 /* We're about to leave the directory. If there are any
1589 * -execdir argument lists which have been built but have not
1590 * yet been processed, do them now because they must be done in
1591 * the same directory.
1593 complete_pending_execdirs(eval_tree);
1596 if (strcmp (name, "."))
1598 enum SafeChdirStatus status;
1599 struct dir_id did;
1600 boolean did_stat = false;
1602 /* We could go back and do the next command-line arg
1603 instead, maybe using longjmp. */
1604 char const *dir;
1605 boolean deref = following_links() ? true : false;
1607 if ( (state.curdepth>0) && !deref)
1608 dir = "..";
1609 else
1611 chdir_back ();
1612 dir = parent;
1615 status = safely_chdir (dir, TraversingUp, &stat_buf, SymlinkHandleDefault, &did_stat);
1616 switch (status)
1618 case SafeChdirOK:
1619 break;
1621 case SafeChdirFailWouldBeUnableToReturn:
1622 error (1, errno, ".");
1623 return;
1625 case SafeChdirFailNonexistent:
1626 case SafeChdirFailStat:
1627 case SafeChdirFailSymlink:
1628 case SafeChdirFailNotDir:
1629 case SafeChdirFailChdirFailed:
1630 error (1, errno, "%s", pathname);
1631 return;
1634 if (dir_curr > 0)
1636 did.dev = dir_ids[dir_curr-1].dev;
1637 did.ino = dir_ids[dir_curr-1].ino;
1639 else
1641 did.dev = starting_stat_buf.st_dev;
1642 did.ino = starting_stat_buf.st_ino;
1646 if (cur_path)
1647 free (cur_path);
1648 free_dirinfo(dirinfo);