Fixed typos in error messages. Correction from Jakub Bogusz <qboosh@pld-linux.org>
[findutils.git] / find / find.c
blob2bf6bf65a6437b0ab7c840f0117dd6b67531107b
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
45 #include <openat.h>
47 #include "../gnulib/lib/xalloc.h"
48 #include "../gnulib/lib/human.h"
49 #include "../gnulib/lib/canonicalize.h"
50 #include <modetype.h>
52 #include "closeout.h"
53 #include "savedirinfo.h"
54 #include "buildcmd.h"
55 #include "dirname.h"
56 #include "quote.h"
57 #include "quotearg.h"
58 #include "xgetcwd.h"
59 #include "error.h"
61 #ifdef HAVE_LOCALE_H
62 #include <locale.h>
63 #endif
65 #if ENABLE_NLS
66 # include <libintl.h>
67 # define _(Text) gettext (Text)
68 #else
69 # define _(Text) Text
70 #define textdomain(Domain)
71 #define bindtextdomain(Package, Directory)
72 #endif
73 #ifdef gettext_noop
74 # define N_(String) gettext_noop (String)
75 #else
76 /* See locate.c for explanation as to why not use (String) */
77 # define N_(String) String
78 #endif
80 #ifdef STAT_MOUNTPOINTS
81 static void init_mounted_dev_list(int mandatory);
82 #endif
84 static void process_top_path PARAMS((char *pathname, mode_t mode));
85 static int process_path PARAMS((char *pathname, char *name, boolean leaf, char *parent, mode_t type));
86 static void process_dir PARAMS((char *pathname, char *name, int pathlen, const struct stat *statp, char *parent));
90 /* Name this program was run with. */
91 char *program_name;
93 /* A file descriptor open to the initial working directory.
94 Doing it this way allows us to work when the i.w.d. has
95 unreadable parents. */
96 int starting_desc;
98 /* The stat buffer of the initial working directory. */
99 static struct stat starting_stat_buf;
101 enum ChdirSymlinkHandling
103 SymlinkHandleDefault, /* Normally the right choice */
104 SymlinkFollowOk /* see comment in process_top_path() */
108 enum TraversalDirection
110 TraversingUp,
111 TraversingDown
114 enum WdSanityCheckFatality
116 FATAL_IF_SANITY_CHECK_FAILS,
117 RETRY_IF_SANITY_CHECK_FAILS,
118 NON_FATAL_IF_SANITY_CHECK_FAILS
122 int get_current_dirfd(void)
124 return AT_FDCWD;
129 main (int argc, char **argv)
131 int i;
132 int end_of_leading_options = 0; /* First arg after any -H/-L etc. */
133 struct predicate *eval_tree;
135 program_name = argv[0];
136 state.exit_status = 0;
138 /* Set the option defaults before we do the the locale
139 * initialisation as check_nofollow() needs to be executed in the
140 * POSIX locale.
142 set_option_defaults(&options);
144 #ifdef HAVE_SETLOCALE
145 setlocale (LC_ALL, "");
146 #endif
147 bindtextdomain (PACKAGE, LOCALEDIR);
148 textdomain (PACKAGE);
149 atexit (close_stdout);
151 /* Check for -P, -H or -L options. */
152 end_of_leading_options = process_leading_options(argc, argv);
154 if (options.debug_options & DebugStat)
155 options.xstat = debug_stat;
157 #ifdef DEBUG
158 fprintf (stderr, "cur_day_start = %s", ctime (&options.cur_day_start));
159 #endif /* DEBUG */
161 /* state.cwd_dir_fd has to be initialised before we call build_expression_tree()
162 * because command-line parsing may lead us to stat some files.
164 state.cwd_dir_fd = AT_FDCWD;
166 /* We are now processing the part of the "find" command line
167 * after the -H/-L options (if any).
169 eval_tree = build_expression_tree(argc, argv, end_of_leading_options);
172 /* safely_chdir() needs to check that it has ended up in the right place.
173 * To avoid bailing out when something gets automounted, it checks if
174 * the target directory appears to have had a directory mounted on it as
175 * we chdir()ed. The problem with this is that in order to notice that
176 * a filesystem was mounted, we would need to lstat() all the mount points.
177 * That strategy loses if our machine is a client of a dead NFS server.
179 * Hence if safely_chdir() and wd_sanity_check() can manage without needing
180 * to know the mounted device list, we do that.
182 if (!options.open_nofollow_available)
184 #ifdef STAT_MOUNTPOINTS
185 init_mounted_dev_list(0);
186 #endif
190 starting_desc = open (".", O_RDONLY
191 #if defined O_LARGEFILE
192 |O_LARGEFILE
193 #endif
195 if (0 <= starting_desc && fchdir (starting_desc) != 0)
197 close (starting_desc);
198 starting_desc = -1;
200 assert(starting_desc >= 0);
202 if (starting_desc < 0)
204 starting_dir = xgetcwd ();
205 if (! starting_dir)
206 error (1, errno, _("cannot get current directory"));
208 set_stat_placeholders(&starting_stat_buf);
209 if ((*options.xstat) (".", &starting_stat_buf) != 0)
210 error (1, errno, _("cannot stat current directory"));
212 /* If no paths are given, default to ".". */
213 for (i = end_of_leading_options; i < argc && !looks_like_expression(argv[i], true); i++)
215 process_top_path (argv[i], 0);
218 /* If there were no path arguments, default to ".". */
219 if (i == end_of_leading_options)
222 * We use a temporary variable here because some actions modify
223 * the path temporarily. Hence if we use a string constant,
224 * we get a coredump. The best example of this is if we say
225 * "find -printf %H" (note, not "find . -printf %H").
227 char defaultpath[2] = ".";
228 process_top_path (defaultpath, 0);
231 /* If "-exec ... {} +" has been used, there may be some
232 * partially-full command lines which have been built,
233 * but which are not yet complete. Execute those now.
235 show_success_rates(eval_tree);
236 cleanup();
237 return state.exit_status;
240 boolean is_fts_enabled(int *ftsoptions)
242 /* this version of find (i.e. this main()) does not use fts. */
243 *ftsoptions = 0;
244 return false;
248 static char *
249 specific_dirname(const char *dir)
251 char dirbuf[1024];
253 if (0 == strcmp(".", dir))
255 /* OK, what's '.'? */
256 if (NULL != getcwd(dirbuf, sizeof(dirbuf)))
258 return strdup(dirbuf);
260 else
262 return strdup(dir);
265 else
267 char *result = canonicalize_filename_mode(dir, CAN_EXISTING);
268 if (NULL == result)
269 return strdup(dir);
270 else
271 return result;
277 /* Return non-zero if FS is the name of a filesystem that is likely to
278 * be automounted
280 static int
281 fs_likely_to_be_automounted(const char *fs)
283 return ( (0==strcmp(fs, "nfs")) || (0==strcmp(fs, "autofs")) || (0==strcmp(fs, "subfs")));
288 #ifdef STAT_MOUNTPOINTS
289 static dev_t *mounted_devices = NULL;
290 static size_t num_mounted_devices = 0u;
293 static void
294 init_mounted_dev_list(int mandatory)
296 assert(NULL == mounted_devices);
297 assert(0 == num_mounted_devices);
298 mounted_devices = get_mounted_devices(&num_mounted_devices);
299 if (mandatory && (NULL == mounted_devices))
301 error(1, 0, "Cannot read list of mounted devices.");
305 static void
306 refresh_mounted_dev_list(void)
308 if (mounted_devices)
310 free(mounted_devices);
311 mounted_devices = 0;
313 num_mounted_devices = 0u;
314 init_mounted_dev_list(1);
318 /* Search for device DEV in the array LIST, which is of size N. */
319 static int
320 dev_present(dev_t dev, const dev_t *list, size_t n)
322 if (list)
324 while (n-- > 0u)
326 if ( (*list++) == dev )
327 return 1;
330 return 0;
333 enum MountPointStateChange
335 MountPointRecentlyMounted,
336 MountPointRecentlyUnmounted,
337 MountPointStateUnchanged
342 static enum MountPointStateChange
343 get_mount_state(dev_t newdev)
345 int new_is_present, new_was_present;
347 new_was_present = dev_present(newdev, mounted_devices, num_mounted_devices);
348 refresh_mounted_dev_list();
349 new_is_present = dev_present(newdev, mounted_devices, num_mounted_devices);
351 if (new_was_present == new_is_present)
352 return MountPointStateUnchanged;
353 else if (new_is_present)
354 return MountPointRecentlyMounted;
355 else
356 return MountPointRecentlyUnmounted;
361 /* We stat()ed a directory, chdir()ed into it (we know this
362 * since direction is TraversingDown), stat()ed it again,
363 * and noticed that the device numbers are different. Check
364 * if the filesystem was recently mounted.
366 * If it was, it looks like chdir()ing into the directory
367 * caused a filesystem to be mounted. Maybe automount is
368 * running. Anyway, that's probably OK - but it happens
369 * only when we are moving downward.
371 * We also allow for the possibility that a similar thing
372 * has happened with the unmounting of a filesystem. This
373 * is much rarer, as it relies on an automounter timeout
374 * occurring at exactly the wrong moment.
376 static enum WdSanityCheckFatality
377 dirchange_is_fatal(const char *specific_what,
378 enum WdSanityCheckFatality isfatal,
379 int silent,
380 struct stat *newinfo)
382 enum MountPointStateChange transition = get_mount_state(newinfo->st_dev);
383 switch (transition)
385 case MountPointRecentlyUnmounted:
386 isfatal = NON_FATAL_IF_SANITY_CHECK_FAILS;
387 if (!silent)
389 error (0, 0,
390 _("Warning: filesystem %s has recently been unmounted."),
391 safely_quote_err_filename(0, specific_what));
393 break;
395 case MountPointRecentlyMounted:
396 isfatal = NON_FATAL_IF_SANITY_CHECK_FAILS;
397 if (!silent)
399 error (0, 0,
400 _("Warning: filesystem %s has recently been mounted."),
401 safely_quote_err_filename(0, specific_what));
403 break;
405 case MountPointStateUnchanged:
406 /* leave isfatal as it is */
407 break;
410 return isfatal;
414 #endif
418 /* Examine the results of the stat() of a directory from before we
419 * entered or left it, with the results of stat()ing it afterward. If
420 * these are different, the filesystem tree has been modified while we
421 * were traversing it. That might be an attempt to use a race
422 * condition to persuade find to do something it didn't intend
423 * (e.g. an attempt by an ordinary user to exploit the fact that root
424 * sometimes runs find on the whole filesystem). However, this can
425 * also happen if automount is running (certainly on Solaris). With
426 * automount, moving into a directory can cause a filesystem to be
427 * mounted there.
429 * To cope sensibly with this, we will raise an error if we see the
430 * device number change unless we are chdir()ing into a subdirectory,
431 * and the directory we moved into has been mounted or unmounted "recently".
432 * Here "recently" means since we started "find" or we last re-read
433 * the /etc/mnttab file.
435 * If the device number does not change but the inode does, that is a
436 * problem.
438 * If the device number and inode are both the same, we are happy.
440 * If a filesystem is (un)mounted as we chdir() into the directory, that
441 * may mean that we're now examining a section of the filesystem that might
442 * have been excluded from consideration (via -prune or -quit for example).
443 * Hence we print a warning message to indicate that the output of find
444 * might be inconsistent due to the change in the filesystem.
446 static boolean
447 wd_sanity_check(const char *thing_to_stat,
448 const char *progname,
449 const char *what,
450 dev_t old_dev,
451 ino_t old_ino,
452 struct stat *newinfo,
453 int parent,
454 int line_no,
455 enum TraversalDirection direction,
456 enum WdSanityCheckFatality isfatal,
457 boolean *changed) /* output parameter */
459 const char *fstype;
460 char *specific_what = NULL;
461 int silent = 0;
462 const char *current_dir = ".";
464 *changed = false;
466 set_stat_placeholders(newinfo);
467 if ((*options.xstat) (current_dir, newinfo) != 0)
468 fatal_file_error(thing_to_stat);
470 if (old_dev != newinfo->st_dev)
472 *changed = true;
473 specific_what = specific_dirname(what);
474 fstype = filesystem_type(newinfo, current_dir);
475 silent = fs_likely_to_be_automounted(fstype);
477 /* This condition is rare, so once we are here it is
478 * reasonable to perform an expensive computation to
479 * determine if we should continue or fail.
481 if (TraversingDown == direction)
483 #ifdef STAT_MOUNTPOINTS
484 isfatal = dirchange_is_fatal(specific_what,isfatal,silent,newinfo);
485 #else
486 isfatal = RETRY_IF_SANITY_CHECK_FAILS;
487 #endif
490 switch (isfatal)
492 case FATAL_IF_SANITY_CHECK_FAILS:
494 fstype = filesystem_type(newinfo, current_dir);
495 error (1, 0,
496 _("%s%s changed during execution of %s (old device number %ld, new device number %ld, filesystem type is %s) [ref %ld]"),
497 safely_quote_err_filename(0, specific_what),
498 parent ? "/.." : "",
499 safely_quote_err_filename(1, progname),
500 (long) old_dev,
501 (long) newinfo->st_dev,
502 fstype,
503 (long)line_no);
504 /*NOTREACHED*/
505 return false;
508 case NON_FATAL_IF_SANITY_CHECK_FAILS:
510 /* Since the device has changed under us, the inode number
511 * will almost certainly also be different. However, we have
512 * already decided that this is not a problem. Hence we return
513 * without checking the inode number.
515 free(specific_what);
516 return true;
519 case RETRY_IF_SANITY_CHECK_FAILS:
520 return false;
524 /* Device number was the same, check if the inode has changed. */
525 if (old_ino != newinfo->st_ino)
527 *changed = true;
528 specific_what = specific_dirname(what);
529 fstype = filesystem_type(newinfo, current_dir);
531 error ((isfatal == FATAL_IF_SANITY_CHECK_FAILS) ? 1 : 0,
532 0, /* no relevant errno value */
533 _("%s%s changed during execution of %s (old inode number %ld, new inode number %ld, filesystem type is %s) [ref %ld]"),
534 safely_quote_err_filename(0, specific_what),
535 parent ? "/.." : "",
536 safely_quote_err_filename(1, progname),
537 (long) old_ino,
538 (long) newinfo->st_ino,
539 fstype,
540 (long)line_no);
541 free(specific_what);
542 return false;
545 return true;
548 enum SafeChdirStatus
550 SafeChdirOK,
551 SafeChdirFailSymlink,
552 SafeChdirFailNotDir,
553 SafeChdirFailStat,
554 SafeChdirFailWouldBeUnableToReturn,
555 SafeChdirFailChdirFailed,
556 SafeChdirFailNonexistent,
557 SafeChdirFailDestUnreadable
560 /* Safely perform a change in directory. We do this by calling
561 * lstat() on the subdirectory, using chdir() to move into it, and
562 * then lstat()ing ".". We compare the results of the two stat calls
563 * to see if they are consistent. If not, we sound the alarm.
565 * If following_links() is true, we do follow symbolic links.
567 static enum SafeChdirStatus
568 safely_chdir_lstat(const char *dest,
569 enum TraversalDirection direction,
570 struct stat *statbuf_dest,
571 enum ChdirSymlinkHandling symlink_follow_option,
572 boolean *did_stat)
574 struct stat statbuf_arrived;
575 int rv, dotfd=-1;
576 int saved_errno; /* specific_dirname() changes errno. */
577 boolean rv_set = false;
578 boolean statflag = false;
579 int tries = 0;
580 enum WdSanityCheckFatality isfatal = RETRY_IF_SANITY_CHECK_FAILS;
582 saved_errno = errno = 0;
584 dotfd = open(".", O_RDONLY
585 #if defined O_LARGEFILE
586 |O_LARGEFILE
587 #endif
590 /* We jump back to here if wd_sanity_check()
591 * recoverably triggers an alert.
593 retry:
594 ++tries;
596 if (dotfd >= 0)
598 /* Stat the directory we're going to. */
599 set_stat_placeholders(statbuf_dest);
600 if (0 == options.xstat(dest, statbuf_dest))
602 statflag = true;
604 #ifdef S_ISLNK
605 /* symlink_follow_option might be set to SymlinkFollowOk, which
606 * would allow us to chdir() into a symbolic link. This is
607 * only useful for the case where the directory we're
608 * chdir()ing into is the basename of a command line
609 * argument, for example where "foo/bar/baz" is specified on
610 * the command line. When -P is in effect (the default),
611 * baz will not be followed if it is a symlink, but if bar
612 * is a symlink, it _should_ be followed. Hence we need the
613 * ability to override the policy set by following_links().
615 if (!following_links() && S_ISLNK(statbuf_dest->st_mode))
617 /* We're not supposed to be following links, but this is
618 * a link. Check symlink_follow_option to see if we should
619 * make a special exception.
621 if (symlink_follow_option == SymlinkFollowOk)
623 /* We need to re-stat() the file so that the
624 * sanity check can pass.
626 if (0 != stat(dest, statbuf_dest))
628 rv = SafeChdirFailNonexistent;
629 rv_set = true;
630 saved_errno = errno;
631 goto fail;
633 statflag = true;
635 else
637 /* Not following symlinks, so the attempt to
638 * chdir() into a symlink should be prevented.
640 rv = SafeChdirFailSymlink;
641 rv_set = true;
642 saved_errno = 0; /* silence the error message */
643 goto fail;
646 #endif
647 #ifdef S_ISDIR
648 /* Although the immediately following chdir() would detect
649 * the fact that this is not a directory for us, this would
650 * result in an extra system call that fails. Anybody
651 * examining the system-call trace should ideally not be
652 * concerned that something is actually failing.
654 if (!S_ISDIR(statbuf_dest->st_mode))
656 rv = SafeChdirFailNotDir;
657 rv_set = true;
658 saved_errno = 0; /* silence the error message */
659 goto fail;
661 #endif
663 if (options.debug_options & DebugSearch)
664 fprintf(stderr, "safely_chdir(): chdir(\"%s\")\n", dest);
666 if (0 == chdir(dest))
668 /* check we ended up where we wanted to go */
669 boolean changed = false;
670 if (!wd_sanity_check(".", program_name, ".",
671 statbuf_dest->st_dev,
672 statbuf_dest->st_ino,
673 &statbuf_arrived,
674 0, __LINE__, direction,
675 isfatal,
676 &changed))
678 /* Only allow one failure. */
679 if (RETRY_IF_SANITY_CHECK_FAILS == isfatal)
681 if (0 == fchdir(dotfd))
683 isfatal = FATAL_IF_SANITY_CHECK_FAILS;
684 goto retry;
686 else
688 /* Failed to return to original directory,
689 * but we know that the current working
690 * directory is not the one that we intend
691 * to be in. Since fchdir() failed, we
692 * can't recover from this and so this error
693 * is fatal.
695 error(1, errno,
696 "failed to return to parent directory");
699 else
701 /* XXX: not sure what to use as an excuse here. */
702 rv = SafeChdirFailNonexistent;
703 rv_set = true;
704 saved_errno = 0;
705 goto fail;
709 close(dotfd);
710 return SafeChdirOK;
712 else
714 saved_errno = errno;
715 if (ENOENT == saved_errno)
717 rv = SafeChdirFailNonexistent;
718 rv_set = true;
719 if (options.ignore_readdir_race)
720 errno = 0; /* don't issue err msg */
722 else if (ENOTDIR == saved_errno)
724 /* This can happen if the we stat a directory,
725 * and then filesystem activity changes it into
726 * a non-directory.
728 saved_errno = 0; /* don't issue err msg */
729 rv = SafeChdirFailNotDir;
730 rv_set = true;
732 else
734 rv = SafeChdirFailChdirFailed;
735 rv_set = true;
737 goto fail;
740 else
742 saved_errno = errno;
743 rv = SafeChdirFailStat;
744 rv_set = true;
746 if ( (ENOENT == saved_errno) || (0 == state.curdepth))
747 saved_errno = 0; /* don't issue err msg */
748 goto fail;
751 else
753 /* We do not have read permissions on "." */
754 rv = SafeChdirFailWouldBeUnableToReturn;
755 rv_set = true;
756 goto fail;
759 /* This is the success path, so we clear errno. The caller probably
760 * won't be calling error() anyway.
762 saved_errno = 0;
764 /* We use the same exit path for success or failure.
765 * which has occurred is recorded in RV.
767 fail:
768 /* We do not call error() as this would result in a duplicate error
769 * message when the caller does the same thing.
771 if (saved_errno)
772 errno = saved_errno;
774 if (dotfd >= 0)
776 close(dotfd);
777 dotfd = -1;
780 *did_stat = statflag;
781 assert(rv_set);
782 return rv;
785 #if defined(O_NOFOLLOW)
786 /* Safely change working directory to the specified subdirectory. If
787 * we are not allowed to follow symbolic links, we use open() with
788 * O_NOFOLLOW, followed by fchdir(). This ensures that we don't
789 * follow symbolic links (of course, we do follow them if the -L
790 * option is in effect).
792 static enum SafeChdirStatus
793 safely_chdir_nofollow(const char *dest,
794 enum TraversalDirection direction,
795 struct stat *statbuf_dest,
796 enum ChdirSymlinkHandling symlink_follow_option,
797 boolean *did_stat)
799 int extraflags, fd;
801 (void) direction;
802 (void) statbuf_dest;
804 extraflags = 0;
805 *did_stat = false;
807 switch (symlink_follow_option)
809 case SymlinkFollowOk:
810 extraflags = 0;
811 break;
813 case SymlinkHandleDefault:
814 if (following_links())
815 extraflags = 0;
816 else
817 extraflags = O_NOFOLLOW;
818 break;
821 errno = 0;
822 fd = open(dest, O_RDONLY
823 #if defined O_LARGEFILE
824 |O_LARGEFILE
825 #endif
826 |extraflags);
827 if (fd < 0)
829 switch (errno)
831 case ELOOP:
832 return SafeChdirFailSymlink; /* This is why we use O_NOFOLLOW */
833 case ENOENT:
834 return SafeChdirFailNonexistent;
835 default:
836 return SafeChdirFailDestUnreadable;
840 errno = 0;
841 if (0 == fchdir(fd))
843 close(fd);
844 return SafeChdirOK;
846 else
848 int saved_errno = errno;
849 close(fd);
850 errno = saved_errno;
852 switch (errno)
854 case ENOTDIR:
855 return SafeChdirFailNotDir;
857 case EACCES:
858 case EBADF: /* Shouldn't happen */
859 case EINTR:
860 case EIO:
861 default:
862 return SafeChdirFailChdirFailed;
866 #endif
868 static enum SafeChdirStatus
869 safely_chdir(const char *dest,
870 enum TraversalDirection direction,
871 struct stat *statbuf_dest,
872 enum ChdirSymlinkHandling symlink_follow_option,
873 boolean *did_stat)
875 enum SafeChdirStatus result;
877 /* We're about to leave a directory. If there are any -execdir
878 * argument lists which have been built but have not yet been
879 * processed, do them now because they must be done in the same
880 * directory.
882 complete_pending_execdirs(get_current_dirfd());
884 #if !defined(O_NOFOLLOW)
885 options.open_nofollow_available = false;
886 #endif
887 if (options.open_nofollow_available)
888 result = safely_chdir_nofollow(dest, direction, statbuf_dest, symlink_follow_option, did_stat);
889 else
890 result = safely_chdir_lstat(dest, direction, statbuf_dest, symlink_follow_option, did_stat);
891 return result;
896 /* Safely go back to the starting directory. */
897 static void
898 chdir_back (void)
900 struct stat stat_buf;
901 boolean dummy;
903 if (starting_desc < 0)
905 if (options.debug_options & DebugSearch)
906 fprintf(stderr, "chdir_back(): chdir(\"%s\")\n", starting_dir);
908 #ifdef STAT_MOUNTPOINTS
909 /* We will need the mounted device list. Get it now if we don't
910 * already have it.
912 if (NULL == mounted_devices)
913 init_mounted_dev_list(1);
914 #endif
916 if (chdir (starting_dir) != 0)
917 fatal_file_error(starting_dir);
919 wd_sanity_check(starting_dir,
920 program_name,
921 starting_dir,
922 starting_stat_buf.st_dev,
923 starting_stat_buf.st_ino,
924 &stat_buf, 0, __LINE__,
925 TraversingUp,
926 FATAL_IF_SANITY_CHECK_FAILS,
927 &dummy);
929 else
931 if (options.debug_options & DebugSearch)
932 fprintf(stderr, "chdir_back(): chdir(<starting-point>)\n");
934 if (fchdir (starting_desc) != 0)
936 fatal_file_error(starting_dir);
941 /* Move to the parent of a given directory and then call a function,
942 * restoring the cwd. Don't bother changing directory if the
943 * specified directory is a child of "." or is the root directory.
945 static void
946 at_top (char *pathname,
947 mode_t mode,
948 struct stat *pstat,
949 void (*action)(char *pathname,
950 char *basename,
951 int mode,
952 struct stat *pstat))
954 int dirchange;
955 char *parent_dir = dir_name(pathname);
956 char *base = base_name(pathname);
958 state.curdepth = 0;
959 state.starting_path_length = strlen (pathname);
961 if (0 == strcmp(pathname, parent_dir)
962 || 0 == strcmp(parent_dir, "."))
964 dirchange = 0;
965 base = pathname;
967 else
969 enum TraversalDirection direction;
970 enum SafeChdirStatus chdir_status;
971 struct stat st;
972 boolean did_stat = false;
974 dirchange = 1;
975 if (0 == strcmp(base, ".."))
976 direction = TraversingUp;
977 else
978 direction = TraversingDown;
980 /* We pass SymlinkFollowOk to safely_chdir(), which allows it to
981 * chdir() into a symbolic link. This is only useful for the
982 * case where the directory we're chdir()ing into is the
983 * basename of a command line argument, for example where
984 * "foo/bar/baz" is specified on the command line. When -P is
985 * in effect (the default), baz will not be followed if it is a
986 * symlink, but if bar is a symlink, it _should_ be followed.
987 * Hence we need the ability to override the policy set by
988 * following_links().
990 chdir_status = safely_chdir(parent_dir, direction, &st, SymlinkFollowOk, &did_stat);
991 if (SafeChdirOK != chdir_status)
993 const char *what = (SafeChdirFailWouldBeUnableToReturn == chdir_status) ? "." : parent_dir;
994 if (errno)
995 error (0, errno, "%s",
996 safely_quote_err_filename(0, what));
997 else
998 error (0, 0, "Failed to safely change directory into %s",
999 safely_quote_err_filename(0, parent_dir));
1001 /* We can't process this command-line argument. */
1002 state.exit_status = 1;
1003 return;
1007 free (parent_dir);
1008 parent_dir = NULL;
1010 action(pathname, base, mode, pstat);
1012 if (dirchange)
1014 chdir_back();
1019 static void do_process_top_dir(char *pathname,
1020 char *base,
1021 int mode,
1022 struct stat *pstat)
1024 (void) pstat;
1026 process_path (pathname, base, false, ".", mode);
1027 complete_pending_execdirs(get_current_dirfd());
1030 static void do_process_predicate(char *pathname,
1031 char *base,
1032 int mode,
1033 struct stat *pstat)
1035 (void) mode;
1037 state.rel_pathname = base; /* cwd_dir_fd was already set by safely_chdir */
1038 apply_predicate (pathname, pstat, get_eval_tree());
1044 /* Descend PATHNAME, which is a command-line argument.
1046 Actions like -execdir assume that we are in the
1047 parent directory of the file we're examining,
1048 and on entry to this function our working directory
1049 is whatever it was when find was invoked. Therefore
1050 If PATHNAME is "." we just leave things as they are.
1051 Otherwise, we figure out what the parent directory is,
1052 and move to that.
1054 static void
1055 process_top_path (char *pathname, mode_t mode)
1057 at_top(pathname, mode, NULL, do_process_top_dir);
1061 /* Info on each directory in the current tree branch, to avoid
1062 getting stuck in symbolic link loops. */
1063 static struct dir_id *dir_ids = NULL;
1064 /* Entries allocated in `dir_ids'. */
1065 static int dir_alloc = 0;
1066 /* Index in `dir_ids' of directory currently being searched.
1067 This is always the last valid entry. */
1068 static int dir_curr = -1;
1069 /* (Arbitrary) number of entries to grow `dir_ids' by. */
1070 #define DIR_ALLOC_STEP 32
1074 /* We've detected a filesystem loop. This is caused by one of
1075 * two things:
1077 * 1. Option -L is in effect and we've hit a symbolic link that
1078 * points to an ancestor. This is harmless. We won't traverse the
1079 * symbolic link.
1081 * 2. We have hit a real cycle in the directory hierarchy. In this
1082 * case, we issue a diagnostic message (POSIX requires this) and we
1083 * skip that directory entry.
1085 static void
1086 issue_loop_warning(const char *name, const char *pathname, int level)
1088 struct stat stbuf_link;
1089 if (lstat(name, &stbuf_link) != 0)
1090 stbuf_link.st_mode = S_IFREG;
1092 if (S_ISLNK(stbuf_link.st_mode))
1094 error(0, 0,
1095 _("Symbolic link %s is part of a loop in the directory hierarchy; we have already visited the directory to which it points."),
1096 safely_quote_err_filename(0, pathname));
1098 else
1100 int distance = 1 + (dir_curr-level);
1101 /* We have found an infinite loop. POSIX requires us to
1102 * issue a diagnostic. Usually we won't get to here
1103 * because when the leaf optimisation is on, it will cause
1104 * the subdirectory to be skipped. If /a/b/c/d is a hard
1105 * link to /a/b, then the link count of /a/b/c is 2,
1106 * because the ".." entry of /b/b/c/d points to /a, not
1107 * to /a/b/c.
1109 error(0, 0,
1110 _("Filesystem loop detected; %s has the same device number and inode as a directory which is %d %s."),
1111 safely_quote_err_filename(0, pathname),
1112 distance,
1113 (distance == 1 ?
1114 _("level higher in the filesystem hierarchy") :
1115 _("levels higher in the filesystem hierarchy")));
1121 /* Recursively descend path PATHNAME, applying the predicates.
1122 LEAF is true if PATHNAME is known to be in a directory that has no
1123 more unexamined subdirectories, and therefore it is not a directory.
1124 Knowing this allows us to avoid calling stat as long as possible for
1125 leaf files.
1127 NAME is PATHNAME relative to the current directory. We access NAME
1128 but print PATHNAME.
1130 PARENT is the path of the parent of NAME, relative to find's
1131 starting directory.
1133 Return nonzero iff PATHNAME is a directory. */
1135 static int
1136 process_path (char *pathname, char *name, boolean leaf, char *parent,
1137 mode_t mode)
1139 struct stat stat_buf;
1140 static dev_t root_dev; /* Device ID of current argument pathname. */
1141 int i;
1142 struct predicate *eval_tree;
1144 eval_tree = get_eval_tree();
1145 /* Assume it is a non-directory initially. */
1146 stat_buf.st_mode = 0;
1147 state.rel_pathname = name;
1148 state.type = 0;
1149 state.have_stat = false;
1150 state.have_type = false;
1152 if (!digest_mode(mode, pathname, name, &stat_buf, leaf))
1153 return 0;
1155 if (!S_ISDIR (state.type))
1157 if (state.curdepth >= options.mindepth)
1158 apply_predicate (pathname, &stat_buf, eval_tree);
1159 return 0;
1162 /* From here on, we're working on a directory. */
1165 /* Now we really need to stat the directory, even if we know the
1166 * type, because we need information like struct stat.st_rdev.
1168 if (get_statinfo(pathname, name, &stat_buf) != 0)
1169 return 0;
1171 state.have_stat = true;
1172 mode = state.type = stat_buf.st_mode; /* use full info now that we have it. */
1173 state.stop_at_current_level =
1174 options.maxdepth >= 0
1175 && state.curdepth >= options.maxdepth;
1177 /* If we've already seen this directory on this branch,
1178 don't descend it again. */
1179 for (i = 0; i <= dir_curr; i++)
1180 if (stat_buf.st_ino == dir_ids[i].ino &&
1181 stat_buf.st_dev == dir_ids[i].dev)
1183 state.stop_at_current_level = true;
1184 issue_loop_warning(name, pathname, i);
1187 if (dir_alloc <= ++dir_curr)
1189 dir_alloc += DIR_ALLOC_STEP;
1190 dir_ids = (struct dir_id *)
1191 xrealloc ((char *) dir_ids, dir_alloc * sizeof (struct dir_id));
1193 dir_ids[dir_curr].ino = stat_buf.st_ino;
1194 dir_ids[dir_curr].dev = stat_buf.st_dev;
1196 if (options.stay_on_filesystem)
1198 if (state.curdepth == 0)
1199 root_dev = stat_buf.st_dev;
1200 else if (stat_buf.st_dev != root_dev)
1201 state.stop_at_current_level = true;
1204 if (options.do_dir_first && state.curdepth >= options.mindepth)
1205 apply_predicate (pathname, &stat_buf, eval_tree);
1207 if (options.debug_options & DebugSearch)
1208 fprintf(stderr, "pathname = %s, stop_at_current_level = %d\n",
1209 pathname, state.stop_at_current_level);
1211 if (state.stop_at_current_level == false)
1213 /* Scan directory on disk. */
1214 process_dir (pathname, name, strlen (pathname), &stat_buf, parent);
1217 if (options.do_dir_first == false && state.curdepth >= options.mindepth)
1219 /* The fields in 'state' are now out of date. Correct them.
1221 if (!digest_mode(mode, pathname, name, &stat_buf, leaf))
1222 return 0;
1224 if (0 == dir_curr)
1226 at_top(pathname, mode, &stat_buf, do_process_predicate);
1228 else
1230 do_process_predicate(pathname, name, mode, &stat_buf);
1234 dir_curr--;
1236 return 1;
1240 /* Scan directory PATHNAME and recurse through process_path for each entry.
1242 PATHLEN is the length of PATHNAME.
1244 NAME is PATHNAME relative to the current directory.
1246 STATP is the results of *options.xstat on it.
1248 PARENT is the path of the parent of NAME, relative to find's
1249 starting directory. */
1251 static void
1252 process_dir (char *pathname, char *name, int pathlen, const struct stat *statp, char *parent)
1254 int subdirs_left; /* Number of unexamined subdirs in PATHNAME. */
1255 boolean subdirs_unreliable; /* if true, cannot use dir link count as subdir limif (if false, it may STILL be unreliable) */
1256 unsigned int idx; /* Which entry are we on? */
1257 struct stat stat_buf;
1258 size_t dircount = 0u;
1259 struct savedir_dirinfo *dirinfo;
1260 #if 0
1261 printf("process_dir: pathname=%s name=%s statp->st_nlink=%d st_ino=%d\n",
1262 pathname,
1263 name,
1264 (int)statp->st_nlink,
1265 (int)statp->st_ino);
1266 #endif
1267 if (statp->st_nlink < 2)
1269 subdirs_unreliable = true;
1270 subdirs_left = 0;
1272 else
1274 subdirs_unreliable = false; /* not necessarily right */
1275 subdirs_left = statp->st_nlink - 2; /* Account for name and ".". */
1278 errno = 0;
1279 dirinfo = xsavedir(name, 0);
1282 if (dirinfo == NULL)
1284 assert(errno != 0);
1285 error (0, errno, "%s", safely_quote_err_filename(0, pathname));
1286 state.exit_status = 1;
1288 else
1290 register char *namep; /* Current point in `name_space'. */
1291 char *cur_path; /* Full path of each file to process. */
1292 char *cur_name; /* Base name of each file to process. */
1293 unsigned cur_path_size; /* Bytes allocated for `cur_path'. */
1294 register unsigned file_len; /* Length of each path to process. */
1295 register unsigned pathname_len; /* PATHLEN plus trailing '/'. */
1296 boolean did_stat = false;
1298 if (pathname[pathlen - 1] == '/')
1299 pathname_len = pathlen + 1; /* For '\0'; already have '/'. */
1300 else
1301 pathname_len = pathlen + 2; /* For '/' and '\0'. */
1302 cur_path_size = 0;
1303 cur_path = NULL;
1305 /* We're about to leave the directory. If there are any
1306 * -execdir argument lists which have been built but have not
1307 * yet been processed, do them now because they must be done in
1308 * the same directory.
1310 complete_pending_execdirs(get_current_dirfd());
1312 if (strcmp (name, "."))
1314 enum SafeChdirStatus status = safely_chdir (name, TraversingDown, &stat_buf, SymlinkHandleDefault, &did_stat);
1315 switch (status)
1317 case SafeChdirOK:
1318 /* If there had been a change but wd_sanity_check()
1319 * accepted it, we need to accept that on the
1320 * way back up as well, so modify our record
1321 * of what we think we should see later.
1322 * If there was no change, the assignments are a no-op.
1324 * However, before performing the assignment, we need to
1325 * check that we have the stat information. If O_NOFOLLOW
1326 * is available, safely_chdir() will not have needed to use
1327 * stat(), and so stat_buf will just contain random data.
1329 if (!did_stat)
1331 /* If there is a link we need to follow it. Hence
1332 * the direct call to stat() not through (options.xstat)
1334 set_stat_placeholders(&stat_buf);
1335 if (0 != stat(".", &stat_buf))
1336 break; /* skip the assignment. */
1338 dir_ids[dir_curr].dev = stat_buf.st_dev;
1339 dir_ids[dir_curr].ino = stat_buf.st_ino;
1341 break;
1343 case SafeChdirFailWouldBeUnableToReturn:
1344 error (0, errno, ".");
1345 state.exit_status = 1;
1346 break;
1348 case SafeChdirFailNonexistent:
1349 case SafeChdirFailDestUnreadable:
1350 case SafeChdirFailStat:
1351 case SafeChdirFailNotDir:
1352 case SafeChdirFailChdirFailed:
1353 error (0, errno, "%s",
1354 safely_quote_err_filename(0, pathname));
1355 state.exit_status = 1;
1356 return;
1358 case SafeChdirFailSymlink:
1359 error (0, 0,
1360 _("warning: not following the symbolic link %s"),
1361 safely_quote_err_filename(0, pathname));
1362 state.exit_status = 1;
1363 return;
1367 for (idx=0; idx < dirinfo->size; ++idx)
1369 /* savedirinfo() may return dirinfo=NULL if extended information
1370 * is not available.
1372 mode_t mode = (dirinfo->entries[idx].flags & SavedirHaveFileType) ?
1373 dirinfo->entries[idx].type_info : 0;
1374 namep = dirinfo->entries[idx].name;
1376 /* Append this directory entry's name to the path being searched. */
1377 file_len = pathname_len + strlen (namep);
1378 if (file_len > cur_path_size)
1380 while (file_len > cur_path_size)
1381 cur_path_size += 1024;
1382 if (cur_path)
1383 free (cur_path);
1384 cur_path = xmalloc (cur_path_size);
1385 strcpy (cur_path, pathname);
1386 cur_path[pathname_len - 2] = '/';
1388 cur_name = cur_path + pathname_len - 1;
1389 strcpy (cur_name, namep);
1391 state.curdepth++;
1392 if (!options.no_leaf_check && !subdirs_unreliable)
1394 if (mode && S_ISDIR(mode) && (subdirs_left == 0))
1396 /* This is a subdirectory, but the number of directories we
1397 * have found now exceeds the number we would expect given
1398 * the hard link count on the parent. This is likely to be
1399 * a bug in the filesystem driver (e.g. Linux's
1400 * /proc filesystem) or may just be a fact that the OS
1401 * doesn't really handle hard links with Unix semantics.
1402 * In the latter case, -noleaf should be used routinely.
1404 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 filesystem driver. Automatically turning on find's -noleaf option. Earlier results may have failed to include directories that should have been searched."),
1405 safely_quote_err_filename(0, pathname),
1406 statp->st_nlink,
1407 dircount);
1408 state.exit_status = 1; /* We know the result is wrong, now */
1409 options.no_leaf_check = true; /* Don't make same
1410 mistake again */
1411 subdirs_unreliable = 1;
1412 subdirs_left = 1; /* band-aid for this iteration. */
1415 /* Normal case optimization. On normal Unix
1416 filesystems, a directory that has no subdirectories
1417 has two links: its name, and ".". Any additional
1418 links are to the ".." entries of its subdirectories.
1419 Once we have processed as many subdirectories as
1420 there are additional links, we know that the rest of
1421 the entries are non-directories -- in other words,
1422 leaf files. */
1424 int count;
1425 count = process_path (cur_path, cur_name,
1426 subdirs_left == 0, pathname,
1427 mode);
1428 subdirs_left -= count;
1429 dircount += count;
1432 else
1434 /* There might be weird (e.g., CD-ROM or MS-DOS) filesystems
1435 mounted, which don't have Unix-like directory link counts. */
1436 process_path (cur_path, cur_name, false, pathname, mode);
1439 state.curdepth--;
1443 /* We're about to leave the directory. If there are any
1444 * -execdir argument lists which have been built but have not
1445 * yet been processed, do them now because they must be done in
1446 * the same directory.
1448 complete_pending_execdirs(get_current_dirfd());
1450 if (strcmp (name, "."))
1452 enum SafeChdirStatus status;
1453 struct dir_id did;
1455 /* We could go back and do the next command-line arg
1456 instead, maybe using longjmp. */
1457 char const *dir;
1458 boolean deref = following_links() ? true : false;
1460 if ( (state.curdepth>0) && !deref)
1461 dir = "..";
1462 else
1464 chdir_back ();
1465 dir = parent;
1468 did_stat = false;
1469 status = safely_chdir (dir, TraversingUp, &stat_buf, SymlinkHandleDefault, &did_stat);
1470 switch (status)
1472 case SafeChdirOK:
1473 break;
1475 case SafeChdirFailWouldBeUnableToReturn:
1476 error (1, errno, ".");
1477 return;
1479 case SafeChdirFailNonexistent:
1480 case SafeChdirFailDestUnreadable:
1481 case SafeChdirFailStat:
1482 case SafeChdirFailSymlink:
1483 case SafeChdirFailNotDir:
1484 case SafeChdirFailChdirFailed:
1485 error (1, errno, "%s", safely_quote_err_filename(0, pathname));
1486 return;
1489 if (dir_curr > 0)
1491 did.dev = dir_ids[dir_curr-1].dev;
1492 did.ino = dir_ids[dir_curr-1].ino;
1494 else
1496 did.dev = starting_stat_buf.st_dev;
1497 did.ino = starting_stat_buf.st_ino;
1501 if (cur_path)
1502 free (cur_path);
1503 free_dirinfo(dirinfo);
1506 if (subdirs_unreliable)
1508 /* Make sure we hasn't used the variable subdirs_left if we knew
1509 * we shouldn't do so.
1511 assert(0 == subdirs_left || options.no_leaf_check);