Migrated from GPL version 2 to GPL version 3
[findutils.git] / find / find.c
blobaa129500f8d1c5c1e3aba467fee3a6699f5ea9e5
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 3 of the License, or
8 (at your option) 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, see <http://www.gnu.org/licenses/>.
18 /* GNU find was written by Eric Decker <cire@cisco.com>,
19 with enhancements by David MacKenzie <djm@gnu.org>,
20 Jay Plett <jay@silence.princeton.nj.us>,
21 and Tim Wood <axolotl!tim@toad.com>.
22 The idea for -print0 and xargs -0 came from
23 Dan Bernstein <brnstnd@kramden.acf.nyu.edu>.
24 Improvements have been made by James Youngman <jay@gnu.org>.
28 #include <config.h>
29 #include "defs.h"
31 #define USE_SAFE_CHDIR 1
32 #undef STAT_MOUNTPOINTS
35 #include <errno.h>
36 #include <assert.h>
38 #include <sys/stat.h>
39 #ifdef HAVE_FCNTL_H
40 #include <fcntl.h>
41 #else
42 #include <sys/file.h>
43 #endif
44 #include <openat.h>
46 #include "xalloc.h"
47 #include "human.h"
48 #include "canonicalize.h"
49 #include <modetype.h>
51 #include "closeout.h"
52 #include "savedirinfo.h"
53 #include "buildcmd.h"
54 #include "dirname.h"
55 #include "quote.h"
56 #include "quotearg.h"
57 #include "xgetcwd.h"
58 #include "error.h"
60 #ifdef HAVE_LOCALE_H
61 #include <locale.h>
62 #endif
64 #if ENABLE_NLS
65 # include <libintl.h>
66 # define _(Text) gettext (Text)
67 #else
68 # define _(Text) Text
69 #define textdomain(Domain)
70 #define bindtextdomain(Package, Directory)
71 #endif
72 #ifdef gettext_noop
73 # define N_(String) gettext_noop (String)
74 #else
75 /* See locate.c for explanation as to why not use (String) */
76 # define N_(String) String
77 #endif
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, const struct stat *statp, char *parent));
89 /* Name this program was run with. */
90 char *program_name;
92 /* A file descriptor open to the initial working directory.
93 Doing it this way allows us to work when the i.w.d. has
94 unreadable parents. */
95 int starting_desc;
97 /* The stat buffer of the initial working directory. */
98 static struct stat starting_stat_buf;
100 enum ChdirSymlinkHandling
102 SymlinkHandleDefault, /* Normally the right choice */
103 SymlinkFollowOk /* see comment in process_top_path() */
107 enum TraversalDirection
109 TraversingUp,
110 TraversingDown
113 enum WdSanityCheckFatality
115 FATAL_IF_SANITY_CHECK_FAILS,
116 RETRY_IF_SANITY_CHECK_FAILS,
117 NON_FATAL_IF_SANITY_CHECK_FAILS
121 int get_current_dirfd(void)
123 return AT_FDCWD;
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];
135 state.exit_status = 0;
137 /* Set the option defaults before we do the locale
138 * initialisation as check_nofollow() needs to be executed in the
139 * POSIX locale.
141 set_option_defaults(&options);
143 #ifdef HAVE_SETLOCALE
144 setlocale (LC_ALL, "");
145 #endif
146 bindtextdomain (PACKAGE, LOCALEDIR);
147 textdomain (PACKAGE);
148 atexit (close_stdout);
150 /* Check for -P, -H or -L options. */
151 end_of_leading_options = process_leading_options(argc, argv);
153 if (options.debug_options & DebugStat)
154 options.xstat = debug_stat;
156 #ifdef DEBUG
157 fprintf (stderr, "cur_day_start = %s", ctime (&options.cur_day_start));
158 #endif /* DEBUG */
160 /* state.cwd_dir_fd has to be initialised before we call build_expression_tree()
161 * because command-line parsing may lead us to stat some files.
163 state.cwd_dir_fd = AT_FDCWD;
165 /* We are now processing the part of the "find" command line
166 * after the -H/-L options (if any).
168 eval_tree = build_expression_tree(argc, argv, end_of_leading_options);
171 /* safely_chdir() needs to check that it has ended up in the right place.
172 * To avoid bailing out when something gets automounted, it checks if
173 * the target directory appears to have had a directory mounted on it as
174 * we chdir()ed. The problem with this is that in order to notice that
175 * a file system was mounted, we would need to lstat() all the mount points.
176 * That strategy loses if our machine is a client of a dead NFS server.
178 * Hence if safely_chdir() and wd_sanity_check() can manage without needing
179 * to know the mounted device list, we do that.
181 if (!options.open_nofollow_available)
183 #ifdef STAT_MOUNTPOINTS
184 init_mounted_dev_list(0);
185 #endif
189 starting_desc = open (".", O_RDONLY
190 #if defined O_LARGEFILE
191 |O_LARGEFILE
192 #endif
194 if (0 <= starting_desc && fchdir (starting_desc) != 0)
196 close (starting_desc);
197 starting_desc = -1;
199 assert (starting_desc >= 0);
201 if (starting_desc < 0)
203 starting_dir = xgetcwd ();
204 if (! starting_dir)
205 error (1, errno, _("cannot get current directory"));
207 set_stat_placeholders(&starting_stat_buf);
208 if ((*options.xstat) (".", &starting_stat_buf) != 0)
209 error (1, errno, _("cannot stat current directory"));
211 /* If no paths are given, default to ".". */
212 for (i = end_of_leading_options; i < argc && !looks_like_expression(argv[i], true); i++)
214 process_top_path (argv[i], 0);
217 /* If there were no path arguments, default to ".". */
218 if (i == end_of_leading_options)
221 * We use a temporary variable here because some actions modify
222 * the path temporarily. Hence if we use a string constant,
223 * we get a coredump. The best example of this is if we say
224 * "find -printf %H" (note, not "find . -printf %H").
226 char defaultpath[2] = ".";
227 process_top_path (defaultpath, 0);
230 /* If "-exec ... {} +" has been used, there may be some
231 * partially-full command lines which have been built,
232 * but which are not yet complete. Execute those now.
234 show_success_rates(eval_tree);
235 cleanup();
236 return state.exit_status;
239 boolean is_fts_enabled(int *ftsoptions)
241 /* this version of find (i.e. this main()) does not use fts. */
242 *ftsoptions = 0;
243 return false;
247 static char *
248 specific_dirname(const char *dir)
250 char dirbuf[1024];
252 if (0 == strcmp(".", dir))
254 /* OK, what's '.'? */
255 if (NULL != getcwd(dirbuf, sizeof(dirbuf)))
257 return strdup(dirbuf);
259 else
261 return strdup(dir);
264 else
266 char *result = canonicalize_filename_mode(dir, CAN_EXISTING);
267 if (NULL == result)
268 return strdup(dir);
269 else
270 return result;
276 /* Return non-zero if FS is the name of a file system that is likely to
277 * be automounted
279 static int
280 fs_likely_to_be_automounted(const char *fs)
282 return ( (0==strcmp(fs, "nfs")) || (0==strcmp(fs, "autofs")) || (0==strcmp(fs, "subfs")));
287 #ifdef STAT_MOUNTPOINTS
288 static dev_t *mounted_devices = NULL;
289 static size_t num_mounted_devices = 0u;
292 static void
293 init_mounted_dev_list(int mandatory)
295 assert (NULL == mounted_devices);
296 assert (0 == num_mounted_devices);
297 mounted_devices = get_mounted_devices(&num_mounted_devices);
298 if (mandatory && (NULL == mounted_devices))
300 error(1, 0, "Cannot read list of mounted devices.");
304 static void
305 refresh_mounted_dev_list(void)
307 if (mounted_devices)
309 free(mounted_devices);
310 mounted_devices = 0;
312 num_mounted_devices = 0u;
313 init_mounted_dev_list(1);
317 /* Search for device DEV in the array LIST, which is of size N. */
318 static int
319 dev_present(dev_t dev, const dev_t *list, size_t n)
321 if (list)
323 while (n-- > 0u)
325 if ( (*list++) == dev )
326 return 1;
329 return 0;
332 enum MountPointStateChange
334 MountPointRecentlyMounted,
335 MountPointRecentlyUnmounted,
336 MountPointStateUnchanged
341 static enum MountPointStateChange
342 get_mount_state(dev_t newdev)
344 int new_is_present, new_was_present;
346 new_was_present = dev_present(newdev, mounted_devices, num_mounted_devices);
347 refresh_mounted_dev_list();
348 new_is_present = dev_present(newdev, mounted_devices, num_mounted_devices);
350 if (new_was_present == new_is_present)
351 return MountPointStateUnchanged;
352 else if (new_is_present)
353 return MountPointRecentlyMounted;
354 else
355 return MountPointRecentlyUnmounted;
360 /* We stat()ed a directory, chdir()ed into it (we know this
361 * since direction is TraversingDown), stat()ed it again,
362 * and noticed that the device numbers are different. Check
363 * if the file system was recently mounted.
365 * If it was, it looks like chdir()ing into the directory
366 * caused a file system to be mounted. Maybe automount is
367 * running. Anyway, that's probably OK - but it happens
368 * only when we are moving downward.
370 * We also allow for the possibility that a similar thing
371 * has happened with the unmounting of a file system. This
372 * is much rarer, as it relies on an automounter timeout
373 * occurring at exactly the wrong moment.
375 static enum WdSanityCheckFatality
376 dirchange_is_fatal(const char *specific_what,
377 enum WdSanityCheckFatality isfatal,
378 int silent,
379 struct stat *newinfo)
381 enum MountPointStateChange transition = get_mount_state(newinfo->st_dev);
382 switch (transition)
384 case MountPointRecentlyUnmounted:
385 isfatal = NON_FATAL_IF_SANITY_CHECK_FAILS;
386 if (!silent)
388 error (0, 0,
389 _("Warning: file system %s has recently been unmounted."),
390 safely_quote_err_filename(0, specific_what));
392 break;
394 case MountPointRecentlyMounted:
395 isfatal = NON_FATAL_IF_SANITY_CHECK_FAILS;
396 if (!silent)
398 error (0, 0,
399 _("Warning: file system %s has recently been mounted."),
400 safely_quote_err_filename(0, specific_what));
402 break;
404 case MountPointStateUnchanged:
405 /* leave isfatal as it is */
406 break;
409 return isfatal;
413 #endif
417 /* Examine the results of the stat() of a directory from before we
418 * entered or left it, with the results of stat()ing it afterward. If
419 * these are different, the file system tree has been modified while we
420 * were traversing it. That might be an attempt to use a race
421 * condition to persuade find to do something it didn't intend
422 * (e.g. an attempt by an ordinary user to exploit the fact that root
423 * sometimes runs find on the whole file system). However, this can
424 * also happen if automount is running (certainly on Solaris). With
425 * automount, moving into a directory can cause a file system to be
426 * mounted there.
428 * To cope sensibly with this, we will raise an error if we see the
429 * device number change unless we are chdir()ing into a subdirectory,
430 * and the directory we moved into has been mounted or unmounted "recently".
431 * Here "recently" means since we started "find" or we last re-read
432 * the /etc/mnttab file.
434 * If the device number does not change but the inode does, that is a
435 * problem.
437 * If the device number and inode are both the same, we are happy.
439 * If a file system is (un)mounted as we chdir() into the directory, that
440 * may mean that we're now examining a section of the file system that might
441 * have been excluded from consideration (via -prune or -quit for example).
442 * Hence we print a warning message to indicate that the output of find
443 * might be inconsistent due to the change in the file system.
445 static boolean
446 wd_sanity_check(const char *thing_to_stat,
447 const char *progname,
448 const char *what,
449 dev_t old_dev,
450 ino_t old_ino,
451 struct stat *newinfo,
452 int parent,
453 int line_no,
454 enum TraversalDirection direction,
455 enum WdSanityCheckFatality isfatal,
456 boolean *changed) /* output parameter */
458 const char *fstype;
459 char *specific_what = NULL;
460 int silent = 0;
461 const char *current_dir = ".";
463 *changed = false;
465 set_stat_placeholders(newinfo);
466 if ((*options.xstat) (current_dir, newinfo) != 0)
467 fatal_file_error(thing_to_stat);
469 if (old_dev != newinfo->st_dev)
471 *changed = true;
472 specific_what = specific_dirname(what);
473 fstype = filesystem_type(newinfo, current_dir);
474 silent = fs_likely_to_be_automounted(fstype);
476 /* This condition is rare, so once we are here it is
477 * reasonable to perform an expensive computation to
478 * determine if we should continue or fail.
480 if (TraversingDown == direction)
482 #ifdef STAT_MOUNTPOINTS
483 isfatal = dirchange_is_fatal(specific_what,isfatal,silent,newinfo);
484 #else
485 isfatal = RETRY_IF_SANITY_CHECK_FAILS;
486 #endif
489 switch (isfatal)
491 case FATAL_IF_SANITY_CHECK_FAILS:
493 fstype = filesystem_type(newinfo, current_dir);
494 error (1, 0,
495 _("%s%s changed during execution of %s (old device number %ld, new device number %ld, file system type is %s) [ref %ld]"),
496 safely_quote_err_filename(0, specific_what),
497 parent ? "/.." : "",
498 safely_quote_err_filename(1, progname),
499 (long) old_dev,
500 (long) newinfo->st_dev,
501 fstype,
502 (long)line_no);
503 /*NOTREACHED*/
504 return false;
507 case NON_FATAL_IF_SANITY_CHECK_FAILS:
509 /* Since the device has changed under us, the inode number
510 * will almost certainly also be different. However, we have
511 * already decided that this is not a problem. Hence we return
512 * without checking the inode number.
514 free(specific_what);
515 return true;
518 case RETRY_IF_SANITY_CHECK_FAILS:
519 return false;
523 /* Device number was the same, check if the inode has changed. */
524 if (old_ino != newinfo->st_ino)
526 *changed = true;
527 specific_what = specific_dirname(what);
528 fstype = filesystem_type(newinfo, current_dir);
530 error ((isfatal == FATAL_IF_SANITY_CHECK_FAILS) ? 1 : 0,
531 0, /* no relevant errno value */
532 _("%s%s changed during execution of %s (old inode number %ld, new inode number %ld, file system type is %s) [ref %ld]"),
533 safely_quote_err_filename(0, specific_what),
534 parent ? "/.." : "",
535 safely_quote_err_filename(1, progname),
536 (long) old_ino,
537 (long) newinfo->st_ino,
538 fstype,
539 (long)line_no);
540 free(specific_what);
541 return false;
544 return true;
547 enum SafeChdirStatus
549 SafeChdirOK,
550 SafeChdirFailSymlink,
551 SafeChdirFailNotDir,
552 SafeChdirFailStat,
553 SafeChdirFailWouldBeUnableToReturn,
554 SafeChdirFailChdirFailed,
555 SafeChdirFailNonexistent,
556 SafeChdirFailDestUnreadable
559 /* Safely perform a change in directory. We do this by calling
560 * lstat() on the subdirectory, using chdir() to move into it, and
561 * then lstat()ing ".". We compare the results of the two stat calls
562 * to see if they are consistent. If not, we sound the alarm.
564 * If following_links() is true, we do follow symbolic links.
566 static enum SafeChdirStatus
567 safely_chdir_lstat(const char *dest,
568 enum TraversalDirection direction,
569 struct stat *statbuf_dest,
570 enum ChdirSymlinkHandling symlink_follow_option,
571 boolean *did_stat)
573 struct stat statbuf_arrived;
574 int rv, dotfd=-1;
575 int saved_errno; /* specific_dirname() changes errno. */
576 boolean rv_set = false;
577 boolean statflag = false;
578 int tries = 0;
579 enum WdSanityCheckFatality isfatal = RETRY_IF_SANITY_CHECK_FAILS;
581 saved_errno = errno = 0;
583 dotfd = open(".", O_RDONLY
584 #if defined O_LARGEFILE
585 |O_LARGEFILE
586 #endif
589 /* We jump back to here if wd_sanity_check()
590 * recoverably triggers an alert.
592 retry:
593 ++tries;
595 if (dotfd >= 0)
597 /* Stat the directory we're going to. */
598 set_stat_placeholders(statbuf_dest);
599 if (0 == options.xstat(dest, statbuf_dest))
601 statflag = true;
603 #ifdef S_ISLNK
604 /* symlink_follow_option might be set to SymlinkFollowOk, which
605 * would allow us to chdir() into a symbolic link. This is
606 * only useful for the case where the directory we're
607 * chdir()ing into is the basename of a command line
608 * argument, for example where "foo/bar/baz" is specified on
609 * the command line. When -P is in effect (the default),
610 * baz will not be followed if it is a symlink, but if bar
611 * is a symlink, it _should_ be followed. Hence we need the
612 * ability to override the policy set by following_links().
614 if (!following_links() && S_ISLNK(statbuf_dest->st_mode))
616 /* We're not supposed to be following links, but this is
617 * a link. Check symlink_follow_option to see if we should
618 * make a special exception.
620 if (symlink_follow_option == SymlinkFollowOk)
622 /* We need to re-stat() the file so that the
623 * sanity check can pass.
625 if (0 != stat(dest, statbuf_dest))
627 rv = SafeChdirFailNonexistent;
628 rv_set = true;
629 saved_errno = errno;
630 goto fail;
632 statflag = true;
634 else
636 /* Not following symlinks, so the attempt to
637 * chdir() into a symlink should be prevented.
639 rv = SafeChdirFailSymlink;
640 rv_set = true;
641 saved_errno = 0; /* silence the error message */
642 goto fail;
645 #endif
646 #ifdef S_ISDIR
647 /* Although the immediately following chdir() would detect
648 * the fact that this is not a directory for us, this would
649 * result in an extra system call that fails. Anybody
650 * examining the system-call trace should ideally not be
651 * concerned that something is actually failing.
653 if (!S_ISDIR(statbuf_dest->st_mode))
655 rv = SafeChdirFailNotDir;
656 rv_set = true;
657 saved_errno = 0; /* silence the error message */
658 goto fail;
660 #endif
662 if (options.debug_options & DebugSearch)
663 fprintf(stderr, "safely_chdir(): chdir(\"%s\")\n", dest);
665 if (0 == chdir(dest))
667 /* check we ended up where we wanted to go */
668 boolean changed = false;
669 if (!wd_sanity_check(".", program_name, ".",
670 statbuf_dest->st_dev,
671 statbuf_dest->st_ino,
672 &statbuf_arrived,
673 0, __LINE__, direction,
674 isfatal,
675 &changed))
677 /* Only allow one failure. */
678 if (RETRY_IF_SANITY_CHECK_FAILS == isfatal)
680 if (0 == fchdir(dotfd))
682 isfatal = FATAL_IF_SANITY_CHECK_FAILS;
683 goto retry;
685 else
687 /* Failed to return to original directory,
688 * but we know that the current working
689 * directory is not the one that we intend
690 * to be in. Since fchdir() failed, we
691 * can't recover from this and so this error
692 * is fatal.
694 error(1, errno,
695 "failed to return to parent directory");
698 else
700 /* XXX: not sure what to use as an excuse here. */
701 rv = SafeChdirFailNonexistent;
702 rv_set = true;
703 saved_errno = 0;
704 goto fail;
708 close(dotfd);
709 return SafeChdirOK;
711 else
713 saved_errno = errno;
714 if (ENOENT == saved_errno)
716 rv = SafeChdirFailNonexistent;
717 rv_set = true;
718 if (options.ignore_readdir_race)
719 errno = 0; /* don't issue err msg */
721 else if (ENOTDIR == saved_errno)
723 /* This can happen if the we stat a directory,
724 * and then file system activity changes it into
725 * a non-directory.
727 saved_errno = 0; /* don't issue err msg */
728 rv = SafeChdirFailNotDir;
729 rv_set = true;
731 else
733 rv = SafeChdirFailChdirFailed;
734 rv_set = true;
736 goto fail;
739 else
741 saved_errno = errno;
742 rv = SafeChdirFailStat;
743 rv_set = true;
745 if ( (ENOENT == saved_errno) || (0 == state.curdepth))
746 saved_errno = 0; /* don't issue err msg */
747 goto fail;
750 else
752 /* We do not have read permissions on "." */
753 rv = SafeChdirFailWouldBeUnableToReturn;
754 rv_set = true;
755 goto fail;
758 /* This is the success path, so we clear errno. The caller probably
759 * won't be calling error() anyway.
761 saved_errno = 0;
763 /* We use the same exit path for success or failure.
764 * which has occurred is recorded in RV.
766 fail:
767 /* We do not call error() as this would result in a duplicate error
768 * message when the caller does the same thing.
770 if (saved_errno)
771 errno = saved_errno;
773 if (dotfd >= 0)
775 close(dotfd);
776 dotfd = -1;
779 *did_stat = statflag;
780 assert (rv_set);
781 return rv;
784 #if defined O_NOFOLLOW
785 /* Safely change working directory to the specified subdirectory. If
786 * we are not allowed to follow symbolic links, we use open() with
787 * O_NOFOLLOW, followed by fchdir(). This ensures that we don't
788 * follow symbolic links (of course, we do follow them if the -L
789 * option is in effect).
791 static enum SafeChdirStatus
792 safely_chdir_nofollow(const char *dest,
793 enum TraversalDirection direction,
794 struct stat *statbuf_dest,
795 enum ChdirSymlinkHandling symlink_follow_option,
796 boolean *did_stat)
798 int extraflags, fd;
800 (void) direction;
801 (void) statbuf_dest;
803 extraflags = 0;
804 *did_stat = false;
806 switch (symlink_follow_option)
808 case SymlinkFollowOk:
809 extraflags = 0;
810 break;
812 case SymlinkHandleDefault:
813 if (following_links())
814 extraflags = 0;
815 else
816 extraflags = O_NOFOLLOW;
817 break;
820 errno = 0;
821 fd = open(dest, O_RDONLY
822 #if defined O_LARGEFILE
823 |O_LARGEFILE
824 #endif
825 |extraflags);
826 if (fd < 0)
828 switch (errno)
830 case ELOOP:
831 return SafeChdirFailSymlink; /* This is why we use O_NOFOLLOW */
832 case ENOENT:
833 return SafeChdirFailNonexistent;
834 default:
835 return SafeChdirFailDestUnreadable;
839 errno = 0;
840 if (0 == fchdir(fd))
842 close(fd);
843 return SafeChdirOK;
845 else
847 int saved_errno = errno;
848 close(fd);
849 errno = saved_errno;
851 switch (errno)
853 case ENOTDIR:
854 return SafeChdirFailNotDir;
856 case EACCES:
857 case EBADF: /* Shouldn't happen */
858 case EINTR:
859 case EIO:
860 default:
861 return SafeChdirFailChdirFailed;
865 #endif
867 static enum SafeChdirStatus
868 safely_chdir(const char *dest,
869 enum TraversalDirection direction,
870 struct stat *statbuf_dest,
871 enum ChdirSymlinkHandling symlink_follow_option,
872 boolean *did_stat)
874 enum SafeChdirStatus result;
876 /* We're about to leave a directory. If there are any -execdir
877 * argument lists which have been built but have not yet been
878 * processed, do them now because they must be done in the same
879 * directory.
881 complete_pending_execdirs(get_current_dirfd());
883 #if !defined(O_NOFOLLOW)
884 options.open_nofollow_available = false;
885 #endif
886 if (options.open_nofollow_available)
887 result = safely_chdir_nofollow(dest, direction, statbuf_dest, symlink_follow_option, did_stat);
888 else
889 result = safely_chdir_lstat(dest, direction, statbuf_dest, symlink_follow_option, did_stat);
890 return result;
895 /* Safely go back to the starting directory. */
896 static void
897 chdir_back (void)
899 struct stat stat_buf;
900 boolean dummy;
902 if (starting_desc < 0)
904 if (options.debug_options & DebugSearch)
905 fprintf(stderr, "chdir_back(): chdir(\"%s\")\n", starting_dir);
907 #ifdef STAT_MOUNTPOINTS
908 /* We will need the mounted device list. Get it now if we don't
909 * already have it.
911 if (NULL == mounted_devices)
912 init_mounted_dev_list(1);
913 #endif
915 if (chdir (starting_dir) != 0)
916 fatal_file_error(starting_dir);
918 wd_sanity_check(starting_dir,
919 program_name,
920 starting_dir,
921 starting_stat_buf.st_dev,
922 starting_stat_buf.st_ino,
923 &stat_buf, 0, __LINE__,
924 TraversingUp,
925 FATAL_IF_SANITY_CHECK_FAILS,
926 &dummy);
928 else
930 if (options.debug_options & DebugSearch)
931 fprintf(stderr, "chdir_back(): chdir(<starting-point>)\n");
933 if (fchdir (starting_desc) != 0)
935 fatal_file_error(starting_dir);
940 /* Move to the parent of a given directory and then call a function,
941 * restoring the cwd. Don't bother changing directory if the
942 * specified directory is a child of "." or is the root directory.
944 static void
945 at_top (char *pathname,
946 mode_t mode,
947 struct stat *pstat,
948 void (*action)(char *pathname,
949 char *basename,
950 int mode,
951 struct stat *pstat))
953 int dirchange;
954 char *parent_dir = dir_name(pathname);
955 char *base = base_name(pathname);
957 state.curdepth = 0;
958 state.starting_path_length = strlen (pathname);
960 if (0 == strcmp(pathname, parent_dir)
961 || 0 == strcmp(parent_dir, "."))
963 dirchange = 0;
964 base = pathname;
966 else
968 enum TraversalDirection direction;
969 enum SafeChdirStatus chdir_status;
970 struct stat st;
971 boolean did_stat = false;
973 dirchange = 1;
974 if (0 == strcmp(base, ".."))
975 direction = TraversingUp;
976 else
977 direction = TraversingDown;
979 /* We pass SymlinkFollowOk to safely_chdir(), which allows it to
980 * chdir() into a symbolic link. This is only useful for the
981 * case where the directory we're chdir()ing into is the
982 * basename of a command line argument, for example where
983 * "foo/bar/baz" is specified on the command line. When -P is
984 * in effect (the default), baz will not be followed if it is a
985 * symlink, but if bar is a symlink, it _should_ be followed.
986 * Hence we need the ability to override the policy set by
987 * following_links().
989 chdir_status = safely_chdir(parent_dir, direction, &st, SymlinkFollowOk, &did_stat);
990 if (SafeChdirOK != chdir_status)
992 const char *what = (SafeChdirFailWouldBeUnableToReturn == chdir_status) ? "." : parent_dir;
993 if (errno)
994 error (0, errno, "%s",
995 safely_quote_err_filename(0, what));
996 else
997 error (0, 0, _("Failed to safely change directory into %s"),
998 safely_quote_err_filename(0, parent_dir));
1000 /* We can't process this command-line argument. */
1001 state.exit_status = 1;
1002 return;
1006 free (parent_dir);
1007 parent_dir = NULL;
1009 action(pathname, base, mode, pstat);
1011 if (dirchange)
1013 chdir_back();
1018 static void do_process_top_dir(char *pathname,
1019 char *base,
1020 int mode,
1021 struct stat *pstat)
1023 (void) pstat;
1025 process_path (pathname, base, false, ".", mode);
1026 complete_pending_execdirs(get_current_dirfd());
1029 static void do_process_predicate(char *pathname,
1030 char *base,
1031 int mode,
1032 struct stat *pstat)
1034 (void) mode;
1036 state.rel_pathname = base; /* cwd_dir_fd was already set by safely_chdir */
1037 apply_predicate (pathname, pstat, get_eval_tree());
1043 /* Descend PATHNAME, which is a command-line argument.
1045 Actions like -execdir assume that we are in the
1046 parent directory of the file we're examining,
1047 and on entry to this function our working directory
1048 is whatever it was when find was invoked. Therefore
1049 If PATHNAME is "." we just leave things as they are.
1050 Otherwise, we figure out what the parent directory is,
1051 and move to that.
1053 static void
1054 process_top_path (char *pathname, mode_t mode)
1056 at_top(pathname, mode, NULL, do_process_top_dir);
1060 /* Info on each directory in the current tree branch, to avoid
1061 getting stuck in symbolic link loops. */
1062 static struct dir_id *dir_ids = NULL;
1063 /* Entries allocated in `dir_ids'. */
1064 static int dir_alloc = 0;
1065 /* Index in `dir_ids' of directory currently being searched.
1066 This is always the last valid entry. */
1067 static int dir_curr = -1;
1068 /* (Arbitrary) number of entries to grow `dir_ids' by. */
1069 #define DIR_ALLOC_STEP 32
1073 /* We've detected a file system loop. This is caused by one of
1074 * two things:
1076 * 1. Option -L is in effect and we've hit a symbolic link that
1077 * points to an ancestor. This is harmless. We won't traverse the
1078 * symbolic link.
1080 * 2. We have hit a real cycle in the directory hierarchy. In this
1081 * case, we issue a diagnostic message (POSIX requires this) and we
1082 * skip that directory entry.
1084 static void
1085 issue_loop_warning(const char *name, const char *pathname, int level)
1087 struct stat stbuf_link;
1088 if (lstat(name, &stbuf_link) != 0)
1089 stbuf_link.st_mode = S_IFREG;
1091 if (S_ISLNK(stbuf_link.st_mode))
1093 error(0, 0,
1094 _("Symbolic link %s is part of a loop in the directory hierarchy; we have already visited the directory to which it points."),
1095 safely_quote_err_filename(0, pathname));
1097 else
1099 int distance = 1 + (dir_curr-level);
1100 /* We have found an infinite loop. POSIX requires us to
1101 * issue a diagnostic. Usually we won't get to here
1102 * because when the leaf optimisation is on, it will cause
1103 * the subdirectory to be skipped. If /a/b/c/d is a hard
1104 * link to /a/b, then the link count of /a/b/c is 2,
1105 * because the ".." entry of /b/b/c/d points to /a, not
1106 * to /a/b/c.
1108 error(0, 0,
1109 _("Filesystem loop detected; %s has the same device number and inode as a directory which is %d %s."),
1110 safely_quote_err_filename(0, pathname),
1111 distance,
1112 (distance == 1 ?
1113 _("level higher in the file system hierarchy") :
1114 _("levels higher in the file system hierarchy")));
1120 /* Recursively descend path PATHNAME, applying the predicates.
1121 LEAF is true if PATHNAME is known to be in a directory that has no
1122 more unexamined subdirectories, and therefore it is not a directory.
1123 Knowing this allows us to avoid calling stat as long as possible for
1124 leaf files.
1126 NAME is PATHNAME relative to the current directory. We access NAME
1127 but print PATHNAME.
1129 PARENT is the path of the parent of NAME, relative to find's
1130 starting directory.
1132 Return nonzero iff PATHNAME is a directory. */
1134 static int
1135 process_path (char *pathname, char *name, boolean leaf, char *parent,
1136 mode_t mode)
1138 struct stat stat_buf;
1139 static dev_t root_dev; /* Device ID of current argument pathname. */
1140 int i;
1141 struct predicate *eval_tree;
1143 eval_tree = get_eval_tree();
1144 /* Assume it is a non-directory initially. */
1145 stat_buf.st_mode = 0;
1146 state.rel_pathname = name;
1147 state.type = 0;
1148 state.have_stat = false;
1149 state.have_type = false;
1151 if (!digest_mode(mode, pathname, name, &stat_buf, leaf))
1152 return 0;
1154 if (!S_ISDIR (state.type))
1156 if (state.curdepth >= options.mindepth)
1157 apply_predicate (pathname, &stat_buf, eval_tree);
1158 return 0;
1161 /* From here on, we're working on a directory. */
1164 /* Now we really need to stat the directory, even if we know the
1165 * type, because we need information like struct stat.st_rdev.
1167 if (get_statinfo(pathname, name, &stat_buf) != 0)
1168 return 0;
1170 state.have_stat = true;
1171 mode = state.type = stat_buf.st_mode; /* use full info now that we have it. */
1172 state.stop_at_current_level =
1173 options.maxdepth >= 0
1174 && state.curdepth >= options.maxdepth;
1176 /* If we've already seen this directory on this branch,
1177 don't descend it again. */
1178 for (i = 0; i <= dir_curr; i++)
1179 if (stat_buf.st_ino == dir_ids[i].ino &&
1180 stat_buf.st_dev == dir_ids[i].dev)
1182 state.stop_at_current_level = true;
1183 issue_loop_warning(name, pathname, i);
1186 if (dir_alloc <= ++dir_curr)
1188 dir_alloc += DIR_ALLOC_STEP;
1189 dir_ids = (struct dir_id *)
1190 xrealloc ((char *) dir_ids, dir_alloc * sizeof (struct dir_id));
1192 dir_ids[dir_curr].ino = stat_buf.st_ino;
1193 dir_ids[dir_curr].dev = stat_buf.st_dev;
1195 if (options.stay_on_filesystem)
1197 if (state.curdepth == 0)
1198 root_dev = stat_buf.st_dev;
1199 else if (stat_buf.st_dev != root_dev)
1200 state.stop_at_current_level = true;
1203 if (options.do_dir_first && state.curdepth >= options.mindepth)
1204 apply_predicate (pathname, &stat_buf, eval_tree);
1206 if (options.debug_options & DebugSearch)
1207 fprintf(stderr, "pathname = %s, stop_at_current_level = %d\n",
1208 pathname, state.stop_at_current_level);
1210 if (state.stop_at_current_level == false)
1212 /* Scan directory on disk. */
1213 process_dir (pathname, name, strlen (pathname), &stat_buf, parent);
1216 if (options.do_dir_first == false && state.curdepth >= options.mindepth)
1218 /* The fields in 'state' are now out of date. Correct them.
1220 if (!digest_mode(mode, pathname, name, &stat_buf, leaf))
1221 return 0;
1223 if (0 == dir_curr)
1225 at_top(pathname, mode, &stat_buf, do_process_predicate);
1227 else
1229 do_process_predicate(pathname, name, mode, &stat_buf);
1233 dir_curr--;
1235 return 1;
1239 /* Scan directory PATHNAME and recurse through process_path for each entry.
1241 PATHLEN is the length of PATHNAME.
1243 NAME is PATHNAME relative to the current directory.
1245 STATP is the results of *options.xstat on it.
1247 PARENT is the path of the parent of NAME, relative to find's
1248 starting directory. */
1250 static void
1251 process_dir (char *pathname, char *name, int pathlen, const struct stat *statp, char *parent)
1253 int subdirs_left; /* Number of unexamined subdirs in PATHNAME. */
1254 boolean subdirs_unreliable; /* if true, cannot use dir link count as subdir limif (if false, it may STILL be unreliable) */
1255 unsigned int idx; /* Which entry are we on? */
1256 struct stat stat_buf;
1257 size_t dircount = 0u;
1258 struct savedir_dirinfo *dirinfo;
1259 #if 0
1260 printf("process_dir: pathname=%s name=%s statp->st_nlink=%d st_ino=%d\n",
1261 pathname,
1262 name,
1263 (int)statp->st_nlink,
1264 (int)statp->st_ino);
1265 #endif
1266 if (statp->st_nlink < 2)
1268 subdirs_unreliable = true;
1269 subdirs_left = 0;
1271 else
1273 subdirs_unreliable = false; /* not necessarily right */
1274 subdirs_left = statp->st_nlink - 2; /* Account for name and ".". */
1277 errno = 0;
1278 dirinfo = xsavedir(name, 0);
1281 if (dirinfo == NULL)
1283 assert (errno != 0);
1284 error (0, errno, "%s", safely_quote_err_filename(0, pathname));
1285 state.exit_status = 1;
1287 else
1289 register char *namep; /* Current point in `name_space'. */
1290 char *cur_path; /* Full path of each file to process. */
1291 char *cur_name; /* Base name of each file to process. */
1292 unsigned cur_path_size; /* Bytes allocated for `cur_path'. */
1293 register unsigned file_len; /* Length of each path to process. */
1294 register unsigned pathname_len; /* PATHLEN plus trailing '/'. */
1295 boolean did_stat = false;
1297 if (pathname[pathlen - 1] == '/')
1298 pathname_len = pathlen + 1; /* For '\0'; already have '/'. */
1299 else
1300 pathname_len = pathlen + 2; /* For '/' and '\0'. */
1301 cur_path_size = 0;
1302 cur_path = NULL;
1304 /* We're about to leave the directory. If there are any
1305 * -execdir argument lists which have been built but have not
1306 * yet been processed, do them now because they must be done in
1307 * the same directory.
1309 complete_pending_execdirs(get_current_dirfd());
1311 if (strcmp (name, "."))
1313 enum SafeChdirStatus status = safely_chdir (name, TraversingDown, &stat_buf, SymlinkHandleDefault, &did_stat);
1314 switch (status)
1316 case SafeChdirOK:
1317 /* If there had been a change but wd_sanity_check()
1318 * accepted it, we need to accept that on the
1319 * way back up as well, so modify our record
1320 * of what we think we should see later.
1321 * If there was no change, the assignments are a no-op.
1323 * However, before performing the assignment, we need to
1324 * check that we have the stat information. If O_NOFOLLOW
1325 * is available, safely_chdir() will not have needed to use
1326 * stat(), and so stat_buf will just contain random data.
1328 if (!did_stat)
1330 /* If there is a link we need to follow it. Hence
1331 * the direct call to stat() not through (options.xstat)
1333 set_stat_placeholders(&stat_buf);
1334 if (0 != stat(".", &stat_buf))
1335 break; /* skip the assignment. */
1337 dir_ids[dir_curr].dev = stat_buf.st_dev;
1338 dir_ids[dir_curr].ino = stat_buf.st_ino;
1340 break;
1342 case SafeChdirFailWouldBeUnableToReturn:
1343 error (0, errno, ".");
1344 state.exit_status = 1;
1345 break;
1347 case SafeChdirFailNonexistent:
1348 case SafeChdirFailDestUnreadable:
1349 case SafeChdirFailStat:
1350 case SafeChdirFailNotDir:
1351 case SafeChdirFailChdirFailed:
1352 error (0, errno, "%s",
1353 safely_quote_err_filename(0, pathname));
1354 state.exit_status = 1;
1355 return;
1357 case SafeChdirFailSymlink:
1358 error (0, 0,
1359 _("warning: not following the symbolic link %s"),
1360 safely_quote_err_filename(0, pathname));
1361 state.exit_status = 1;
1362 return;
1366 for (idx=0; idx < dirinfo->size; ++idx)
1368 /* savedirinfo() may return dirinfo=NULL if extended information
1369 * is not available.
1371 mode_t mode = (dirinfo->entries[idx].flags & SavedirHaveFileType) ?
1372 dirinfo->entries[idx].type_info : 0;
1373 namep = dirinfo->entries[idx].name;
1375 /* Append this directory entry's name to the path being searched. */
1376 file_len = pathname_len + strlen (namep);
1377 if (file_len > cur_path_size)
1379 while (file_len > cur_path_size)
1380 cur_path_size += 1024;
1381 if (cur_path)
1382 free (cur_path);
1383 cur_path = xmalloc (cur_path_size);
1384 strcpy (cur_path, pathname);
1385 cur_path[pathname_len - 2] = '/';
1387 cur_name = cur_path + pathname_len - 1;
1388 strcpy (cur_name, namep);
1390 state.curdepth++;
1391 if (!options.no_leaf_check && !subdirs_unreliable)
1393 if (mode && S_ISDIR(mode) && (subdirs_left == 0))
1395 /* This is a subdirectory, but the number of directories we
1396 * have found now exceeds the number we would expect given
1397 * the hard link count on the parent. This is likely to be
1398 * a bug in the file system driver (e.g. Linux's
1399 * /proc file system) or may just be a fact that the OS
1400 * doesn't really handle hard links with Unix semantics.
1401 * In the latter case, -noleaf should be used routinely.
1403 error(0, 0, _("WARNING: Hard link count is wrong for %s (saw only st_nlink=%d but we already saw %d subdirectories): this may be a bug in your file system driver. Automatically turning on find's -noleaf option. Earlier results may have failed to include directories that should have been searched."),
1404 safely_quote_err_filename(0, pathname),
1405 statp->st_nlink,
1406 dircount);
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_unreliable = 1;
1411 subdirs_left = 1; /* band-aid for this iteration. */
1414 /* Normal case optimization. On normal Unix
1415 file systems, a directory that has no subdirectories
1416 has two links: its name, and ".". Any additional
1417 links are to the ".." entries of its subdirectories.
1418 Once we have processed as many subdirectories as
1419 there are additional links, we know that the rest of
1420 the entries are non-directories -- in other words,
1421 leaf files. */
1423 int count;
1424 count = process_path (cur_path, cur_name,
1425 subdirs_left == 0, pathname,
1426 mode);
1427 subdirs_left -= count;
1428 dircount += count;
1431 else
1433 /* There might be weird (e.g., CD-ROM or MS-DOS) file systems
1434 mounted, which don't have Unix-like directory link counts. */
1435 process_path (cur_path, cur_name, false, pathname, mode);
1438 state.curdepth--;
1442 /* We're about to leave the directory. If there are any
1443 * -execdir argument lists which have been built but have not
1444 * yet been processed, do them now because they must be done in
1445 * the same directory.
1447 complete_pending_execdirs(get_current_dirfd());
1449 if (strcmp (name, "."))
1451 enum SafeChdirStatus status;
1452 struct dir_id did;
1454 /* We could go back and do the next command-line arg
1455 instead, maybe using longjmp. */
1456 char const *dir;
1457 boolean deref = following_links() ? true : false;
1459 if ( (state.curdepth>0) && !deref)
1460 dir = "..";
1461 else
1463 chdir_back ();
1464 dir = parent;
1467 did_stat = false;
1468 status = safely_chdir (dir, TraversingUp, &stat_buf, SymlinkHandleDefault, &did_stat);
1469 switch (status)
1471 case SafeChdirOK:
1472 break;
1474 case SafeChdirFailWouldBeUnableToReturn:
1475 error (1, errno, ".");
1476 return;
1478 case SafeChdirFailNonexistent:
1479 case SafeChdirFailDestUnreadable:
1480 case SafeChdirFailStat:
1481 case SafeChdirFailSymlink:
1482 case SafeChdirFailNotDir:
1483 case SafeChdirFailChdirFailed:
1484 error (1, errno, "%s", safely_quote_err_filename(0, pathname));
1485 return;
1488 if (dir_curr > 0)
1490 did.dev = dir_ids[dir_curr-1].dev;
1491 did.ino = dir_ids[dir_curr-1].ino;
1493 else
1495 did.dev = starting_stat_buf.st_dev;
1496 did.ino = starting_stat_buf.st_ino;
1500 if (cur_path)
1501 free (cur_path);
1502 free_dirinfo(dirinfo);
1505 if (subdirs_unreliable)
1507 /* Make sure we hasn't used the variable subdirs_left if we knew
1508 * we shouldn't do so.
1510 assert (0 == subdirs_left || options.no_leaf_check);