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)
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,
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>.
32 #define USE_SAFE_CHDIR 1
33 #undef STAT_MOUNTPOINTS
47 #include "../gnulib/lib/xalloc.h"
48 #include "../gnulib/lib/human.h"
49 #include "../gnulib/lib/canonicalize.h"
53 #include "savedirinfo.h"
65 # define _(Text) gettext (Text)
68 #define textdomain(Domain)
69 #define bindtextdomain(Package, Directory)
72 # define N_(String) gettext_noop (String)
74 /* See locate.c for explanation as to why not use (String) */
75 # define N_(String) String
78 #define apply_predicate(pathname, stat_buf_ptr, node) \
79 (*(node)->pred_func)((pathname), (stat_buf_ptr), (node))
81 #ifdef STAT_MOUNTPOINTS
82 static void init_mounted_dev_list(int mandatory
);
85 static void process_top_path
PARAMS((char *pathname
, mode_t mode
));
86 static int process_path
PARAMS((char *pathname
, char *name
, boolean leaf
, char *parent
, mode_t type
));
87 static void process_dir
PARAMS((char *pathname
, char *name
, int pathlen
, struct stat
*statp
, char *parent
));
91 /* Name this program was run with. */
94 /* A file descriptor open to the initial working directory.
95 Doing it this way allows us to work when the i.w.d. has
96 unreadable parents. */
99 /* The stat buffer of the initial working directory. */
100 static struct stat starting_stat_buf
;
102 enum ChdirSymlinkHandling
104 SymlinkHandleDefault
, /* Normally the right choice */
105 SymlinkFollowOk
/* see comment in process_top_path() */
109 enum TraversalDirection
115 enum WdSanityCheckFatality
117 FATAL_IF_SANITY_CHECK_FAILS
,
118 RETRY_IF_SANITY_CHECK_FAILS
,
119 NON_FATAL_IF_SANITY_CHECK_FAILS
123 int get_current_dirfd(void)
130 main (int argc
, char **argv
)
133 int end_of_leading_options
= 0; /* First arg after any -H/-L etc. */
134 struct predicate
*eval_tree
;
136 program_name
= argv
[0];
137 state
.exit_status
= 0;
139 /* Set the option defaults before we do the the locale
140 * initialisation as check_nofollow() needs to be executed in the
143 set_option_defaults(&options
);
145 #ifdef HAVE_SETLOCALE
146 setlocale (LC_ALL
, "");
148 bindtextdomain (PACKAGE
, LOCALEDIR
);
149 textdomain (PACKAGE
);
150 atexit (close_stdout
);
152 /* Check for -P, -H or -L options. */
153 end_of_leading_options
= process_leading_options(argc
, argv
);
155 if (options
.debug_options
& DebugStat
)
156 options
.xstat
= debug_stat
;
159 fprintf (stderr
, "cur_day_start = %s", ctime (&options
.cur_day_start
));
162 /* state.cwd_dir_fd has to be initialised before we call build_expression_tree()
163 * because command-line parsing may lead us to stat some files.
165 state
.cwd_dir_fd
= AT_FDCWD
;
167 /* We are now processing the part of the "find" command line
168 * after the -H/-L options (if any).
170 eval_tree
= build_expression_tree(argc
, argv
, end_of_leading_options
);
173 /* safely_chdir() needs to check that it has ended up in the right place.
174 * To avoid bailing out when something gets automounted, it checks if
175 * the target directory appears to have had a directory mounted on it as
176 * we chdir()ed. The problem with this is that in order to notice that
177 * a filesystem was mounted, we would need to lstat() all the mount points.
178 * That strategy loses if our machine is a client of a dead NFS server.
180 * Hence if safely_chdir() and wd_sanity_check() can manage without needing
181 * to know the mounted device list, we do that.
183 if (!options
.open_nofollow_available
)
185 #ifdef STAT_MOUNTPOINTS
186 init_mounted_dev_list(0);
191 starting_desc
= open (".", O_RDONLY
192 #if defined O_LARGEFILE
196 if (0 <= starting_desc
&& fchdir (starting_desc
) != 0)
198 close (starting_desc
);
201 assert(starting_desc
>= 0);
203 if (starting_desc
< 0)
205 starting_dir
= xgetcwd ();
207 error (1, errno
, _("cannot get current directory"));
209 set_stat_placeholders(&starting_stat_buf
);
210 if ((*options
.xstat
) (".", &starting_stat_buf
) != 0)
211 error (1, errno
, _("cannot stat current directory"));
213 /* If no paths are given, default to ".". */
214 for (i
= end_of_leading_options
; i
< argc
&& !looks_like_expression(argv
[i
], true); i
++)
216 process_top_path (argv
[i
], 0);
219 /* If there were no path arguments, default to ".". */
220 if (i
== end_of_leading_options
)
223 * We use a temporary variable here because some actions modify
224 * the path temporarily. Hence if we use a string constant,
225 * we get a coredump. The best example of this is if we say
226 * "find -printf %H" (note, not "find . -printf %H").
228 char defaultpath
[2] = ".";
229 process_top_path (defaultpath
, 0);
232 /* If "-exec ... {} +" has been used, there may be some
233 * partially-full command lines which have been built,
234 * but which are not yet complete. Execute those now.
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. */
249 specific_dirname(const char *dir
)
253 if (0 == strcmp(".", dir
))
255 /* OK, what's '.'? */
256 if (NULL
!= getcwd(dirbuf
, sizeof(dirbuf
)))
258 return strdup(dirbuf
);
267 char *result
= canonicalize_filename_mode(dir
, CAN_EXISTING
);
277 /* Return non-zero if FS is the name of a filesystem that is likely to
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;
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.");
306 refresh_mounted_dev_list(void)
310 free(mounted_devices
);
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. */
320 dev_present(dev_t dev
, const dev_t
*list
, size_t n
)
326 if ( (*list
++) == dev
)
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
;
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
,
380 struct stat
*newinfo
)
382 enum MountPointStateChange transition
= get_mount_state(newinfo
->st_dev
);
385 case MountPointRecentlyUnmounted
:
386 isfatal
= NON_FATAL_IF_SANITY_CHECK_FAILS
;
390 _("Warning: filesystem %s has recently been unmounted."),
395 case MountPointRecentlyMounted
:
396 isfatal
= NON_FATAL_IF_SANITY_CHECK_FAILS
;
400 _("Warning: filesystem %s has recently been mounted."),
405 case MountPointStateUnchanged
:
406 /* leave isfatal as it is */
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
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
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.
447 wd_sanity_check(const char *thing_to_stat
,
448 const char *progname
,
452 struct stat
*newinfo
,
455 enum TraversalDirection direction
,
456 enum WdSanityCheckFatality isfatal
,
457 boolean
*changed
) /* output parameter */
460 char *specific_what
= NULL
;
462 const char *current_dir
= ".";
466 set_stat_placeholders(newinfo
);
467 if ((*options
.xstat
) (current_dir
, newinfo
) != 0)
468 error (1, errno
, "%s", thing_to_stat
);
470 if (old_dev
!= newinfo
->st_dev
)
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
);
486 isfatal
= RETRY_IF_SANITY_CHECK_FAILS
;
492 case FATAL_IF_SANITY_CHECK_FAILS
:
494 fstype
= filesystem_type(newinfo
, current_dir
);
496 _("%s%s changed during execution of %s (old device number %ld, new device number %ld, filesystem type is %s) [ref %ld]"),
501 (long) newinfo
->st_dev
,
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.
519 case RETRY_IF_SANITY_CHECK_FAILS
:
524 /* Device number was the same, check if the inode has changed. */
525 if (old_ino
!= newinfo
->st_ino
)
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]"),
538 (long) newinfo
->st_ino
,
551 SafeChdirFailSymlink
,
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
,
574 struct stat statbuf_arrived
;
576 int saved_errno
; /* specific_dirname() changes errno. */
577 boolean rv_set
= false;
578 boolean statflag
= false;
580 enum WdSanityCheckFatality isfatal
= RETRY_IF_SANITY_CHECK_FAILS
;
582 saved_errno
= errno
= 0;
584 dotfd
= open(".", O_RDONLY
585 #if defined O_LARGEFILE
590 /* We jump back to here if wd_sanity_check()
591 * recoverably triggers an alert.
598 /* Stat the directory we're going to. */
599 set_stat_placeholders(statbuf_dest
);
600 if (0 == options
.xstat(dest
, statbuf_dest
))
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
;
637 /* Not following symlinks, so the attempt to
638 * chdir() into a symlink should be prevented.
640 rv
= SafeChdirFailSymlink
;
642 saved_errno
= 0; /* silence the error message */
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
;
658 saved_errno
= 0; /* silence the error message */
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
,
674 0, __LINE__
, direction
,
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
;
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
696 "failed to return to parent directory");
701 /* XXX: not sure what to use as an excuse here. */
702 rv
= SafeChdirFailNonexistent
;
715 if (ENOENT
== saved_errno
)
717 rv
= SafeChdirFailNonexistent
;
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
728 saved_errno
= 0; /* don't issue err msg */
729 rv
= SafeChdirFailNotDir
;
734 rv
= SafeChdirFailChdirFailed
;
743 rv
= SafeChdirFailStat
;
746 if ( (ENOENT
== saved_errno
) || (0 == state
.curdepth
))
747 saved_errno
= 0; /* don't issue err msg */
753 /* We do not have read permissions on "." */
754 rv
= SafeChdirFailWouldBeUnableToReturn
;
759 /* This is the success path, so we clear errno. The caller probably
760 * won't be calling error() anyway.
764 /* We use the same exit path for success or failure.
765 * which has occurred is recorded in RV.
768 /* We do not call error() as this would result in a duplicate error
769 * message when the caller does the same thing.
780 *did_stat
= statflag
;
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
,
807 switch (symlink_follow_option
)
809 case SymlinkFollowOk
:
813 case SymlinkHandleDefault
:
814 if (following_links())
817 extraflags
= O_NOFOLLOW
;
822 fd
= open(dest
, O_RDONLY
823 #if defined O_LARGEFILE
832 return SafeChdirFailSymlink
; /* This is why we use O_NOFOLLOW */
834 return SafeChdirFailNonexistent
;
836 return SafeChdirFailDestUnreadable
;
848 int saved_errno
= errno
;
855 return SafeChdirFailNotDir
;
858 case EBADF
: /* Shouldn't happen */
862 return SafeChdirFailChdirFailed
;
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
,
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
882 complete_pending_execdirs(get_current_dirfd());
884 #if !defined(O_NOFOLLOW)
885 options
.open_nofollow_available
= false;
887 if (options
.open_nofollow_available
)
888 result
= safely_chdir_nofollow(dest
, direction
, statbuf_dest
, symlink_follow_option
, did_stat
);
890 result
= safely_chdir_lstat(dest
, direction
, statbuf_dest
, symlink_follow_option
, did_stat
);
896 /* Safely go back to the starting directory. */
900 struct stat stat_buf
;
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
912 if (NULL
== mounted_devices
)
913 init_mounted_dev_list(1);
916 if (chdir (starting_dir
) != 0)
917 error (1, errno
, "%s", starting_dir
);
919 wd_sanity_check(starting_dir
,
922 starting_stat_buf
.st_dev
,
923 starting_stat_buf
.st_ino
,
924 &stat_buf
, 0, __LINE__
,
926 FATAL_IF_SANITY_CHECK_FAILS
,
931 if (options
.debug_options
& DebugSearch
)
932 fprintf(stderr
, "chdir_back(): chdir(<starting-point>)\n");
934 if (fchdir (starting_desc
) != 0)
936 error (1, errno
, "%s", 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.
946 at_top (char *pathname
,
949 void (*action
)(char *pathname
,
955 char *parent_dir
= dir_name(pathname
);
956 char *base
= base_name(pathname
);
959 state
.starting_path_length
= strlen (pathname
);
961 if (0 == strcmp(pathname
, parent_dir
)
962 || 0 == strcmp(parent_dir
, "."))
969 enum TraversalDirection direction
;
970 enum SafeChdirStatus chdir_status
;
972 boolean did_stat
= false;
975 if (0 == strcmp(base
, ".."))
976 direction
= TraversingUp
;
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
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
;
995 error (0, errno
, "%s", what
);
997 error (0, 0, "Failed to safely change directory into `%s'",
1000 /* We can't process this command-line argument. */
1001 state
.exit_status
= 1;
1009 action(pathname
, base
, mode
, pstat
);
1018 static void do_process_top_dir(char *pathname
,
1025 process_path (pathname
, base
, false, ".", mode
);
1026 complete_pending_execdirs(get_current_dirfd());
1029 static void do_process_predicate(char *pathname
,
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,
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 filesystem loop. This is caused by one of
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
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.
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
))
1094 _("Symbolic link `%s' is part of a loop in the directory hierarchy; we have already visited the directory to which it points."),
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
1109 _("Filesystem loop detected; `%s' has the same device number and inode as a directory which is %d %s."),
1113 _("level higher in the filesystem hierarchy") :
1114 _("levels higher in the filesystem 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
1126 NAME is PATHNAME relative to the current directory. We access NAME
1129 PARENT is the path of the parent of NAME, relative to find's
1132 Return nonzero iff PATHNAME is a directory. */
1135 process_path (char *pathname
, char *name
, boolean leaf
, char *parent
,
1138 struct stat stat_buf
;
1139 static dev_t root_dev
; /* Device ID of current argument pathname. */
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
;
1148 state
.have_stat
= false;
1149 state
.have_type
= false;
1151 if (!digest_mode(mode
, pathname
, name
, &stat_buf
, leaf
))
1154 if (!S_ISDIR (state
.type
))
1156 if (state
.curdepth
>= options
.mindepth
)
1157 apply_predicate (pathname
, &stat_buf
, eval_tree
);
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 (0 == stat_buf
.st_mode
)
1169 /* This call was made conditional on Sat Apr 14 16:01:01 2007,
1170 * and at that time the test suite passed without it. Omitting
1171 * this stat call saves a lot of system calls.
1173 if (get_statinfo(pathname
, name
, &stat_buf
) != 0)
1176 state
.have_stat
= true;
1178 mode
= state
.type
= stat_buf
.st_mode
; /* use full info now that we have it. */
1179 state
.stop_at_current_level
=
1180 options
.maxdepth
>= 0
1181 && state
.curdepth
>= options
.maxdepth
;
1183 /* If we've already seen this directory on this branch,
1184 don't descend it again. */
1185 for (i
= 0; i
<= dir_curr
; i
++)
1186 if (stat_buf
.st_ino
== dir_ids
[i
].ino
&&
1187 stat_buf
.st_dev
== dir_ids
[i
].dev
)
1189 state
.stop_at_current_level
= true;
1190 issue_loop_warning(name
, pathname
, i
);
1193 if (dir_alloc
<= ++dir_curr
)
1195 dir_alloc
+= DIR_ALLOC_STEP
;
1196 dir_ids
= (struct dir_id
*)
1197 xrealloc ((char *) dir_ids
, dir_alloc
* sizeof (struct dir_id
));
1199 dir_ids
[dir_curr
].ino
= stat_buf
.st_ino
;
1200 dir_ids
[dir_curr
].dev
= stat_buf
.st_dev
;
1202 if (options
.stay_on_filesystem
)
1204 if (state
.curdepth
== 0)
1205 root_dev
= stat_buf
.st_dev
;
1206 else if (stat_buf
.st_dev
!= root_dev
)
1207 state
.stop_at_current_level
= true;
1210 if (options
.do_dir_first
&& state
.curdepth
>= options
.mindepth
)
1211 apply_predicate (pathname
, &stat_buf
, eval_tree
);
1213 if (options
.debug_options
& DebugSearch
)
1214 fprintf(stderr
, "pathname = %s, stop_at_current_level = %d\n",
1215 pathname
, state
.stop_at_current_level
);
1217 if (state
.stop_at_current_level
== false)
1218 /* Scan directory on disk. */
1219 process_dir (pathname
, name
, strlen (pathname
), &stat_buf
, parent
);
1221 if (options
.do_dir_first
== false && state
.curdepth
>= options
.mindepth
)
1223 /* The fields in 'state' are now out of date. Correct them.
1225 if (!digest_mode(mode
, pathname
, name
, &stat_buf
, leaf
))
1230 at_top(pathname
, mode
, &stat_buf
, do_process_predicate
);
1234 do_process_predicate(pathname
, name
, mode
, &stat_buf
);
1244 /* Scan directory PATHNAME and recurse through process_path for each entry.
1246 PATHLEN is the length of PATHNAME.
1248 NAME is PATHNAME relative to the current directory.
1250 STATP is the results of *options.xstat on it.
1252 PARENT is the path of the parent of NAME, relative to find's
1253 starting directory. */
1256 process_dir (char *pathname
, char *name
, int pathlen
, struct stat
*statp
, char *parent
)
1258 int subdirs_left
; /* Number of unexamined subdirs in PATHNAME. */
1259 boolean subdirs_unreliable
; /* if true, cannot use dir link count as subdir limif (if false, it may STILL be unreliable) */
1260 unsigned int idx
; /* Which entry are we on? */
1261 struct stat stat_buf
;
1263 struct savedir_dirinfo
*dirinfo
;
1265 if (statp
->st_nlink
< 2)
1267 subdirs_unreliable
= true;
1272 subdirs_unreliable
= false; /* not necessarily right */
1273 subdirs_left
= statp
->st_nlink
- 2; /* Account for name and ".". */
1277 dirinfo
= xsavedir(name
, 0);
1280 if (dirinfo
== NULL
)
1283 error (0, errno
, "%s", pathname
);
1284 state
.exit_status
= 1;
1288 register char *namep
; /* Current point in `name_space'. */
1289 char *cur_path
; /* Full path of each file to process. */
1290 char *cur_name
; /* Base name of each file to process. */
1291 unsigned cur_path_size
; /* Bytes allocated for `cur_path'. */
1292 register unsigned file_len
; /* Length of each path to process. */
1293 register unsigned pathname_len
; /* PATHLEN plus trailing '/'. */
1294 boolean did_stat
= false;
1296 if (pathname
[pathlen
- 1] == '/')
1297 pathname_len
= pathlen
+ 1; /* For '\0'; already have '/'. */
1299 pathname_len
= pathlen
+ 2; /* For '/' and '\0'. */
1303 /* We're about to leave the directory. If there are any
1304 * -execdir argument lists which have been built but have not
1305 * yet been processed, do them now because they must be done in
1306 * the same directory.
1308 complete_pending_execdirs(get_current_dirfd());
1310 if (strcmp (name
, "."))
1312 enum SafeChdirStatus status
= safely_chdir (name
, TraversingDown
, &stat_buf
, SymlinkHandleDefault
, &did_stat
);
1316 /* If there had been a change but wd_sanity_check()
1317 * accepted it, we need to accept that on the
1318 * way back up as well, so modify our record
1319 * of what we think we should see later.
1320 * If there was no change, the assignments are a no-op.
1322 * However, before performing the assignment, we need to
1323 * check that we have the stat information. If O_NOFOLLOW
1324 * is available, safely_chdir() will not have needed to use
1325 * stat(), and so stat_buf will just contain random data.
1329 /* If there is a link we need to follow it. Hence
1330 * the direct call to stat() not through (options.xstat)
1332 set_stat_placeholders(&stat_buf
);
1333 if (0 != stat(".", &stat_buf
))
1334 break; /* skip the assignment. */
1336 dir_ids
[dir_curr
].dev
= stat_buf
.st_dev
;
1337 dir_ids
[dir_curr
].ino
= stat_buf
.st_ino
;
1341 case SafeChdirFailWouldBeUnableToReturn
:
1342 error (0, errno
, ".");
1343 state
.exit_status
= 1;
1346 case SafeChdirFailNonexistent
:
1347 case SafeChdirFailDestUnreadable
:
1348 case SafeChdirFailStat
:
1349 case SafeChdirFailNotDir
:
1350 case SafeChdirFailChdirFailed
:
1351 error (0, errno
, "%s", pathname
);
1352 state
.exit_status
= 1;
1355 case SafeChdirFailSymlink
:
1357 _("warning: not following the symbolic link %s"),
1359 state
.exit_status
= 1;
1364 for (idx
=0; idx
< dirinfo
->size
; ++idx
)
1366 /* savedirinfo() may return dirinfo=NULL if extended information
1369 mode_t mode
= (dirinfo
->entries
[idx
].flags
& SavedirHaveFileType
) ?
1370 dirinfo
->entries
[idx
].type_info
: 0;
1371 namep
= dirinfo
->entries
[idx
].name
;
1373 /* Append this directory entry's name to the path being searched. */
1374 file_len
= pathname_len
+ strlen (namep
);
1375 if (file_len
> cur_path_size
)
1377 while (file_len
> cur_path_size
)
1378 cur_path_size
+= 1024;
1381 cur_path
= xmalloc (cur_path_size
);
1382 strcpy (cur_path
, pathname
);
1383 cur_path
[pathname_len
- 2] = '/';
1385 cur_name
= cur_path
+ pathname_len
- 1;
1386 strcpy (cur_name
, namep
);
1389 if (!options
.no_leaf_check
&& !subdirs_unreliable
)
1391 if (mode
&& S_ISDIR(mode
) && (subdirs_left
== 0))
1393 /* This is a subdirectory, but the number of directories we
1394 * have found now exceeds the number we would expect given
1395 * the hard link count on the parent. This is likely to be
1396 * a bug in the filesystem driver (e.g. Linux's
1397 * /proc filesystem) or may just be a fact that the OS
1398 * doesn't really handle hard links with Unix semantics.
1399 * In the latter case, -noleaf should be used routinely.
1401 error(0, 0, _("WARNING: Hard link count is wrong for %s: this may be a bug in your filesystem driver. Automatically turning on find's -noleaf option. Earlier results may have failed to include directories that should have been searched."),
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 filesystems, 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,
1418 subdirs_left
-= process_path (cur_path
, cur_name
,
1419 subdirs_left
== 0, pathname
,
1424 /* There might be weird (e.g., CD-ROM or MS-DOS) filesystems
1425 mounted, which don't have Unix-like directory link counts. */
1426 process_path (cur_path
, cur_name
, false, pathname
, mode
);
1433 /* We're about to leave the directory. If there are any
1434 * -execdir argument lists which have been built but have not
1435 * yet been processed, do them now because they must be done in
1436 * the same directory.
1438 complete_pending_execdirs(get_current_dirfd());
1440 if (strcmp (name
, "."))
1442 enum SafeChdirStatus status
;
1445 /* We could go back and do the next command-line arg
1446 instead, maybe using longjmp. */
1448 boolean deref
= following_links() ? true : false;
1450 if ( (state
.curdepth
>0) && !deref
)
1459 status
= safely_chdir (dir
, TraversingUp
, &stat_buf
, SymlinkHandleDefault
, &did_stat
);
1465 case SafeChdirFailWouldBeUnableToReturn
:
1466 error (1, errno
, ".");
1469 case SafeChdirFailNonexistent
:
1470 case SafeChdirFailDestUnreadable
:
1471 case SafeChdirFailStat
:
1472 case SafeChdirFailSymlink
:
1473 case SafeChdirFailNotDir
:
1474 case SafeChdirFailChdirFailed
:
1475 error (1, errno
, "%s", pathname
);
1481 did
.dev
= dir_ids
[dir_curr
-1].dev
;
1482 did
.ino
= dir_ids
[dir_curr
-1].ino
;
1486 did
.dev
= starting_stat_buf
.st_dev
;
1487 did
.ino
= starting_stat_buf
.st_ino
;
1493 free_dirinfo(dirinfo
);
1496 if (subdirs_unreliable
)
1498 /* Make sure we hasn't used the variable subdirs_left if we knew
1499 * we shouldn't do so.
1501 assert(0 == subdirs_left
|| options
.no_leaf_check
);