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>.
31 #define USE_SAFE_CHDIR 1
32 #undef STAT_MOUNTPOINTS
44 #include "canonicalize.h"
48 #include "savedirinfo.h"
62 # define _(Text) gettext (Text)
65 #define textdomain(Domain)
66 #define bindtextdomain(Package, Directory)
69 # define N_(String) gettext_noop (String)
71 /* See locate.c for explanation as to why not use (String) */
72 # define N_(String) String
75 #ifdef STAT_MOUNTPOINTS
76 static void init_mounted_dev_list(int mandatory
);
79 static void process_top_path
PARAMS((char *pathname
, mode_t mode
));
80 static int process_path
PARAMS((char *pathname
, char *name
, boolean leaf
, char *parent
, mode_t type
));
81 static void process_dir
PARAMS((char *pathname
, char *name
, int pathlen
, const struct stat
*statp
, char *parent
));
85 /* Name this program was run with. */
88 /* A file descriptor open to the initial working directory.
89 Doing it this way allows us to work when the i.w.d. has
90 unreadable parents. */
93 /* The stat buffer of the initial working directory. */
94 static struct stat starting_stat_buf
;
96 enum ChdirSymlinkHandling
98 SymlinkHandleDefault
, /* Normally the right choice */
99 SymlinkFollowOk
/* see comment in process_top_path() */
103 enum TraversalDirection
109 enum WdSanityCheckFatality
111 FATAL_IF_SANITY_CHECK_FAILS
,
112 RETRY_IF_SANITY_CHECK_FAILS
,
113 NON_FATAL_IF_SANITY_CHECK_FAILS
117 int get_current_dirfd(void)
124 main (int argc
, char **argv
)
127 int end_of_leading_options
= 0; /* First arg after any -H/-L etc. */
128 struct predicate
*eval_tree
;
130 program_name
= argv
[0];
131 state
.exit_status
= 0;
133 /* Set the option defaults before we do the locale
134 * initialisation as check_nofollow() needs to be executed in the
137 set_option_defaults(&options
);
139 #ifdef HAVE_SETLOCALE
140 setlocale (LC_ALL
, "");
142 bindtextdomain (PACKAGE
, LOCALEDIR
);
143 textdomain (PACKAGE
);
144 atexit (close_stdout
);
146 /* Check for -P, -H or -L options. */
147 end_of_leading_options
= process_leading_options(argc
, argv
);
149 if (options
.debug_options
& DebugStat
)
150 options
.xstat
= debug_stat
;
153 fprintf (stderr
, "cur_day_start = %s", ctime (&options
.cur_day_start
));
156 /* state.cwd_dir_fd has to be initialised before we call build_expression_tree()
157 * because command-line parsing may lead us to stat some files.
159 state
.cwd_dir_fd
= AT_FDCWD
;
161 /* We are now processing the part of the "find" command line
162 * after the -H/-L options (if any).
164 eval_tree
= build_expression_tree(argc
, argv
, end_of_leading_options
);
167 /* safely_chdir() needs to check that it has ended up in the right place.
168 * To avoid bailing out when something gets automounted, it checks if
169 * the target directory appears to have had a directory mounted on it as
170 * we chdir()ed. The problem with this is that in order to notice that
171 * a file system was mounted, we would need to lstat() all the mount points.
172 * That strategy loses if our machine is a client of a dead NFS server.
174 * Hence if safely_chdir() and wd_sanity_check() can manage without needing
175 * to know the mounted device list, we do that.
177 if (!options
.open_nofollow_available
)
179 #ifdef STAT_MOUNTPOINTS
180 init_mounted_dev_list(0);
185 starting_desc
= open (".", O_RDONLY
186 #if defined O_LARGEFILE
190 if (0 <= starting_desc
&& fchdir (starting_desc
) != 0)
192 close (starting_desc
);
195 assert (starting_desc
>= 0);
197 if (starting_desc
< 0)
199 starting_dir
= xgetcwd ();
201 error (1, errno
, _("cannot get current directory"));
203 set_stat_placeholders(&starting_stat_buf
);
204 if ((*options
.xstat
) (".", &starting_stat_buf
) != 0)
205 error (1, errno
, _("cannot stat current directory"));
207 /* If no paths are given, default to ".". */
208 for (i
= end_of_leading_options
; i
< argc
&& !looks_like_expression(argv
[i
], true); i
++)
210 process_top_path (argv
[i
], 0);
213 /* If there were no path arguments, default to ".". */
214 if (i
== end_of_leading_options
)
217 * We use a temporary variable here because some actions modify
218 * the path temporarily. Hence if we use a string constant,
219 * we get a coredump. The best example of this is if we say
220 * "find -printf %H" (note, not "find . -printf %H").
222 char defaultpath
[2] = ".";
223 process_top_path (defaultpath
, 0);
226 /* If "-exec ... {} +" has been used, there may be some
227 * partially-full command lines which have been built,
228 * but which are not yet complete. Execute those now.
230 show_success_rates(eval_tree
);
232 return state
.exit_status
;
235 boolean
is_fts_enabled(int *ftsoptions
)
237 /* this version of find (i.e. this main()) does not use fts. */
244 specific_dirname(const char *dir
)
248 if (0 == strcmp(".", dir
))
250 /* OK, what's '.'? */
251 if (NULL
!= getcwd(dirbuf
, sizeof(dirbuf
)))
253 return strdup(dirbuf
);
262 char *result
= canonicalize_filename_mode(dir
, CAN_EXISTING
);
272 /* Return non-zero if FS is the name of a file system that is likely to
276 fs_likely_to_be_automounted(const char *fs
)
278 return ( (0==strcmp(fs
, "nfs")) || (0==strcmp(fs
, "autofs")) || (0==strcmp(fs
, "subfs")));
283 #ifdef STAT_MOUNTPOINTS
284 static dev_t
*mounted_devices
= NULL
;
285 static size_t num_mounted_devices
= 0u;
289 init_mounted_dev_list(int mandatory
)
291 assert (NULL
== mounted_devices
);
292 assert (0 == num_mounted_devices
);
293 mounted_devices
= get_mounted_devices(&num_mounted_devices
);
294 if (mandatory
&& (NULL
== mounted_devices
))
296 error(1, 0, "Cannot read list of mounted devices.");
301 refresh_mounted_dev_list(void)
305 free(mounted_devices
);
308 num_mounted_devices
= 0u;
309 init_mounted_dev_list(1);
313 /* Search for device DEV in the array LIST, which is of size N. */
315 dev_present(dev_t dev
, const dev_t
*list
, size_t n
)
321 if ( (*list
++) == dev
)
328 enum MountPointStateChange
330 MountPointRecentlyMounted
,
331 MountPointRecentlyUnmounted
,
332 MountPointStateUnchanged
337 static enum MountPointStateChange
338 get_mount_state(dev_t newdev
)
340 int new_is_present
, new_was_present
;
342 new_was_present
= dev_present(newdev
, mounted_devices
, num_mounted_devices
);
343 refresh_mounted_dev_list();
344 new_is_present
= dev_present(newdev
, mounted_devices
, num_mounted_devices
);
346 if (new_was_present
== new_is_present
)
347 return MountPointStateUnchanged
;
348 else if (new_is_present
)
349 return MountPointRecentlyMounted
;
351 return MountPointRecentlyUnmounted
;
356 /* We stat()ed a directory, chdir()ed into it (we know this
357 * since direction is TraversingDown), stat()ed it again,
358 * and noticed that the device numbers are different. Check
359 * if the file system was recently mounted.
361 * If it was, it looks like chdir()ing into the directory
362 * caused a file system to be mounted. Maybe automount is
363 * running. Anyway, that's probably OK - but it happens
364 * only when we are moving downward.
366 * We also allow for the possibility that a similar thing
367 * has happened with the unmounting of a file system. This
368 * is much rarer, as it relies on an automounter timeout
369 * occurring at exactly the wrong moment.
371 static enum WdSanityCheckFatality
372 dirchange_is_fatal(const char *specific_what
,
373 enum WdSanityCheckFatality isfatal
,
375 struct stat
*newinfo
)
377 enum MountPointStateChange transition
= get_mount_state(newinfo
->st_dev
);
380 case MountPointRecentlyUnmounted
:
381 isfatal
= NON_FATAL_IF_SANITY_CHECK_FAILS
;
385 _("Warning: file system %s has recently been unmounted."),
386 safely_quote_err_filename(0, specific_what
));
390 case MountPointRecentlyMounted
:
391 isfatal
= NON_FATAL_IF_SANITY_CHECK_FAILS
;
395 _("Warning: file system %s has recently been mounted."),
396 safely_quote_err_filename(0, specific_what
));
400 case MountPointStateUnchanged
:
401 /* leave isfatal as it is */
413 /* Examine the results of the stat() of a directory from before we
414 * entered or left it, with the results of stat()ing it afterward. If
415 * these are different, the file system tree has been modified while we
416 * were traversing it. That might be an attempt to use a race
417 * condition to persuade find to do something it didn't intend
418 * (e.g. an attempt by an ordinary user to exploit the fact that root
419 * sometimes runs find on the whole file system). However, this can
420 * also happen if automount is running (certainly on Solaris). With
421 * automount, moving into a directory can cause a file system to be
424 * To cope sensibly with this, we will raise an error if we see the
425 * device number change unless we are chdir()ing into a subdirectory,
426 * and the directory we moved into has been mounted or unmounted "recently".
427 * Here "recently" means since we started "find" or we last re-read
428 * the /etc/mnttab file.
430 * If the device number does not change but the inode does, that is a
433 * If the device number and inode are both the same, we are happy.
435 * If a file system is (un)mounted as we chdir() into the directory, that
436 * may mean that we're now examining a section of the file system that might
437 * have been excluded from consideration (via -prune or -quit for example).
438 * Hence we print a warning message to indicate that the output of find
439 * might be inconsistent due to the change in the file system.
442 wd_sanity_check(const char *thing_to_stat
,
443 const char *progname
,
447 struct stat
*newinfo
,
450 enum TraversalDirection direction
,
451 enum WdSanityCheckFatality isfatal
,
452 boolean
*changed
) /* output parameter */
455 char *specific_what
= NULL
;
457 const char *current_dir
= ".";
461 set_stat_placeholders(newinfo
);
462 if ((*options
.xstat
) (current_dir
, newinfo
) != 0)
463 fatal_file_error(thing_to_stat
);
465 if (old_dev
!= newinfo
->st_dev
)
468 specific_what
= specific_dirname(what
);
469 fstype
= filesystem_type(newinfo
, current_dir
);
470 silent
= fs_likely_to_be_automounted(fstype
);
472 /* This condition is rare, so once we are here it is
473 * reasonable to perform an expensive computation to
474 * determine if we should continue or fail.
476 if (TraversingDown
== direction
)
478 #ifdef STAT_MOUNTPOINTS
479 isfatal
= dirchange_is_fatal(specific_what
,isfatal
,silent
,newinfo
);
481 isfatal
= RETRY_IF_SANITY_CHECK_FAILS
;
487 case FATAL_IF_SANITY_CHECK_FAILS
:
489 fstype
= filesystem_type(newinfo
, current_dir
);
491 _("%s%s changed during execution of %s (old device number %ld, new device number %ld, file system type is %s) [ref %ld]"),
492 safely_quote_err_filename(0, specific_what
),
494 safely_quote_err_filename(1, progname
),
496 (long) newinfo
->st_dev
,
503 case NON_FATAL_IF_SANITY_CHECK_FAILS
:
505 /* Since the device has changed under us, the inode number
506 * will almost certainly also be different. However, we have
507 * already decided that this is not a problem. Hence we return
508 * without checking the inode number.
514 case RETRY_IF_SANITY_CHECK_FAILS
:
519 /* Device number was the same, check if the inode has changed. */
520 if (old_ino
!= newinfo
->st_ino
)
523 specific_what
= specific_dirname(what
);
524 fstype
= filesystem_type(newinfo
, current_dir
);
526 error ((isfatal
== FATAL_IF_SANITY_CHECK_FAILS
) ? 1 : 0,
527 0, /* no relevant errno value */
528 _("%s%s changed during execution of %s (old inode number %ld, new inode number %ld, file system type is %s) [ref %ld]"),
529 safely_quote_err_filename(0, specific_what
),
531 safely_quote_err_filename(1, progname
),
533 (long) newinfo
->st_ino
,
546 SafeChdirFailSymlink
,
549 SafeChdirFailWouldBeUnableToReturn
,
550 SafeChdirFailChdirFailed
,
551 SafeChdirFailNonexistent
,
552 SafeChdirFailDestUnreadable
555 /* Safely perform a change in directory. We do this by calling
556 * lstat() on the subdirectory, using chdir() to move into it, and
557 * then lstat()ing ".". We compare the results of the two stat calls
558 * to see if they are consistent. If not, we sound the alarm.
560 * If following_links() is true, we do follow symbolic links.
562 static enum SafeChdirStatus
563 safely_chdir_lstat(const char *dest
,
564 enum TraversalDirection direction
,
565 struct stat
*statbuf_dest
,
566 enum ChdirSymlinkHandling symlink_follow_option
,
569 struct stat statbuf_arrived
;
571 int saved_errno
; /* specific_dirname() changes errno. */
572 boolean rv_set
= false;
573 boolean statflag
= false;
575 enum WdSanityCheckFatality isfatal
= RETRY_IF_SANITY_CHECK_FAILS
;
577 saved_errno
= errno
= 0;
579 dotfd
= open(".", O_RDONLY
580 #if defined O_LARGEFILE
585 /* We jump back to here if wd_sanity_check()
586 * recoverably triggers an alert.
593 /* Stat the directory we're going to. */
594 set_stat_placeholders(statbuf_dest
);
595 if (0 == options
.xstat(dest
, statbuf_dest
))
600 /* symlink_follow_option might be set to SymlinkFollowOk, which
601 * would allow us to chdir() into a symbolic link. This is
602 * only useful for the case where the directory we're
603 * chdir()ing into is the basename of a command line
604 * argument, for example where "foo/bar/baz" is specified on
605 * the command line. When -P is in effect (the default),
606 * baz will not be followed if it is a symlink, but if bar
607 * is a symlink, it _should_ be followed. Hence we need the
608 * ability to override the policy set by following_links().
610 if (!following_links() && S_ISLNK(statbuf_dest
->st_mode
))
612 /* We're not supposed to be following links, but this is
613 * a link. Check symlink_follow_option to see if we should
614 * make a special exception.
616 if (symlink_follow_option
== SymlinkFollowOk
)
618 /* We need to re-stat() the file so that the
619 * sanity check can pass.
621 if (0 != stat(dest
, statbuf_dest
))
623 rv
= SafeChdirFailNonexistent
;
632 /* Not following symlinks, so the attempt to
633 * chdir() into a symlink should be prevented.
635 rv
= SafeChdirFailSymlink
;
637 saved_errno
= 0; /* silence the error message */
643 /* Although the immediately following chdir() would detect
644 * the fact that this is not a directory for us, this would
645 * result in an extra system call that fails. Anybody
646 * examining the system-call trace should ideally not be
647 * concerned that something is actually failing.
649 if (!S_ISDIR(statbuf_dest
->st_mode
))
651 rv
= SafeChdirFailNotDir
;
653 saved_errno
= 0; /* silence the error message */
658 if (options
.debug_options
& DebugSearch
)
659 fprintf(stderr
, "safely_chdir(): chdir(\"%s\")\n", dest
);
661 if (0 == chdir(dest
))
663 /* check we ended up where we wanted to go */
664 boolean changed
= false;
665 if (!wd_sanity_check(".", program_name
, ".",
666 statbuf_dest
->st_dev
,
667 statbuf_dest
->st_ino
,
669 0, __LINE__
, direction
,
673 /* Only allow one failure. */
674 if (RETRY_IF_SANITY_CHECK_FAILS
== isfatal
)
676 if (0 == fchdir(dotfd
))
678 isfatal
= FATAL_IF_SANITY_CHECK_FAILS
;
683 /* Failed to return to original directory,
684 * but we know that the current working
685 * directory is not the one that we intend
686 * to be in. Since fchdir() failed, we
687 * can't recover from this and so this error
691 "failed to return to parent directory");
696 /* XXX: not sure what to use as an excuse here. */
697 rv
= SafeChdirFailNonexistent
;
710 if (ENOENT
== saved_errno
)
712 rv
= SafeChdirFailNonexistent
;
714 if (options
.ignore_readdir_race
)
715 errno
= 0; /* don't issue err msg */
717 else if (ENOTDIR
== saved_errno
)
719 /* This can happen if the we stat a directory,
720 * and then file system activity changes it into
723 saved_errno
= 0; /* don't issue err msg */
724 rv
= SafeChdirFailNotDir
;
729 rv
= SafeChdirFailChdirFailed
;
738 rv
= SafeChdirFailStat
;
741 if ( (ENOENT
== saved_errno
) || (0 == state
.curdepth
))
742 saved_errno
= 0; /* don't issue err msg */
748 /* We do not have read permissions on "." */
749 rv
= SafeChdirFailWouldBeUnableToReturn
;
754 /* This is the success path, so we clear errno. The caller probably
755 * won't be calling error() anyway.
759 /* We use the same exit path for success or failure.
760 * which has occurred is recorded in RV.
763 /* We do not call error() as this would result in a duplicate error
764 * message when the caller does the same thing.
775 *did_stat
= statflag
;
780 #if defined O_NOFOLLOW
781 /* Safely change working directory to the specified subdirectory. If
782 * we are not allowed to follow symbolic links, we use open() with
783 * O_NOFOLLOW, followed by fchdir(). This ensures that we don't
784 * follow symbolic links (of course, we do follow them if the -L
785 * option is in effect).
787 static enum SafeChdirStatus
788 safely_chdir_nofollow(const char *dest
,
789 enum TraversalDirection direction
,
790 struct stat
*statbuf_dest
,
791 enum ChdirSymlinkHandling symlink_follow_option
,
802 switch (symlink_follow_option
)
804 case SymlinkFollowOk
:
808 case SymlinkHandleDefault
:
809 if (following_links())
812 extraflags
= O_NOFOLLOW
;
817 fd
= open(dest
, O_RDONLY
818 #if defined O_LARGEFILE
827 return SafeChdirFailSymlink
; /* This is why we use O_NOFOLLOW */
829 return SafeChdirFailNonexistent
;
831 return SafeChdirFailDestUnreadable
;
843 int saved_errno
= errno
;
850 return SafeChdirFailNotDir
;
853 case EBADF
: /* Shouldn't happen */
857 return SafeChdirFailChdirFailed
;
863 static enum SafeChdirStatus
864 safely_chdir(const char *dest
,
865 enum TraversalDirection direction
,
866 struct stat
*statbuf_dest
,
867 enum ChdirSymlinkHandling symlink_follow_option
,
870 enum SafeChdirStatus result
;
872 /* We're about to leave a directory. If there are any -execdir
873 * argument lists which have been built but have not yet been
874 * processed, do them now because they must be done in the same
877 complete_pending_execdirs(get_current_dirfd());
879 #if !defined(O_NOFOLLOW)
880 options
.open_nofollow_available
= false;
882 if (options
.open_nofollow_available
)
883 result
= safely_chdir_nofollow(dest
, direction
, statbuf_dest
, symlink_follow_option
, did_stat
);
885 result
= safely_chdir_lstat(dest
, direction
, statbuf_dest
, symlink_follow_option
, did_stat
);
891 /* Safely go back to the starting directory. */
895 struct stat stat_buf
;
898 if (starting_desc
< 0)
900 if (options
.debug_options
& DebugSearch
)
901 fprintf(stderr
, "chdir_back(): chdir(\"%s\")\n", starting_dir
);
903 #ifdef STAT_MOUNTPOINTS
904 /* We will need the mounted device list. Get it now if we don't
907 if (NULL
== mounted_devices
)
908 init_mounted_dev_list(1);
911 if (chdir (starting_dir
) != 0)
912 fatal_file_error(starting_dir
);
914 wd_sanity_check(starting_dir
,
917 starting_stat_buf
.st_dev
,
918 starting_stat_buf
.st_ino
,
919 &stat_buf
, 0, __LINE__
,
921 FATAL_IF_SANITY_CHECK_FAILS
,
926 if (options
.debug_options
& DebugSearch
)
927 fprintf(stderr
, "chdir_back(): chdir(<starting-point>)\n");
929 if (fchdir (starting_desc
) != 0)
931 fatal_file_error(starting_dir
);
936 /* Move to the parent of a given directory and then call a function,
937 * restoring the cwd. Don't bother changing directory if the
938 * specified directory is a child of "." or is the root directory.
941 at_top (char *pathname
,
944 void (*action
)(char *pathname
,
950 char *parent_dir
= dir_name(pathname
);
951 char *base
= base_name(pathname
);
954 state
.starting_path_length
= strlen (pathname
);
956 if (0 == strcmp(pathname
, parent_dir
)
957 || 0 == strcmp(parent_dir
, "."))
964 enum TraversalDirection direction
;
965 enum SafeChdirStatus chdir_status
;
967 boolean did_stat
= false;
970 if (0 == strcmp(base
, ".."))
971 direction
= TraversingUp
;
973 direction
= TraversingDown
;
975 /* We pass SymlinkFollowOk to safely_chdir(), which allows it to
976 * chdir() into a symbolic link. This is only useful for the
977 * case where the directory we're chdir()ing into is the
978 * basename of a command line argument, for example where
979 * "foo/bar/baz" is specified on the command line. When -P is
980 * in effect (the default), baz will not be followed if it is a
981 * symlink, but if bar is a symlink, it _should_ be followed.
982 * Hence we need the ability to override the policy set by
985 chdir_status
= safely_chdir(parent_dir
, direction
, &st
, SymlinkFollowOk
, &did_stat
);
986 if (SafeChdirOK
!= chdir_status
)
988 const char *what
= (SafeChdirFailWouldBeUnableToReturn
== chdir_status
) ? "." : parent_dir
;
990 error (0, errno
, "%s",
991 safely_quote_err_filename(0, what
));
993 error (0, 0, _("Failed to safely change directory into %s"),
994 safely_quote_err_filename(0, parent_dir
));
996 /* We can't process this command-line argument. */
997 state
.exit_status
= 1;
1005 action(pathname
, base
, mode
, pstat
);
1014 static void do_process_top_dir(char *pathname
,
1021 process_path (pathname
, base
, false, ".", mode
);
1022 complete_pending_execdirs(get_current_dirfd());
1025 static void do_process_predicate(char *pathname
,
1032 state
.rel_pathname
= base
; /* cwd_dir_fd was already set by safely_chdir */
1033 apply_predicate (pathname
, pstat
, get_eval_tree());
1039 /* Descend PATHNAME, which is a command-line argument.
1041 Actions like -execdir assume that we are in the
1042 parent directory of the file we're examining,
1043 and on entry to this function our working directory
1044 is whatever it was when find was invoked. Therefore
1045 If PATHNAME is "." we just leave things as they are.
1046 Otherwise, we figure out what the parent directory is,
1050 process_top_path (char *pathname
, mode_t mode
)
1052 at_top(pathname
, mode
, NULL
, do_process_top_dir
);
1056 /* Info on each directory in the current tree branch, to avoid
1057 getting stuck in symbolic link loops. */
1058 static struct dir_id
*dir_ids
= NULL
;
1059 /* Entries allocated in `dir_ids'. */
1060 static int dir_alloc
= 0;
1061 /* Index in `dir_ids' of directory currently being searched.
1062 This is always the last valid entry. */
1063 static int dir_curr
= -1;
1064 /* (Arbitrary) number of entries to grow `dir_ids' by. */
1065 #define DIR_ALLOC_STEP 32
1069 /* We've detected a file system loop. This is caused by one of
1072 * 1. Option -L is in effect and we've hit a symbolic link that
1073 * points to an ancestor. This is harmless. We won't traverse the
1076 * 2. We have hit a real cycle in the directory hierarchy. In this
1077 * case, we issue a diagnostic message (POSIX requires this) and we
1078 * skip that directory entry.
1081 issue_loop_warning(const char *name
, const char *pathname
, int level
)
1083 struct stat stbuf_link
;
1084 if (lstat(name
, &stbuf_link
) != 0)
1085 stbuf_link
.st_mode
= S_IFREG
;
1087 if (S_ISLNK(stbuf_link
.st_mode
))
1090 _("Symbolic link %s is part of a loop in the directory hierarchy; we have already visited the directory to which it points."),
1091 safely_quote_err_filename(0, pathname
));
1095 int distance
= 1 + (dir_curr
-level
);
1096 /* We have found an infinite loop. POSIX requires us to
1097 * issue a diagnostic. Usually we won't get to here
1098 * because when the leaf optimisation is on, it will cause
1099 * the subdirectory to be skipped. If /a/b/c/d is a hard
1100 * link to /a/b, then the link count of /a/b/c is 2,
1101 * because the ".." entry of /b/b/c/d points to /a, not
1105 _("Filesystem loop detected; %s has the same device number and inode as a directory which is %d %s."),
1106 safely_quote_err_filename(0, pathname
),
1109 _("level higher in the file system hierarchy") :
1110 _("levels higher in the file system hierarchy")));
1116 /* Recursively descend path PATHNAME, applying the predicates.
1117 LEAF is true if PATHNAME is known to be in a directory that has no
1118 more unexamined subdirectories, and therefore it is not a directory.
1119 Knowing this allows us to avoid calling stat as long as possible for
1122 NAME is PATHNAME relative to the current directory. We access NAME
1125 PARENT is the path of the parent of NAME, relative to find's
1128 Return nonzero iff PATHNAME is a directory. */
1131 process_path (char *pathname
, char *name
, boolean leaf
, char *parent
,
1134 struct stat stat_buf
;
1135 static dev_t root_dev
; /* Device ID of current argument pathname. */
1137 struct predicate
*eval_tree
;
1139 eval_tree
= get_eval_tree();
1140 /* Assume it is a non-directory initially. */
1141 stat_buf
.st_mode
= 0;
1142 state
.rel_pathname
= name
;
1144 state
.have_stat
= false;
1145 state
.have_type
= false;
1147 if (!digest_mode(mode
, pathname
, name
, &stat_buf
, leaf
))
1150 if (!S_ISDIR (state
.type
))
1152 if (state
.curdepth
>= options
.mindepth
)
1153 apply_predicate (pathname
, &stat_buf
, eval_tree
);
1157 /* From here on, we're working on a directory. */
1160 /* Now we really need to stat the directory, even if we know the
1161 * type, because we need information like struct stat.st_rdev.
1163 if (get_statinfo(pathname
, name
, &stat_buf
) != 0)
1166 state
.have_stat
= true;
1167 mode
= state
.type
= stat_buf
.st_mode
; /* use full info now that we have it. */
1168 state
.stop_at_current_level
=
1169 options
.maxdepth
>= 0
1170 && state
.curdepth
>= options
.maxdepth
;
1172 /* If we've already seen this directory on this branch,
1173 don't descend it again. */
1174 for (i
= 0; i
<= dir_curr
; i
++)
1175 if (stat_buf
.st_ino
== dir_ids
[i
].ino
&&
1176 stat_buf
.st_dev
== dir_ids
[i
].dev
)
1178 state
.stop_at_current_level
= true;
1179 issue_loop_warning(name
, pathname
, i
);
1182 if (dir_alloc
<= ++dir_curr
)
1184 dir_alloc
+= DIR_ALLOC_STEP
;
1185 dir_ids
= (struct dir_id
*)
1186 xrealloc ((char *) dir_ids
, dir_alloc
* sizeof (struct dir_id
));
1188 dir_ids
[dir_curr
].ino
= stat_buf
.st_ino
;
1189 dir_ids
[dir_curr
].dev
= stat_buf
.st_dev
;
1191 if (options
.stay_on_filesystem
)
1193 if (state
.curdepth
== 0)
1194 root_dev
= stat_buf
.st_dev
;
1195 else if (stat_buf
.st_dev
!= root_dev
)
1196 state
.stop_at_current_level
= true;
1199 if (options
.do_dir_first
&& state
.curdepth
>= options
.mindepth
)
1200 apply_predicate (pathname
, &stat_buf
, eval_tree
);
1202 if (options
.debug_options
& DebugSearch
)
1203 fprintf(stderr
, "pathname = %s, stop_at_current_level = %d\n",
1204 pathname
, state
.stop_at_current_level
);
1206 if (state
.stop_at_current_level
== false)
1208 /* Scan directory on disk. */
1209 process_dir (pathname
, name
, strlen (pathname
), &stat_buf
, parent
);
1212 if (options
.do_dir_first
== false && state
.curdepth
>= options
.mindepth
)
1214 /* The fields in 'state' are now out of date. Correct them.
1216 if (!digest_mode(mode
, pathname
, name
, &stat_buf
, leaf
))
1221 at_top(pathname
, mode
, &stat_buf
, do_process_predicate
);
1225 do_process_predicate(pathname
, name
, mode
, &stat_buf
);
1235 /* Scan directory PATHNAME and recurse through process_path for each entry.
1237 PATHLEN is the length of PATHNAME.
1239 NAME is PATHNAME relative to the current directory.
1241 STATP is the results of *options.xstat on it.
1243 PARENT is the path of the parent of NAME, relative to find's
1244 starting directory. */
1247 process_dir (char *pathname
, char *name
, int pathlen
, const struct stat
*statp
, char *parent
)
1249 int subdirs_left
; /* Number of unexamined subdirs in PATHNAME. */
1250 boolean subdirs_unreliable
; /* if true, cannot use dir link count as subdir limif (if false, it may STILL be unreliable) */
1251 unsigned int idx
; /* Which entry are we on? */
1252 struct stat stat_buf
;
1253 size_t dircount
= 0u;
1254 struct savedir_dirinfo
*dirinfo
;
1256 printf("process_dir: pathname=%s name=%s statp->st_nlink=%d st_ino=%d\n",
1259 (int)statp
->st_nlink
,
1260 (int)statp
->st_ino
);
1262 if (statp
->st_nlink
< 2)
1264 subdirs_unreliable
= true;
1269 subdirs_unreliable
= false; /* not necessarily right */
1270 subdirs_left
= statp
->st_nlink
- 2; /* Account for name and ".". */
1274 dirinfo
= xsavedir(name
, 0);
1277 if (dirinfo
== NULL
)
1279 assert (errno
!= 0);
1280 error (0, errno
, "%s", safely_quote_err_filename(0, pathname
));
1281 state
.exit_status
= 1;
1285 register char *namep
; /* Current point in `name_space'. */
1286 char *cur_path
; /* Full path of each file to process. */
1287 char *cur_name
; /* Base name of each file to process. */
1288 unsigned cur_path_size
; /* Bytes allocated for `cur_path'. */
1289 register unsigned file_len
; /* Length of each path to process. */
1290 register unsigned pathname_len
; /* PATHLEN plus trailing '/'. */
1291 boolean did_stat
= false;
1293 if (pathname
[pathlen
- 1] == '/')
1294 pathname_len
= pathlen
+ 1; /* For '\0'; already have '/'. */
1296 pathname_len
= pathlen
+ 2; /* For '/' and '\0'. */
1300 /* We're about to leave the directory. If there are any
1301 * -execdir argument lists which have been built but have not
1302 * yet been processed, do them now because they must be done in
1303 * the same directory.
1305 complete_pending_execdirs(get_current_dirfd());
1307 if (strcmp (name
, "."))
1309 enum SafeChdirStatus status
= safely_chdir (name
, TraversingDown
, &stat_buf
, SymlinkHandleDefault
, &did_stat
);
1313 /* If there had been a change but wd_sanity_check()
1314 * accepted it, we need to accept that on the
1315 * way back up as well, so modify our record
1316 * of what we think we should see later.
1317 * If there was no change, the assignments are a no-op.
1319 * However, before performing the assignment, we need to
1320 * check that we have the stat information. If O_NOFOLLOW
1321 * is available, safely_chdir() will not have needed to use
1322 * stat(), and so stat_buf will just contain random data.
1326 /* If there is a link we need to follow it. Hence
1327 * the direct call to stat() not through (options.xstat)
1329 set_stat_placeholders(&stat_buf
);
1330 if (0 != stat(".", &stat_buf
))
1331 break; /* skip the assignment. */
1333 dir_ids
[dir_curr
].dev
= stat_buf
.st_dev
;
1334 dir_ids
[dir_curr
].ino
= stat_buf
.st_ino
;
1338 case SafeChdirFailWouldBeUnableToReturn
:
1339 error (0, errno
, ".");
1340 state
.exit_status
= 1;
1343 case SafeChdirFailNonexistent
:
1344 case SafeChdirFailDestUnreadable
:
1345 case SafeChdirFailStat
:
1346 case SafeChdirFailNotDir
:
1347 case SafeChdirFailChdirFailed
:
1348 error (0, errno
, "%s",
1349 safely_quote_err_filename(0, pathname
));
1350 state
.exit_status
= 1;
1353 case SafeChdirFailSymlink
:
1355 _("warning: not following the symbolic link %s"),
1356 safely_quote_err_filename(0, pathname
));
1357 state
.exit_status
= 1;
1362 for (idx
=0; idx
< dirinfo
->size
; ++idx
)
1364 /* savedirinfo() may return dirinfo=NULL if extended information
1367 mode_t mode
= (dirinfo
->entries
[idx
].flags
& SavedirHaveFileType
) ?
1368 dirinfo
->entries
[idx
].type_info
: 0;
1369 namep
= dirinfo
->entries
[idx
].name
;
1371 /* Append this directory entry's name to the path being searched. */
1372 file_len
= pathname_len
+ strlen (namep
);
1373 if (file_len
> cur_path_size
)
1375 while (file_len
> cur_path_size
)
1376 cur_path_size
+= 1024;
1379 cur_path
= xmalloc (cur_path_size
);
1380 strcpy (cur_path
, pathname
);
1381 cur_path
[pathname_len
- 2] = '/';
1383 cur_name
= cur_path
+ pathname_len
- 1;
1384 strcpy (cur_name
, namep
);
1387 if (!options
.no_leaf_check
&& !subdirs_unreliable
)
1389 if (mode
&& S_ISDIR(mode
) && (subdirs_left
== 0))
1391 /* This is a subdirectory, but the number of directories we
1392 * have found now exceeds the number we would expect given
1393 * the hard link count on the parent. This is likely to be
1394 * a bug in the file system driver (e.g. Linux's
1395 * /proc file system) or may just be a fact that the OS
1396 * doesn't really handle hard links with Unix semantics.
1397 * In the latter case, -noleaf should be used routinely.
1399 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."),
1400 safely_quote_err_filename(0, pathname
),
1403 state
.exit_status
= 1; /* We know the result is wrong, now */
1404 options
.no_leaf_check
= true; /* Don't make same
1406 subdirs_unreliable
= 1;
1407 subdirs_left
= 1; /* band-aid for this iteration. */
1410 /* Normal case optimization. On normal Unix
1411 file systems, a directory that has no subdirectories
1412 has two links: its name, and ".". Any additional
1413 links are to the ".." entries of its subdirectories.
1414 Once we have processed as many subdirectories as
1415 there are additional links, we know that the rest of
1416 the entries are non-directories -- in other words,
1420 count
= process_path (cur_path
, cur_name
,
1421 subdirs_left
== 0, pathname
,
1423 subdirs_left
-= count
;
1429 /* There might be weird (e.g., CD-ROM or MS-DOS) file systems
1430 mounted, which don't have Unix-like directory link counts. */
1431 process_path (cur_path
, cur_name
, false, pathname
, mode
);
1438 /* We're about to leave the directory. If there are any
1439 * -execdir argument lists which have been built but have not
1440 * yet been processed, do them now because they must be done in
1441 * the same directory.
1443 complete_pending_execdirs(get_current_dirfd());
1445 if (strcmp (name
, "."))
1447 enum SafeChdirStatus status
;
1450 /* We could go back and do the next command-line arg
1451 instead, maybe using longjmp. */
1453 boolean deref
= following_links() ? true : false;
1455 if ( (state
.curdepth
>0) && !deref
)
1464 status
= safely_chdir (dir
, TraversingUp
, &stat_buf
, SymlinkHandleDefault
, &did_stat
);
1470 case SafeChdirFailWouldBeUnableToReturn
:
1471 error (1, errno
, ".");
1474 case SafeChdirFailNonexistent
:
1475 case SafeChdirFailDestUnreadable
:
1476 case SafeChdirFailStat
:
1477 case SafeChdirFailSymlink
:
1478 case SafeChdirFailNotDir
:
1479 case SafeChdirFailChdirFailed
:
1480 error (1, errno
, "%s", safely_quote_err_filename(0, pathname
));
1486 did
.dev
= dir_ids
[dir_curr
-1].dev
;
1487 did
.ino
= dir_ids
[dir_curr
-1].ino
;
1491 did
.dev
= starting_stat_buf
.st_dev
;
1492 did
.ino
= starting_stat_buf
.st_ino
;
1498 free_dirinfo(dirinfo
);
1501 if (subdirs_unreliable
)
1503 /* Make sure we hasn't used the variable subdirs_left if we knew
1504 * we shouldn't do so.
1506 assert (0 == subdirs_left
|| options
.no_leaf_check
);