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
46 #include "../gnulib/lib/xalloc.h"
47 #include "../gnulib/lib/human.h"
48 #include "../gnulib/lib/canonicalize.h"
51 #include "savedirinfo.h"
63 # define _(Text) gettext (Text)
66 #define textdomain(Domain)
67 #define bindtextdomain(Package, Directory)
70 # define N_(String) gettext_noop (String)
72 /* See locate.c for explanation as to why not use (String) */
73 # define N_(String) String
76 #define apply_predicate(pathname, stat_buf_ptr, node) \
77 (*(node)->pred_func)((pathname), (stat_buf_ptr), (node))
79 #ifdef STAT_MOUNTPOINTS
80 static void init_mounted_dev_list(int mandatory
);
83 static void process_top_path
PARAMS((char *pathname
, mode_t mode
));
84 static int process_path
PARAMS((char *pathname
, char *name
, boolean leaf
, char *parent
, mode_t type
));
85 static void process_dir
PARAMS((char *pathname
, char *name
, int pathlen
, struct stat
*statp
, char *parent
));
89 /* Name this program was run with. */
92 /* All predicates for each path to process. */
93 struct predicate
*predicates
;
95 /* The last predicate allocated. */
96 struct predicate
*last_pred
;
98 /* A file descriptor open to the initial working directory.
99 Doing it this way allows us to work when the i.w.d. has
100 unreadable parents. */
103 /* The stat buffer of the initial working directory. */
104 static struct stat starting_stat_buf
;
106 enum ChdirSymlinkHandling
108 SymlinkHandleDefault
, /* Normally the right choice */
109 SymlinkFollowOk
/* see comment in process_top_path() */
113 enum TraversalDirection
119 enum WdSanityCheckFatality
121 FATAL_IF_SANITY_CHECK_FAILS
,
122 RETRY_IF_SANITY_CHECK_FAILS
,
123 NON_FATAL_IF_SANITY_CHECK_FAILS
128 main (int argc
, char **argv
)
131 const struct parser_table
*entry_close
, *entry_print
, *entry_open
;
132 const struct parser_table
*parse_entry
; /* Pointer to the parsing table entry for this expression. */
133 struct predicate
*cur_pred
;
134 char *predicate_name
; /* Name of predicate being parsed. */
135 int end_of_leading_options
= 0; /* First arg after any -H/-L etc. */
138 program_name
= argv
[0];
140 /* We call check_nofollow() before setlocale() because the numbers
141 * for which we check (in the results of uname) definitiely have "."
142 * as the decimal point indicator even under locales for which that
143 * is not normally true. Hence atof() would do the wrong thing
144 * if we call it after setlocale().
147 options
.open_nofollow_available
= check_nofollow();
149 options
.open_nofollow_available
= false;
152 options
.regex_options
= RE_SYNTAX_EMACS
;
154 #ifdef HAVE_SETLOCALE
155 setlocale (LC_ALL
, "");
157 bindtextdomain (PACKAGE
, LOCALEDIR
);
158 textdomain (PACKAGE
);
159 atexit (close_stdout
);
164 options
.warnings
= true;
165 options
.literal_control_chars
= false;
169 options
.warnings
= false;
170 options
.literal_control_chars
= false; /* may change */
176 options
.do_dir_first
= true;
177 options
.maxdepth
= options
.mindepth
= -1;
178 options
.start_time
= time (NULL
);
179 options
.cur_day_start
= options
.start_time
- DAYSECS
;
180 options
.full_days
= false;
181 options
.stay_on_filesystem
= false;
182 options
.ignore_readdir_race
= false;
184 state
.exit_status
= 0;
186 #if defined(DEBUG_STAT)
187 options
.xstat
= debug_stat
;
188 #endif /* !DEBUG_STAT */
190 if (getenv("POSIXLY_CORRECT"))
191 options
.output_block_size
= 512;
193 options
.output_block_size
= 1024;
195 if (getenv("FIND_BLOCK_SIZE"))
197 error (1, 0, _("The environment variable FIND_BLOCK_SIZE is not supported, the only thing that affects the block size is the POSIXLY_CORRECT environment variable"));
200 #if LEAF_OPTIMISATION
201 /* The leaf optimisation is enabled. */
202 options
.no_leaf_check
= false;
204 /* The leaf optimisation is disabled. */
205 options
.no_leaf_check
= true;
208 set_follow_state(SYMLINK_NEVER_DEREF
); /* The default is equivalent to -P. */
211 fprintf (stderr
, "cur_day_start = %s", ctime (&options
.cur_day_start
));
214 /* Check for -P, -H or -L options. */
215 for (i
=1; (end_of_leading_options
= i
) < argc
; ++i
)
217 if (0 == strcmp("-H", argv
[i
]))
219 /* Meaning: dereference symbolic links on command line, but nowhere else. */
220 set_follow_state(SYMLINK_DEREF_ARGSONLY
);
222 else if (0 == strcmp("-L", argv
[i
]))
224 /* Meaning: dereference all symbolic links. */
225 set_follow_state(SYMLINK_ALWAYS_DEREF
);
227 else if (0 == strcmp("-P", argv
[i
]))
229 /* Meaning: never dereference symbolic links (default). */
230 set_follow_state(SYMLINK_NEVER_DEREF
);
232 else if (0 == strcmp("--", argv
[i
]))
234 /* -- signifies the end of options. */
235 end_of_leading_options
= i
+1; /* Next time start with the next option */
240 /* Hmm, must be one of
244 end_of_leading_options
= i
; /* Next time start with this option */
249 /* We are now processing the part of the "find" command line
250 * after the -H/-L options (if any).
253 /* fprintf(stderr, "rest: optind=%ld\n", (long)optind); */
255 /* Find where in ARGV the predicates begin. */
256 for (i
= end_of_leading_options
; i
< argc
&& strchr ("-!(),", argv
[i
][0]) == NULL
; i
++)
258 /* fprintf(stderr, "Looks like %s is not a predicate\n", argv[i]); */
262 /* Enclose the expression in `( ... )' so a default -print will
263 apply to the whole expression. */
264 entry_open
= find_parser("(");
265 entry_close
= find_parser(")");
266 entry_print
= find_parser("print");
267 assert(entry_open
!= NULL
);
268 assert(entry_close
!= NULL
);
269 assert(entry_print
!= NULL
);
271 parse_open (entry_open
, argv
, &argc
);
272 parse_begin_user_args(argv
, argc
, last_pred
, predicates
);
273 pred_sanity_check(last_pred
);
275 /* Build the input order list. */
278 if (strchr ("-!(),", argv
[i
][0]) == NULL
)
279 usage (_("paths must precede expression"));
280 predicate_name
= argv
[i
];
281 parse_entry
= find_parser (predicate_name
);
282 if (parse_entry
== NULL
)
284 /* Command line option not recognized */
285 error (1, 0, _("invalid predicate `%s'"), predicate_name
);
289 if (!(*(parse_entry
->parser_func
)) (parse_entry
, argv
, &i
))
292 /* Command line option requires an argument */
293 error (1, 0, _("missing argument to `%s'"), predicate_name
);
295 error (1, 0, _("invalid argument `%s' to `%s'"),
296 argv
[i
], predicate_name
);
299 pred_sanity_check(last_pred
);
300 pred_sanity_check(predicates
); /* XXX: expensive */
302 parse_end_user_args(argv
, argc
, last_pred
, predicates
);
304 if (predicates
->pred_next
== NULL
)
306 /* No predicates that do something other than set a global variable
307 were given; remove the unneeded initial `(' and add `-print'. */
308 cur_pred
= predicates
;
309 predicates
= last_pred
= predicates
->pred_next
;
310 free ((char *) cur_pred
);
311 parse_print (entry_print
, argv
, &argc
);
312 pred_sanity_check(last_pred
);
313 pred_sanity_check(predicates
); /* XXX: expensive */
315 else if (!default_prints (predicates
->pred_next
))
317 /* One or more predicates that produce output were given;
318 remove the unneeded initial `('. */
319 cur_pred
= predicates
;
320 predicates
= predicates
->pred_next
;
321 pred_sanity_check(predicates
); /* XXX: expensive */
322 free ((char *) cur_pred
);
326 /* `( user-supplied-expression ) -print'. */
327 parse_close (entry_close
, argv
, &argc
);
328 pred_sanity_check(last_pred
);
329 parse_print (entry_print
, argv
, &argc
);
330 pred_sanity_check(last_pred
);
331 pred_sanity_check(predicates
); /* XXX: expensive */
335 fprintf (stderr
, "Predicate List:\n");
336 print_list (stderr
, predicates
);
339 /* do a sanity check */
340 pred_sanity_check(predicates
);
342 /* Done parsing the predicates. Build the evaluation tree. */
343 cur_pred
= predicates
;
344 eval_tree
= get_expr (&cur_pred
, NO_PREC
);
346 /* Check if we have any left-over predicates (this fixes
347 * Debian bug #185202).
349 if (cur_pred
!= NULL
)
351 error (1, 0, _("unexpected extra predicate"));
355 fprintf (stderr
, "Eval Tree:\n");
356 print_tree (stderr
, eval_tree
, 0);
359 /* Rearrange the eval tree in optimal-predicate order. */
360 opt_expr (&eval_tree
);
362 /* Determine the point, if any, at which to stat the file. */
363 mark_stat (eval_tree
);
364 /* Determine the point, if any, at which to determine file type. */
365 mark_type (eval_tree
);
368 fprintf (stderr
, "Optimized Eval Tree:\n");
369 print_tree (stderr
, eval_tree
, 0);
370 fprintf (stderr
, "Optimized command line:\n");
371 print_optlist(stderr
, eval_tree
);
372 fprintf(stderr
, "\n");
375 /* safely_chdir() needs to check that it has ended up in the right place.
376 * To avoid bailing out when something gets automounted, it checks if
377 * the target directory appears to have had a directory mounted on it as
378 * we chdir()ed. The problem with this is that in order to notice that
379 * a filesystem was mounted, we would need to lstat() all the mount points.
380 * That strategy loses if our machine is a client of a dead NFS server.
382 * Hence if safely_chdir() and wd_sanity_check() can manage without needing
383 * to know the mounted device list, we do that.
385 if (!options
.open_nofollow_available
)
387 #ifdef STAT_MOUNTPOINTS
388 init_mounted_dev_list(0);
393 starting_desc
= open (".", O_RDONLY
);
394 if (0 <= starting_desc
&& fchdir (starting_desc
) != 0)
396 close (starting_desc
);
399 if (starting_desc
< 0)
401 starting_dir
= xgetcwd ();
403 error (1, errno
, _("cannot get current directory"));
405 if ((*options
.xstat
) (".", &starting_stat_buf
) != 0)
406 error (1, errno
, _("cannot get current directory"));
408 /* If no paths are given, default to ".". */
409 for (i
= end_of_leading_options
; i
< argc
&& strchr ("-!(),", argv
[i
][0]) == NULL
; i
++)
411 process_top_path (argv
[i
], 0);
414 /* If there were no path arguments, default to ".". */
415 if (i
== end_of_leading_options
)
418 * We use a temporary variable here because some actions modify
419 * the path temporarily. Hence if we use a string constant,
420 * we get a coredump. The best example of this is if we say
421 * "find -printf %H" (note, not "find . -printf %H").
423 char defaultpath
[2] = ".";
424 process_top_path (defaultpath
, 0);
427 /* If "-exec ... {} +" has been used, there may be some
428 * partially-full command lines which have been built,
429 * but which are not yet complete. Execute those now.
432 return state
.exit_status
;
435 boolean
is_fts_enabled()
437 /* this version of find (i.e. this main()) does not use fts. */
443 specific_dirname(const char *dir
)
447 if (0 == strcmp(".", dir
))
449 /* OK, what's '.'? */
450 if (NULL
!= getcwd(dirbuf
, sizeof(dirbuf
)))
452 return strdup(dirbuf
);
461 char *result
= canonicalize_filename_mode(dir
, CAN_EXISTING
);
471 /* Return non-zero if FS is the name of a filesystem that is likely to
475 fs_likely_to_be_automounted(const char *fs
)
477 return ( (0==strcmp(fs
, "nfs")) || (0==strcmp(fs
, "autofs")) || (0==strcmp(fs
, "subfs")));
482 #ifdef STAT_MOUNTPOINTS
483 static dev_t
*mounted_devices
= NULL
;
484 static size_t num_mounted_devices
= 0u;
488 init_mounted_dev_list(int mandatory
)
490 assert(NULL
== mounted_devices
);
491 assert(0 == num_mounted_devices
);
492 mounted_devices
= get_mounted_devices(&num_mounted_devices
);
493 if (mandatory
&& (NULL
== mounted_devices
))
495 error(1, 0, "Cannot read list of mounted devices.");
500 refresh_mounted_dev_list(void)
504 free(mounted_devices
);
507 num_mounted_devices
= 0u;
508 init_mounted_dev_list(1);
512 /* Search for device DEV in the array LIST, which is of size N. */
514 dev_present(dev_t dev
, const dev_t
*list
, size_t n
)
520 if ( (*list
++) == dev
)
527 enum MountPointStateChange
529 MountPointRecentlyMounted
,
530 MountPointRecentlyUnmounted
,
531 MountPointStateUnchanged
536 static enum MountPointStateChange
537 get_mount_state(dev_t newdev
)
539 int new_is_present
, new_was_present
;
541 new_was_present
= dev_present(newdev
, mounted_devices
, num_mounted_devices
);
542 refresh_mounted_dev_list();
543 new_is_present
= dev_present(newdev
, mounted_devices
, num_mounted_devices
);
545 if (new_was_present
== new_is_present
)
546 return MountPointStateUnchanged
;
547 else if (new_is_present
)
548 return MountPointRecentlyMounted
;
550 return MountPointRecentlyUnmounted
;
555 /* We stat()ed a directory, chdir()ed into it (we know this
556 * since direction is TraversingDown), stat()ed it again,
557 * and noticed that the device numbers are different. Check
558 * if the filesystem was recently mounted.
560 * If it was, it looks like chdir()ing into the directory
561 * caused a filesystem to be mounted. Maybe automount is
562 * running. Anyway, that's probably OK - but it happens
563 * only when we are moving downward.
565 * We also allow for the possibility that a similar thing
566 * has happened with the unmounting of a filesystem. This
567 * is much rarer, as it relies on an automounter timeout
568 * occurring at exactly the wrong moment.
570 static enum WdSanityCheckFatality
571 dirchange_is_fatal(const char *specific_what
,
572 enum WdSanityCheckFatality isfatal
,
574 struct stat
*newinfo
)
576 enum MountPointStateChange transition
= get_mount_state(newinfo
->st_dev
);
579 case MountPointRecentlyUnmounted
:
580 isfatal
= NON_FATAL_IF_SANITY_CHECK_FAILS
;
584 _("Warning: filesystem %s has recently been unmounted."),
589 case MountPointRecentlyMounted
:
590 isfatal
= NON_FATAL_IF_SANITY_CHECK_FAILS
;
594 _("Warning: filesystem %s has recently been mounted."),
599 case MountPointStateUnchanged
:
600 /* leave isfatal as it is */
612 /* Examine the results of the stat() of a directory from before we
613 * entered or left it, with the results of stat()ing it afterward. If
614 * these are different, the filesystem tree has been modified while we
615 * were traversing it. That might be an attempt to use a race
616 * condition to persuade find to do something it didn't intend
617 * (e.g. an attempt by an ordinary user to exploit the fact that root
618 * sometimes runs find on the whole filesystem). However, this can
619 * also happen if automount is running (certainly on Solaris). With
620 * automount, moving into a directory can cause a filesystem to be
623 * To cope sensibly with this, we will raise an error if we see the
624 * device number change unless we are chdir()ing into a subdirectory,
625 * and the directory we moved into has been mounted or unmounted "recently".
626 * Here "recently" means since we started "find" or we last re-read
627 * the /etc/mnttab file.
629 * If the device number does not change but the inode does, that is a
632 * If the device number and inode are both the same, we are happy.
634 * If a filesystem is (un)mounted as we chdir() into the directory, that
635 * may mean that we're now examining a section of the filesystem that might
636 * have been excluded from consideration (via -prune or -quit for example).
637 * Hence we print a warning message to indicate that the output of find
638 * might be inconsistent due to the change in the filesystem.
641 wd_sanity_check(const char *thing_to_stat
,
642 const char *progname
,
646 struct stat
*newinfo
,
649 enum TraversalDirection direction
,
650 enum WdSanityCheckFatality isfatal
,
651 boolean
*changed
) /* output parameter */
654 char *specific_what
= NULL
;
656 const char *current_dir
= ".";
660 if ((*options
.xstat
) (current_dir
, newinfo
) != 0)
661 error (1, errno
, "%s", thing_to_stat
);
663 if (old_dev
!= newinfo
->st_dev
)
666 specific_what
= specific_dirname(what
);
667 fstype
= filesystem_type(newinfo
, current_dir
);
668 silent
= fs_likely_to_be_automounted(fstype
);
670 /* This condition is rare, so once we are here it is
671 * reasonable to perform an expensive computation to
672 * determine if we should continue or fail.
674 if (TraversingDown
== direction
)
676 #ifdef STAT_MOUNTPOINTS
677 isfatal
= dirchange_is_fatal(specific_what
,isfatal
,silent
,newinfo
);
679 isfatal
= RETRY_IF_SANITY_CHECK_FAILS
;
685 case FATAL_IF_SANITY_CHECK_FAILS
:
687 fstype
= filesystem_type(newinfo
, current_dir
);
689 _("%s%s changed during execution of %s (old device number %ld, new device number %ld, filesystem type is %s) [ref %ld]"),
694 (long) newinfo
->st_dev
,
701 case NON_FATAL_IF_SANITY_CHECK_FAILS
:
703 /* Since the device has changed under us, the inode number
704 * will almost certainly also be different. However, we have
705 * already decided that this is not a problem. Hence we return
706 * without checking the inode number.
712 case RETRY_IF_SANITY_CHECK_FAILS
:
717 /* Device number was the same, check if the inode has changed. */
718 if (old_ino
!= newinfo
->st_ino
)
721 specific_what
= specific_dirname(what
);
722 fstype
= filesystem_type(newinfo
, current_dir
);
724 error ((isfatal
== FATAL_IF_SANITY_CHECK_FAILS
) ? 1 : 0,
725 0, /* no relevant errno value */
726 _("%s%s changed during execution of %s (old inode number %ld, new inode number %ld, filesystem type is %s) [ref %ld]"),
731 (long) newinfo
->st_ino
,
744 SafeChdirFailSymlink
,
747 SafeChdirFailWouldBeUnableToReturn
,
748 SafeChdirFailChdirFailed
,
749 SafeChdirFailNonexistent
752 /* Safely perform a change in directory. We do this by calling
753 * lstat() on the subdirectory, using chdir() to move into it, and
754 * then lstat()ing ".". We compare the results of the two stat calls
755 * to see if they are consistent. If not, we sound the alarm.
757 * If following_links() is true, we do follow symbolic links.
759 static enum SafeChdirStatus
760 safely_chdir_lstat(const char *dest
,
761 enum TraversalDirection direction
,
762 struct stat
*statbuf_dest
,
763 enum ChdirSymlinkHandling symlink_follow_option
,
766 struct stat statbuf_arrived
;
768 int saved_errno
; /* specific_dirname() changes errno. */
769 boolean rv_set
= false;
770 boolean statflag
= false;
772 enum WdSanityCheckFatality isfatal
= RETRY_IF_SANITY_CHECK_FAILS
;
774 saved_errno
= errno
= 0;
776 dotfd
= open(".", O_RDONLY
);
778 /* We jump back to here if wd_sanity_check()
779 * recoverably triggers an alert.
786 /* Stat the directory we're going to. */
787 if (0 == options
.xstat(dest
, statbuf_dest
))
792 /* symlink_follow_option might be set to SymlinkFollowOk, which
793 * would allow us to chdir() into a symbolic link. This is
794 * only useful for the case where the directory we're
795 * chdir()ing into is the basename of a command line
796 * argument, for example where "foo/bar/baz" is specified on
797 * the command line. When -P is in effect (the default),
798 * baz will not be followed if it is a symlink, but if bar
799 * is a symlink, it _should_ be followed. Hence we need the
800 * ability to override the policy set by following_links().
802 if (!following_links() && S_ISLNK(statbuf_dest
->st_mode
))
804 /* We're not supposed to be following links, but this is
805 * a link. Check symlink_follow_option to see if we should
806 * make a special exception.
808 if (symlink_follow_option
== SymlinkFollowOk
)
810 /* We need to re-stat() the file so that the
811 * sanity check can pass.
813 if (0 != stat(dest
, statbuf_dest
))
815 rv
= SafeChdirFailNonexistent
;
824 /* Not following symlinks, so the attempt to
825 * chdir() into a symlink should be prevented.
827 rv
= SafeChdirFailSymlink
;
829 saved_errno
= 0; /* silence the error message */
835 /* Although the immediately following chdir() would detect
836 * the fact that this is not a directory for us, this would
837 * result in an extra system call that fails. Anybody
838 * examining the system-call trace should ideally not be
839 * concerned that something is actually failing.
841 if (!S_ISDIR(statbuf_dest
->st_mode
))
843 rv
= SafeChdirFailNotDir
;
845 saved_errno
= 0; /* silence the error message */
850 fprintf(stderr
, "safely_chdir(): chdir(\"%s\")\n", dest
);
852 if (0 == chdir(dest
))
854 /* check we ended up where we wanted to go */
855 boolean changed
= false;
856 if (!wd_sanity_check(".", program_name
, ".",
857 statbuf_dest
->st_dev
,
858 statbuf_dest
->st_ino
,
860 0, __LINE__
, direction
,
864 /* Only allow one failure. */
865 if (RETRY_IF_SANITY_CHECK_FAILS
== isfatal
)
867 if (0 == fchdir(dotfd
))
869 isfatal
= FATAL_IF_SANITY_CHECK_FAILS
;
874 /* Failed to return to original directory,
875 * but we know that the current working
876 * directory is not the one that we intend
877 * to be in. Since fchdir() failed, we
878 * can't recover from this and so this error
882 "failed to return to parent directory");
887 /* XXX: not sure what to use as an excuse here. */
888 rv
= SafeChdirFailNonexistent
;
901 if (ENOENT
== saved_errno
)
903 rv
= SafeChdirFailNonexistent
;
905 if (options
.ignore_readdir_race
)
906 errno
= 0; /* don't issue err msg */
908 else if (ENOTDIR
== saved_errno
)
910 /* This can happen if the we stat a directory,
911 * and then filesystem activity changes it into
914 saved_errno
= 0; /* don't issue err msg */
915 rv
= SafeChdirFailNotDir
;
920 rv
= SafeChdirFailChdirFailed
;
929 rv
= SafeChdirFailStat
;
932 if ( (ENOENT
== saved_errno
) || (0 == state
.curdepth
))
933 saved_errno
= 0; /* don't issue err msg */
939 /* We do not have read permissions on "." */
940 rv
= SafeChdirFailWouldBeUnableToReturn
;
945 /* This is the success path, so we clear errno. The caller probably
946 * won't be calling error() anyway.
950 /* We use the same exit path for success or failure.
951 * which has occurred is recorded in RV.
954 /* We do not call error() as this would result in a duplicate error
955 * message when the caller does the same thing.
966 *did_stat
= statflag
;
971 #if defined(O_NOFOLLOW)
972 /* Safely change working directory to the specified subdirectory. If
973 * we are not allowed to follow symbolic links, we use open() with
974 * O_NOFOLLOW, followed by fchdir(). This ensures that we don't
975 * follow symbolic links (of course, we do follow them if the -L
976 * option is in effect).
978 static enum SafeChdirStatus
979 safely_chdir_nofollow(const char *dest
,
980 enum TraversalDirection direction
,
981 struct stat
*statbuf_dest
,
982 enum ChdirSymlinkHandling symlink_follow_option
,
990 switch (symlink_follow_option
)
992 case SymlinkFollowOk
:
996 case SymlinkHandleDefault
:
997 if (following_links())
1000 extraflags
= O_NOFOLLOW
;
1005 fd
= open(dest
, O_RDONLY
|extraflags
);
1011 return SafeChdirFailSymlink
; /* This is why we use O_NOFOLLOW */
1013 return SafeChdirFailNonexistent
;
1015 return SafeChdirFailChdirFailed
;
1020 if (0 == fchdir(fd
))
1027 int saved_errno
= errno
;
1029 errno
= saved_errno
;
1034 return SafeChdirFailNotDir
;
1037 case EBADF
: /* Shouldn't happen */
1041 return SafeChdirFailChdirFailed
;
1047 static enum SafeChdirStatus
1048 safely_chdir(const char *dest
,
1049 enum TraversalDirection direction
,
1050 struct stat
*statbuf_dest
,
1051 enum ChdirSymlinkHandling symlink_follow_option
,
1054 /* We're about to leave a directory. If there are any -execdir
1055 * argument lists which have been built but have not yet been
1056 * processed, do them now because they must be done in the same
1059 complete_pending_execdirs(eval_tree
);
1061 #if defined(O_NOFOLLOW)
1062 if (options
.open_nofollow_available
)
1063 return safely_chdir_nofollow(dest
, direction
, statbuf_dest
, symlink_follow_option
, did_stat
);
1065 return safely_chdir_lstat(dest
, direction
, statbuf_dest
, symlink_follow_option
, did_stat
);
1070 /* Safely go back to the starting directory. */
1074 struct stat stat_buf
;
1077 if (starting_desc
< 0)
1080 fprintf(stderr
, "chdir_back(): chdir(\"%s\")\n", starting_dir
);
1083 #ifdef STAT_MOUNTPOINTS
1084 /* We will need the mounted device list. Get it now if we don't
1087 if (NULL
== mounted_devices
)
1088 init_mounted_dev_list(1);
1091 if (chdir (starting_dir
) != 0)
1092 error (1, errno
, "%s", starting_dir
);
1094 wd_sanity_check(starting_dir
,
1097 starting_stat_buf
.st_dev
,
1098 starting_stat_buf
.st_ino
,
1099 &stat_buf
, 0, __LINE__
,
1101 FATAL_IF_SANITY_CHECK_FAILS
,
1107 fprintf(stderr
, "chdir_back(): chdir(<starting-point>)\n");
1109 if (fchdir (starting_desc
) != 0)
1110 error (1, errno
, "%s", starting_dir
);
1114 /* Move to the parent of a given directory and then call a function,
1115 * restoring the cwd. Don't bother changing directory if the
1116 * specified directory is a child of "." or is the root directory.
1119 at_top (char *pathname
,
1122 void (*action
)(char *pathname
,
1125 struct stat
*pstat
))
1128 char *parent_dir
= dir_name(pathname
);
1129 char *base
= base_name(pathname
);
1132 state
.starting_path_length
= strlen (pathname
);
1134 if (0 == strcmp(pathname
, parent_dir
)
1135 || 0 == strcmp(parent_dir
, "."))
1142 enum TraversalDirection direction
;
1143 enum SafeChdirStatus chdir_status
;
1145 boolean did_stat
= false;
1148 if (0 == strcmp(base
, ".."))
1149 direction
= TraversingUp
;
1151 direction
= TraversingDown
;
1153 /* We pass SymlinkFollowOk to safely_chdir(), which allows it to
1154 * chdir() into a symbolic link. This is only useful for the
1155 * case where the directory we're chdir()ing into is the
1156 * basename of a command line argument, for example where
1157 * "foo/bar/baz" is specified on the command line. When -P is
1158 * in effect (the default), baz will not be followed if it is a
1159 * symlink, but if bar is a symlink, it _should_ be followed.
1160 * Hence we need the ability to override the policy set by
1161 * following_links().
1163 chdir_status
= safely_chdir(parent_dir
, direction
, &st
, SymlinkFollowOk
, &did_stat
);
1164 if (SafeChdirOK
!= chdir_status
)
1166 const char *what
= (SafeChdirFailWouldBeUnableToReturn
== chdir_status
) ? "." : parent_dir
;
1168 error (0, errno
, "%s", what
);
1170 error (0, 0, "Failed to safely change directory into `%s'",
1173 /* We can't process this command-line argument. */
1174 state
.exit_status
= 1;
1182 action(pathname
, base
, mode
, pstat
);
1191 static void do_process_top_dir(char *pathname
,
1196 process_path (pathname
, base
, false, ".", mode
);
1197 complete_pending_execdirs(eval_tree
);
1200 static void do_process_predicate(char *pathname
,
1205 state
.rel_pathname
= base
;
1206 apply_predicate (pathname
, pstat
, eval_tree
);
1212 /* Descend PATHNAME, which is a command-line argument.
1214 Actions like -execdir assume that we are in the
1215 parent directory of the file we're examining,
1216 and on entry to this function our working directory
1217 is whatever it was when find was invoked. Therefore
1218 If PATHNAME is "." we just leave things as they are.
1219 Otherwise, we figure out what the parent directory is,
1223 process_top_path (char *pathname
, mode_t mode
)
1225 at_top(pathname
, mode
, NULL
, do_process_top_dir
);
1229 /* Info on each directory in the current tree branch, to avoid
1230 getting stuck in symbolic link loops. */
1231 static struct dir_id
*dir_ids
= NULL
;
1232 /* Entries allocated in `dir_ids'. */
1233 static int dir_alloc
= 0;
1234 /* Index in `dir_ids' of directory currently being searched.
1235 This is always the last valid entry. */
1236 static int dir_curr
= -1;
1237 /* (Arbitrary) number of entries to grow `dir_ids' by. */
1238 #define DIR_ALLOC_STEP 32
1242 /* We've detected a filesystem loop. This is caused by one of
1245 * 1. Option -L is in effect and we've hit a symbolic link that
1246 * points to an ancestor. This is harmless. We won't traverse the
1249 * 2. We have hit a real cycle in the directory hierarchy. In this
1250 * case, we issue a diagnostic message (POSIX requires this) and we
1251 * skip that directory entry.
1254 issue_loop_warning(const char *name
, const char *pathname
, int level
)
1256 struct stat stbuf_link
;
1257 if (lstat(name
, &stbuf_link
) != 0)
1258 stbuf_link
.st_mode
= S_IFREG
;
1260 if (S_ISLNK(stbuf_link
.st_mode
))
1263 _("Symbolic link `%s' is part of a loop in the directory hierarchy; we have already visited the directory to which it points."),
1268 int distance
= 1 + (dir_curr
-level
);
1269 /* We have found an infinite loop. POSIX requires us to
1270 * issue a diagnostic. Usually we won't get to here
1271 * because when the leaf optimisation is on, it will cause
1272 * the subdirectory to be skipped. If /a/b/c/d is a hard
1273 * link to /a/b, then the link count of /a/b/c is 2,
1274 * because the ".." entry of /b/b/c/d points to /a, not
1278 _("Filesystem loop detected; `%s' has the same device number and inode as a directory which is %d %s."),
1282 _("level higher in the filesystem hierarchy") :
1283 _("levels higher in the filesystem hierarchy")));
1289 /* Recursively descend path PATHNAME, applying the predicates.
1290 LEAF is true if PATHNAME is known to be in a directory that has no
1291 more unexamined subdirectories, and therefore it is not a directory.
1292 Knowing this allows us to avoid calling stat as long as possible for
1295 NAME is PATHNAME relative to the current directory. We access NAME
1298 PARENT is the path of the parent of NAME, relative to find's
1301 Return nonzero iff PATHNAME is a directory. */
1304 process_path (char *pathname
, char *name
, boolean leaf
, char *parent
,
1307 struct stat stat_buf
;
1308 static dev_t root_dev
; /* Device ID of current argument pathname. */
1311 /* Assume it is a non-directory initially. */
1312 stat_buf
.st_mode
= 0;
1313 state
.rel_pathname
= name
;
1315 state
.have_stat
= false;
1316 state
.have_type
= false;
1318 if (!digest_mode(mode
, pathname
, name
, &stat_buf
, leaf
))
1321 if (!S_ISDIR (state
.type
))
1323 if (state
.curdepth
>= options
.mindepth
)
1324 apply_predicate (pathname
, &stat_buf
, eval_tree
);
1328 /* From here on, we're working on a directory. */
1331 /* Now we really need to stat the directory, even if we know the
1332 * type, because we need information like struct stat.st_rdev.
1334 if (get_statinfo(pathname
, name
, &stat_buf
) != 0)
1337 state
.have_stat
= true;
1338 mode
= state
.type
= stat_buf
.st_mode
; /* use full info now that we have it. */
1339 state
.stop_at_current_level
=
1340 options
.maxdepth
>= 0
1341 && state
.curdepth
>= options
.maxdepth
;
1343 /* If we've already seen this directory on this branch,
1344 don't descend it again. */
1345 for (i
= 0; i
<= dir_curr
; i
++)
1346 if (stat_buf
.st_ino
== dir_ids
[i
].ino
&&
1347 stat_buf
.st_dev
== dir_ids
[i
].dev
)
1349 state
.stop_at_current_level
= true;
1350 issue_loop_warning(name
, pathname
, i
);
1353 if (dir_alloc
<= ++dir_curr
)
1355 dir_alloc
+= DIR_ALLOC_STEP
;
1356 dir_ids
= (struct dir_id
*)
1357 xrealloc ((char *) dir_ids
, dir_alloc
* sizeof (struct dir_id
));
1359 dir_ids
[dir_curr
].ino
= stat_buf
.st_ino
;
1360 dir_ids
[dir_curr
].dev
= stat_buf
.st_dev
;
1362 if (options
.stay_on_filesystem
)
1364 if (state
.curdepth
== 0)
1365 root_dev
= stat_buf
.st_dev
;
1366 else if (stat_buf
.st_dev
!= root_dev
)
1367 state
.stop_at_current_level
= true;
1370 if (options
.do_dir_first
&& state
.curdepth
>= options
.mindepth
)
1371 apply_predicate (pathname
, &stat_buf
, eval_tree
);
1374 fprintf(stderr
, "pathname = %s, stop_at_current_level = %d\n",
1375 pathname
, state
.stop_at_current_level
);
1378 if (state
.stop_at_current_level
== false)
1379 /* Scan directory on disk. */
1380 process_dir (pathname
, name
, strlen (pathname
), &stat_buf
, parent
);
1382 if (options
.do_dir_first
== false && state
.curdepth
>= options
.mindepth
)
1384 /* The fields in 'state' are now out of date. Correct them.
1386 if (!digest_mode(mode
, pathname
, name
, &stat_buf
, leaf
))
1391 at_top(pathname
, mode
, &stat_buf
, do_process_predicate
);
1395 do_process_predicate(pathname
, name
, mode
, &stat_buf
);
1405 /* Scan directory PATHNAME and recurse through process_path for each entry.
1407 PATHLEN is the length of PATHNAME.
1409 NAME is PATHNAME relative to the current directory.
1411 STATP is the results of *options.xstat on it.
1413 PARENT is the path of the parent of NAME, relative to find's
1414 starting directory. */
1417 process_dir (char *pathname
, char *name
, int pathlen
, struct stat
*statp
, char *parent
)
1419 int subdirs_left
; /* Number of unexamined subdirs in PATHNAME. */
1420 boolean subdirs_unreliable
; /* if true, cannot use dir link count as subdir limif (if false, it may STILL be unreliable) */
1421 int idx
; /* Which entry are we on? */
1422 struct stat stat_buf
;
1424 struct savedir_dirinfo
*dirinfo
;
1426 if (statp
->st_nlink
< 2)
1428 subdirs_unreliable
= true;
1432 subdirs_unreliable
= false; /* not necessarily right */
1433 subdirs_left
= statp
->st_nlink
- 2; /* Account for name and ".". */
1437 dirinfo
= xsavedir(name
, 0);
1440 if (dirinfo
== NULL
)
1443 error (0, errno
, "%s", pathname
);
1444 state
.exit_status
= 1;
1448 register char *namep
; /* Current point in `name_space'. */
1449 char *cur_path
; /* Full path of each file to process. */
1450 char *cur_name
; /* Base name of each file to process. */
1451 unsigned cur_path_size
; /* Bytes allocated for `cur_path'. */
1452 register unsigned file_len
; /* Length of each path to process. */
1453 register unsigned pathname_len
; /* PATHLEN plus trailing '/'. */
1454 boolean did_stat
= false;
1456 if (pathname
[pathlen
- 1] == '/')
1457 pathname_len
= pathlen
+ 1; /* For '\0'; already have '/'. */
1459 pathname_len
= pathlen
+ 2; /* For '/' and '\0'. */
1463 /* We're about to leave the directory. If there are any
1464 * -execdir argument lists which have been built but have not
1465 * yet been processed, do them now because they must be done in
1466 * the same directory.
1468 complete_pending_execdirs(eval_tree
);
1470 if (strcmp (name
, "."))
1472 enum SafeChdirStatus status
= safely_chdir (name
, TraversingDown
, &stat_buf
, SymlinkHandleDefault
, &did_stat
);
1476 /* If there had been a change but wd_sanity_check()
1477 * accepted it, we need to accept that on the
1478 * way back up as well, so modify our record
1479 * of what we think we should see later.
1480 * If there was no change, the assignments are a no-op.
1482 * However, before performing the assignment, we need to
1483 * check that we have the stat information. If O_NOFOLLOW
1484 * is available, safely_chdir() will not have needed to use
1485 * stat(), and so stat_buf will just contain random data.
1489 /* If there is a link we need to follow it. Hence
1490 * the direct call to stat() not through (options.xstat)
1492 if (0 != stat(".", &stat_buf
))
1493 break; /* skip the assignment. */
1495 dir_ids
[dir_curr
].dev
= stat_buf
.st_dev
;
1496 dir_ids
[dir_curr
].ino
= stat_buf
.st_ino
;
1500 case SafeChdirFailWouldBeUnableToReturn
:
1501 error (0, errno
, ".");
1502 state
.exit_status
= 1;
1505 case SafeChdirFailNonexistent
:
1506 case SafeChdirFailStat
:
1507 case SafeChdirFailNotDir
:
1508 case SafeChdirFailChdirFailed
:
1509 error (0, errno
, "%s", pathname
);
1510 state
.exit_status
= 1;
1513 case SafeChdirFailSymlink
:
1515 _("warning: not following the symbolic link %s"),
1517 state
.exit_status
= 1;
1522 for (idx
=0; idx
< dirinfo
->size
; ++idx
)
1524 /* savedirinfo() may return dirinfo=NULL if extended information
1527 mode_t mode
= (dirinfo
->entries
[idx
].flags
& SavedirHaveFileType
) ?
1528 dirinfo
->entries
[idx
].type_info
: 0;
1529 namep
= dirinfo
->entries
[idx
].name
;
1531 /* Append this directory entry's name to the path being searched. */
1532 file_len
= pathname_len
+ strlen (namep
);
1533 if (file_len
> cur_path_size
)
1535 while (file_len
> cur_path_size
)
1536 cur_path_size
+= 1024;
1539 cur_path
= xmalloc (cur_path_size
);
1540 strcpy (cur_path
, pathname
);
1541 cur_path
[pathname_len
- 2] = '/';
1543 cur_name
= cur_path
+ pathname_len
- 1;
1544 strcpy (cur_name
, namep
);
1547 if (!options
.no_leaf_check
&& !subdirs_unreliable
)
1549 if (mode
&& S_ISDIR(mode
) && (subdirs_left
== 0))
1551 /* This is a subdirectory, but the number of directories we
1552 * have found now exceeds the number we would expect given
1553 * the hard link count on the parent. This is likely to be
1554 * a bug in the filesystem driver (e.g. Linux's
1555 * /proc filesystem) or may just be a fact that the OS
1556 * doesn't really handle hard links with Unix semantics.
1557 * In the latter case, -noleaf should be used routinely.
1559 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."),
1561 state
.exit_status
= 1; /* We know the result is wrong, now */
1562 options
.no_leaf_check
= true; /* Don't make same
1564 subdirs_left
= 1; /* band-aid for this iteration. */
1567 /* Normal case optimization. On normal Unix
1568 filesystems, a directory that has no subdirectories
1569 has two links: its name, and ".". Any additional
1570 links are to the ".." entries of its subdirectories.
1571 Once we have processed as many subdirectories as
1572 there are additional links, we know that the rest of
1573 the entries are non-directories -- in other words,
1575 subdirs_left
-= process_path (cur_path
, cur_name
,
1576 subdirs_left
== 0, pathname
,
1581 /* There might be weird (e.g., CD-ROM or MS-DOS) filesystems
1582 mounted, which don't have Unix-like directory link counts. */
1583 process_path (cur_path
, cur_name
, false, pathname
, mode
);
1590 /* We're about to leave the directory. If there are any
1591 * -execdir argument lists which have been built but have not
1592 * yet been processed, do them now because they must be done in
1593 * the same directory.
1595 complete_pending_execdirs(eval_tree
);
1598 if (strcmp (name
, "."))
1600 enum SafeChdirStatus status
;
1602 boolean did_stat
= false;
1604 /* We could go back and do the next command-line arg
1605 instead, maybe using longjmp. */
1607 boolean deref
= following_links() ? true : false;
1609 if ( (state
.curdepth
>0) && !deref
)
1617 status
= safely_chdir (dir
, TraversingUp
, &stat_buf
, SymlinkHandleDefault
, &did_stat
);
1623 case SafeChdirFailWouldBeUnableToReturn
:
1624 error (1, errno
, ".");
1627 case SafeChdirFailNonexistent
:
1628 case SafeChdirFailStat
:
1629 case SafeChdirFailSymlink
:
1630 case SafeChdirFailNotDir
:
1631 case SafeChdirFailChdirFailed
:
1632 error (1, errno
, "%s", pathname
);
1638 did
.dev
= dir_ids
[dir_curr
-1].dev
;
1639 did
.ino
= dir_ids
[dir_curr
-1].ino
;
1643 did
.dev
= starting_stat_buf
.st_dev
;
1644 did
.ino
= starting_stat_buf
.st_ino
;
1650 free_dirinfo(dirinfo
);