1 /* find -- search for files in a directory hierarchy
2 Copyright (C) 1990, 91, 92, 93, 94, 2000, 2003, 2004 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 /* GNU find was written by Eric Decker <cire@cisco.com>,
20 with enhancements by David MacKenzie <djm@gnu.org>,
21 Jay Plett <jay@silence.princeton.nj.us>,
22 and Tim Wood <axolotl!tim@toad.com>.
23 The idea for -print0 and xargs -0 came from
24 Dan Bernstein <brnstnd@kramden.acf.nyu.edu>. */
29 #define USE_SAFE_CHDIR 1
41 #ifdef HAVE_SYS_UTSNAME_H
42 #include <sys/utsname.h>
45 #include "../gnulib/lib/xalloc.h"
46 #include "../gnulib/lib/human.h"
47 #include "../gnulib/lib/canonicalize.h"
50 #include "savedirinfo.h"
60 # define _(Text) gettext (Text)
63 #define textdomain(Domain)
64 #define bindtextdomain(Package, Directory)
67 # define N_(String) gettext_noop (String)
69 /* See locate.c for explanation as to why not use (String) */
70 # define N_(String) String
73 #define apply_predicate(pathname, stat_buf_ptr, node) \
74 (*(node)->pred_func)((pathname), (stat_buf_ptr), (node))
77 static void init_mounted_dev_list(void);
78 static void process_top_path
PARAMS((char *pathname
, mode_t mode
));
79 static int process_path
PARAMS((char *pathname
, char *name
, boolean leaf
, char *parent
, mode_t type
));
80 static void process_dir
PARAMS((char *pathname
, char *name
, int pathlen
, struct stat
*statp
, char *parent
));
82 static void complete_pending_execdirs(struct predicate
*p
);
83 static void complete_pending_execs (struct predicate
*p
);
87 static boolean default_prints
PARAMS((struct predicate
*pred
));
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 /* The root of the evaluation tree. */
99 static struct predicate
*eval_tree
= NULL
;
102 struct options options
;
105 /* The full path of the initial working directory, or "." if
106 STARTING_DESC is nonnegative. */
107 char const *starting_dir
= ".";
109 /* A file descriptor open to the initial working directory.
110 Doing it this way allows us to work when the i.w.d. has
111 unreadable parents. */
114 /* The stat buffer of the initial working directory. */
115 struct stat starting_stat_buf
;
117 enum ChdirSymlinkHandling
119 SymlinkHandleDefault
, /* Normally the right choice */
120 SymlinkFollowOk
/* see comment in process_top_path() */
124 enum TraversalDirection
132 following_links(void)
134 switch (options
.symlink_handling
)
136 case SYMLINK_ALWAYS_DEREF
:
138 case SYMLINK_DEREF_ARGSONLY
:
139 return (state
.curdepth
== 0);
140 case SYMLINK_NEVER_DEREF
:
148 fallback_stat(const char *name
, struct stat
*p
, int prev_rv
)
150 /* Our original stat() call failed. Perhaps we can't follow a
151 * symbolic link. If that might be the problem, lstat() the link.
152 * Otherwise, admit defeat.
159 fprintf(stderr
, "fallback_stat(): stat(%s) failed; falling back on lstat()\n", name
);
161 return lstat(name
, p
);
168 case EOVERFLOW
: /* EOVERFLOW is not #defined on UNICOS. */
176 /* optionh_stat() implements the stat operation when the -H option is
179 * If the item to be examined is a command-line argument, we follow
180 * symbolic links. If the stat() call fails on the command-line item,
181 * we fall back on the properties of the symbolic link.
183 * If the item to be examined is not a command-line argument, we
184 * examine the link itself.
187 optionh_stat(const char *name
, struct stat
*p
)
189 if (0 == state
.curdepth
)
191 /* This file is from the command line; deference the link (if it
194 int rv
= stat(name
, p
);
196 return 0; /* success */
198 return fallback_stat(name
, p
, rv
);
202 /* Not a file on the command line; do not derefernce the link.
204 return lstat(name
, p
);
208 /* optionl_stat() implements the stat operation when the -L option is
209 * in effect. That option makes us examine the thing the symbolic
210 * link points to, not the symbolic link itself.
213 optionl_stat(const char *name
, struct stat
*p
)
215 int rv
= stat(name
, p
);
217 return 0; /* normal case. */
219 return fallback_stat(name
, p
, rv
);
222 /* optionp_stat() implements the stat operation when the -P option is
223 * in effect (this is also the default). That option makes us examine
224 * the symbolic link itself, not the thing it points to.
227 optionp_stat(const char *name
, struct stat
*p
)
229 return lstat(name
, p
);
233 static uintmax_t stat_count
= 0u;
236 debug_stat (const char *file
, struct stat
*bufp
)
239 fprintf (stderr
, "debug_stat (%s)\n", file
);
240 switch (options
.symlink_handling
)
242 case SYMLINK_ALWAYS_DEREF
:
243 return optionl_stat(file
, bufp
);
244 case SYMLINK_DEREF_ARGSONLY
:
245 return optionh_stat(file
, bufp
);
246 case SYMLINK_NEVER_DEREF
:
247 return optionp_stat(file
, bufp
);
250 #endif /* DEBUG_STAT */
253 set_follow_state(enum SymlinkOption opt
)
257 case SYMLINK_ALWAYS_DEREF
: /* -L */
258 options
.xstat
= optionl_stat
;
259 options
.no_leaf_check
= true;
262 case SYMLINK_NEVER_DEREF
: /* -P (default) */
263 options
.xstat
= optionp_stat
;
264 /* Can't turn no_leaf_check off because the user might have specified
269 case SYMLINK_DEREF_ARGSONLY
: /* -H */
270 options
.xstat
= optionh_stat
;
271 options
.no_leaf_check
= true;
274 options
.symlink_handling
= opt
;
276 /* For DBEUG_STAT, the choice is made at runtime within debug_stat()
277 * by checking the contents of the symlink_handling variable.
279 #if defined(DEBUG_STAT)
280 options
.xstat
= debug_stat
;
281 #endif /* !DEBUG_STAT */
285 /* Complete any outstanding commands.
292 complete_pending_execs(eval_tree
);
293 complete_pending_execdirs(eval_tree
);
297 /* Get the stat information for a file, if it is
301 get_statinfo (const char *pathname
, const char *name
, struct stat
*p
)
303 if (!state
.have_stat
&& (*options
.xstat
) (name
, p
) != 0)
305 if (!options
.ignore_readdir_race
|| (errno
!= ENOENT
) )
307 error (0, errno
, "%s", pathname
);
308 state
.exit_status
= 1;
312 state
.have_stat
= true;
313 state
.have_type
= true;
314 state
.type
= p
->st_mode
;
318 /* Get the stat/type information for a file, if it is
322 get_info (const char *pathname
,
325 struct predicate
*pred_ptr
)
327 /* If we need the full stat info, or we need the type info but don't
328 * already have it, stat the file now.
331 if (pred_ptr
->need_stat
)
333 return get_statinfo(pathname
, state
.rel_pathname
, p
);
335 if ((pred_ptr
->need_type
&& (0 == state
.have_type
)))
337 return get_statinfo(pathname
, state
.rel_pathname
, p
);
342 /* Determine if we can use O_NOFOLLOW.
344 #if defined(O_NOFOLLOW)
351 if (0 == uname(&uts
))
353 /* POSIX requires that atof() ignore "unrecognised suffixes". */
354 release
= atof(uts
.release
);
356 if (0 == strcmp("Linux", uts
.sysname
))
358 /* Linux kernels 2.1.126 and earlier ignore the O_NOFOLLOW flag. */
359 return release
>= 2.2; /* close enough */
361 else if (0 == strcmp("FreeBSD", uts
.sysname
))
363 /* FreeBSD 3.0-CURRENT and later support it */
364 return release
>= 3.1;
368 /* Well, O_NOFOLLOW was defined, so we'll try to use it. */
374 main (int argc
, char **argv
)
377 PFB parse_function
; /* Pointer to the function which parses. */
378 struct predicate
*cur_pred
;
379 char *predicate_name
; /* Name of predicate being parsed. */
380 int end_of_leading_options
= 0; /* First arg after any -H/-L etc. */
381 program_name
= argv
[0];
383 #ifdef HAVE_SETLOCALE
384 setlocale (LC_ALL
, "");
386 bindtextdomain (PACKAGE
, LOCALEDIR
);
387 textdomain (PACKAGE
);
388 atexit (close_stdout
);
393 options
.warnings
= true;
397 options
.warnings
= false;
403 options
.do_dir_first
= true;
404 options
.maxdepth
= options
.mindepth
= -1;
405 options
.start_time
= time (NULL
);
406 options
.cur_day_start
= options
.start_time
- DAYSECS
;
407 options
.full_days
= false;
408 options
.stay_on_filesystem
= false;
409 options
.ignore_readdir_race
= false;
411 state
.exit_status
= 0;
413 #if defined(DEBUG_STAT)
414 options
.xstat
= debug_stat
;
415 #endif /* !DEBUG_STAT */
417 if (getenv("POSIXLY_CORRECT"))
418 options
.output_block_size
= 512;
420 options
.output_block_size
= 1024;
423 options
.open_nofollow_available
= check_nofollow();
425 options
.open_nofollow_available
= false;
428 if (getenv("FIND_BLOCK_SIZE"))
430 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"));
433 options
.no_leaf_check
= false;
434 set_follow_state(SYMLINK_NEVER_DEREF
); /* The default is equivalent to -P. */
436 /* safely_chdir() needs to check that it has ended up in the right place.
437 * To avoid bailing out when something gets automounted, it checks if
438 * the target directory appears to have had a directory mounted on it as
439 * we chdir()ed. The problem with this is that in order to notice that
440 * a filesystem was mounted, we would need to lstat() all the mount points.
441 * That strategy loses if our machine is a client of a dead NFS server.
443 * Hence if safely_chdir() and wd_sanity_check() can manage without needing
444 * to know the mounted device list, we do that.
446 if (!options
.open_nofollow_available
)
448 init_mounted_dev_list();
452 fprintf (stderr
, "cur_day_start = %s", ctime (&options
.cur_day_start
));
455 /* Check for -P, -H or -L options. */
456 for (i
=1; (end_of_leading_options
= i
) < argc
; ++i
)
458 if (0 == strcmp("-H", argv
[i
]))
460 /* Meaning: dereference symbolic links on command line, but nowhere else. */
461 set_follow_state(SYMLINK_DEREF_ARGSONLY
);
463 else if (0 == strcmp("-L", argv
[i
]))
465 /* Meaning: dereference all symbolic links. */
466 set_follow_state(SYMLINK_ALWAYS_DEREF
);
468 else if (0 == strcmp("-P", argv
[i
]))
470 /* Meaning: never dereference symbolic links (default). */
471 set_follow_state(SYMLINK_NEVER_DEREF
);
473 else if (0 == strcmp("--", argv
[i
]))
475 /* -- signifies the end of options. */
476 end_of_leading_options
= i
+1; /* Next time start with the next option */
481 /* Hmm, must be one of
485 end_of_leading_options
= i
; /* Next time start with this option */
490 /* We are now processing the part of the "find" command line
491 * after the -H/-L options (if any).
494 /* fprintf(stderr, "rest: optind=%ld\n", (long)optind); */
496 /* Find where in ARGV the predicates begin. */
497 for (i
= end_of_leading_options
; i
< argc
&& strchr ("-!(),", argv
[i
][0]) == NULL
; i
++)
499 /* fprintf(stderr, "Looks like %s is not a predicate\n", argv[i]); */
503 /* Enclose the expression in `( ... )' so a default -print will
504 apply to the whole expression. */
505 parse_open (argv
, &argc
);
506 /* Build the input order list. */
509 if (strchr ("-!(),", argv
[i
][0]) == NULL
)
510 usage (_("paths must precede expression"));
511 predicate_name
= argv
[i
];
512 parse_function
= find_parser (predicate_name
);
513 if (parse_function
== NULL
)
514 /* Command line option not recognized */
515 error (1, 0, _("invalid predicate `%s'"), predicate_name
);
517 if (!(*parse_function
) (argv
, &i
))
520 /* Command line option requires an argument */
521 error (1, 0, _("missing argument to `%s'"), predicate_name
);
523 error (1, 0, _("invalid argument `%s' to `%s'"),
524 argv
[i
], predicate_name
);
527 if (predicates
->pred_next
== NULL
)
529 /* No predicates that do something other than set a global variable
530 were given; remove the unneeded initial `(' and add `-print'. */
531 cur_pred
= predicates
;
532 predicates
= last_pred
= predicates
->pred_next
;
533 free ((char *) cur_pred
);
534 parse_print (argv
, &argc
);
536 else if (!default_prints (predicates
->pred_next
))
538 /* One or more predicates that produce output were given;
539 remove the unneeded initial `('. */
540 cur_pred
= predicates
;
541 predicates
= predicates
->pred_next
;
542 free ((char *) cur_pred
);
546 /* `( user-supplied-expression ) -print'. */
547 parse_close (argv
, &argc
);
548 parse_print (argv
, &argc
);
552 fprintf (stderr
, _("Predicate List:\n"));
553 print_list (stderr
, predicates
);
556 /* Done parsing the predicates. Build the evaluation tree. */
557 cur_pred
= predicates
;
558 eval_tree
= get_expr (&cur_pred
, NO_PREC
);
560 /* Check if we have any left-over predicates (this fixes
561 * Debian bug #185202).
563 if (cur_pred
!= NULL
)
565 error (1, 0, _("unexpected extra predicate"));
569 fprintf (stderr
, _("Eval Tree:\n"));
570 print_tree (stderr
, eval_tree
, 0);
573 /* Rearrange the eval tree in optimal-predicate order. */
574 opt_expr (&eval_tree
);
576 /* Determine the point, if any, at which to stat the file. */
577 mark_stat (eval_tree
);
578 /* Determine the point, if any, at which to determine file type. */
579 mark_type (eval_tree
);
582 fprintf (stderr
, _("Optimized Eval Tree:\n"));
583 print_tree (stderr
, eval_tree
, 0);
584 fprintf (stderr
, _("Optimized command line:\n"));
585 print_optlist(stderr
, eval_tree
);
586 fprintf(stderr
, "\n");
589 starting_desc
= open (".", O_RDONLY
);
590 if (0 <= starting_desc
&& fchdir (starting_desc
) != 0)
592 close (starting_desc
);
595 if (starting_desc
< 0)
597 starting_dir
= xgetcwd ();
599 error (1, errno
, _("cannot get current directory"));
601 if ((*options
.xstat
) (".", &starting_stat_buf
) != 0)
602 error (1, errno
, _("cannot get current directory"));
604 /* If no paths are given, default to ".". */
605 for (i
= end_of_leading_options
; i
< argc
&& strchr ("-!(),", argv
[i
][0]) == NULL
; i
++)
607 process_top_path (argv
[i
], 0);
610 /* If there were no path arguments, default to ".". */
611 if (i
== end_of_leading_options
)
614 * We use a temporary variable here because some actions modify
615 * the path temporarily. Hence if we use a string constant,
616 * we get a coredump. The best example of this is if we say
617 * "find -printf %H" (note, not "find . -printf %H").
619 char defaultpath
[2] = ".";
620 process_top_path (defaultpath
, 0);
623 /* If "-exec ... {} +" has been used, there may be some
624 * partially-full command lines which have been built,
625 * but which are not yet complete. Execute those now.
628 return state
.exit_status
;
633 specific_dirname(const char *dir
)
637 if (0 == strcmp(".", dir
))
639 /* OK, what's '.'? */
640 if (NULL
!= getcwd(dirname
, sizeof(dirname
)))
642 return strdup(dirname
);
651 char *result
= canonicalize_filename_mode(dir
, CAN_EXISTING
);
659 static dev_t
*mounted_devices
= NULL
;
660 static size_t num_mounted_devices
= 0u;
664 init_mounted_dev_list()
666 assert(NULL
== mounted_devices
);
667 assert(0 == num_mounted_devices
);
668 mounted_devices
= get_mounted_devices(&num_mounted_devices
);
672 refresh_mounted_dev_list(void)
676 free(mounted_devices
);
679 num_mounted_devices
= 0u;
680 init_mounted_dev_list();
684 /* Search for device DEV in the array LIST, which is of size N. */
686 dev_present(dev_t dev
, const dev_t
*list
, size_t n
)
692 if ( (*list
++) == dev
)
699 enum MountPointStateChange
701 MountPointRecentlyMounted
,
702 MountPointRecentlyUnmounted
,
703 MountPointStateUnchanged
708 static enum MountPointStateChange
709 get_mount_state(dev_t newdev
)
711 int new_is_present
, new_was_present
;
713 new_was_present
= dev_present(newdev
, mounted_devices
, num_mounted_devices
);
714 refresh_mounted_dev_list();
715 new_is_present
= dev_present(newdev
, mounted_devices
, num_mounted_devices
);
717 if (new_was_present
== new_is_present
)
718 return MountPointStateUnchanged
;
719 else if (new_is_present
)
720 return MountPointRecentlyMounted
;
722 return MountPointRecentlyUnmounted
;
726 /* Return non-zero if FS is the name of a filesystem that is likely to
730 fs_likely_to_be_automounted(const char *fs
)
732 return ( (0==strcmp(fs
, "nfs")) || (0==strcmp(fs
, "autofs")) || (0==strcmp(fs
, "subfs")));
735 enum WdSanityCheckFatality
737 FATAL_IF_SANITY_CHECK_FAILS
,
738 NON_FATAL_IF_SANITY_CHECK_FAILS
742 /* Examine the results of the stat() of a directory from before we
743 * entered or left it, with the results of stat()ing it afterward. If
744 * these are different, the filesystem tree has been modified while we
745 * were traversing it. That might be an attempt to use a race
746 * condition to persuade find to do something it didn't intend
747 * (e.g. an attempt by an ordinary user to exploit the fact that root
748 * sometimes runs find on the whole filesystem). However, this can
749 * also happen if automount is running (certainly on Solaris). With
750 * automount, moving into a directory can cause a filesystem to be
753 * To cope sensibly with this, we will raise an error if we see the
754 * device number change unless we are chdir()ing into a subdirectory,
755 * and the directory we moved into has been mounted or unmounted "recently".
756 * Here "recently" means since we started "find" or we last re-read
757 * the /etc/mnttab file.
759 * If the device number does not change but the inode does, that is a
762 * If the device number and inode are both the same, we are happy.
764 * If a filesystem is (un)mounted as we chdir() into the directory, that
765 * may mean that we're now examining a section of the filesystem that might
766 * have been excluded from consideration (via -prune or -quit for example).
767 * Hence we print a warning message to indicate that the output of find
768 * might be inconsistent due to the change in the filesystem.
771 wd_sanity_check(const char *thing_to_stat
,
772 const char *program_name
,
776 struct stat
*newinfo
,
779 enum TraversalDirection direction
,
780 enum WdSanityCheckFatality isfatal
,
781 boolean
*changed
) /* output parameter */
784 char *specific_what
= NULL
;
789 if ((*options
.xstat
) (".", newinfo
) != 0)
790 error (1, errno
, "%s", thing_to_stat
);
792 if (old_dev
!= newinfo
->st_dev
)
795 specific_what
= specific_dirname(what
);
796 fstype
= filesystem_type(newinfo
);
797 silent
= fs_likely_to_be_automounted(fstype
);
799 /* This condition is rare, so once we are here it is
800 * reasonable to perform an expensive computation to
801 * determine if we should continue or fail.
803 if (TraversingDown
== direction
)
805 /* We stat()ed a directory, chdir()ed into it (we know this
806 * since direction is TraversingDown), stat()ed it again,
807 * and noticed that the device numbers are different. Check
808 * if the filesystem was recently mounted.
810 * If it was, it looks like chdir()ing into the directory
811 * caused a filesystem to be mounted. Maybe automount is
812 * running. Anyway, that's probably OK - but it happens
813 * only when we are moving downward.
815 * We also allow for the possibility that a similar thing
816 * has happened with the unmounting of a filesystem. This
817 * is much rarer, as it relies on an automounter timeout
818 * occurring at exactly the wrong moment.
820 enum MountPointStateChange transition
= get_mount_state(newinfo
->st_dev
);
823 case MountPointRecentlyUnmounted
:
824 isfatal
= NON_FATAL_IF_SANITY_CHECK_FAILS
;
828 _("Warning: filesystem %s has recently been unmounted."),
833 case MountPointRecentlyMounted
:
834 isfatal
= NON_FATAL_IF_SANITY_CHECK_FAILS
;
838 _("Warning: filesystem %s has recently been mounted."),
843 case MountPointStateUnchanged
:
844 /* leave isfatal as it is */
849 if (FATAL_IF_SANITY_CHECK_FAILS
== isfatal
)
851 fstype
= filesystem_type(newinfo
);
853 _("%s%s changed during execution of %s (old device number %ld, new device number %ld, filesystem type is %s) [ref %ld]"),
858 (long) newinfo
->st_dev
,
866 /* Since the device has changed under us, the inode number
867 * will almost certainly also be different. However, we have
868 * already decided that this is not a problem. Hence we return
869 * without checking the inode number.
876 /* Device number was the same, check if the inode has changed. */
877 if (old_ino
!= newinfo
->st_ino
)
880 specific_what
= specific_dirname(what
);
881 fstype
= filesystem_type(newinfo
);
883 error ((isfatal
== FATAL_IF_SANITY_CHECK_FAILS
) ? 1 : 0,
884 0, /* no relevant errno value */
885 _("%s%s changed during execution of %s (old inode number %ld, new inode number %ld, filesystem type is %s) [ref %ld]"),
890 (long) newinfo
->st_ino
,
903 SafeChdirFailSymlink
,
906 SafeChdirFailWouldBeUnableToReturn
,
907 SafeChdirFailChdirFailed
,
908 SafeChdirFailNonexistent
911 /* Safely perform a change in directory. We do this by calling
912 * lstat() on the subdirectory, using chdir() tro move into it, and
913 * then lstat()ing ".". We compare the results of the two stat calls
914 * to see if they are consistent. If not, we sound the alarm.
916 * If following_links() is true, we do follow symbolic links.
918 static enum SafeChdirStatus
919 safely_chdir_lstat(const char *dest
,
920 enum TraversalDirection direction
,
921 struct stat
*statbuf_dest
,
922 enum ChdirSymlinkHandling symlink_handling
)
924 struct stat statbuf_arrived
;
926 int saved_errno
; /* specific_dirname() changes errno. */
927 boolean rv_set
= false;
929 saved_errno
= errno
= 0;
931 dotfd
= open(".", O_RDONLY
);
934 /* Stat the directory we're going to. */
935 if (0 == options
.xstat(dest
, statbuf_dest
))
938 /* symlink_handling might be set to SymlinkFollowOk, which
939 * would allow us to chdir() into a symbolic link. This is
940 * only useful for the case where the directory we're
941 * chdir()ing into is the basename of a command line
942 * argument, for example where "foo/bar/baz" is specified on
943 * the command line. When -P is in effect (the default),
944 * baz will not be followed if it is a symlink, but if bar
945 * is a symlink, it _should_ be followed. Hence we need the
946 * ability to override the policy set by following_links().
948 if (!following_links() && S_ISLNK(statbuf_dest
->st_mode
))
950 /* We're not supposed to be following links, but this is
951 * a link. Check symlink_handling to see if we should
952 * make a special exception.
954 if (symlink_handling
== SymlinkFollowOk
)
956 /* We need to re-stat() the file so that the
957 * sanity check can pass.
959 if (0 != stat(dest
, statbuf_dest
))
961 rv
= SafeChdirFailNonexistent
;
969 /* Not following symlinks, so the attempt to
970 * chdir() into a symlink should be prevented.
972 rv
= SafeChdirFailSymlink
;
974 saved_errno
= 0; /* silence the error message */
980 /* Although the immediately following chdir() would detect
981 * the fact that this is not a directory for us, this would
982 * result in an extra system call that fails. Anybody
983 * examining the system-call trace should ideally not be
984 * concerned that something is actually failing.
986 if (!S_ISDIR(statbuf_dest
->st_mode
))
988 rv
= SafeChdirFailNotDir
;
990 saved_errno
= 0; /* silence the error message */
995 fprintf(stderr
, "safely_chdir(): chdir(\"%s\")\n", dest
);
997 if (0 == chdir(dest
))
999 /* check we ended up where we wanted to go */
1000 boolean changed
= false;
1001 wd_sanity_check(".", program_name
, ".",
1002 statbuf_dest
->st_dev
,
1003 statbuf_dest
->st_ino
,
1005 0, __LINE__
, direction
,
1006 FATAL_IF_SANITY_CHECK_FAILS
,
1013 saved_errno
= errno
;
1014 if (ENOENT
== saved_errno
)
1016 rv
= SafeChdirFailNonexistent
;
1018 if (options
.ignore_readdir_race
)
1019 errno
= 0; /* don't issue err msg */
1021 else if (ENOTDIR
== saved_errno
)
1023 /* This can happen if the we stat a directory,
1024 * and then filesystem activity changes it into
1027 saved_errno
= 0; /* don't issue err msg */
1028 rv
= SafeChdirFailNotDir
;
1033 rv
= SafeChdirFailChdirFailed
;
1041 saved_errno
= errno
;
1042 rv
= SafeChdirFailStat
;
1045 if ( (ENOENT
== saved_errno
) || (0 == state
.curdepth
))
1046 saved_errno
= 0; /* don't issue err msg */
1052 /* We do not have read permissions on "." */
1053 rv
= SafeChdirFailWouldBeUnableToReturn
;
1058 /* This is the success path, so we clear errno. The caller probably
1059 * won't be calling error() anyway.
1063 /* We use the same exit path for successs or failure.
1064 * which has occurred is recorded in RV.
1067 /* We do not call error() as this would result in a duplicate error
1068 * message when the caller does the same thing.
1071 errno
= saved_errno
;
1082 #if defined(O_NOFOLLOW)
1083 /* Safely change working directory to the specified subdirectory.
1084 * We use open() with O_NOFOLLOW, followed by fchdir(). This ensures
1085 * that we don't follow symbolic links (of course, we do follow them
1086 * if the -L option is in effect).
1088 static enum SafeChdirStatus
1089 safely_chdir_nofollow(const char *dest
,
1090 enum TraversalDirection direction
,
1091 struct stat
*statbuf_dest
,
1092 enum ChdirSymlinkHandling symlink_handling
)
1094 int extraflags
= following_links() ? O_NOFOLLOW
: 0;
1097 int fd
= open(dest
, O_RDONLY
|extraflags
);
1103 return SafeChdirFailSymlink
; /* This is why we use O_NOFOLLOW */
1105 return SafeChdirFailNonexistent
;
1107 return SafeChdirFailChdirFailed
;
1112 if (0 == fchdir(fd
))
1119 int saved_errno
= errno
;
1121 errno
= saved_errno
;
1126 return SafeChdirFailNotDir
;
1129 case EBADF
: /* Shouldn't happen */
1133 return SafeChdirFailChdirFailed
;
1139 static enum SafeChdirStatus
1140 safely_chdir(const char *dest
,
1141 enum TraversalDirection direction
,
1142 struct stat
*statbuf_dest
,
1143 enum ChdirSymlinkHandling symlink_handling
)
1145 /* We're about to leave a directory. If there are any -execdir
1146 * argument lists which have been built but have not yet been
1147 * processed, do them now because they must be done in the same
1150 complete_pending_execdirs(eval_tree
);
1152 #if defined(O_NOFOLLOW)
1153 if (options
.open_nofollow_available
)
1154 return safely_chdir_nofollow(dest
, direction
, statbuf_dest
, symlink_handling
);
1156 return safely_chdir_lstat(dest
, direction
, statbuf_dest
, symlink_handling
);
1161 /* Safely go back to the starting directory. */
1165 struct stat stat_buf
;
1168 if (starting_desc
< 0)
1171 fprintf(stderr
, "chdir_back(): chdir(\"%s\")\n", starting_dir
);
1174 /* We will need the mounted device list. Get it now if we don't
1177 if (NULL
== mounted_devices
)
1178 init_mounted_dev_list();
1180 if (chdir (starting_dir
) != 0)
1181 error (1, errno
, "%s", starting_dir
);
1183 wd_sanity_check(starting_dir
,
1186 starting_stat_buf
.st_dev
,
1187 starting_stat_buf
.st_ino
,
1188 &stat_buf
, 0, __LINE__
,
1190 FATAL_IF_SANITY_CHECK_FAILS
,
1196 fprintf(stderr
, "chdir_back(): chdir(<starting-point>)\n");
1198 if (fchdir (starting_desc
) != 0)
1199 error (1, errno
, "%s", starting_dir
);
1203 /* Descend PATHNAME, which is a command-line argument.
1204 Actions like -execdir assume that we are in the
1205 parent directory of the file we're examining,
1206 and on entry to this function our working directory
1207 is whetever it was when find was invoked. Therefore
1208 If PATHNAME is "." we just leave things as they are.
1209 Otherwise, we figure out what the parent directory is,
1213 process_top_path (char *pathname
, mode_t mode
)
1216 char *parent_dir
= dir_name(pathname
);
1217 char *base
= base_name(pathname
);
1220 state
.path_length
= strlen (pathname
);
1222 if (0 == strcmp(pathname
, parent_dir
))
1229 enum TraversalDirection direction
;
1230 enum SafeChdirStatus chdir_status
;
1234 if (0 == strcmp(base
, ".."))
1235 direction
= TraversingUp
;
1237 direction
= TraversingDown
;
1239 /* We pass SymlinkFollowOk to safely_chdir(), which allows it to
1240 * chdir() into a symbolic link. This is only useful for the
1241 * case where the directory we're chdir()ing into is the
1242 * basename of a command line argument, for example where
1243 * "foo/bar/baz" is specified on the command line. When -P is
1244 * in effect (the default), baz will not be followed if it is a
1245 * symlink, but if bar is a symlink, it _should_ be followed.
1246 * Hence we need the ability to override the policy set by
1247 * following_links().
1249 chdir_status
= safely_chdir(parent_dir
, direction
, &st
, SymlinkFollowOk
);
1250 if (SafeChdirOK
!= chdir_status
)
1252 const char *what
= (SafeChdirFailWouldBeUnableToReturn
== chdir_status
) ? "." : parent_dir
;
1254 error (0, errno
, "%s", what
);
1256 error (0, 0, "Failed to safely change directory into `%s'",
1259 /* We can't process this command-line argument. */
1260 state
.exit_status
= 1;
1268 process_path (pathname
, base
, false, ".", mode
);
1269 complete_pending_execdirs(eval_tree
);
1278 /* Info on each directory in the current tree branch, to avoid
1279 getting stuck in symbolic link loops. */
1280 static struct dir_id
*dir_ids
= NULL
;
1281 /* Entries allocated in `dir_ids'. */
1282 static int dir_alloc
= 0;
1283 /* Index in `dir_ids' of directory currently being searched.
1284 This is always the last valid entry. */
1285 static int dir_curr
= -1;
1286 /* (Arbitrary) number of entries to grow `dir_ids' by. */
1287 #define DIR_ALLOC_STEP 32
1291 /* We've detected a filesystem loop. This is caused by one of
1294 * 1. Option -L is in effect and we've hit a symbolic link that
1295 * points to an ancestor. This is harmless. We won't traverse the
1298 * 2. We have hit a real cycle in the directory hierarchy. In this
1299 * case, we issue a diagnostic message (POSIX requires this) and we
1300 * skip that directory entry.
1303 issue_loop_warning(const char *name
, const char *pathname
, int level
)
1305 struct stat stbuf_link
;
1306 if (lstat(name
, &stbuf_link
) != 0)
1307 stbuf_link
.st_mode
= S_IFREG
;
1309 if (S_ISLNK(stbuf_link
.st_mode
))
1312 _("Symbolic link `%s' is part of a loop in the directory hierarchy; we have already visited the directory to which it points."),
1317 int distance
= 1 + (dir_curr
-level
);
1318 /* We have found an infinite loop. POSIX requires us to
1319 * issue a diagnostic. Usually we won't get to here
1320 * because when the leaf optimisation is on, it will cause
1321 * the subdirectory to be skipped. If /a/b/c/d is a hard
1322 * link to /a/b, then the link count of /a/b/c is 2,
1323 * because the ".." entry of /b/b/c/d points to /a, not
1327 _("Filesystem loop detected; `%s' has the same device number and inode as a directory which is %d %s."),
1331 _("level higher in the filesystem hierarchy") :
1332 _("levels higher in the filesystem hierarchy")));
1336 /* Take a "mode" indicator and fill in the files of 'state'.
1339 digest_mode(mode_t mode
,
1340 const char *pathname
,
1345 /* If we know the type of the directory entry, and it is not a
1346 * symbolic link, we may be able to avoid a stat() or lstat() call.
1350 if (S_ISLNK(mode
) && following_links())
1352 /* mode is wrong because we should have followed the symlink. */
1353 if (get_statinfo(pathname
, name
, pstat
) != 0)
1355 mode
= state
.type
= pstat
->st_mode
;
1356 state
.have_type
= true;
1360 state
.have_type
= true;
1361 pstat
->st_mode
= state
.type
= mode
;
1366 /* Mode is not yet known; may have to stat the file unless we
1367 * can deduce that it is not a directory (which is all we need to
1368 * know at this stage)
1372 state
.have_stat
= false;
1373 state
.have_type
= false;;
1378 if (get_statinfo(pathname
, name
, pstat
) != 0)
1381 /* If -L is in effect and we are dealing with a symlink,
1382 * st_mode is the mode of the pointed-to file, while mode is
1383 * the mode of the directory entry (S_IFLNK). Hence now
1384 * that we have the stat information, override "mode".
1386 state
.type
= pstat
->st_mode
;
1387 state
.have_type
= true;
1397 /* Recursively descend path PATHNAME, applying the predicates.
1398 LEAF is true if PATHNAME is known to be in a directory that has no
1399 more unexamined subdirectories, and therefore it is not a directory.
1400 Knowing this allows us to avoid calling stat as long as possible for
1403 NAME is PATHNAME relative to the current directory. We access NAME
1406 PARENT is the path of the parent of NAME, relative to find's
1409 Return nonzero iff PATHNAME is a directory. */
1412 process_path (char *pathname
, char *name
, boolean leaf
, char *parent
,
1415 struct stat stat_buf
;
1416 static dev_t root_dev
; /* Device ID of current argument pathname. */
1419 /* Assume it is a non-directory initially. */
1420 stat_buf
.st_mode
= 0;
1421 state
.rel_pathname
= name
;
1423 state
.have_stat
= false;
1424 state
.have_type
= false;
1426 if (!digest_mode(mode
, pathname
, name
, &stat_buf
, leaf
))
1429 if (!S_ISDIR (state
.type
))
1431 if (state
.curdepth
>= options
.mindepth
)
1432 apply_predicate (pathname
, &stat_buf
, eval_tree
);
1436 /* From here on, we're working on a directory. */
1439 /* Now we really need to stat the directory, even if we knoe the
1440 * type, because we need information like struct stat.st_rdev.
1442 if (get_statinfo(pathname
, name
, &stat_buf
) != 0)
1445 state
.have_stat
= true;
1446 mode
= state
.type
= stat_buf
.st_mode
; /* use full info now that we have it. */
1447 state
.stop_at_current_level
=
1448 options
.maxdepth
>= 0
1449 && state
.curdepth
>= options
.maxdepth
;
1451 /* If we've already seen this directory on this branch,
1452 don't descend it again. */
1453 for (i
= 0; i
<= dir_curr
; i
++)
1454 if (stat_buf
.st_ino
== dir_ids
[i
].ino
&&
1455 stat_buf
.st_dev
== dir_ids
[i
].dev
)
1457 state
.stop_at_current_level
= true;
1458 issue_loop_warning(name
, pathname
, i
);
1461 if (dir_alloc
<= ++dir_curr
)
1463 dir_alloc
+= DIR_ALLOC_STEP
;
1464 dir_ids
= (struct dir_id
*)
1465 xrealloc ((char *) dir_ids
, dir_alloc
* sizeof (struct dir_id
));
1467 dir_ids
[dir_curr
].ino
= stat_buf
.st_ino
;
1468 dir_ids
[dir_curr
].dev
= stat_buf
.st_dev
;
1470 if (options
.stay_on_filesystem
)
1472 if (state
.curdepth
== 0)
1473 root_dev
= stat_buf
.st_dev
;
1474 else if (stat_buf
.st_dev
!= root_dev
)
1475 state
.stop_at_current_level
= true;
1478 if (options
.do_dir_first
&& state
.curdepth
>= options
.mindepth
)
1479 apply_predicate (pathname
, &stat_buf
, eval_tree
);
1482 fprintf(stderr
, "pathname = %s, stop_at_current_level = %d\n",
1483 pathname
, state
.stop_at_current_level
);
1486 if (state
.stop_at_current_level
== false)
1487 /* Scan directory on disk. */
1488 process_dir (pathname
, name
, strlen (pathname
), &stat_buf
, parent
);
1490 if (options
.do_dir_first
== false && state
.curdepth
>= options
.mindepth
)
1492 /* The fields in 'state' are now out of date. Correct them.
1494 if (!digest_mode(mode
, pathname
, name
, &stat_buf
, leaf
))
1497 state
.rel_pathname
= name
;
1498 apply_predicate (pathname
, &stat_buf
, eval_tree
);
1506 /* Examine the predicate list for instances of -execdir or -okdir
1507 * which have been terminated with '+' (build argument list) rather
1508 * than ';' (singles only). If there are any, run them (this will
1509 * have no effect if there are no arguments waiting).
1512 complete_pending_execdirs(struct predicate
*p
)
1514 #if defined(NEW_EXEC)
1518 complete_pending_execdirs(p
->pred_left
);
1520 if (p
->pred_func
== pred_execdir
|| p
->pred_func
== pred_okdir
)
1522 /* It's an exec-family predicate. p->args.exec_val is valid. */
1523 if (p
->args
.exec_vec
.multiple
)
1525 struct exec_val
*execp
= &p
->args
.exec_vec
;
1527 /* This one was terminated by '+' and so might have some
1528 * left... Run it if neccessary.
1530 if (execp
->state
.todo
)
1532 /* There are not-yet-executed arguments. */
1533 launch (&execp
->ctl
, &execp
->state
);
1538 complete_pending_execdirs(p
->pred_right
);
1540 /* nothing to do. */
1545 /* Examine the predicate list for instances of -exec which have been
1546 * terminated with '+' (build argument list) rather than ';' (singles
1547 * only). If there are any, run them (this will have no effect if
1548 * there are no arguments waiting).
1551 complete_pending_execs(struct predicate
*p
)
1553 #if defined(NEW_EXEC)
1557 complete_pending_execs(p
->pred_left
);
1559 /* It's an exec-family predicate then p->args.exec_val is valid
1560 * and we can check it.
1562 if (p
->pred_func
== pred_exec
&& p
->args
.exec_vec
.multiple
)
1564 struct exec_val
*execp
= &p
->args
.exec_vec
;
1566 /* This one was terminated by '+' and so might have some
1567 * left... Run it if neccessary. Set state.exit_status if
1568 * there are any problems.
1570 if (execp
->state
.todo
)
1572 /* There are not-yet-executed arguments. */
1573 launch (&execp
->ctl
, &execp
->state
);
1577 complete_pending_execs(p
->pred_right
);
1579 /* nothing to do. */
1585 /* Scan directory PATHNAME and recurse through process_path for each entry.
1587 PATHLEN is the length of PATHNAME.
1589 NAME is PATHNAME relative to the current directory.
1591 STATP is the results of *options.xstat on it.
1593 PARENT is the path of the parent of NAME, relative to find's
1594 starting directory. */
1597 process_dir (char *pathname
, char *name
, int pathlen
, struct stat
*statp
, char *parent
)
1599 char *name_space
; /* Names of files in PATHNAME. */
1600 int subdirs_left
; /* Number of unexamined subdirs in PATHNAME. */
1601 int idx
; /* Which entry are we on? */
1602 struct stat stat_buf
;
1603 struct savedir_dirinfo
*dirinfo
;
1605 subdirs_left
= statp
->st_nlink
- 2; /* Account for name and ".". */
1608 name_space
= savedirinfo (name
, &dirinfo
);
1610 if (name_space
== NULL
)
1613 error (0, errno
, "%s", pathname
);
1614 state
.exit_status
= 1;
1618 register char *namep
; /* Current point in `name_space'. */
1619 char *cur_path
; /* Full path of each file to process. */
1620 char *cur_name
; /* Base name of each file to process. */
1621 unsigned cur_path_size
; /* Bytes allocated for `cur_path'. */
1622 register unsigned file_len
; /* Length of each path to process. */
1623 register unsigned pathname_len
; /* PATHLEN plus trailing '/'. */
1625 if (pathname
[pathlen
- 1] == '/')
1626 pathname_len
= pathlen
+ 1; /* For '\0'; already have '/'. */
1628 pathname_len
= pathlen
+ 2; /* For '/' and '\0'. */
1632 /* We're about to leave the directory. If there are any
1633 * -execdir argument lists which have been built but have not
1634 * yet been processed, do them now because they must be done in
1635 * the same directory.
1637 complete_pending_execdirs(eval_tree
);
1639 if (strcmp (name
, "."))
1641 enum SafeChdirStatus status
= safely_chdir (name
, TraversingDown
, &stat_buf
, SymlinkHandleDefault
);
1645 /* If there had been a change but wd_sanity_check()
1646 * accepted it, we need to accept that on the
1647 * way back up as well, so modify our record
1648 * of what we think we should see later.
1649 * If there was no change, the assignments are a no-op.
1651 dir_ids
[dir_curr
].dev
= stat_buf
.st_dev
;
1652 dir_ids
[dir_curr
].ino
= stat_buf
.st_ino
;
1655 case SafeChdirFailWouldBeUnableToReturn
:
1656 error (0, errno
, ".");
1657 state
.exit_status
= 1;
1660 case SafeChdirFailNonexistent
:
1661 case SafeChdirFailStat
:
1662 case SafeChdirFailSymlink
:
1663 case SafeChdirFailNotDir
:
1664 case SafeChdirFailChdirFailed
:
1665 error (0, errno
, "%s", pathname
);
1666 state
.exit_status
= 1;
1672 for (idx
=0, namep
= name_space
; *namep
; namep
+= file_len
- pathname_len
+ 1, ++idx
)
1674 /* savedirinfo() may return dirinfo=NULL if extended information
1677 mode_t mode
= dirinfo
? dirinfo
[idx
].type_info
: 0;
1679 /* Append this directory entry's name to the path being searched. */
1680 file_len
= pathname_len
+ strlen (namep
);
1681 if (file_len
> cur_path_size
)
1683 while (file_len
> cur_path_size
)
1684 cur_path_size
+= 1024;
1687 cur_path
= xmalloc (cur_path_size
);
1688 strcpy (cur_path
, pathname
);
1689 cur_path
[pathname_len
- 2] = '/';
1691 cur_name
= cur_path
+ pathname_len
- 1;
1692 strcpy (cur_name
, namep
);
1695 if (!options
.no_leaf_check
)
1697 if (mode
&& S_ISDIR(mode
) && (subdirs_left
== 0))
1699 /* This is a subdirectory, but the number of directories we
1700 * have found now exceeds the number we would expect given
1701 * the hard link count on the parent. This is likely to be
1702 * a bug in the filesystem driver (e.g. Linux's
1703 * /proc filesystem) or may just be a fact that the OS
1704 * doesn't really handle hard links with Unix semantics.
1705 * In the latter case, -noleaf should be used routinely.
1707 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."),
1709 state
.exit_status
= 1; /* We know the result is wrong, now */
1710 options
.no_leaf_check
= true; /* Don't make same
1712 subdirs_left
= 1; /* band-aid for this iteration. */
1715 /* Normal case optimization. On normal Unix
1716 filesystems, a directory that has no subdirectories
1717 has two links: its name, and ".". Any additional
1718 links are to the ".." entries of its subdirectories.
1719 Once we have processed as many subdirectories as
1720 there are additional links, we know that the rest of
1721 the entries are non-directories -- in other words,
1723 subdirs_left
-= process_path (cur_path
, cur_name
,
1724 subdirs_left
== 0, pathname
,
1729 /* There might be weird (e.g., CD-ROM or MS-DOS) filesystems
1730 mounted, which don't have Unix-like directory link counts. */
1731 process_path (cur_path
, cur_name
, false, pathname
, mode
);
1738 /* We're about to leave the directory. If there are any
1739 * -execdir argument lists which have been built but have not
1740 * yet been processed, do them now because they must be done in
1741 * the same directory.
1743 complete_pending_execdirs(eval_tree
);
1746 if (strcmp (name
, "."))
1748 enum SafeChdirStatus status
;
1750 boolean changed
= false;
1752 /* We could go back and do the next command-line arg
1753 instead, maybe using longjmp. */
1755 boolean deref
= following_links() ? true : false;
1757 if ( (state
.curdepth
>0) && !deref
)
1765 status
= safely_chdir (dir
, TraversingUp
, &stat_buf
, SymlinkHandleDefault
);
1771 case SafeChdirFailWouldBeUnableToReturn
:
1772 error (1, errno
, ".");
1775 case SafeChdirFailNonexistent
:
1776 case SafeChdirFailStat
:
1777 case SafeChdirFailSymlink
:
1778 case SafeChdirFailNotDir
:
1779 case SafeChdirFailChdirFailed
:
1780 error (1, errno
, "%s", pathname
);
1786 did
.dev
= dir_ids
[dir_curr
-1].dev
;
1787 did
.ino
= dir_ids
[dir_curr
-1].ino
;
1791 did
.dev
= starting_stat_buf
.st_dev
;
1792 did
.ino
= starting_stat_buf
.st_ino
;
1803 /* Return true if there are no predicates with no_default_print in
1804 predicate list PRED, false if there are any.
1805 Returns true if default print should be performed */
1808 default_prints (struct predicate
*pred
)
1810 while (pred
!= NULL
)
1812 if (pred
->no_default_print
)
1814 pred
= pred
->pred_next
;