Use the new find program, and the new way of locating it (/ instead of )
[findutils.git] / find / find.c
blob38cb771cc72a3cc268d0d1123a0c28a6145b4f70
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;
165 options.literal_control_chars = false;
167 else
169 options.warnings = false;
170 options.literal_control_chars = false; /* may change */
174 predicates = NULL;
175 last_pred = NULL;
176 options.do_dir_first = true;
177 options.maxdepth = options.mindepth = -1;
178 options.start_time = time (NULL);
179 options.cur_day_start = options.start_time - DAYSECS;
180 options.full_days = false;
181 options.stay_on_filesystem = false;
182 options.ignore_readdir_race = false;
184 state.exit_status = 0;
186 #if defined(DEBUG_STAT)
187 options.xstat = debug_stat;
188 #endif /* !DEBUG_STAT */
190 if (getenv("POSIXLY_CORRECT"))
191 options.output_block_size = 512;
192 else
193 options.output_block_size = 1024;
195 if (getenv("FIND_BLOCK_SIZE"))
197 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"));
200 #if LEAF_OPTIMISATION
201 /* The leaf optimisation is enabled. */
202 options.no_leaf_check = false;
203 #else
204 /* The leaf optimisation is disabled. */
205 options.no_leaf_check = true;
206 #endif
208 set_follow_state(SYMLINK_NEVER_DEREF); /* The default is equivalent to -P. */
210 #ifdef DEBUG
211 fprintf (stderr, "cur_day_start = %s", ctime (&options.cur_day_start));
212 #endif /* DEBUG */
214 /* Check for -P, -H or -L options. */
215 for (i=1; (end_of_leading_options = i) < argc; ++i)
217 if (0 == strcmp("-H", argv[i]))
219 /* Meaning: dereference symbolic links on command line, but nowhere else. */
220 set_follow_state(SYMLINK_DEREF_ARGSONLY);
222 else if (0 == strcmp("-L", argv[i]))
224 /* Meaning: dereference all symbolic links. */
225 set_follow_state(SYMLINK_ALWAYS_DEREF);
227 else if (0 == strcmp("-P", argv[i]))
229 /* Meaning: never dereference symbolic links (default). */
230 set_follow_state(SYMLINK_NEVER_DEREF);
232 else if (0 == strcmp("--", argv[i]))
234 /* -- signifies the end of options. */
235 end_of_leading_options = i+1; /* Next time start with the next option */
236 break;
238 else
240 /* Hmm, must be one of
241 * (a) A path name
242 * (b) A predicate
244 end_of_leading_options = i; /* Next time start with this option */
245 break;
249 /* We are now processing the part of the "find" command line
250 * after the -H/-L options (if any).
253 /* fprintf(stderr, "rest: optind=%ld\n", (long)optind); */
255 /* Find where in ARGV the predicates begin. */
256 for (i = end_of_leading_options; i < argc && !looks_like_expression(argv[i]); i++)
258 /* fprintf(stderr, "Looks like %s is not a predicate\n", argv[i]); */
259 /* Do nothing. */ ;
262 /* Enclose the expression in `( ... )' so a default -print will
263 apply to the whole expression. */
264 entry_open = find_parser("(");
265 entry_close = find_parser(")");
266 entry_print = find_parser("print");
267 assert(entry_open != NULL);
268 assert(entry_close != NULL);
269 assert(entry_print != NULL);
271 parse_open (entry_open, argv, &argc);
272 parse_begin_user_args(argv, argc, last_pred, predicates);
273 pred_sanity_check(last_pred);
275 /* Build the input order list. */
276 while (i < argc)
278 if (!looks_like_expression(argv[i]))
280 if (0 != strcmp(argv[i], ")"))
282 error (0, 0, _("paths must precede expression: %s"), argv[i]);
283 usage(NULL);
287 predicate_name = argv[i];
288 parse_entry = find_parser (predicate_name);
289 if (parse_entry == NULL)
291 /* Command line option not recognized */
292 error (1, 0, _("invalid predicate `%s'"), predicate_name);
295 i++;
296 if (!(*(parse_entry->parser_func)) (parse_entry, argv, &i))
298 if (argv[i] == NULL)
299 /* Command line option requires an argument */
300 error (1, 0, _("missing argument to `%s'"), predicate_name);
301 else
302 error (1, 0, _("invalid argument `%s' to `%s'"),
303 argv[i], predicate_name);
306 pred_sanity_check(last_pred);
307 pred_sanity_check(predicates); /* XXX: expensive */
309 parse_end_user_args(argv, argc, last_pred, predicates);
311 if (predicates->pred_next == NULL)
313 /* No predicates that do something other than set a global variable
314 were given; remove the unneeded initial `(' and add `-print'. */
315 cur_pred = predicates;
316 predicates = last_pred = predicates->pred_next;
317 free ((char *) cur_pred);
318 parse_print (entry_print, argv, &argc);
319 pred_sanity_check(last_pred);
320 pred_sanity_check(predicates); /* XXX: expensive */
322 else if (!default_prints (predicates->pred_next))
324 /* One or more predicates that produce output were given;
325 remove the unneeded initial `('. */
326 cur_pred = predicates;
327 predicates = predicates->pred_next;
328 pred_sanity_check(predicates); /* XXX: expensive */
329 free ((char *) cur_pred);
331 else
333 /* `( user-supplied-expression ) -print'. */
334 parse_close (entry_close, argv, &argc);
335 pred_sanity_check(last_pred);
336 parse_print (entry_print, argv, &argc);
337 pred_sanity_check(last_pred);
338 pred_sanity_check(predicates); /* XXX: expensive */
341 #ifdef DEBUG
342 fprintf (stderr, "Predicate List:\n");
343 print_list (stderr, predicates);
344 #endif /* DEBUG */
346 /* do a sanity check */
347 pred_sanity_check(predicates);
349 /* Done parsing the predicates. Build the evaluation tree. */
350 cur_pred = predicates;
351 eval_tree = get_expr (&cur_pred, NO_PREC);
353 /* Check if we have any left-over predicates (this fixes
354 * Debian bug #185202).
356 if (cur_pred != NULL)
358 error (1, 0, _("unexpected extra predicate"));
361 #ifdef DEBUG
362 fprintf (stderr, "Eval Tree:\n");
363 print_tree (stderr, eval_tree, 0);
364 #endif /* DEBUG */
366 /* Rearrange the eval tree in optimal-predicate order. */
367 opt_expr (&eval_tree);
369 /* Determine the point, if any, at which to stat the file. */
370 mark_stat (eval_tree);
371 /* Determine the point, if any, at which to determine file type. */
372 mark_type (eval_tree);
374 #ifdef DEBUG
375 fprintf (stderr, "Optimized Eval Tree:\n");
376 print_tree (stderr, eval_tree, 0);
377 fprintf (stderr, "Optimized command line:\n");
378 print_optlist(stderr, eval_tree);
379 fprintf(stderr, "\n");
380 #endif /* DEBUG */
382 /* safely_chdir() needs to check that it has ended up in the right place.
383 * To avoid bailing out when something gets automounted, it checks if
384 * the target directory appears to have had a directory mounted on it as
385 * we chdir()ed. The problem with this is that in order to notice that
386 * a filesystem was mounted, we would need to lstat() all the mount points.
387 * That strategy loses if our machine is a client of a dead NFS server.
389 * Hence if safely_chdir() and wd_sanity_check() can manage without needing
390 * to know the mounted device list, we do that.
392 if (!options.open_nofollow_available)
394 #ifdef STAT_MOUNTPOINTS
395 init_mounted_dev_list(0);
396 #endif
400 starting_desc = open (".", O_RDONLY);
401 if (0 <= starting_desc && fchdir (starting_desc) != 0)
403 close (starting_desc);
404 starting_desc = -1;
406 if (starting_desc < 0)
408 starting_dir = xgetcwd ();
409 if (! starting_dir)
410 error (1, errno, _("cannot get current directory"));
412 if ((*options.xstat) (".", &starting_stat_buf) != 0)
413 error (1, errno, _("cannot get current directory"));
415 /* If no paths are given, default to ".". */
416 for (i = end_of_leading_options; i < argc && !looks_like_expression(argv[i]); i++)
418 process_top_path (argv[i], 0);
421 /* If there were no path arguments, default to ".". */
422 if (i == end_of_leading_options)
425 * We use a temporary variable here because some actions modify
426 * the path temporarily. Hence if we use a string constant,
427 * we get a coredump. The best example of this is if we say
428 * "find -printf %H" (note, not "find . -printf %H").
430 char defaultpath[2] = ".";
431 process_top_path (defaultpath, 0);
434 /* If "-exec ... {} +" has been used, there may be some
435 * partially-full command lines which have been built,
436 * but which are not yet complete. Execute those now.
438 cleanup();
439 return state.exit_status;
442 boolean is_fts_enabled()
444 /* this version of find (i.e. this main()) does not use fts. */
445 return false;
449 static char *
450 specific_dirname(const char *dir)
452 char dirbuf[1024];
454 if (0 == strcmp(".", dir))
456 /* OK, what's '.'? */
457 if (NULL != getcwd(dirbuf, sizeof(dirbuf)))
459 return strdup(dirbuf);
461 else
463 return strdup(dir);
466 else
468 char *result = canonicalize_filename_mode(dir, CAN_EXISTING);
469 if (NULL == result)
470 return strdup(dir);
471 else
472 return result;
478 /* Return non-zero if FS is the name of a filesystem that is likely to
479 * be automounted
481 static int
482 fs_likely_to_be_automounted(const char *fs)
484 return ( (0==strcmp(fs, "nfs")) || (0==strcmp(fs, "autofs")) || (0==strcmp(fs, "subfs")));
489 #ifdef STAT_MOUNTPOINTS
490 static dev_t *mounted_devices = NULL;
491 static size_t num_mounted_devices = 0u;
494 static void
495 init_mounted_dev_list(int mandatory)
497 assert(NULL == mounted_devices);
498 assert(0 == num_mounted_devices);
499 mounted_devices = get_mounted_devices(&num_mounted_devices);
500 if (mandatory && (NULL == mounted_devices))
502 error(1, 0, "Cannot read list of mounted devices.");
506 static void
507 refresh_mounted_dev_list(void)
509 if (mounted_devices)
511 free(mounted_devices);
512 mounted_devices = 0;
514 num_mounted_devices = 0u;
515 init_mounted_dev_list(1);
519 /* Search for device DEV in the array LIST, which is of size N. */
520 static int
521 dev_present(dev_t dev, const dev_t *list, size_t n)
523 if (list)
525 while (n-- > 0u)
527 if ( (*list++) == dev )
528 return 1;
531 return 0;
534 enum MountPointStateChange
536 MountPointRecentlyMounted,
537 MountPointRecentlyUnmounted,
538 MountPointStateUnchanged
543 static enum MountPointStateChange
544 get_mount_state(dev_t newdev)
546 int new_is_present, new_was_present;
548 new_was_present = dev_present(newdev, mounted_devices, num_mounted_devices);
549 refresh_mounted_dev_list();
550 new_is_present = dev_present(newdev, mounted_devices, num_mounted_devices);
552 if (new_was_present == new_is_present)
553 return MountPointStateUnchanged;
554 else if (new_is_present)
555 return MountPointRecentlyMounted;
556 else
557 return MountPointRecentlyUnmounted;
562 /* We stat()ed a directory, chdir()ed into it (we know this
563 * since direction is TraversingDown), stat()ed it again,
564 * and noticed that the device numbers are different. Check
565 * if the filesystem was recently mounted.
567 * If it was, it looks like chdir()ing into the directory
568 * caused a filesystem to be mounted. Maybe automount is
569 * running. Anyway, that's probably OK - but it happens
570 * only when we are moving downward.
572 * We also allow for the possibility that a similar thing
573 * has happened with the unmounting of a filesystem. This
574 * is much rarer, as it relies on an automounter timeout
575 * occurring at exactly the wrong moment.
577 static enum WdSanityCheckFatality
578 dirchange_is_fatal(const char *specific_what,
579 enum WdSanityCheckFatality isfatal,
580 int silent,
581 struct stat *newinfo)
583 enum MountPointStateChange transition = get_mount_state(newinfo->st_dev);
584 switch (transition)
586 case MountPointRecentlyUnmounted:
587 isfatal = NON_FATAL_IF_SANITY_CHECK_FAILS;
588 if (!silent)
590 error (0, 0,
591 _("Warning: filesystem %s has recently been unmounted."),
592 specific_what);
594 break;
596 case MountPointRecentlyMounted:
597 isfatal = NON_FATAL_IF_SANITY_CHECK_FAILS;
598 if (!silent)
600 error (0, 0,
601 _("Warning: filesystem %s has recently been mounted."),
602 specific_what);
604 break;
606 case MountPointStateUnchanged:
607 /* leave isfatal as it is */
608 break;
611 return isfatal;
615 #endif
619 /* Examine the results of the stat() of a directory from before we
620 * entered or left it, with the results of stat()ing it afterward. If
621 * these are different, the filesystem tree has been modified while we
622 * were traversing it. That might be an attempt to use a race
623 * condition to persuade find to do something it didn't intend
624 * (e.g. an attempt by an ordinary user to exploit the fact that root
625 * sometimes runs find on the whole filesystem). However, this can
626 * also happen if automount is running (certainly on Solaris). With
627 * automount, moving into a directory can cause a filesystem to be
628 * mounted there.
630 * To cope sensibly with this, we will raise an error if we see the
631 * device number change unless we are chdir()ing into a subdirectory,
632 * and the directory we moved into has been mounted or unmounted "recently".
633 * Here "recently" means since we started "find" or we last re-read
634 * the /etc/mnttab file.
636 * If the device number does not change but the inode does, that is a
637 * problem.
639 * If the device number and inode are both the same, we are happy.
641 * If a filesystem is (un)mounted as we chdir() into the directory, that
642 * may mean that we're now examining a section of the filesystem that might
643 * have been excluded from consideration (via -prune or -quit for example).
644 * Hence we print a warning message to indicate that the output of find
645 * might be inconsistent due to the change in the filesystem.
647 static boolean
648 wd_sanity_check(const char *thing_to_stat,
649 const char *progname,
650 const char *what,
651 dev_t old_dev,
652 ino_t old_ino,
653 struct stat *newinfo,
654 int parent,
655 int line_no,
656 enum TraversalDirection direction,
657 enum WdSanityCheckFatality isfatal,
658 boolean *changed) /* output parameter */
660 const char *fstype;
661 char *specific_what = NULL;
662 int silent = 0;
663 const char *current_dir = ".";
665 *changed = false;
667 if ((*options.xstat) (current_dir, newinfo) != 0)
668 error (1, errno, "%s", thing_to_stat);
670 if (old_dev != newinfo->st_dev)
672 *changed = true;
673 specific_what = specific_dirname(what);
674 fstype = filesystem_type(newinfo, current_dir);
675 silent = fs_likely_to_be_automounted(fstype);
677 /* This condition is rare, so once we are here it is
678 * reasonable to perform an expensive computation to
679 * determine if we should continue or fail.
681 if (TraversingDown == direction)
683 #ifdef STAT_MOUNTPOINTS
684 isfatal = dirchange_is_fatal(specific_what,isfatal,silent,newinfo);
685 #else
686 isfatal = RETRY_IF_SANITY_CHECK_FAILS;
687 #endif
690 switch (isfatal)
692 case FATAL_IF_SANITY_CHECK_FAILS:
694 fstype = filesystem_type(newinfo, current_dir);
695 error (1, 0,
696 _("%s%s changed during execution of %s (old device number %ld, new device number %ld, filesystem type is %s) [ref %ld]"),
697 specific_what,
698 parent ? "/.." : "",
699 progname,
700 (long) old_dev,
701 (long) newinfo->st_dev,
702 fstype,
703 line_no);
704 /*NOTREACHED*/
705 return false;
708 case NON_FATAL_IF_SANITY_CHECK_FAILS:
710 /* Since the device has changed under us, the inode number
711 * will almost certainly also be different. However, we have
712 * already decided that this is not a problem. Hence we return
713 * without checking the inode number.
715 free(specific_what);
716 return true;
719 case RETRY_IF_SANITY_CHECK_FAILS:
720 return false;
724 /* Device number was the same, check if the inode has changed. */
725 if (old_ino != newinfo->st_ino)
727 *changed = true;
728 specific_what = specific_dirname(what);
729 fstype = filesystem_type(newinfo, current_dir);
731 error ((isfatal == FATAL_IF_SANITY_CHECK_FAILS) ? 1 : 0,
732 0, /* no relevant errno value */
733 _("%s%s changed during execution of %s (old inode number %ld, new inode number %ld, filesystem type is %s) [ref %ld]"),
734 specific_what,
735 parent ? "/.." : "",
736 progname,
737 (long) old_ino,
738 (long) newinfo->st_ino,
739 fstype,
740 line_no);
741 free(specific_what);
742 return false;
745 return true;
748 enum SafeChdirStatus
750 SafeChdirOK,
751 SafeChdirFailSymlink,
752 SafeChdirFailNotDir,
753 SafeChdirFailStat,
754 SafeChdirFailWouldBeUnableToReturn,
755 SafeChdirFailChdirFailed,
756 SafeChdirFailNonexistent
759 /* Safely perform a change in directory. We do this by calling
760 * lstat() on the subdirectory, using chdir() to move into it, and
761 * then lstat()ing ".". We compare the results of the two stat calls
762 * to see if they are consistent. If not, we sound the alarm.
764 * If following_links() is true, we do follow symbolic links.
766 static enum SafeChdirStatus
767 safely_chdir_lstat(const char *dest,
768 enum TraversalDirection direction,
769 struct stat *statbuf_dest,
770 enum ChdirSymlinkHandling symlink_follow_option,
771 boolean *did_stat)
773 struct stat statbuf_arrived;
774 int rv, dotfd=-1;
775 int saved_errno; /* specific_dirname() changes errno. */
776 boolean rv_set = false;
777 boolean statflag = false;
778 int tries = 0;
779 enum WdSanityCheckFatality isfatal = RETRY_IF_SANITY_CHECK_FAILS;
781 saved_errno = errno = 0;
783 dotfd = open(".", O_RDONLY);
785 /* We jump back to here if wd_sanity_check()
786 * recoverably triggers an alert.
788 retry:
789 ++tries;
791 if (dotfd >= 0)
793 /* Stat the directory we're going to. */
794 if (0 == options.xstat(dest, statbuf_dest))
796 statflag = true;
798 #ifdef S_ISLNK
799 /* symlink_follow_option might be set to SymlinkFollowOk, which
800 * would allow us to chdir() into a symbolic link. This is
801 * only useful for the case where the directory we're
802 * chdir()ing into is the basename of a command line
803 * argument, for example where "foo/bar/baz" is specified on
804 * the command line. When -P is in effect (the default),
805 * baz will not be followed if it is a symlink, but if bar
806 * is a symlink, it _should_ be followed. Hence we need the
807 * ability to override the policy set by following_links().
809 if (!following_links() && S_ISLNK(statbuf_dest->st_mode))
811 /* We're not supposed to be following links, but this is
812 * a link. Check symlink_follow_option to see if we should
813 * make a special exception.
815 if (symlink_follow_option == SymlinkFollowOk)
817 /* We need to re-stat() the file so that the
818 * sanity check can pass.
820 if (0 != stat(dest, statbuf_dest))
822 rv = SafeChdirFailNonexistent;
823 rv_set = true;
824 saved_errno = errno;
825 goto fail;
827 statflag = true;
829 else
831 /* Not following symlinks, so the attempt to
832 * chdir() into a symlink should be prevented.
834 rv = SafeChdirFailSymlink;
835 rv_set = true;
836 saved_errno = 0; /* silence the error message */
837 goto fail;
840 #endif
841 #ifdef S_ISDIR
842 /* Although the immediately following chdir() would detect
843 * the fact that this is not a directory for us, this would
844 * result in an extra system call that fails. Anybody
845 * examining the system-call trace should ideally not be
846 * concerned that something is actually failing.
848 if (!S_ISDIR(statbuf_dest->st_mode))
850 rv = SafeChdirFailNotDir;
851 rv_set = true;
852 saved_errno = 0; /* silence the error message */
853 goto fail;
855 #endif
856 #ifdef DEBUG_STAT
857 fprintf(stderr, "safely_chdir(): chdir(\"%s\")\n", dest);
858 #endif
859 if (0 == chdir(dest))
861 /* check we ended up where we wanted to go */
862 boolean changed = false;
863 if (!wd_sanity_check(".", program_name, ".",
864 statbuf_dest->st_dev,
865 statbuf_dest->st_ino,
866 &statbuf_arrived,
867 0, __LINE__, direction,
868 isfatal,
869 &changed))
871 /* Only allow one failure. */
872 if (RETRY_IF_SANITY_CHECK_FAILS == isfatal)
874 if (0 == fchdir(dotfd))
876 isfatal = FATAL_IF_SANITY_CHECK_FAILS;
877 goto retry;
879 else
881 /* Failed to return to original directory,
882 * but we know that the current working
883 * directory is not the one that we intend
884 * to be in. Since fchdir() failed, we
885 * can't recover from this and so this error
886 * is fatal.
888 error(1, errno,
889 "failed to return to parent directory");
892 else
894 /* XXX: not sure what to use as an excuse here. */
895 rv = SafeChdirFailNonexistent;
896 rv_set = true;
897 saved_errno = 0;
898 goto fail;
902 close(dotfd);
903 return SafeChdirOK;
905 else
907 saved_errno = errno;
908 if (ENOENT == saved_errno)
910 rv = SafeChdirFailNonexistent;
911 rv_set = true;
912 if (options.ignore_readdir_race)
913 errno = 0; /* don't issue err msg */
915 else if (ENOTDIR == saved_errno)
917 /* This can happen if the we stat a directory,
918 * and then filesystem activity changes it into
919 * a non-directory.
921 saved_errno = 0; /* don't issue err msg */
922 rv = SafeChdirFailNotDir;
923 rv_set = true;
925 else
927 rv = SafeChdirFailChdirFailed;
928 rv_set = true;
930 goto fail;
933 else
935 saved_errno = errno;
936 rv = SafeChdirFailStat;
937 rv_set = true;
939 if ( (ENOENT == saved_errno) || (0 == state.curdepth))
940 saved_errno = 0; /* don't issue err msg */
941 goto fail;
944 else
946 /* We do not have read permissions on "." */
947 rv = SafeChdirFailWouldBeUnableToReturn;
948 rv_set = true;
949 goto fail;
952 /* This is the success path, so we clear errno. The caller probably
953 * won't be calling error() anyway.
955 saved_errno = 0;
957 /* We use the same exit path for success or failure.
958 * which has occurred is recorded in RV.
960 fail:
961 /* We do not call error() as this would result in a duplicate error
962 * message when the caller does the same thing.
964 if (saved_errno)
965 errno = saved_errno;
967 if (dotfd >= 0)
969 close(dotfd);
970 dotfd = -1;
973 *did_stat = statflag;
974 assert(rv_set);
975 return rv;
978 #if defined(O_NOFOLLOW)
979 /* Safely change working directory to the specified subdirectory. If
980 * we are not allowed to follow symbolic links, we use open() with
981 * O_NOFOLLOW, followed by fchdir(). This ensures that we don't
982 * follow symbolic links (of course, we do follow them if the -L
983 * option is in effect).
985 static enum SafeChdirStatus
986 safely_chdir_nofollow(const char *dest,
987 enum TraversalDirection direction,
988 struct stat *statbuf_dest,
989 enum ChdirSymlinkHandling symlink_follow_option,
990 boolean *did_stat)
992 int extraflags, fd;
993 extraflags = 0;
995 *did_stat = false;
997 switch (symlink_follow_option)
999 case SymlinkFollowOk:
1000 extraflags = 0;
1001 break;
1003 case SymlinkHandleDefault:
1004 if (following_links())
1005 extraflags = 0;
1006 else
1007 extraflags = O_NOFOLLOW;
1008 break;
1011 errno = 0;
1012 fd = open(dest, O_RDONLY|extraflags);
1013 if (fd < 0)
1015 switch (errno)
1017 case ELOOP:
1018 return SafeChdirFailSymlink; /* This is why we use O_NOFOLLOW */
1019 case ENOENT:
1020 return SafeChdirFailNonexistent;
1021 default:
1022 return SafeChdirFailChdirFailed;
1026 errno = 0;
1027 if (0 == fchdir(fd))
1029 close(fd);
1030 return SafeChdirOK;
1032 else
1034 int saved_errno = errno;
1035 close(fd);
1036 errno = saved_errno;
1038 switch (errno)
1040 case ENOTDIR:
1041 return SafeChdirFailNotDir;
1043 case EACCES:
1044 case EBADF: /* Shouldn't happen */
1045 case EINTR:
1046 case EIO:
1047 default:
1048 return SafeChdirFailChdirFailed;
1052 #endif
1054 static enum SafeChdirStatus
1055 safely_chdir(const char *dest,
1056 enum TraversalDirection direction,
1057 struct stat *statbuf_dest,
1058 enum ChdirSymlinkHandling symlink_follow_option,
1059 boolean *did_stat)
1061 /* We're about to leave a directory. If there are any -execdir
1062 * argument lists which have been built but have not yet been
1063 * processed, do them now because they must be done in the same
1064 * directory.
1066 complete_pending_execdirs(eval_tree);
1068 #if defined(O_NOFOLLOW)
1069 if (options.open_nofollow_available)
1070 return safely_chdir_nofollow(dest, direction, statbuf_dest, symlink_follow_option, did_stat);
1071 #endif
1072 return safely_chdir_lstat(dest, direction, statbuf_dest, symlink_follow_option, did_stat);
1077 /* Safely go back to the starting directory. */
1078 static void
1079 chdir_back (void)
1081 struct stat stat_buf;
1082 boolean dummy;
1084 if (starting_desc < 0)
1086 #ifdef DEBUG_STAT
1087 fprintf(stderr, "chdir_back(): chdir(\"%s\")\n", starting_dir);
1088 #endif
1090 #ifdef STAT_MOUNTPOINTS
1091 /* We will need the mounted device list. Get it now if we don't
1092 * already have it.
1094 if (NULL == mounted_devices)
1095 init_mounted_dev_list(1);
1096 #endif
1098 if (chdir (starting_dir) != 0)
1099 error (1, errno, "%s", starting_dir);
1101 wd_sanity_check(starting_dir,
1102 program_name,
1103 starting_dir,
1104 starting_stat_buf.st_dev,
1105 starting_stat_buf.st_ino,
1106 &stat_buf, 0, __LINE__,
1107 TraversingUp,
1108 FATAL_IF_SANITY_CHECK_FAILS,
1109 &dummy);
1111 else
1113 #ifdef DEBUG_STAT
1114 fprintf(stderr, "chdir_back(): chdir(<starting-point>)\n");
1115 #endif
1116 if (fchdir (starting_desc) != 0)
1117 error (1, errno, "%s", starting_dir);
1121 /* Move to the parent of a given directory and then call a function,
1122 * restoring the cwd. Don't bother changing directory if the
1123 * specified directory is a child of "." or is the root directory.
1125 static void
1126 at_top (char *pathname,
1127 mode_t mode,
1128 struct stat *pstat,
1129 void (*action)(char *pathname,
1130 char *basename,
1131 int mode,
1132 struct stat *pstat))
1134 int dirchange;
1135 char *parent_dir = dir_name(pathname);
1136 char *base = base_name(pathname);
1138 state.curdepth = 0;
1139 state.starting_path_length = strlen (pathname);
1141 if (0 == strcmp(pathname, parent_dir)
1142 || 0 == strcmp(parent_dir, "."))
1144 dirchange = 0;
1145 base = pathname;
1147 else
1149 enum TraversalDirection direction;
1150 enum SafeChdirStatus chdir_status;
1151 struct stat st;
1152 boolean did_stat = false;
1154 dirchange = 1;
1155 if (0 == strcmp(base, ".."))
1156 direction = TraversingUp;
1157 else
1158 direction = TraversingDown;
1160 /* We pass SymlinkFollowOk to safely_chdir(), which allows it to
1161 * chdir() into a symbolic link. This is only useful for the
1162 * case where the directory we're chdir()ing into is the
1163 * basename of a command line argument, for example where
1164 * "foo/bar/baz" is specified on the command line. When -P is
1165 * in effect (the default), baz will not be followed if it is a
1166 * symlink, but if bar is a symlink, it _should_ be followed.
1167 * Hence we need the ability to override the policy set by
1168 * following_links().
1170 chdir_status = safely_chdir(parent_dir, direction, &st, SymlinkFollowOk, &did_stat);
1171 if (SafeChdirOK != chdir_status)
1173 const char *what = (SafeChdirFailWouldBeUnableToReturn == chdir_status) ? "." : parent_dir;
1174 if (errno)
1175 error (0, errno, "%s", what);
1176 else
1177 error (0, 0, "Failed to safely change directory into `%s'",
1178 parent_dir);
1180 /* We can't process this command-line argument. */
1181 state.exit_status = 1;
1182 return;
1186 free (parent_dir);
1187 parent_dir = NULL;
1189 action(pathname, base, mode, pstat);
1191 if (dirchange)
1193 chdir_back();
1198 static void do_process_top_dir(char *pathname,
1199 char *base,
1200 int mode,
1201 struct stat *pstat)
1203 process_path (pathname, base, false, ".", mode);
1204 complete_pending_execdirs(eval_tree);
1207 static void do_process_predicate(char *pathname,
1208 char *base,
1209 int mode,
1210 struct stat *pstat)
1212 state.rel_pathname = base;
1213 apply_predicate (pathname, pstat, eval_tree);
1219 /* Descend PATHNAME, which is a command-line argument.
1221 Actions like -execdir assume that we are in the
1222 parent directory of the file we're examining,
1223 and on entry to this function our working directory
1224 is whatever it was when find was invoked. Therefore
1225 If PATHNAME is "." we just leave things as they are.
1226 Otherwise, we figure out what the parent directory is,
1227 and move to that.
1229 static void
1230 process_top_path (char *pathname, mode_t mode)
1232 at_top(pathname, mode, NULL, do_process_top_dir);
1236 /* Info on each directory in the current tree branch, to avoid
1237 getting stuck in symbolic link loops. */
1238 static struct dir_id *dir_ids = NULL;
1239 /* Entries allocated in `dir_ids'. */
1240 static int dir_alloc = 0;
1241 /* Index in `dir_ids' of directory currently being searched.
1242 This is always the last valid entry. */
1243 static int dir_curr = -1;
1244 /* (Arbitrary) number of entries to grow `dir_ids' by. */
1245 #define DIR_ALLOC_STEP 32
1249 /* We've detected a filesystem loop. This is caused by one of
1250 * two things:
1252 * 1. Option -L is in effect and we've hit a symbolic link that
1253 * points to an ancestor. This is harmless. We won't traverse the
1254 * symbolic link.
1256 * 2. We have hit a real cycle in the directory hierarchy. In this
1257 * case, we issue a diagnostic message (POSIX requires this) and we
1258 * skip that directory entry.
1260 static void
1261 issue_loop_warning(const char *name, const char *pathname, int level)
1263 struct stat stbuf_link;
1264 if (lstat(name, &stbuf_link) != 0)
1265 stbuf_link.st_mode = S_IFREG;
1267 if (S_ISLNK(stbuf_link.st_mode))
1269 error(0, 0,
1270 _("Symbolic link `%s' is part of a loop in the directory hierarchy; we have already visited the directory to which it points."),
1271 pathname);
1273 else
1275 int distance = 1 + (dir_curr-level);
1276 /* We have found an infinite loop. POSIX requires us to
1277 * issue a diagnostic. Usually we won't get to here
1278 * because when the leaf optimisation is on, it will cause
1279 * the subdirectory to be skipped. If /a/b/c/d is a hard
1280 * link to /a/b, then the link count of /a/b/c is 2,
1281 * because the ".." entry of /b/b/c/d points to /a, not
1282 * to /a/b/c.
1284 error(0, 0,
1285 _("Filesystem loop detected; `%s' has the same device number and inode as a directory which is %d %s."),
1286 pathname,
1287 distance,
1288 (distance == 1 ?
1289 _("level higher in the filesystem hierarchy") :
1290 _("levels higher in the filesystem hierarchy")));
1296 /* Recursively descend path PATHNAME, applying the predicates.
1297 LEAF is true if PATHNAME is known to be in a directory that has no
1298 more unexamined subdirectories, and therefore it is not a directory.
1299 Knowing this allows us to avoid calling stat as long as possible for
1300 leaf files.
1302 NAME is PATHNAME relative to the current directory. We access NAME
1303 but print PATHNAME.
1305 PARENT is the path of the parent of NAME, relative to find's
1306 starting directory.
1308 Return nonzero iff PATHNAME is a directory. */
1310 static int
1311 process_path (char *pathname, char *name, boolean leaf, char *parent,
1312 mode_t mode)
1314 struct stat stat_buf;
1315 static dev_t root_dev; /* Device ID of current argument pathname. */
1316 int i;
1318 /* Assume it is a non-directory initially. */
1319 stat_buf.st_mode = 0;
1320 state.rel_pathname = name;
1321 state.type = 0;
1322 state.have_stat = false;
1323 state.have_type = false;
1325 if (!digest_mode(mode, pathname, name, &stat_buf, leaf))
1326 return 0;
1328 if (!S_ISDIR (state.type))
1330 if (state.curdepth >= options.mindepth)
1331 apply_predicate (pathname, &stat_buf, eval_tree);
1332 return 0;
1335 /* From here on, we're working on a directory. */
1338 /* Now we really need to stat the directory, even if we know the
1339 * type, because we need information like struct stat.st_rdev.
1341 if (get_statinfo(pathname, name, &stat_buf) != 0)
1342 return 0;
1344 state.have_stat = true;
1345 mode = state.type = stat_buf.st_mode; /* use full info now that we have it. */
1346 state.stop_at_current_level =
1347 options.maxdepth >= 0
1348 && state.curdepth >= options.maxdepth;
1350 /* If we've already seen this directory on this branch,
1351 don't descend it again. */
1352 for (i = 0; i <= dir_curr; i++)
1353 if (stat_buf.st_ino == dir_ids[i].ino &&
1354 stat_buf.st_dev == dir_ids[i].dev)
1356 state.stop_at_current_level = true;
1357 issue_loop_warning(name, pathname, i);
1360 if (dir_alloc <= ++dir_curr)
1362 dir_alloc += DIR_ALLOC_STEP;
1363 dir_ids = (struct dir_id *)
1364 xrealloc ((char *) dir_ids, dir_alloc * sizeof (struct dir_id));
1366 dir_ids[dir_curr].ino = stat_buf.st_ino;
1367 dir_ids[dir_curr].dev = stat_buf.st_dev;
1369 if (options.stay_on_filesystem)
1371 if (state.curdepth == 0)
1372 root_dev = stat_buf.st_dev;
1373 else if (stat_buf.st_dev != root_dev)
1374 state.stop_at_current_level = true;
1377 if (options.do_dir_first && state.curdepth >= options.mindepth)
1378 apply_predicate (pathname, &stat_buf, eval_tree);
1380 #ifdef DEBUG
1381 fprintf(stderr, "pathname = %s, stop_at_current_level = %d\n",
1382 pathname, state.stop_at_current_level);
1383 #endif /* DEBUG */
1385 if (state.stop_at_current_level == false)
1386 /* Scan directory on disk. */
1387 process_dir (pathname, name, strlen (pathname), &stat_buf, parent);
1389 if (options.do_dir_first == false && state.curdepth >= options.mindepth)
1391 /* The fields in 'state' are now out of date. Correct them.
1393 if (!digest_mode(mode, pathname, name, &stat_buf, leaf))
1394 return 0;
1396 if (0 == dir_curr)
1398 at_top(pathname, mode, &stat_buf, do_process_predicate);
1400 else
1402 do_process_predicate(pathname, name, mode, &stat_buf);
1406 dir_curr--;
1408 return 1;
1412 /* Scan directory PATHNAME and recurse through process_path for each entry.
1414 PATHLEN is the length of PATHNAME.
1416 NAME is PATHNAME relative to the current directory.
1418 STATP is the results of *options.xstat on it.
1420 PARENT is the path of the parent of NAME, relative to find's
1421 starting directory. */
1423 static void
1424 process_dir (char *pathname, char *name, int pathlen, struct stat *statp, char *parent)
1426 int subdirs_left; /* Number of unexamined subdirs in PATHNAME. */
1427 boolean subdirs_unreliable; /* if true, cannot use dir link count as subdir limif (if false, it may STILL be unreliable) */
1428 int idx; /* Which entry are we on? */
1429 struct stat stat_buf;
1431 struct savedir_dirinfo *dirinfo;
1433 if (statp->st_nlink < 2)
1435 subdirs_unreliable = true;
1437 else
1439 subdirs_unreliable = false; /* not necessarily right */
1440 subdirs_left = statp->st_nlink - 2; /* Account for name and ".". */
1443 errno = 0;
1444 dirinfo = xsavedir(name, 0);
1447 if (dirinfo == NULL)
1449 assert(errno != 0);
1450 error (0, errno, "%s", pathname);
1451 state.exit_status = 1;
1453 else
1455 register char *namep; /* Current point in `name_space'. */
1456 char *cur_path; /* Full path of each file to process. */
1457 char *cur_name; /* Base name of each file to process. */
1458 unsigned cur_path_size; /* Bytes allocated for `cur_path'. */
1459 register unsigned file_len; /* Length of each path to process. */
1460 register unsigned pathname_len; /* PATHLEN plus trailing '/'. */
1461 boolean did_stat = false;
1463 if (pathname[pathlen - 1] == '/')
1464 pathname_len = pathlen + 1; /* For '\0'; already have '/'. */
1465 else
1466 pathname_len = pathlen + 2; /* For '/' and '\0'. */
1467 cur_path_size = 0;
1468 cur_path = NULL;
1470 /* We're about to leave the directory. If there are any
1471 * -execdir argument lists which have been built but have not
1472 * yet been processed, do them now because they must be done in
1473 * the same directory.
1475 complete_pending_execdirs(eval_tree);
1477 if (strcmp (name, "."))
1479 enum SafeChdirStatus status = safely_chdir (name, TraversingDown, &stat_buf, SymlinkHandleDefault, &did_stat);
1480 switch (status)
1482 case SafeChdirOK:
1483 /* If there had been a change but wd_sanity_check()
1484 * accepted it, we need to accept that on the
1485 * way back up as well, so modify our record
1486 * of what we think we should see later.
1487 * If there was no change, the assignments are a no-op.
1489 * However, before performing the assignment, we need to
1490 * check that we have the stat information. If O_NOFOLLOW
1491 * is available, safely_chdir() will not have needed to use
1492 * stat(), and so stat_buf will just contain random data.
1494 if (!did_stat)
1496 /* If there is a link we need to follow it. Hence
1497 * the direct call to stat() not through (options.xstat)
1499 if (0 != stat(".", &stat_buf))
1500 break; /* skip the assignment. */
1502 dir_ids[dir_curr].dev = stat_buf.st_dev;
1503 dir_ids[dir_curr].ino = stat_buf.st_ino;
1505 break;
1507 case SafeChdirFailWouldBeUnableToReturn:
1508 error (0, errno, ".");
1509 state.exit_status = 1;
1510 break;
1512 case SafeChdirFailNonexistent:
1513 case SafeChdirFailStat:
1514 case SafeChdirFailNotDir:
1515 case SafeChdirFailChdirFailed:
1516 error (0, errno, "%s", pathname);
1517 state.exit_status = 1;
1518 return;
1520 case SafeChdirFailSymlink:
1521 error (0, 0,
1522 _("warning: not following the symbolic link %s"),
1523 pathname);
1524 state.exit_status = 1;
1525 return;
1529 for (idx=0; idx < dirinfo->size; ++idx)
1531 /* savedirinfo() may return dirinfo=NULL if extended information
1532 * is not available.
1534 mode_t mode = (dirinfo->entries[idx].flags & SavedirHaveFileType) ?
1535 dirinfo->entries[idx].type_info : 0;
1536 namep = dirinfo->entries[idx].name;
1538 /* Append this directory entry's name to the path being searched. */
1539 file_len = pathname_len + strlen (namep);
1540 if (file_len > cur_path_size)
1542 while (file_len > cur_path_size)
1543 cur_path_size += 1024;
1544 if (cur_path)
1545 free (cur_path);
1546 cur_path = xmalloc (cur_path_size);
1547 strcpy (cur_path, pathname);
1548 cur_path[pathname_len - 2] = '/';
1550 cur_name = cur_path + pathname_len - 1;
1551 strcpy (cur_name, namep);
1553 state.curdepth++;
1554 if (!options.no_leaf_check && !subdirs_unreliable)
1556 if (mode && S_ISDIR(mode) && (subdirs_left == 0))
1558 /* This is a subdirectory, but the number of directories we
1559 * have found now exceeds the number we would expect given
1560 * the hard link count on the parent. This is likely to be
1561 * a bug in the filesystem driver (e.g. Linux's
1562 * /proc filesystem) or may just be a fact that the OS
1563 * doesn't really handle hard links with Unix semantics.
1564 * In the latter case, -noleaf should be used routinely.
1566 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."),
1567 parent);
1568 state.exit_status = 1; /* We know the result is wrong, now */
1569 options.no_leaf_check = true; /* Don't make same
1570 mistake again */
1571 subdirs_left = 1; /* band-aid for this iteration. */
1574 /* Normal case optimization. On normal Unix
1575 filesystems, a directory that has no subdirectories
1576 has two links: its name, and ".". Any additional
1577 links are to the ".." entries of its subdirectories.
1578 Once we have processed as many subdirectories as
1579 there are additional links, we know that the rest of
1580 the entries are non-directories -- in other words,
1581 leaf files. */
1582 subdirs_left -= process_path (cur_path, cur_name,
1583 subdirs_left == 0, pathname,
1584 mode);
1586 else
1588 /* There might be weird (e.g., CD-ROM or MS-DOS) filesystems
1589 mounted, which don't have Unix-like directory link counts. */
1590 process_path (cur_path, cur_name, false, pathname, mode);
1593 state.curdepth--;
1597 /* We're about to leave the directory. If there are any
1598 * -execdir argument lists which have been built but have not
1599 * yet been processed, do them now because they must be done in
1600 * the same directory.
1602 complete_pending_execdirs(eval_tree);
1605 if (strcmp (name, "."))
1607 enum SafeChdirStatus status;
1608 struct dir_id did;
1609 boolean did_stat = false;
1611 /* We could go back and do the next command-line arg
1612 instead, maybe using longjmp. */
1613 char const *dir;
1614 boolean deref = following_links() ? true : false;
1616 if ( (state.curdepth>0) && !deref)
1617 dir = "..";
1618 else
1620 chdir_back ();
1621 dir = parent;
1624 status = safely_chdir (dir, TraversingUp, &stat_buf, SymlinkHandleDefault, &did_stat);
1625 switch (status)
1627 case SafeChdirOK:
1628 break;
1630 case SafeChdirFailWouldBeUnableToReturn:
1631 error (1, errno, ".");
1632 return;
1634 case SafeChdirFailNonexistent:
1635 case SafeChdirFailStat:
1636 case SafeChdirFailSymlink:
1637 case SafeChdirFailNotDir:
1638 case SafeChdirFailChdirFailed:
1639 error (1, errno, "%s", pathname);
1640 return;
1643 if (dir_curr > 0)
1645 did.dev = dir_ids[dir_curr-1].dev;
1646 did.ino = dir_ids[dir_curr-1].ino;
1648 else
1650 did.dev = starting_stat_buf.st_dev;
1651 did.ino = starting_stat_buf.st_ino;
1655 if (cur_path)
1656 free (cur_path);
1657 free_dirinfo(dirinfo);