Evaluating the predicates can change the contents of the data structure (for example...
[findutils.git] / find / find.c
blob876e271919257ff9e5261c9ee03bfd86dcf2c1be
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 int end_of_leading_options = 0; /* First arg after any -H/-L etc. */
132 struct predicate *eval_tree;
134 program_name = argv[0];
136 /* We call check_nofollow() before setlocale() because the numbers
137 * for which we check (in the results of uname) definitiely have "."
138 * as the decimal point indicator even under locales for which that
139 * is not normally true. Hence atof() would do the wrong thing
140 * if we call it after setlocale().
142 #ifdef O_NOFOLLOW
143 options.open_nofollow_available = check_nofollow();
144 #else
145 options.open_nofollow_available = false;
146 #endif
148 options.regex_options = RE_SYNTAX_EMACS;
150 #ifdef HAVE_SETLOCALE
151 setlocale (LC_ALL, "");
152 #endif
153 bindtextdomain (PACKAGE, LOCALEDIR);
154 textdomain (PACKAGE);
155 atexit (close_stdout);
158 if (isatty(0))
160 options.warnings = true;
161 options.literal_control_chars = false;
163 else
165 options.warnings = false;
166 options.literal_control_chars = false; /* may change */
170 predicates = NULL;
171 last_pred = NULL;
172 options.do_dir_first = true;
173 options.maxdepth = options.mindepth = -1;
174 options.start_time = time (NULL);
175 options.cur_day_start = options.start_time - DAYSECS;
176 options.full_days = false;
177 options.stay_on_filesystem = false;
178 options.ignore_readdir_race = false;
180 state.exit_status = 0;
182 #if defined(DEBUG_STAT)
183 options.xstat = debug_stat;
184 #endif /* !DEBUG_STAT */
186 if (getenv("POSIXLY_CORRECT"))
187 options.output_block_size = 512;
188 else
189 options.output_block_size = 1024;
191 if (getenv("FIND_BLOCK_SIZE"))
193 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"));
196 #if LEAF_OPTIMISATION
197 /* The leaf optimisation is enabled. */
198 options.no_leaf_check = false;
199 #else
200 /* The leaf optimisation is disabled. */
201 options.no_leaf_check = true;
202 #endif
204 set_follow_state(SYMLINK_NEVER_DEREF); /* The default is equivalent to -P. */
206 #ifdef DEBUG
207 fprintf (stderr, "cur_day_start = %s", ctime (&options.cur_day_start));
208 #endif /* DEBUG */
210 /* Check for -P, -H or -L options. */
211 end_of_leading_options = process_leading_options(argc, argv);
213 /* We are now processing the part of the "find" command line
214 * after the -H/-L options (if any).
216 eval_tree = build_expression_tree(argc, argv, end_of_leading_options);
219 /* safely_chdir() needs to check that it has ended up in the right place.
220 * To avoid bailing out when something gets automounted, it checks if
221 * the target directory appears to have had a directory mounted on it as
222 * we chdir()ed. The problem with this is that in order to notice that
223 * a filesystem was mounted, we would need to lstat() all the mount points.
224 * That strategy loses if our machine is a client of a dead NFS server.
226 * Hence if safely_chdir() and wd_sanity_check() can manage without needing
227 * to know the mounted device list, we do that.
229 if (!options.open_nofollow_available)
231 #ifdef STAT_MOUNTPOINTS
232 init_mounted_dev_list(0);
233 #endif
237 starting_desc = open (".", O_RDONLY);
238 if (0 <= starting_desc && fchdir (starting_desc) != 0)
240 close (starting_desc);
241 starting_desc = -1;
243 if (starting_desc < 0)
245 starting_dir = xgetcwd ();
246 if (! starting_dir)
247 error (1, errno, _("cannot get current directory"));
249 if ((*options.xstat) (".", &starting_stat_buf) != 0)
250 error (1, errno, _("cannot get current directory"));
252 /* If no paths are given, default to ".". */
253 for (i = end_of_leading_options; i < argc && !looks_like_expression(argv[i], true); i++)
255 process_top_path (argv[i], 0);
258 /* If there were no path arguments, default to ".". */
259 if (i == end_of_leading_options)
262 * We use a temporary variable here because some actions modify
263 * the path temporarily. Hence if we use a string constant,
264 * we get a coredump. The best example of this is if we say
265 * "find -printf %H" (note, not "find . -printf %H").
267 char defaultpath[2] = ".";
268 process_top_path (defaultpath, 0);
271 /* If "-exec ... {} +" has been used, there may be some
272 * partially-full command lines which have been built,
273 * but which are not yet complete. Execute those now.
275 cleanup();
276 return state.exit_status;
279 boolean is_fts_enabled()
281 /* this version of find (i.e. this main()) does not use fts. */
282 return false;
286 static char *
287 specific_dirname(const char *dir)
289 char dirbuf[1024];
291 if (0 == strcmp(".", dir))
293 /* OK, what's '.'? */
294 if (NULL != getcwd(dirbuf, sizeof(dirbuf)))
296 return strdup(dirbuf);
298 else
300 return strdup(dir);
303 else
305 char *result = canonicalize_filename_mode(dir, CAN_EXISTING);
306 if (NULL == result)
307 return strdup(dir);
308 else
309 return result;
315 /* Return non-zero if FS is the name of a filesystem that is likely to
316 * be automounted
318 static int
319 fs_likely_to_be_automounted(const char *fs)
321 return ( (0==strcmp(fs, "nfs")) || (0==strcmp(fs, "autofs")) || (0==strcmp(fs, "subfs")));
326 #ifdef STAT_MOUNTPOINTS
327 static dev_t *mounted_devices = NULL;
328 static size_t num_mounted_devices = 0u;
331 static void
332 init_mounted_dev_list(int mandatory)
334 assert(NULL == mounted_devices);
335 assert(0 == num_mounted_devices);
336 mounted_devices = get_mounted_devices(&num_mounted_devices);
337 if (mandatory && (NULL == mounted_devices))
339 error(1, 0, "Cannot read list of mounted devices.");
343 static void
344 refresh_mounted_dev_list(void)
346 if (mounted_devices)
348 free(mounted_devices);
349 mounted_devices = 0;
351 num_mounted_devices = 0u;
352 init_mounted_dev_list(1);
356 /* Search for device DEV in the array LIST, which is of size N. */
357 static int
358 dev_present(dev_t dev, const dev_t *list, size_t n)
360 if (list)
362 while (n-- > 0u)
364 if ( (*list++) == dev )
365 return 1;
368 return 0;
371 enum MountPointStateChange
373 MountPointRecentlyMounted,
374 MountPointRecentlyUnmounted,
375 MountPointStateUnchanged
380 static enum MountPointStateChange
381 get_mount_state(dev_t newdev)
383 int new_is_present, new_was_present;
385 new_was_present = dev_present(newdev, mounted_devices, num_mounted_devices);
386 refresh_mounted_dev_list();
387 new_is_present = dev_present(newdev, mounted_devices, num_mounted_devices);
389 if (new_was_present == new_is_present)
390 return MountPointStateUnchanged;
391 else if (new_is_present)
392 return MountPointRecentlyMounted;
393 else
394 return MountPointRecentlyUnmounted;
399 /* We stat()ed a directory, chdir()ed into it (we know this
400 * since direction is TraversingDown), stat()ed it again,
401 * and noticed that the device numbers are different. Check
402 * if the filesystem was recently mounted.
404 * If it was, it looks like chdir()ing into the directory
405 * caused a filesystem to be mounted. Maybe automount is
406 * running. Anyway, that's probably OK - but it happens
407 * only when we are moving downward.
409 * We also allow for the possibility that a similar thing
410 * has happened with the unmounting of a filesystem. This
411 * is much rarer, as it relies on an automounter timeout
412 * occurring at exactly the wrong moment.
414 static enum WdSanityCheckFatality
415 dirchange_is_fatal(const char *specific_what,
416 enum WdSanityCheckFatality isfatal,
417 int silent,
418 struct stat *newinfo)
420 enum MountPointStateChange transition = get_mount_state(newinfo->st_dev);
421 switch (transition)
423 case MountPointRecentlyUnmounted:
424 isfatal = NON_FATAL_IF_SANITY_CHECK_FAILS;
425 if (!silent)
427 error (0, 0,
428 _("Warning: filesystem %s has recently been unmounted."),
429 specific_what);
431 break;
433 case MountPointRecentlyMounted:
434 isfatal = NON_FATAL_IF_SANITY_CHECK_FAILS;
435 if (!silent)
437 error (0, 0,
438 _("Warning: filesystem %s has recently been mounted."),
439 specific_what);
441 break;
443 case MountPointStateUnchanged:
444 /* leave isfatal as it is */
445 break;
448 return isfatal;
452 #endif
456 /* Examine the results of the stat() of a directory from before we
457 * entered or left it, with the results of stat()ing it afterward. If
458 * these are different, the filesystem tree has been modified while we
459 * were traversing it. That might be an attempt to use a race
460 * condition to persuade find to do something it didn't intend
461 * (e.g. an attempt by an ordinary user to exploit the fact that root
462 * sometimes runs find on the whole filesystem). However, this can
463 * also happen if automount is running (certainly on Solaris). With
464 * automount, moving into a directory can cause a filesystem to be
465 * mounted there.
467 * To cope sensibly with this, we will raise an error if we see the
468 * device number change unless we are chdir()ing into a subdirectory,
469 * and the directory we moved into has been mounted or unmounted "recently".
470 * Here "recently" means since we started "find" or we last re-read
471 * the /etc/mnttab file.
473 * If the device number does not change but the inode does, that is a
474 * problem.
476 * If the device number and inode are both the same, we are happy.
478 * If a filesystem is (un)mounted as we chdir() into the directory, that
479 * may mean that we're now examining a section of the filesystem that might
480 * have been excluded from consideration (via -prune or -quit for example).
481 * Hence we print a warning message to indicate that the output of find
482 * might be inconsistent due to the change in the filesystem.
484 static boolean
485 wd_sanity_check(const char *thing_to_stat,
486 const char *progname,
487 const char *what,
488 dev_t old_dev,
489 ino_t old_ino,
490 struct stat *newinfo,
491 int parent,
492 int line_no,
493 enum TraversalDirection direction,
494 enum WdSanityCheckFatality isfatal,
495 boolean *changed) /* output parameter */
497 const char *fstype;
498 char *specific_what = NULL;
499 int silent = 0;
500 const char *current_dir = ".";
502 *changed = false;
504 if ((*options.xstat) (current_dir, newinfo) != 0)
505 error (1, errno, "%s", thing_to_stat);
507 if (old_dev != newinfo->st_dev)
509 *changed = true;
510 specific_what = specific_dirname(what);
511 fstype = filesystem_type(newinfo, current_dir);
512 silent = fs_likely_to_be_automounted(fstype);
514 /* This condition is rare, so once we are here it is
515 * reasonable to perform an expensive computation to
516 * determine if we should continue or fail.
518 if (TraversingDown == direction)
520 #ifdef STAT_MOUNTPOINTS
521 isfatal = dirchange_is_fatal(specific_what,isfatal,silent,newinfo);
522 #else
523 isfatal = RETRY_IF_SANITY_CHECK_FAILS;
524 #endif
527 switch (isfatal)
529 case FATAL_IF_SANITY_CHECK_FAILS:
531 fstype = filesystem_type(newinfo, current_dir);
532 error (1, 0,
533 _("%s%s changed during execution of %s (old device number %ld, new device number %ld, filesystem type is %s) [ref %ld]"),
534 specific_what,
535 parent ? "/.." : "",
536 progname,
537 (long) old_dev,
538 (long) newinfo->st_dev,
539 fstype,
540 line_no);
541 /*NOTREACHED*/
542 return false;
545 case NON_FATAL_IF_SANITY_CHECK_FAILS:
547 /* Since the device has changed under us, the inode number
548 * will almost certainly also be different. However, we have
549 * already decided that this is not a problem. Hence we return
550 * without checking the inode number.
552 free(specific_what);
553 return true;
556 case RETRY_IF_SANITY_CHECK_FAILS:
557 return false;
561 /* Device number was the same, check if the inode has changed. */
562 if (old_ino != newinfo->st_ino)
564 *changed = true;
565 specific_what = specific_dirname(what);
566 fstype = filesystem_type(newinfo, current_dir);
568 error ((isfatal == FATAL_IF_SANITY_CHECK_FAILS) ? 1 : 0,
569 0, /* no relevant errno value */
570 _("%s%s changed during execution of %s (old inode number %ld, new inode number %ld, filesystem type is %s) [ref %ld]"),
571 specific_what,
572 parent ? "/.." : "",
573 progname,
574 (long) old_ino,
575 (long) newinfo->st_ino,
576 fstype,
577 line_no);
578 free(specific_what);
579 return false;
582 return true;
585 enum SafeChdirStatus
587 SafeChdirOK,
588 SafeChdirFailSymlink,
589 SafeChdirFailNotDir,
590 SafeChdirFailStat,
591 SafeChdirFailWouldBeUnableToReturn,
592 SafeChdirFailChdirFailed,
593 SafeChdirFailNonexistent
596 /* Safely perform a change in directory. We do this by calling
597 * lstat() on the subdirectory, using chdir() to move into it, and
598 * then lstat()ing ".". We compare the results of the two stat calls
599 * to see if they are consistent. If not, we sound the alarm.
601 * If following_links() is true, we do follow symbolic links.
603 static enum SafeChdirStatus
604 safely_chdir_lstat(const char *dest,
605 enum TraversalDirection direction,
606 struct stat *statbuf_dest,
607 enum ChdirSymlinkHandling symlink_follow_option,
608 boolean *did_stat)
610 struct stat statbuf_arrived;
611 int rv, dotfd=-1;
612 int saved_errno; /* specific_dirname() changes errno. */
613 boolean rv_set = false;
614 boolean statflag = false;
615 int tries = 0;
616 enum WdSanityCheckFatality isfatal = RETRY_IF_SANITY_CHECK_FAILS;
618 saved_errno = errno = 0;
620 dotfd = open(".", O_RDONLY);
622 /* We jump back to here if wd_sanity_check()
623 * recoverably triggers an alert.
625 retry:
626 ++tries;
628 if (dotfd >= 0)
630 /* Stat the directory we're going to. */
631 if (0 == options.xstat(dest, statbuf_dest))
633 statflag = true;
635 #ifdef S_ISLNK
636 /* symlink_follow_option might be set to SymlinkFollowOk, which
637 * would allow us to chdir() into a symbolic link. This is
638 * only useful for the case where the directory we're
639 * chdir()ing into is the basename of a command line
640 * argument, for example where "foo/bar/baz" is specified on
641 * the command line. When -P is in effect (the default),
642 * baz will not be followed if it is a symlink, but if bar
643 * is a symlink, it _should_ be followed. Hence we need the
644 * ability to override the policy set by following_links().
646 if (!following_links() && S_ISLNK(statbuf_dest->st_mode))
648 /* We're not supposed to be following links, but this is
649 * a link. Check symlink_follow_option to see if we should
650 * make a special exception.
652 if (symlink_follow_option == SymlinkFollowOk)
654 /* We need to re-stat() the file so that the
655 * sanity check can pass.
657 if (0 != stat(dest, statbuf_dest))
659 rv = SafeChdirFailNonexistent;
660 rv_set = true;
661 saved_errno = errno;
662 goto fail;
664 statflag = true;
666 else
668 /* Not following symlinks, so the attempt to
669 * chdir() into a symlink should be prevented.
671 rv = SafeChdirFailSymlink;
672 rv_set = true;
673 saved_errno = 0; /* silence the error message */
674 goto fail;
677 #endif
678 #ifdef S_ISDIR
679 /* Although the immediately following chdir() would detect
680 * the fact that this is not a directory for us, this would
681 * result in an extra system call that fails. Anybody
682 * examining the system-call trace should ideally not be
683 * concerned that something is actually failing.
685 if (!S_ISDIR(statbuf_dest->st_mode))
687 rv = SafeChdirFailNotDir;
688 rv_set = true;
689 saved_errno = 0; /* silence the error message */
690 goto fail;
692 #endif
693 #ifdef DEBUG_STAT
694 fprintf(stderr, "safely_chdir(): chdir(\"%s\")\n", dest);
695 #endif
696 if (0 == chdir(dest))
698 /* check we ended up where we wanted to go */
699 boolean changed = false;
700 if (!wd_sanity_check(".", program_name, ".",
701 statbuf_dest->st_dev,
702 statbuf_dest->st_ino,
703 &statbuf_arrived,
704 0, __LINE__, direction,
705 isfatal,
706 &changed))
708 /* Only allow one failure. */
709 if (RETRY_IF_SANITY_CHECK_FAILS == isfatal)
711 if (0 == fchdir(dotfd))
713 isfatal = FATAL_IF_SANITY_CHECK_FAILS;
714 goto retry;
716 else
718 /* Failed to return to original directory,
719 * but we know that the current working
720 * directory is not the one that we intend
721 * to be in. Since fchdir() failed, we
722 * can't recover from this and so this error
723 * is fatal.
725 error(1, errno,
726 "failed to return to parent directory");
729 else
731 /* XXX: not sure what to use as an excuse here. */
732 rv = SafeChdirFailNonexistent;
733 rv_set = true;
734 saved_errno = 0;
735 goto fail;
739 close(dotfd);
740 return SafeChdirOK;
742 else
744 saved_errno = errno;
745 if (ENOENT == saved_errno)
747 rv = SafeChdirFailNonexistent;
748 rv_set = true;
749 if (options.ignore_readdir_race)
750 errno = 0; /* don't issue err msg */
752 else if (ENOTDIR == saved_errno)
754 /* This can happen if the we stat a directory,
755 * and then filesystem activity changes it into
756 * a non-directory.
758 saved_errno = 0; /* don't issue err msg */
759 rv = SafeChdirFailNotDir;
760 rv_set = true;
762 else
764 rv = SafeChdirFailChdirFailed;
765 rv_set = true;
767 goto fail;
770 else
772 saved_errno = errno;
773 rv = SafeChdirFailStat;
774 rv_set = true;
776 if ( (ENOENT == saved_errno) || (0 == state.curdepth))
777 saved_errno = 0; /* don't issue err msg */
778 goto fail;
781 else
783 /* We do not have read permissions on "." */
784 rv = SafeChdirFailWouldBeUnableToReturn;
785 rv_set = true;
786 goto fail;
789 /* This is the success path, so we clear errno. The caller probably
790 * won't be calling error() anyway.
792 saved_errno = 0;
794 /* We use the same exit path for success or failure.
795 * which has occurred is recorded in RV.
797 fail:
798 /* We do not call error() as this would result in a duplicate error
799 * message when the caller does the same thing.
801 if (saved_errno)
802 errno = saved_errno;
804 if (dotfd >= 0)
806 close(dotfd);
807 dotfd = -1;
810 *did_stat = statflag;
811 assert(rv_set);
812 return rv;
815 #if defined(O_NOFOLLOW)
816 /* Safely change working directory to the specified subdirectory. If
817 * we are not allowed to follow symbolic links, we use open() with
818 * O_NOFOLLOW, followed by fchdir(). This ensures that we don't
819 * follow symbolic links (of course, we do follow them if the -L
820 * option is in effect).
822 static enum SafeChdirStatus
823 safely_chdir_nofollow(const char *dest,
824 enum TraversalDirection direction,
825 struct stat *statbuf_dest,
826 enum ChdirSymlinkHandling symlink_follow_option,
827 boolean *did_stat)
829 int extraflags, fd;
830 extraflags = 0;
832 *did_stat = false;
834 switch (symlink_follow_option)
836 case SymlinkFollowOk:
837 extraflags = 0;
838 break;
840 case SymlinkHandleDefault:
841 if (following_links())
842 extraflags = 0;
843 else
844 extraflags = O_NOFOLLOW;
845 break;
848 errno = 0;
849 fd = open(dest, O_RDONLY|extraflags);
850 if (fd < 0)
852 switch (errno)
854 case ELOOP:
855 return SafeChdirFailSymlink; /* This is why we use O_NOFOLLOW */
856 case ENOENT:
857 return SafeChdirFailNonexistent;
858 default:
859 return SafeChdirFailChdirFailed;
863 errno = 0;
864 if (0 == fchdir(fd))
866 close(fd);
867 return SafeChdirOK;
869 else
871 int saved_errno = errno;
872 close(fd);
873 errno = saved_errno;
875 switch (errno)
877 case ENOTDIR:
878 return SafeChdirFailNotDir;
880 case EACCES:
881 case EBADF: /* Shouldn't happen */
882 case EINTR:
883 case EIO:
884 default:
885 return SafeChdirFailChdirFailed;
889 #endif
891 static enum SafeChdirStatus
892 safely_chdir(const char *dest,
893 enum TraversalDirection direction,
894 struct stat *statbuf_dest,
895 enum ChdirSymlinkHandling symlink_follow_option,
896 boolean *did_stat)
898 /* We're about to leave a directory. If there are any -execdir
899 * argument lists which have been built but have not yet been
900 * processed, do them now because they must be done in the same
901 * directory.
903 complete_pending_execdirs(get_eval_tree());
905 #if defined(O_NOFOLLOW)
906 if (options.open_nofollow_available)
907 return safely_chdir_nofollow(dest, direction, statbuf_dest, symlink_follow_option, did_stat);
908 #endif
909 return safely_chdir_lstat(dest, direction, statbuf_dest, symlink_follow_option, did_stat);
914 /* Safely go back to the starting directory. */
915 static void
916 chdir_back (void)
918 struct stat stat_buf;
919 boolean dummy;
921 if (starting_desc < 0)
923 #ifdef DEBUG_STAT
924 fprintf(stderr, "chdir_back(): chdir(\"%s\")\n", starting_dir);
925 #endif
927 #ifdef STAT_MOUNTPOINTS
928 /* We will need the mounted device list. Get it now if we don't
929 * already have it.
931 if (NULL == mounted_devices)
932 init_mounted_dev_list(1);
933 #endif
935 if (chdir (starting_dir) != 0)
936 error (1, errno, "%s", starting_dir);
938 wd_sanity_check(starting_dir,
939 program_name,
940 starting_dir,
941 starting_stat_buf.st_dev,
942 starting_stat_buf.st_ino,
943 &stat_buf, 0, __LINE__,
944 TraversingUp,
945 FATAL_IF_SANITY_CHECK_FAILS,
946 &dummy);
948 else
950 #ifdef DEBUG_STAT
951 fprintf(stderr, "chdir_back(): chdir(<starting-point>)\n");
952 #endif
953 if (fchdir (starting_desc) != 0)
954 error (1, errno, "%s", starting_dir);
958 /* Move to the parent of a given directory and then call a function,
959 * restoring the cwd. Don't bother changing directory if the
960 * specified directory is a child of "." or is the root directory.
962 static void
963 at_top (char *pathname,
964 mode_t mode,
965 struct stat *pstat,
966 void (*action)(char *pathname,
967 char *basename,
968 int mode,
969 struct stat *pstat))
971 int dirchange;
972 char *parent_dir = dir_name(pathname);
973 char *base = base_name(pathname);
975 state.curdepth = 0;
976 state.starting_path_length = strlen (pathname);
978 if (0 == strcmp(pathname, parent_dir)
979 || 0 == strcmp(parent_dir, "."))
981 dirchange = 0;
982 base = pathname;
984 else
986 enum TraversalDirection direction;
987 enum SafeChdirStatus chdir_status;
988 struct stat st;
989 boolean did_stat = false;
991 dirchange = 1;
992 if (0 == strcmp(base, ".."))
993 direction = TraversingUp;
994 else
995 direction = TraversingDown;
997 /* We pass SymlinkFollowOk to safely_chdir(), which allows it to
998 * chdir() into a symbolic link. This is only useful for the
999 * case where the directory we're chdir()ing into is the
1000 * basename of a command line argument, for example where
1001 * "foo/bar/baz" is specified on the command line. When -P is
1002 * in effect (the default), baz will not be followed if it is a
1003 * symlink, but if bar is a symlink, it _should_ be followed.
1004 * Hence we need the ability to override the policy set by
1005 * following_links().
1007 chdir_status = safely_chdir(parent_dir, direction, &st, SymlinkFollowOk, &did_stat);
1008 if (SafeChdirOK != chdir_status)
1010 const char *what = (SafeChdirFailWouldBeUnableToReturn == chdir_status) ? "." : parent_dir;
1011 if (errno)
1012 error (0, errno, "%s", what);
1013 else
1014 error (0, 0, "Failed to safely change directory into `%s'",
1015 parent_dir);
1017 /* We can't process this command-line argument. */
1018 state.exit_status = 1;
1019 return;
1023 free (parent_dir);
1024 parent_dir = NULL;
1026 action(pathname, base, mode, pstat);
1028 if (dirchange)
1030 chdir_back();
1035 static void do_process_top_dir(char *pathname,
1036 char *base,
1037 int mode,
1038 struct stat *pstat)
1040 process_path (pathname, base, false, ".", mode);
1041 complete_pending_execdirs(get_eval_tree());
1044 static void do_process_predicate(char *pathname,
1045 char *base,
1046 int mode,
1047 struct stat *pstat)
1049 state.rel_pathname = base;
1050 apply_predicate (pathname, pstat, get_eval_tree());
1056 /* Descend PATHNAME, which is a command-line argument.
1058 Actions like -execdir assume that we are in the
1059 parent directory of the file we're examining,
1060 and on entry to this function our working directory
1061 is whatever it was when find was invoked. Therefore
1062 If PATHNAME is "." we just leave things as they are.
1063 Otherwise, we figure out what the parent directory is,
1064 and move to that.
1066 static void
1067 process_top_path (char *pathname, mode_t mode)
1069 at_top(pathname, mode, NULL, do_process_top_dir);
1073 /* Info on each directory in the current tree branch, to avoid
1074 getting stuck in symbolic link loops. */
1075 static struct dir_id *dir_ids = NULL;
1076 /* Entries allocated in `dir_ids'. */
1077 static int dir_alloc = 0;
1078 /* Index in `dir_ids' of directory currently being searched.
1079 This is always the last valid entry. */
1080 static int dir_curr = -1;
1081 /* (Arbitrary) number of entries to grow `dir_ids' by. */
1082 #define DIR_ALLOC_STEP 32
1086 /* We've detected a filesystem loop. This is caused by one of
1087 * two things:
1089 * 1. Option -L is in effect and we've hit a symbolic link that
1090 * points to an ancestor. This is harmless. We won't traverse the
1091 * symbolic link.
1093 * 2. We have hit a real cycle in the directory hierarchy. In this
1094 * case, we issue a diagnostic message (POSIX requires this) and we
1095 * skip that directory entry.
1097 static void
1098 issue_loop_warning(const char *name, const char *pathname, int level)
1100 struct stat stbuf_link;
1101 if (lstat(name, &stbuf_link) != 0)
1102 stbuf_link.st_mode = S_IFREG;
1104 if (S_ISLNK(stbuf_link.st_mode))
1106 error(0, 0,
1107 _("Symbolic link `%s' is part of a loop in the directory hierarchy; we have already visited the directory to which it points."),
1108 pathname);
1110 else
1112 int distance = 1 + (dir_curr-level);
1113 /* We have found an infinite loop. POSIX requires us to
1114 * issue a diagnostic. Usually we won't get to here
1115 * because when the leaf optimisation is on, it will cause
1116 * the subdirectory to be skipped. If /a/b/c/d is a hard
1117 * link to /a/b, then the link count of /a/b/c is 2,
1118 * because the ".." entry of /b/b/c/d points to /a, not
1119 * to /a/b/c.
1121 error(0, 0,
1122 _("Filesystem loop detected; `%s' has the same device number and inode as a directory which is %d %s."),
1123 pathname,
1124 distance,
1125 (distance == 1 ?
1126 _("level higher in the filesystem hierarchy") :
1127 _("levels higher in the filesystem hierarchy")));
1133 /* Recursively descend path PATHNAME, applying the predicates.
1134 LEAF is true if PATHNAME is known to be in a directory that has no
1135 more unexamined subdirectories, and therefore it is not a directory.
1136 Knowing this allows us to avoid calling stat as long as possible for
1137 leaf files.
1139 NAME is PATHNAME relative to the current directory. We access NAME
1140 but print PATHNAME.
1142 PARENT is the path of the parent of NAME, relative to find's
1143 starting directory.
1145 Return nonzero iff PATHNAME is a directory. */
1147 static int
1148 process_path (char *pathname, char *name, boolean leaf, char *parent,
1149 mode_t mode)
1151 struct stat stat_buf;
1152 static dev_t root_dev; /* Device ID of current argument pathname. */
1153 int i;
1154 struct predicate *eval_tree;
1156 eval_tree = get_eval_tree();
1157 /* Assume it is a non-directory initially. */
1158 stat_buf.st_mode = 0;
1159 state.rel_pathname = name;
1160 state.type = 0;
1161 state.have_stat = false;
1162 state.have_type = false;
1164 if (!digest_mode(mode, pathname, name, &stat_buf, leaf))
1165 return 0;
1167 if (!S_ISDIR (state.type))
1169 if (state.curdepth >= options.mindepth)
1170 apply_predicate (pathname, &stat_buf, eval_tree);
1171 return 0;
1174 /* From here on, we're working on a directory. */
1177 /* Now we really need to stat the directory, even if we know the
1178 * type, because we need information like struct stat.st_rdev.
1180 if (get_statinfo(pathname, name, &stat_buf) != 0)
1181 return 0;
1183 state.have_stat = true;
1184 mode = state.type = stat_buf.st_mode; /* use full info now that we have it. */
1185 state.stop_at_current_level =
1186 options.maxdepth >= 0
1187 && state.curdepth >= options.maxdepth;
1189 /* If we've already seen this directory on this branch,
1190 don't descend it again. */
1191 for (i = 0; i <= dir_curr; i++)
1192 if (stat_buf.st_ino == dir_ids[i].ino &&
1193 stat_buf.st_dev == dir_ids[i].dev)
1195 state.stop_at_current_level = true;
1196 issue_loop_warning(name, pathname, i);
1199 if (dir_alloc <= ++dir_curr)
1201 dir_alloc += DIR_ALLOC_STEP;
1202 dir_ids = (struct dir_id *)
1203 xrealloc ((char *) dir_ids, dir_alloc * sizeof (struct dir_id));
1205 dir_ids[dir_curr].ino = stat_buf.st_ino;
1206 dir_ids[dir_curr].dev = stat_buf.st_dev;
1208 if (options.stay_on_filesystem)
1210 if (state.curdepth == 0)
1211 root_dev = stat_buf.st_dev;
1212 else if (stat_buf.st_dev != root_dev)
1213 state.stop_at_current_level = true;
1216 if (options.do_dir_first && state.curdepth >= options.mindepth)
1217 apply_predicate (pathname, &stat_buf, eval_tree);
1219 #ifdef DEBUG
1220 fprintf(stderr, "pathname = %s, stop_at_current_level = %d\n",
1221 pathname, state.stop_at_current_level);
1222 #endif /* DEBUG */
1224 if (state.stop_at_current_level == false)
1225 /* Scan directory on disk. */
1226 process_dir (pathname, name, strlen (pathname), &stat_buf, parent);
1228 if (options.do_dir_first == false && state.curdepth >= options.mindepth)
1230 /* The fields in 'state' are now out of date. Correct them.
1232 if (!digest_mode(mode, pathname, name, &stat_buf, leaf))
1233 return 0;
1235 if (0 == dir_curr)
1237 at_top(pathname, mode, &stat_buf, do_process_predicate);
1239 else
1241 do_process_predicate(pathname, name, mode, &stat_buf);
1245 dir_curr--;
1247 return 1;
1251 /* Scan directory PATHNAME and recurse through process_path for each entry.
1253 PATHLEN is the length of PATHNAME.
1255 NAME is PATHNAME relative to the current directory.
1257 STATP is the results of *options.xstat on it.
1259 PARENT is the path of the parent of NAME, relative to find's
1260 starting directory. */
1262 static void
1263 process_dir (char *pathname, char *name, int pathlen, struct stat *statp, char *parent)
1265 int subdirs_left; /* Number of unexamined subdirs in PATHNAME. */
1266 boolean subdirs_unreliable; /* if true, cannot use dir link count as subdir limif (if false, it may STILL be unreliable) */
1267 int idx; /* Which entry are we on? */
1268 struct stat stat_buf;
1270 struct savedir_dirinfo *dirinfo;
1272 if (statp->st_nlink < 2)
1274 subdirs_unreliable = true;
1276 else
1278 subdirs_unreliable = false; /* not necessarily right */
1279 subdirs_left = statp->st_nlink - 2; /* Account for name and ".". */
1282 errno = 0;
1283 dirinfo = xsavedir(name, 0);
1286 if (dirinfo == NULL)
1288 assert(errno != 0);
1289 error (0, errno, "%s", pathname);
1290 state.exit_status = 1;
1292 else
1294 register char *namep; /* Current point in `name_space'. */
1295 char *cur_path; /* Full path of each file to process. */
1296 char *cur_name; /* Base name of each file to process. */
1297 unsigned cur_path_size; /* Bytes allocated for `cur_path'. */
1298 register unsigned file_len; /* Length of each path to process. */
1299 register unsigned pathname_len; /* PATHLEN plus trailing '/'. */
1300 boolean did_stat = false;
1302 if (pathname[pathlen - 1] == '/')
1303 pathname_len = pathlen + 1; /* For '\0'; already have '/'. */
1304 else
1305 pathname_len = pathlen + 2; /* For '/' and '\0'. */
1306 cur_path_size = 0;
1307 cur_path = NULL;
1309 /* We're about to leave the directory. If there are any
1310 * -execdir argument lists which have been built but have not
1311 * yet been processed, do them now because they must be done in
1312 * the same directory.
1314 complete_pending_execdirs(get_eval_tree());
1316 if (strcmp (name, "."))
1318 enum SafeChdirStatus status = safely_chdir (name, TraversingDown, &stat_buf, SymlinkHandleDefault, &did_stat);
1319 switch (status)
1321 case SafeChdirOK:
1322 /* If there had been a change but wd_sanity_check()
1323 * accepted it, we need to accept that on the
1324 * way back up as well, so modify our record
1325 * of what we think we should see later.
1326 * If there was no change, the assignments are a no-op.
1328 * However, before performing the assignment, we need to
1329 * check that we have the stat information. If O_NOFOLLOW
1330 * is available, safely_chdir() will not have needed to use
1331 * stat(), and so stat_buf will just contain random data.
1333 if (!did_stat)
1335 /* If there is a link we need to follow it. Hence
1336 * the direct call to stat() not through (options.xstat)
1338 if (0 != stat(".", &stat_buf))
1339 break; /* skip the assignment. */
1341 dir_ids[dir_curr].dev = stat_buf.st_dev;
1342 dir_ids[dir_curr].ino = stat_buf.st_ino;
1344 break;
1346 case SafeChdirFailWouldBeUnableToReturn:
1347 error (0, errno, ".");
1348 state.exit_status = 1;
1349 break;
1351 case SafeChdirFailNonexistent:
1352 case SafeChdirFailStat:
1353 case SafeChdirFailNotDir:
1354 case SafeChdirFailChdirFailed:
1355 error (0, errno, "%s", pathname);
1356 state.exit_status = 1;
1357 return;
1359 case SafeChdirFailSymlink:
1360 error (0, 0,
1361 _("warning: not following the symbolic link %s"),
1362 pathname);
1363 state.exit_status = 1;
1364 return;
1368 for (idx=0; idx < dirinfo->size; ++idx)
1370 /* savedirinfo() may return dirinfo=NULL if extended information
1371 * is not available.
1373 mode_t mode = (dirinfo->entries[idx].flags & SavedirHaveFileType) ?
1374 dirinfo->entries[idx].type_info : 0;
1375 namep = dirinfo->entries[idx].name;
1377 /* Append this directory entry's name to the path being searched. */
1378 file_len = pathname_len + strlen (namep);
1379 if (file_len > cur_path_size)
1381 while (file_len > cur_path_size)
1382 cur_path_size += 1024;
1383 if (cur_path)
1384 free (cur_path);
1385 cur_path = xmalloc (cur_path_size);
1386 strcpy (cur_path, pathname);
1387 cur_path[pathname_len - 2] = '/';
1389 cur_name = cur_path + pathname_len - 1;
1390 strcpy (cur_name, namep);
1392 state.curdepth++;
1393 if (!options.no_leaf_check && !subdirs_unreliable)
1395 if (mode && S_ISDIR(mode) && (subdirs_left == 0))
1397 /* This is a subdirectory, but the number of directories we
1398 * have found now exceeds the number we would expect given
1399 * the hard link count on the parent. This is likely to be
1400 * a bug in the filesystem driver (e.g. Linux's
1401 * /proc filesystem) or may just be a fact that the OS
1402 * doesn't really handle hard links with Unix semantics.
1403 * In the latter case, -noleaf should be used routinely.
1405 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."),
1406 parent);
1407 state.exit_status = 1; /* We know the result is wrong, now */
1408 options.no_leaf_check = true; /* Don't make same
1409 mistake again */
1410 subdirs_left = 1; /* band-aid for this iteration. */
1413 /* Normal case optimization. On normal Unix
1414 filesystems, a directory that has no subdirectories
1415 has two links: its name, and ".". Any additional
1416 links are to the ".." entries of its subdirectories.
1417 Once we have processed as many subdirectories as
1418 there are additional links, we know that the rest of
1419 the entries are non-directories -- in other words,
1420 leaf files. */
1421 subdirs_left -= process_path (cur_path, cur_name,
1422 subdirs_left == 0, pathname,
1423 mode);
1425 else
1427 /* There might be weird (e.g., CD-ROM or MS-DOS) filesystems
1428 mounted, which don't have Unix-like directory link counts. */
1429 process_path (cur_path, cur_name, false, pathname, mode);
1432 state.curdepth--;
1436 /* We're about to leave the directory. If there are any
1437 * -execdir argument lists which have been built but have not
1438 * yet been processed, do them now because they must be done in
1439 * the same directory.
1441 complete_pending_execdirs(get_eval_tree());
1443 if (strcmp (name, "."))
1445 enum SafeChdirStatus status;
1446 struct dir_id did;
1447 boolean did_stat = false;
1449 /* We could go back and do the next command-line arg
1450 instead, maybe using longjmp. */
1451 char const *dir;
1452 boolean deref = following_links() ? true : false;
1454 if ( (state.curdepth>0) && !deref)
1455 dir = "..";
1456 else
1458 chdir_back ();
1459 dir = parent;
1462 status = safely_chdir (dir, TraversingUp, &stat_buf, SymlinkHandleDefault, &did_stat);
1463 switch (status)
1465 case SafeChdirOK:
1466 break;
1468 case SafeChdirFailWouldBeUnableToReturn:
1469 error (1, errno, ".");
1470 return;
1472 case SafeChdirFailNonexistent:
1473 case SafeChdirFailStat:
1474 case SafeChdirFailSymlink:
1475 case SafeChdirFailNotDir:
1476 case SafeChdirFailChdirFailed:
1477 error (1, errno, "%s", pathname);
1478 return;
1481 if (dir_curr > 0)
1483 did.dev = dir_ids[dir_curr-1].dev;
1484 did.ino = dir_ids[dir_curr-1].ino;
1486 else
1488 did.dev = starting_stat_buf.st_dev;
1489 did.ino = starting_stat_buf.st_ino;
1493 if (cur_path)
1494 free (cur_path);
1495 free_dirinfo(dirinfo);