Savannah bug #15195: more helpful error messages for 'find . ( )' or 'find . \!'
[findutils.git] / find / find.c
blobe1e46bf81775d4bf796c76caa59cbc9b9401bf9a
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], true); 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 predicates->artificial = true;
273 parse_begin_user_args(argv, argc, last_pred, predicates);
274 pred_sanity_check(last_pred);
276 /* Build the input order list. */
277 while (i < argc)
279 if (!looks_like_expression(argv[i], false))
281 error (0, 0, _("paths must precede expression: %s"), argv[i]);
282 usage(NULL);
285 predicate_name = argv[i];
286 parse_entry = find_parser (predicate_name);
287 if (parse_entry == NULL)
289 /* Command line option not recognized */
290 error (1, 0, _("invalid predicate `%s'"), predicate_name);
293 i++;
294 if (!(*(parse_entry->parser_func)) (parse_entry, argv, &i))
296 if (argv[i] == NULL)
297 /* Command line option requires an argument */
298 error (1, 0, _("missing argument to `%s'"), predicate_name);
299 else
300 error (1, 0, _("invalid argument `%s' to `%s'"),
301 argv[i], predicate_name);
303 else
305 last_pred->p_name = predicate_name;
308 pred_sanity_check(last_pred);
309 pred_sanity_check(predicates); /* XXX: expensive */
311 parse_end_user_args(argv, argc, last_pred, predicates);
313 if (predicates->pred_next == NULL)
315 /* No predicates that do something other than set a global variable
316 were given; remove the unneeded initial `(' and add `-print'. */
317 cur_pred = predicates;
318 predicates = last_pred = predicates->pred_next;
319 free ((char *) cur_pred);
320 parse_print (entry_print, argv, &argc);
321 last_pred->artificial = true;
322 pred_sanity_check(last_pred);
323 pred_sanity_check(predicates); /* XXX: expensive */
325 else if (!default_prints (predicates->pred_next))
327 /* One or more predicates that produce output were given;
328 remove the unneeded initial `('. */
329 cur_pred = predicates;
330 predicates = predicates->pred_next;
331 pred_sanity_check(predicates); /* XXX: expensive */
332 free ((char *) cur_pred);
334 else
336 /* `( user-supplied-expression ) -print'. */
337 parse_close (entry_close, argv, &argc);
338 last_pred->artificial = true;
339 pred_sanity_check(last_pred);
340 parse_print (entry_print, argv, &argc);
341 last_pred->artificial = true;
342 pred_sanity_check(last_pred);
343 pred_sanity_check(predicates); /* XXX: expensive */
346 #ifdef DEBUG
347 fprintf (stderr, "Predicate List:\n");
348 print_list (stderr, predicates);
349 #endif /* DEBUG */
351 /* do a sanity check */
352 pred_sanity_check(predicates);
354 /* Done parsing the predicates. Build the evaluation tree. */
355 cur_pred = predicates;
356 eval_tree = get_expr (&cur_pred, NO_PREC, NULL);
358 /* Check if we have any left-over predicates (this fixes
359 * Debian bug #185202).
361 if (cur_pred != NULL)
363 if (0 == strcmp(cur_pred->p_name, ")"))
364 error (1, 0, _("you have too many '%s'"), cur_pred->p_name);
365 else
366 error (1, 0, _("unexpected extra predicate '%s'"), cur_pred->p_name);
369 #ifdef DEBUG
370 fprintf (stderr, "Eval Tree:\n");
371 print_tree (stderr, eval_tree, 0);
372 #endif /* DEBUG */
374 /* Rearrange the eval tree in optimal-predicate order. */
375 opt_expr (&eval_tree);
377 /* Determine the point, if any, at which to stat the file. */
378 mark_stat (eval_tree);
379 /* Determine the point, if any, at which to determine file type. */
380 mark_type (eval_tree);
382 #ifdef DEBUG
383 fprintf (stderr, "Optimized Eval Tree:\n");
384 print_tree (stderr, eval_tree, 0);
385 fprintf (stderr, "Optimized command line:\n");
386 print_optlist(stderr, eval_tree);
387 fprintf(stderr, "\n");
388 #endif /* DEBUG */
390 /* safely_chdir() needs to check that it has ended up in the right place.
391 * To avoid bailing out when something gets automounted, it checks if
392 * the target directory appears to have had a directory mounted on it as
393 * we chdir()ed. The problem with this is that in order to notice that
394 * a filesystem was mounted, we would need to lstat() all the mount points.
395 * That strategy loses if our machine is a client of a dead NFS server.
397 * Hence if safely_chdir() and wd_sanity_check() can manage without needing
398 * to know the mounted device list, we do that.
400 if (!options.open_nofollow_available)
402 #ifdef STAT_MOUNTPOINTS
403 init_mounted_dev_list(0);
404 #endif
408 starting_desc = open (".", O_RDONLY);
409 if (0 <= starting_desc && fchdir (starting_desc) != 0)
411 close (starting_desc);
412 starting_desc = -1;
414 if (starting_desc < 0)
416 starting_dir = xgetcwd ();
417 if (! starting_dir)
418 error (1, errno, _("cannot get current directory"));
420 if ((*options.xstat) (".", &starting_stat_buf) != 0)
421 error (1, errno, _("cannot get current directory"));
423 /* If no paths are given, default to ".". */
424 for (i = end_of_leading_options; i < argc && !looks_like_expression(argv[i], true); i++)
426 process_top_path (argv[i], 0);
429 /* If there were no path arguments, default to ".". */
430 if (i == end_of_leading_options)
433 * We use a temporary variable here because some actions modify
434 * the path temporarily. Hence if we use a string constant,
435 * we get a coredump. The best example of this is if we say
436 * "find -printf %H" (note, not "find . -printf %H").
438 char defaultpath[2] = ".";
439 process_top_path (defaultpath, 0);
442 /* If "-exec ... {} +" has been used, there may be some
443 * partially-full command lines which have been built,
444 * but which are not yet complete. Execute those now.
446 cleanup();
447 return state.exit_status;
450 boolean is_fts_enabled()
452 /* this version of find (i.e. this main()) does not use fts. */
453 return false;
457 static char *
458 specific_dirname(const char *dir)
460 char dirbuf[1024];
462 if (0 == strcmp(".", dir))
464 /* OK, what's '.'? */
465 if (NULL != getcwd(dirbuf, sizeof(dirbuf)))
467 return strdup(dirbuf);
469 else
471 return strdup(dir);
474 else
476 char *result = canonicalize_filename_mode(dir, CAN_EXISTING);
477 if (NULL == result)
478 return strdup(dir);
479 else
480 return result;
486 /* Return non-zero if FS is the name of a filesystem that is likely to
487 * be automounted
489 static int
490 fs_likely_to_be_automounted(const char *fs)
492 return ( (0==strcmp(fs, "nfs")) || (0==strcmp(fs, "autofs")) || (0==strcmp(fs, "subfs")));
497 #ifdef STAT_MOUNTPOINTS
498 static dev_t *mounted_devices = NULL;
499 static size_t num_mounted_devices = 0u;
502 static void
503 init_mounted_dev_list(int mandatory)
505 assert(NULL == mounted_devices);
506 assert(0 == num_mounted_devices);
507 mounted_devices = get_mounted_devices(&num_mounted_devices);
508 if (mandatory && (NULL == mounted_devices))
510 error(1, 0, "Cannot read list of mounted devices.");
514 static void
515 refresh_mounted_dev_list(void)
517 if (mounted_devices)
519 free(mounted_devices);
520 mounted_devices = 0;
522 num_mounted_devices = 0u;
523 init_mounted_dev_list(1);
527 /* Search for device DEV in the array LIST, which is of size N. */
528 static int
529 dev_present(dev_t dev, const dev_t *list, size_t n)
531 if (list)
533 while (n-- > 0u)
535 if ( (*list++) == dev )
536 return 1;
539 return 0;
542 enum MountPointStateChange
544 MountPointRecentlyMounted,
545 MountPointRecentlyUnmounted,
546 MountPointStateUnchanged
551 static enum MountPointStateChange
552 get_mount_state(dev_t newdev)
554 int new_is_present, new_was_present;
556 new_was_present = dev_present(newdev, mounted_devices, num_mounted_devices);
557 refresh_mounted_dev_list();
558 new_is_present = dev_present(newdev, mounted_devices, num_mounted_devices);
560 if (new_was_present == new_is_present)
561 return MountPointStateUnchanged;
562 else if (new_is_present)
563 return MountPointRecentlyMounted;
564 else
565 return MountPointRecentlyUnmounted;
570 /* We stat()ed a directory, chdir()ed into it (we know this
571 * since direction is TraversingDown), stat()ed it again,
572 * and noticed that the device numbers are different. Check
573 * if the filesystem was recently mounted.
575 * If it was, it looks like chdir()ing into the directory
576 * caused a filesystem to be mounted. Maybe automount is
577 * running. Anyway, that's probably OK - but it happens
578 * only when we are moving downward.
580 * We also allow for the possibility that a similar thing
581 * has happened with the unmounting of a filesystem. This
582 * is much rarer, as it relies on an automounter timeout
583 * occurring at exactly the wrong moment.
585 static enum WdSanityCheckFatality
586 dirchange_is_fatal(const char *specific_what,
587 enum WdSanityCheckFatality isfatal,
588 int silent,
589 struct stat *newinfo)
591 enum MountPointStateChange transition = get_mount_state(newinfo->st_dev);
592 switch (transition)
594 case MountPointRecentlyUnmounted:
595 isfatal = NON_FATAL_IF_SANITY_CHECK_FAILS;
596 if (!silent)
598 error (0, 0,
599 _("Warning: filesystem %s has recently been unmounted."),
600 specific_what);
602 break;
604 case MountPointRecentlyMounted:
605 isfatal = NON_FATAL_IF_SANITY_CHECK_FAILS;
606 if (!silent)
608 error (0, 0,
609 _("Warning: filesystem %s has recently been mounted."),
610 specific_what);
612 break;
614 case MountPointStateUnchanged:
615 /* leave isfatal as it is */
616 break;
619 return isfatal;
623 #endif
627 /* Examine the results of the stat() of a directory from before we
628 * entered or left it, with the results of stat()ing it afterward. If
629 * these are different, the filesystem tree has been modified while we
630 * were traversing it. That might be an attempt to use a race
631 * condition to persuade find to do something it didn't intend
632 * (e.g. an attempt by an ordinary user to exploit the fact that root
633 * sometimes runs find on the whole filesystem). However, this can
634 * also happen if automount is running (certainly on Solaris). With
635 * automount, moving into a directory can cause a filesystem to be
636 * mounted there.
638 * To cope sensibly with this, we will raise an error if we see the
639 * device number change unless we are chdir()ing into a subdirectory,
640 * and the directory we moved into has been mounted or unmounted "recently".
641 * Here "recently" means since we started "find" or we last re-read
642 * the /etc/mnttab file.
644 * If the device number does not change but the inode does, that is a
645 * problem.
647 * If the device number and inode are both the same, we are happy.
649 * If a filesystem is (un)mounted as we chdir() into the directory, that
650 * may mean that we're now examining a section of the filesystem that might
651 * have been excluded from consideration (via -prune or -quit for example).
652 * Hence we print a warning message to indicate that the output of find
653 * might be inconsistent due to the change in the filesystem.
655 static boolean
656 wd_sanity_check(const char *thing_to_stat,
657 const char *progname,
658 const char *what,
659 dev_t old_dev,
660 ino_t old_ino,
661 struct stat *newinfo,
662 int parent,
663 int line_no,
664 enum TraversalDirection direction,
665 enum WdSanityCheckFatality isfatal,
666 boolean *changed) /* output parameter */
668 const char *fstype;
669 char *specific_what = NULL;
670 int silent = 0;
671 const char *current_dir = ".";
673 *changed = false;
675 if ((*options.xstat) (current_dir, newinfo) != 0)
676 error (1, errno, "%s", thing_to_stat);
678 if (old_dev != newinfo->st_dev)
680 *changed = true;
681 specific_what = specific_dirname(what);
682 fstype = filesystem_type(newinfo, current_dir);
683 silent = fs_likely_to_be_automounted(fstype);
685 /* This condition is rare, so once we are here it is
686 * reasonable to perform an expensive computation to
687 * determine if we should continue or fail.
689 if (TraversingDown == direction)
691 #ifdef STAT_MOUNTPOINTS
692 isfatal = dirchange_is_fatal(specific_what,isfatal,silent,newinfo);
693 #else
694 isfatal = RETRY_IF_SANITY_CHECK_FAILS;
695 #endif
698 switch (isfatal)
700 case FATAL_IF_SANITY_CHECK_FAILS:
702 fstype = filesystem_type(newinfo, current_dir);
703 error (1, 0,
704 _("%s%s changed during execution of %s (old device number %ld, new device number %ld, filesystem type is %s) [ref %ld]"),
705 specific_what,
706 parent ? "/.." : "",
707 progname,
708 (long) old_dev,
709 (long) newinfo->st_dev,
710 fstype,
711 line_no);
712 /*NOTREACHED*/
713 return false;
716 case NON_FATAL_IF_SANITY_CHECK_FAILS:
718 /* Since the device has changed under us, the inode number
719 * will almost certainly also be different. However, we have
720 * already decided that this is not a problem. Hence we return
721 * without checking the inode number.
723 free(specific_what);
724 return true;
727 case RETRY_IF_SANITY_CHECK_FAILS:
728 return false;
732 /* Device number was the same, check if the inode has changed. */
733 if (old_ino != newinfo->st_ino)
735 *changed = true;
736 specific_what = specific_dirname(what);
737 fstype = filesystem_type(newinfo, current_dir);
739 error ((isfatal == FATAL_IF_SANITY_CHECK_FAILS) ? 1 : 0,
740 0, /* no relevant errno value */
741 _("%s%s changed during execution of %s (old inode number %ld, new inode number %ld, filesystem type is %s) [ref %ld]"),
742 specific_what,
743 parent ? "/.." : "",
744 progname,
745 (long) old_ino,
746 (long) newinfo->st_ino,
747 fstype,
748 line_no);
749 free(specific_what);
750 return false;
753 return true;
756 enum SafeChdirStatus
758 SafeChdirOK,
759 SafeChdirFailSymlink,
760 SafeChdirFailNotDir,
761 SafeChdirFailStat,
762 SafeChdirFailWouldBeUnableToReturn,
763 SafeChdirFailChdirFailed,
764 SafeChdirFailNonexistent
767 /* Safely perform a change in directory. We do this by calling
768 * lstat() on the subdirectory, using chdir() to move into it, and
769 * then lstat()ing ".". We compare the results of the two stat calls
770 * to see if they are consistent. If not, we sound the alarm.
772 * If following_links() is true, we do follow symbolic links.
774 static enum SafeChdirStatus
775 safely_chdir_lstat(const char *dest,
776 enum TraversalDirection direction,
777 struct stat *statbuf_dest,
778 enum ChdirSymlinkHandling symlink_follow_option,
779 boolean *did_stat)
781 struct stat statbuf_arrived;
782 int rv, dotfd=-1;
783 int saved_errno; /* specific_dirname() changes errno. */
784 boolean rv_set = false;
785 boolean statflag = false;
786 int tries = 0;
787 enum WdSanityCheckFatality isfatal = RETRY_IF_SANITY_CHECK_FAILS;
789 saved_errno = errno = 0;
791 dotfd = open(".", O_RDONLY);
793 /* We jump back to here if wd_sanity_check()
794 * recoverably triggers an alert.
796 retry:
797 ++tries;
799 if (dotfd >= 0)
801 /* Stat the directory we're going to. */
802 if (0 == options.xstat(dest, statbuf_dest))
804 statflag = true;
806 #ifdef S_ISLNK
807 /* symlink_follow_option might be set to SymlinkFollowOk, which
808 * would allow us to chdir() into a symbolic link. This is
809 * only useful for the case where the directory we're
810 * chdir()ing into is the basename of a command line
811 * argument, for example where "foo/bar/baz" is specified on
812 * the command line. When -P is in effect (the default),
813 * baz will not be followed if it is a symlink, but if bar
814 * is a symlink, it _should_ be followed. Hence we need the
815 * ability to override the policy set by following_links().
817 if (!following_links() && S_ISLNK(statbuf_dest->st_mode))
819 /* We're not supposed to be following links, but this is
820 * a link. Check symlink_follow_option to see if we should
821 * make a special exception.
823 if (symlink_follow_option == SymlinkFollowOk)
825 /* We need to re-stat() the file so that the
826 * sanity check can pass.
828 if (0 != stat(dest, statbuf_dest))
830 rv = SafeChdirFailNonexistent;
831 rv_set = true;
832 saved_errno = errno;
833 goto fail;
835 statflag = true;
837 else
839 /* Not following symlinks, so the attempt to
840 * chdir() into a symlink should be prevented.
842 rv = SafeChdirFailSymlink;
843 rv_set = true;
844 saved_errno = 0; /* silence the error message */
845 goto fail;
848 #endif
849 #ifdef S_ISDIR
850 /* Although the immediately following chdir() would detect
851 * the fact that this is not a directory for us, this would
852 * result in an extra system call that fails. Anybody
853 * examining the system-call trace should ideally not be
854 * concerned that something is actually failing.
856 if (!S_ISDIR(statbuf_dest->st_mode))
858 rv = SafeChdirFailNotDir;
859 rv_set = true;
860 saved_errno = 0; /* silence the error message */
861 goto fail;
863 #endif
864 #ifdef DEBUG_STAT
865 fprintf(stderr, "safely_chdir(): chdir(\"%s\")\n", dest);
866 #endif
867 if (0 == chdir(dest))
869 /* check we ended up where we wanted to go */
870 boolean changed = false;
871 if (!wd_sanity_check(".", program_name, ".",
872 statbuf_dest->st_dev,
873 statbuf_dest->st_ino,
874 &statbuf_arrived,
875 0, __LINE__, direction,
876 isfatal,
877 &changed))
879 /* Only allow one failure. */
880 if (RETRY_IF_SANITY_CHECK_FAILS == isfatal)
882 if (0 == fchdir(dotfd))
884 isfatal = FATAL_IF_SANITY_CHECK_FAILS;
885 goto retry;
887 else
889 /* Failed to return to original directory,
890 * but we know that the current working
891 * directory is not the one that we intend
892 * to be in. Since fchdir() failed, we
893 * can't recover from this and so this error
894 * is fatal.
896 error(1, errno,
897 "failed to return to parent directory");
900 else
902 /* XXX: not sure what to use as an excuse here. */
903 rv = SafeChdirFailNonexistent;
904 rv_set = true;
905 saved_errno = 0;
906 goto fail;
910 close(dotfd);
911 return SafeChdirOK;
913 else
915 saved_errno = errno;
916 if (ENOENT == saved_errno)
918 rv = SafeChdirFailNonexistent;
919 rv_set = true;
920 if (options.ignore_readdir_race)
921 errno = 0; /* don't issue err msg */
923 else if (ENOTDIR == saved_errno)
925 /* This can happen if the we stat a directory,
926 * and then filesystem activity changes it into
927 * a non-directory.
929 saved_errno = 0; /* don't issue err msg */
930 rv = SafeChdirFailNotDir;
931 rv_set = true;
933 else
935 rv = SafeChdirFailChdirFailed;
936 rv_set = true;
938 goto fail;
941 else
943 saved_errno = errno;
944 rv = SafeChdirFailStat;
945 rv_set = true;
947 if ( (ENOENT == saved_errno) || (0 == state.curdepth))
948 saved_errno = 0; /* don't issue err msg */
949 goto fail;
952 else
954 /* We do not have read permissions on "." */
955 rv = SafeChdirFailWouldBeUnableToReturn;
956 rv_set = true;
957 goto fail;
960 /* This is the success path, so we clear errno. The caller probably
961 * won't be calling error() anyway.
963 saved_errno = 0;
965 /* We use the same exit path for success or failure.
966 * which has occurred is recorded in RV.
968 fail:
969 /* We do not call error() as this would result in a duplicate error
970 * message when the caller does the same thing.
972 if (saved_errno)
973 errno = saved_errno;
975 if (dotfd >= 0)
977 close(dotfd);
978 dotfd = -1;
981 *did_stat = statflag;
982 assert(rv_set);
983 return rv;
986 #if defined(O_NOFOLLOW)
987 /* Safely change working directory to the specified subdirectory. If
988 * we are not allowed to follow symbolic links, we use open() with
989 * O_NOFOLLOW, followed by fchdir(). This ensures that we don't
990 * follow symbolic links (of course, we do follow them if the -L
991 * option is in effect).
993 static enum SafeChdirStatus
994 safely_chdir_nofollow(const char *dest,
995 enum TraversalDirection direction,
996 struct stat *statbuf_dest,
997 enum ChdirSymlinkHandling symlink_follow_option,
998 boolean *did_stat)
1000 int extraflags, fd;
1001 extraflags = 0;
1003 *did_stat = false;
1005 switch (symlink_follow_option)
1007 case SymlinkFollowOk:
1008 extraflags = 0;
1009 break;
1011 case SymlinkHandleDefault:
1012 if (following_links())
1013 extraflags = 0;
1014 else
1015 extraflags = O_NOFOLLOW;
1016 break;
1019 errno = 0;
1020 fd = open(dest, O_RDONLY|extraflags);
1021 if (fd < 0)
1023 switch (errno)
1025 case ELOOP:
1026 return SafeChdirFailSymlink; /* This is why we use O_NOFOLLOW */
1027 case ENOENT:
1028 return SafeChdirFailNonexistent;
1029 default:
1030 return SafeChdirFailChdirFailed;
1034 errno = 0;
1035 if (0 == fchdir(fd))
1037 close(fd);
1038 return SafeChdirOK;
1040 else
1042 int saved_errno = errno;
1043 close(fd);
1044 errno = saved_errno;
1046 switch (errno)
1048 case ENOTDIR:
1049 return SafeChdirFailNotDir;
1051 case EACCES:
1052 case EBADF: /* Shouldn't happen */
1053 case EINTR:
1054 case EIO:
1055 default:
1056 return SafeChdirFailChdirFailed;
1060 #endif
1062 static enum SafeChdirStatus
1063 safely_chdir(const char *dest,
1064 enum TraversalDirection direction,
1065 struct stat *statbuf_dest,
1066 enum ChdirSymlinkHandling symlink_follow_option,
1067 boolean *did_stat)
1069 /* We're about to leave a directory. If there are any -execdir
1070 * argument lists which have been built but have not yet been
1071 * processed, do them now because they must be done in the same
1072 * directory.
1074 complete_pending_execdirs(eval_tree);
1076 #if defined(O_NOFOLLOW)
1077 if (options.open_nofollow_available)
1078 return safely_chdir_nofollow(dest, direction, statbuf_dest, symlink_follow_option, did_stat);
1079 #endif
1080 return safely_chdir_lstat(dest, direction, statbuf_dest, symlink_follow_option, did_stat);
1085 /* Safely go back to the starting directory. */
1086 static void
1087 chdir_back (void)
1089 struct stat stat_buf;
1090 boolean dummy;
1092 if (starting_desc < 0)
1094 #ifdef DEBUG_STAT
1095 fprintf(stderr, "chdir_back(): chdir(\"%s\")\n", starting_dir);
1096 #endif
1098 #ifdef STAT_MOUNTPOINTS
1099 /* We will need the mounted device list. Get it now if we don't
1100 * already have it.
1102 if (NULL == mounted_devices)
1103 init_mounted_dev_list(1);
1104 #endif
1106 if (chdir (starting_dir) != 0)
1107 error (1, errno, "%s", starting_dir);
1109 wd_sanity_check(starting_dir,
1110 program_name,
1111 starting_dir,
1112 starting_stat_buf.st_dev,
1113 starting_stat_buf.st_ino,
1114 &stat_buf, 0, __LINE__,
1115 TraversingUp,
1116 FATAL_IF_SANITY_CHECK_FAILS,
1117 &dummy);
1119 else
1121 #ifdef DEBUG_STAT
1122 fprintf(stderr, "chdir_back(): chdir(<starting-point>)\n");
1123 #endif
1124 if (fchdir (starting_desc) != 0)
1125 error (1, errno, "%s", starting_dir);
1129 /* Move to the parent of a given directory and then call a function,
1130 * restoring the cwd. Don't bother changing directory if the
1131 * specified directory is a child of "." or is the root directory.
1133 static void
1134 at_top (char *pathname,
1135 mode_t mode,
1136 struct stat *pstat,
1137 void (*action)(char *pathname,
1138 char *basename,
1139 int mode,
1140 struct stat *pstat))
1142 int dirchange;
1143 char *parent_dir = dir_name(pathname);
1144 char *base = base_name(pathname);
1146 state.curdepth = 0;
1147 state.starting_path_length = strlen (pathname);
1149 if (0 == strcmp(pathname, parent_dir)
1150 || 0 == strcmp(parent_dir, "."))
1152 dirchange = 0;
1153 base = pathname;
1155 else
1157 enum TraversalDirection direction;
1158 enum SafeChdirStatus chdir_status;
1159 struct stat st;
1160 boolean did_stat = false;
1162 dirchange = 1;
1163 if (0 == strcmp(base, ".."))
1164 direction = TraversingUp;
1165 else
1166 direction = TraversingDown;
1168 /* We pass SymlinkFollowOk to safely_chdir(), which allows it to
1169 * chdir() into a symbolic link. This is only useful for the
1170 * case where the directory we're chdir()ing into is the
1171 * basename of a command line argument, for example where
1172 * "foo/bar/baz" is specified on the command line. When -P is
1173 * in effect (the default), baz will not be followed if it is a
1174 * symlink, but if bar is a symlink, it _should_ be followed.
1175 * Hence we need the ability to override the policy set by
1176 * following_links().
1178 chdir_status = safely_chdir(parent_dir, direction, &st, SymlinkFollowOk, &did_stat);
1179 if (SafeChdirOK != chdir_status)
1181 const char *what = (SafeChdirFailWouldBeUnableToReturn == chdir_status) ? "." : parent_dir;
1182 if (errno)
1183 error (0, errno, "%s", what);
1184 else
1185 error (0, 0, "Failed to safely change directory into `%s'",
1186 parent_dir);
1188 /* We can't process this command-line argument. */
1189 state.exit_status = 1;
1190 return;
1194 free (parent_dir);
1195 parent_dir = NULL;
1197 action(pathname, base, mode, pstat);
1199 if (dirchange)
1201 chdir_back();
1206 static void do_process_top_dir(char *pathname,
1207 char *base,
1208 int mode,
1209 struct stat *pstat)
1211 process_path (pathname, base, false, ".", mode);
1212 complete_pending_execdirs(eval_tree);
1215 static void do_process_predicate(char *pathname,
1216 char *base,
1217 int mode,
1218 struct stat *pstat)
1220 state.rel_pathname = base;
1221 apply_predicate (pathname, pstat, eval_tree);
1227 /* Descend PATHNAME, which is a command-line argument.
1229 Actions like -execdir assume that we are in the
1230 parent directory of the file we're examining,
1231 and on entry to this function our working directory
1232 is whatever it was when find was invoked. Therefore
1233 If PATHNAME is "." we just leave things as they are.
1234 Otherwise, we figure out what the parent directory is,
1235 and move to that.
1237 static void
1238 process_top_path (char *pathname, mode_t mode)
1240 at_top(pathname, mode, NULL, do_process_top_dir);
1244 /* Info on each directory in the current tree branch, to avoid
1245 getting stuck in symbolic link loops. */
1246 static struct dir_id *dir_ids = NULL;
1247 /* Entries allocated in `dir_ids'. */
1248 static int dir_alloc = 0;
1249 /* Index in `dir_ids' of directory currently being searched.
1250 This is always the last valid entry. */
1251 static int dir_curr = -1;
1252 /* (Arbitrary) number of entries to grow `dir_ids' by. */
1253 #define DIR_ALLOC_STEP 32
1257 /* We've detected a filesystem loop. This is caused by one of
1258 * two things:
1260 * 1. Option -L is in effect and we've hit a symbolic link that
1261 * points to an ancestor. This is harmless. We won't traverse the
1262 * symbolic link.
1264 * 2. We have hit a real cycle in the directory hierarchy. In this
1265 * case, we issue a diagnostic message (POSIX requires this) and we
1266 * skip that directory entry.
1268 static void
1269 issue_loop_warning(const char *name, const char *pathname, int level)
1271 struct stat stbuf_link;
1272 if (lstat(name, &stbuf_link) != 0)
1273 stbuf_link.st_mode = S_IFREG;
1275 if (S_ISLNK(stbuf_link.st_mode))
1277 error(0, 0,
1278 _("Symbolic link `%s' is part of a loop in the directory hierarchy; we have already visited the directory to which it points."),
1279 pathname);
1281 else
1283 int distance = 1 + (dir_curr-level);
1284 /* We have found an infinite loop. POSIX requires us to
1285 * issue a diagnostic. Usually we won't get to here
1286 * because when the leaf optimisation is on, it will cause
1287 * the subdirectory to be skipped. If /a/b/c/d is a hard
1288 * link to /a/b, then the link count of /a/b/c is 2,
1289 * because the ".." entry of /b/b/c/d points to /a, not
1290 * to /a/b/c.
1292 error(0, 0,
1293 _("Filesystem loop detected; `%s' has the same device number and inode as a directory which is %d %s."),
1294 pathname,
1295 distance,
1296 (distance == 1 ?
1297 _("level higher in the filesystem hierarchy") :
1298 _("levels higher in the filesystem hierarchy")));
1304 /* Recursively descend path PATHNAME, applying the predicates.
1305 LEAF is true if PATHNAME is known to be in a directory that has no
1306 more unexamined subdirectories, and therefore it is not a directory.
1307 Knowing this allows us to avoid calling stat as long as possible for
1308 leaf files.
1310 NAME is PATHNAME relative to the current directory. We access NAME
1311 but print PATHNAME.
1313 PARENT is the path of the parent of NAME, relative to find's
1314 starting directory.
1316 Return nonzero iff PATHNAME is a directory. */
1318 static int
1319 process_path (char *pathname, char *name, boolean leaf, char *parent,
1320 mode_t mode)
1322 struct stat stat_buf;
1323 static dev_t root_dev; /* Device ID of current argument pathname. */
1324 int i;
1326 /* Assume it is a non-directory initially. */
1327 stat_buf.st_mode = 0;
1328 state.rel_pathname = name;
1329 state.type = 0;
1330 state.have_stat = false;
1331 state.have_type = false;
1333 if (!digest_mode(mode, pathname, name, &stat_buf, leaf))
1334 return 0;
1336 if (!S_ISDIR (state.type))
1338 if (state.curdepth >= options.mindepth)
1339 apply_predicate (pathname, &stat_buf, eval_tree);
1340 return 0;
1343 /* From here on, we're working on a directory. */
1346 /* Now we really need to stat the directory, even if we know the
1347 * type, because we need information like struct stat.st_rdev.
1349 if (get_statinfo(pathname, name, &stat_buf) != 0)
1350 return 0;
1352 state.have_stat = true;
1353 mode = state.type = stat_buf.st_mode; /* use full info now that we have it. */
1354 state.stop_at_current_level =
1355 options.maxdepth >= 0
1356 && state.curdepth >= options.maxdepth;
1358 /* If we've already seen this directory on this branch,
1359 don't descend it again. */
1360 for (i = 0; i <= dir_curr; i++)
1361 if (stat_buf.st_ino == dir_ids[i].ino &&
1362 stat_buf.st_dev == dir_ids[i].dev)
1364 state.stop_at_current_level = true;
1365 issue_loop_warning(name, pathname, i);
1368 if (dir_alloc <= ++dir_curr)
1370 dir_alloc += DIR_ALLOC_STEP;
1371 dir_ids = (struct dir_id *)
1372 xrealloc ((char *) dir_ids, dir_alloc * sizeof (struct dir_id));
1374 dir_ids[dir_curr].ino = stat_buf.st_ino;
1375 dir_ids[dir_curr].dev = stat_buf.st_dev;
1377 if (options.stay_on_filesystem)
1379 if (state.curdepth == 0)
1380 root_dev = stat_buf.st_dev;
1381 else if (stat_buf.st_dev != root_dev)
1382 state.stop_at_current_level = true;
1385 if (options.do_dir_first && state.curdepth >= options.mindepth)
1386 apply_predicate (pathname, &stat_buf, eval_tree);
1388 #ifdef DEBUG
1389 fprintf(stderr, "pathname = %s, stop_at_current_level = %d\n",
1390 pathname, state.stop_at_current_level);
1391 #endif /* DEBUG */
1393 if (state.stop_at_current_level == false)
1394 /* Scan directory on disk. */
1395 process_dir (pathname, name, strlen (pathname), &stat_buf, parent);
1397 if (options.do_dir_first == false && state.curdepth >= options.mindepth)
1399 /* The fields in 'state' are now out of date. Correct them.
1401 if (!digest_mode(mode, pathname, name, &stat_buf, leaf))
1402 return 0;
1404 if (0 == dir_curr)
1406 at_top(pathname, mode, &stat_buf, do_process_predicate);
1408 else
1410 do_process_predicate(pathname, name, mode, &stat_buf);
1414 dir_curr--;
1416 return 1;
1420 /* Scan directory PATHNAME and recurse through process_path for each entry.
1422 PATHLEN is the length of PATHNAME.
1424 NAME is PATHNAME relative to the current directory.
1426 STATP is the results of *options.xstat on it.
1428 PARENT is the path of the parent of NAME, relative to find's
1429 starting directory. */
1431 static void
1432 process_dir (char *pathname, char *name, int pathlen, struct stat *statp, char *parent)
1434 int subdirs_left; /* Number of unexamined subdirs in PATHNAME. */
1435 boolean subdirs_unreliable; /* if true, cannot use dir link count as subdir limif (if false, it may STILL be unreliable) */
1436 int idx; /* Which entry are we on? */
1437 struct stat stat_buf;
1439 struct savedir_dirinfo *dirinfo;
1441 if (statp->st_nlink < 2)
1443 subdirs_unreliable = true;
1445 else
1447 subdirs_unreliable = false; /* not necessarily right */
1448 subdirs_left = statp->st_nlink - 2; /* Account for name and ".". */
1451 errno = 0;
1452 dirinfo = xsavedir(name, 0);
1455 if (dirinfo == NULL)
1457 assert(errno != 0);
1458 error (0, errno, "%s", pathname);
1459 state.exit_status = 1;
1461 else
1463 register char *namep; /* Current point in `name_space'. */
1464 char *cur_path; /* Full path of each file to process. */
1465 char *cur_name; /* Base name of each file to process. */
1466 unsigned cur_path_size; /* Bytes allocated for `cur_path'. */
1467 register unsigned file_len; /* Length of each path to process. */
1468 register unsigned pathname_len; /* PATHLEN plus trailing '/'. */
1469 boolean did_stat = false;
1471 if (pathname[pathlen - 1] == '/')
1472 pathname_len = pathlen + 1; /* For '\0'; already have '/'. */
1473 else
1474 pathname_len = pathlen + 2; /* For '/' and '\0'. */
1475 cur_path_size = 0;
1476 cur_path = NULL;
1478 /* We're about to leave the directory. If there are any
1479 * -execdir argument lists which have been built but have not
1480 * yet been processed, do them now because they must be done in
1481 * the same directory.
1483 complete_pending_execdirs(eval_tree);
1485 if (strcmp (name, "."))
1487 enum SafeChdirStatus status = safely_chdir (name, TraversingDown, &stat_buf, SymlinkHandleDefault, &did_stat);
1488 switch (status)
1490 case SafeChdirOK:
1491 /* If there had been a change but wd_sanity_check()
1492 * accepted it, we need to accept that on the
1493 * way back up as well, so modify our record
1494 * of what we think we should see later.
1495 * If there was no change, the assignments are a no-op.
1497 * However, before performing the assignment, we need to
1498 * check that we have the stat information. If O_NOFOLLOW
1499 * is available, safely_chdir() will not have needed to use
1500 * stat(), and so stat_buf will just contain random data.
1502 if (!did_stat)
1504 /* If there is a link we need to follow it. Hence
1505 * the direct call to stat() not through (options.xstat)
1507 if (0 != stat(".", &stat_buf))
1508 break; /* skip the assignment. */
1510 dir_ids[dir_curr].dev = stat_buf.st_dev;
1511 dir_ids[dir_curr].ino = stat_buf.st_ino;
1513 break;
1515 case SafeChdirFailWouldBeUnableToReturn:
1516 error (0, errno, ".");
1517 state.exit_status = 1;
1518 break;
1520 case SafeChdirFailNonexistent:
1521 case SafeChdirFailStat:
1522 case SafeChdirFailNotDir:
1523 case SafeChdirFailChdirFailed:
1524 error (0, errno, "%s", pathname);
1525 state.exit_status = 1;
1526 return;
1528 case SafeChdirFailSymlink:
1529 error (0, 0,
1530 _("warning: not following the symbolic link %s"),
1531 pathname);
1532 state.exit_status = 1;
1533 return;
1537 for (idx=0; idx < dirinfo->size; ++idx)
1539 /* savedirinfo() may return dirinfo=NULL if extended information
1540 * is not available.
1542 mode_t mode = (dirinfo->entries[idx].flags & SavedirHaveFileType) ?
1543 dirinfo->entries[idx].type_info : 0;
1544 namep = dirinfo->entries[idx].name;
1546 /* Append this directory entry's name to the path being searched. */
1547 file_len = pathname_len + strlen (namep);
1548 if (file_len > cur_path_size)
1550 while (file_len > cur_path_size)
1551 cur_path_size += 1024;
1552 if (cur_path)
1553 free (cur_path);
1554 cur_path = xmalloc (cur_path_size);
1555 strcpy (cur_path, pathname);
1556 cur_path[pathname_len - 2] = '/';
1558 cur_name = cur_path + pathname_len - 1;
1559 strcpy (cur_name, namep);
1561 state.curdepth++;
1562 if (!options.no_leaf_check && !subdirs_unreliable)
1564 if (mode && S_ISDIR(mode) && (subdirs_left == 0))
1566 /* This is a subdirectory, but the number of directories we
1567 * have found now exceeds the number we would expect given
1568 * the hard link count on the parent. This is likely to be
1569 * a bug in the filesystem driver (e.g. Linux's
1570 * /proc filesystem) or may just be a fact that the OS
1571 * doesn't really handle hard links with Unix semantics.
1572 * In the latter case, -noleaf should be used routinely.
1574 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."),
1575 parent);
1576 state.exit_status = 1; /* We know the result is wrong, now */
1577 options.no_leaf_check = true; /* Don't make same
1578 mistake again */
1579 subdirs_left = 1; /* band-aid for this iteration. */
1582 /* Normal case optimization. On normal Unix
1583 filesystems, a directory that has no subdirectories
1584 has two links: its name, and ".". Any additional
1585 links are to the ".." entries of its subdirectories.
1586 Once we have processed as many subdirectories as
1587 there are additional links, we know that the rest of
1588 the entries are non-directories -- in other words,
1589 leaf files. */
1590 subdirs_left -= process_path (cur_path, cur_name,
1591 subdirs_left == 0, pathname,
1592 mode);
1594 else
1596 /* There might be weird (e.g., CD-ROM or MS-DOS) filesystems
1597 mounted, which don't have Unix-like directory link counts. */
1598 process_path (cur_path, cur_name, false, pathname, mode);
1601 state.curdepth--;
1605 /* We're about to leave the directory. If there are any
1606 * -execdir argument lists which have been built but have not
1607 * yet been processed, do them now because they must be done in
1608 * the same directory.
1610 complete_pending_execdirs(eval_tree);
1613 if (strcmp (name, "."))
1615 enum SafeChdirStatus status;
1616 struct dir_id did;
1617 boolean did_stat = false;
1619 /* We could go back and do the next command-line arg
1620 instead, maybe using longjmp. */
1621 char const *dir;
1622 boolean deref = following_links() ? true : false;
1624 if ( (state.curdepth>0) && !deref)
1625 dir = "..";
1626 else
1628 chdir_back ();
1629 dir = parent;
1632 status = safely_chdir (dir, TraversingUp, &stat_buf, SymlinkHandleDefault, &did_stat);
1633 switch (status)
1635 case SafeChdirOK:
1636 break;
1638 case SafeChdirFailWouldBeUnableToReturn:
1639 error (1, errno, ".");
1640 return;
1642 case SafeChdirFailNonexistent:
1643 case SafeChdirFailStat:
1644 case SafeChdirFailSymlink:
1645 case SafeChdirFailNotDir:
1646 case SafeChdirFailChdirFailed:
1647 error (1, errno, "%s", pathname);
1648 return;
1651 if (dir_curr > 0)
1653 did.dev = dir_ids[dir_curr-1].dev;
1654 did.ino = dir_ids[dir_curr-1].ino;
1656 else
1658 did.dev = starting_stat_buf.st_dev;
1659 did.ino = starting_stat_buf.st_ino;
1663 if (cur_path)
1664 free (cur_path);
1665 free_dirinfo(dirinfo);