Removed spurious blank line
[findutils.git] / find / find.c
blobacee35e604eacfa22258468669c0c8c1104aeca9
1 /* find -- search for files in a directory hierarchy
2 Copyright (C) 1990, 91, 92, 93, 94, 2000,
3 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /* GNU find was written by Eric Decker <cire@cisco.com>,
19 with enhancements by David MacKenzie <djm@gnu.org>,
20 Jay Plett <jay@silence.princeton.nj.us>,
21 and Tim Wood <axolotl!tim@toad.com>.
22 The idea for -print0 and xargs -0 came from
23 Dan Bernstein <brnstnd@kramden.acf.nyu.edu>.
24 Improvements have been made by James Youngman <jay@gnu.org>.
28 #include <config.h>
29 #include "defs.h"
31 #define USE_SAFE_CHDIR 1
32 #undef STAT_MOUNTPOINTS
35 #include <errno.h>
36 #include <assert.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <openat.h>
42 #include "xalloc.h"
43 #include "human.h"
44 #include "canonicalize.h"
45 #include <modetype.h>
47 #include "closein.h"
48 #include "savedirinfo.h"
49 #include "buildcmd.h"
50 #include "dirname.h"
51 #include "quote.h"
52 #include "quotearg.h"
53 #include "xgetcwd.h"
54 #include "error.h"
56 #ifdef HAVE_LOCALE_H
57 #include <locale.h>
58 #endif
60 #if ENABLE_NLS
61 # include <libintl.h>
62 # define _(Text) gettext (Text)
63 #else
64 # define _(Text) Text
65 #define textdomain(Domain)
66 #define bindtextdomain(Package, Directory)
67 #endif
68 #ifdef gettext_noop
69 # define N_(String) gettext_noop (String)
70 #else
71 /* See locate.c for explanation as to why not use (String) */
72 # define N_(String) String
73 #endif
75 #ifdef STAT_MOUNTPOINTS
76 static void init_mounted_dev_list(int mandatory);
77 #endif
79 static void process_top_path PARAMS((char *pathname, mode_t mode));
80 static int process_path PARAMS((char *pathname, char *name, boolean leaf, char *parent, mode_t type));
81 static void process_dir PARAMS((char *pathname, char *name, int pathlen, const struct stat *statp, char *parent));
85 /* Name this program was run with. */
86 char *program_name;
88 /* A file descriptor open to the initial working directory.
89 Doing it this way allows us to work when the i.w.d. has
90 unreadable parents. */
91 int starting_desc;
93 /* The stat buffer of the initial working directory. */
94 static struct stat starting_stat_buf;
96 enum ChdirSymlinkHandling
98 SymlinkHandleDefault, /* Normally the right choice */
99 SymlinkFollowOk /* see comment in process_top_path() */
103 enum TraversalDirection
105 TraversingUp,
106 TraversingDown
109 enum WdSanityCheckFatality
111 FATAL_IF_SANITY_CHECK_FAILS,
112 RETRY_IF_SANITY_CHECK_FAILS,
113 NON_FATAL_IF_SANITY_CHECK_FAILS
117 int get_current_dirfd(void)
119 return AT_FDCWD;
124 main (int argc, char **argv)
126 int i;
127 int end_of_leading_options = 0; /* First arg after any -H/-L etc. */
128 struct predicate *eval_tree;
130 program_name = argv[0];
131 state.exit_status = 0;
133 /* Set the option defaults before we do the locale
134 * initialisation as check_nofollow() needs to be executed in the
135 * POSIX locale.
137 set_option_defaults(&options);
139 #ifdef HAVE_SETLOCALE
140 setlocale (LC_ALL, "");
141 #endif
142 bindtextdomain (PACKAGE, LOCALEDIR);
143 textdomain (PACKAGE);
144 atexit (close_stdin);
146 /* Check for -P, -H or -L options. */
147 end_of_leading_options = process_leading_options(argc, argv);
149 if (options.debug_options & DebugStat)
150 options.xstat = debug_stat;
152 #ifdef DEBUG
153 fprintf (stderr, "cur_day_start = %s", ctime (&options.cur_day_start));
154 #endif /* DEBUG */
156 /* state.cwd_dir_fd has to be initialised before we call build_expression_tree()
157 * because command-line parsing may lead us to stat some files.
159 state.cwd_dir_fd = AT_FDCWD;
161 /* We are now processing the part of the "find" command line
162 * after the -H/-L options (if any).
164 eval_tree = build_expression_tree(argc, argv, end_of_leading_options);
167 /* safely_chdir() needs to check that it has ended up in the right place.
168 * To avoid bailing out when something gets automounted, it checks if
169 * the target directory appears to have had a directory mounted on it as
170 * we chdir()ed. The problem with this is that in order to notice that
171 * a file system was mounted, we would need to lstat() all the mount points.
172 * That strategy loses if our machine is a client of a dead NFS server.
174 * Hence if safely_chdir() and wd_sanity_check() can manage without needing
175 * to know the mounted device list, we do that.
177 if (!options.open_nofollow_available)
179 #ifdef STAT_MOUNTPOINTS
180 init_mounted_dev_list(0);
181 #endif
185 starting_desc = open (".", O_RDONLY
186 #if defined O_LARGEFILE
187 |O_LARGEFILE
188 #endif
190 if (0 <= starting_desc && fchdir (starting_desc) != 0)
192 close (starting_desc);
193 starting_desc = -1;
196 if (starting_desc < 0)
198 starting_dir = xgetcwd ();
199 if (! starting_dir)
200 error (1, errno, _("cannot get current directory"));
202 set_stat_placeholders(&starting_stat_buf);
203 if ((*options.xstat) (".", &starting_stat_buf) != 0)
204 error (1, errno, _("cannot stat current directory"));
206 /* If no paths are given, default to ".". */
207 for (i = end_of_leading_options; i < argc && !looks_like_expression(argv[i], true); i++)
209 process_top_path (argv[i], 0);
212 /* If there were no path arguments, default to ".". */
213 if (i == end_of_leading_options)
216 * We use a temporary variable here because some actions modify
217 * the path temporarily. Hence if we use a string constant,
218 * we get a coredump. The best example of this is if we say
219 * "find -printf %H" (note, not "find . -printf %H").
221 char defaultpath[2] = ".";
222 process_top_path (defaultpath, 0);
225 /* If "-exec ... {} +" has been used, there may be some
226 * partially-full command lines which have been built,
227 * but which are not yet complete. Execute those now.
229 show_success_rates(eval_tree);
230 cleanup();
231 return state.exit_status;
234 boolean is_fts_enabled(int *ftsoptions)
236 /* this version of find (i.e. this main()) does not use fts. */
237 *ftsoptions = 0;
238 return false;
242 static char *
243 specific_dirname(const char *dir)
245 char dirbuf[1024];
247 if (0 == strcmp(".", dir))
249 /* OK, what's '.'? */
250 if (NULL != getcwd(dirbuf, sizeof(dirbuf)))
252 return strdup(dirbuf);
254 else
256 return strdup(dir);
259 else
261 char *result = canonicalize_filename_mode(dir, CAN_EXISTING);
262 if (NULL == result)
263 return strdup(dir);
264 else
265 return result;
271 /* Return non-zero if FS is the name of a file system that is likely to
272 * be automounted
274 static int
275 fs_likely_to_be_automounted(const char *fs)
277 return ( (0==strcmp(fs, "nfs")) || (0==strcmp(fs, "autofs")) || (0==strcmp(fs, "subfs")));
282 #ifdef STAT_MOUNTPOINTS
283 static dev_t *mounted_devices = NULL;
284 static size_t num_mounted_devices = 0u;
287 static void
288 init_mounted_dev_list(int mandatory)
290 assert (NULL == mounted_devices);
291 assert (0 == num_mounted_devices);
292 mounted_devices = get_mounted_devices(&num_mounted_devices);
293 if (mandatory && (NULL == mounted_devices))
295 error(1, 0, "Cannot read list of mounted devices.");
299 static void
300 refresh_mounted_dev_list(void)
302 if (mounted_devices)
304 free(mounted_devices);
305 mounted_devices = 0;
307 num_mounted_devices = 0u;
308 init_mounted_dev_list(1);
312 /* Search for device DEV in the array LIST, which is of size N. */
313 static int
314 dev_present(dev_t dev, const dev_t *list, size_t n)
316 if (list)
318 while (n-- > 0u)
320 if ( (*list++) == dev )
321 return 1;
324 return 0;
327 enum MountPointStateChange
329 MountPointRecentlyMounted,
330 MountPointRecentlyUnmounted,
331 MountPointStateUnchanged
336 static enum MountPointStateChange
337 get_mount_state(dev_t newdev)
339 int new_is_present, new_was_present;
341 new_was_present = dev_present(newdev, mounted_devices, num_mounted_devices);
342 refresh_mounted_dev_list();
343 new_is_present = dev_present(newdev, mounted_devices, num_mounted_devices);
345 if (new_was_present == new_is_present)
346 return MountPointStateUnchanged;
347 else if (new_is_present)
348 return MountPointRecentlyMounted;
349 else
350 return MountPointRecentlyUnmounted;
355 /* We stat()ed a directory, chdir()ed into it (we know this
356 * since direction is TraversingDown), stat()ed it again,
357 * and noticed that the device numbers are different. Check
358 * if the file system was recently mounted.
360 * If it was, it looks like chdir()ing into the directory
361 * caused a file system to be mounted. Maybe automount is
362 * running. Anyway, that's probably OK - but it happens
363 * only when we are moving downward.
365 * We also allow for the possibility that a similar thing
366 * has happened with the unmounting of a file system. This
367 * is much rarer, as it relies on an automounter timeout
368 * occurring at exactly the wrong moment.
370 static enum WdSanityCheckFatality
371 dirchange_is_fatal(const char *specific_what,
372 enum WdSanityCheckFatality isfatal,
373 int silent,
374 struct stat *newinfo)
376 enum MountPointStateChange transition = get_mount_state(newinfo->st_dev);
377 switch (transition)
379 case MountPointRecentlyUnmounted:
380 isfatal = NON_FATAL_IF_SANITY_CHECK_FAILS;
381 if (!silent)
383 error (0, 0,
384 _("Warning: file system %s has recently been unmounted."),
385 safely_quote_err_filename(0, specific_what));
387 break;
389 case MountPointRecentlyMounted:
390 isfatal = NON_FATAL_IF_SANITY_CHECK_FAILS;
391 if (!silent)
393 error (0, 0,
394 _("Warning: file system %s has recently been mounted."),
395 safely_quote_err_filename(0, specific_what));
397 break;
399 case MountPointStateUnchanged:
400 /* leave isfatal as it is */
401 break;
404 return isfatal;
408 #endif
412 /* Examine the results of the stat() of a directory from before we
413 * entered or left it, with the results of stat()ing it afterward. If
414 * these are different, the file system tree has been modified while we
415 * were traversing it. That might be an attempt to use a race
416 * condition to persuade find to do something it didn't intend
417 * (e.g. an attempt by an ordinary user to exploit the fact that root
418 * sometimes runs find on the whole file system). However, this can
419 * also happen if automount is running (certainly on Solaris). With
420 * automount, moving into a directory can cause a file system to be
421 * mounted there.
423 * To cope sensibly with this, we will raise an error if we see the
424 * device number change unless we are chdir()ing into a subdirectory,
425 * and the directory we moved into has been mounted or unmounted "recently".
426 * Here "recently" means since we started "find" or we last re-read
427 * the /etc/mnttab file.
429 * If the device number does not change but the inode does, that is a
430 * problem.
432 * If the device number and inode are both the same, we are happy.
434 * If a file system is (un)mounted as we chdir() into the directory, that
435 * may mean that we're now examining a section of the file system that might
436 * have been excluded from consideration (via -prune or -quit for example).
437 * Hence we print a warning message to indicate that the output of find
438 * might be inconsistent due to the change in the file system.
440 static boolean
441 wd_sanity_check(const char *thing_to_stat,
442 const char *progname,
443 const char *what,
444 dev_t old_dev,
445 ino_t old_ino,
446 struct stat *newinfo,
447 int parent,
448 int line_no,
449 enum TraversalDirection direction,
450 enum WdSanityCheckFatality isfatal,
451 boolean *changed) /* output parameter */
453 const char *fstype;
454 char *specific_what = NULL;
455 int silent = 0;
456 const char *current_dir = ".";
458 *changed = false;
460 set_stat_placeholders(newinfo);
461 if ((*options.xstat) (current_dir, newinfo) != 0)
462 fatal_file_error(thing_to_stat);
464 if (old_dev != newinfo->st_dev)
466 *changed = true;
467 specific_what = specific_dirname(what);
468 fstype = filesystem_type(newinfo, current_dir);
469 silent = fs_likely_to_be_automounted(fstype);
471 /* This condition is rare, so once we are here it is
472 * reasonable to perform an expensive computation to
473 * determine if we should continue or fail.
475 if (TraversingDown == direction)
477 #ifdef STAT_MOUNTPOINTS
478 isfatal = dirchange_is_fatal(specific_what,isfatal,silent,newinfo);
479 #else
480 isfatal = RETRY_IF_SANITY_CHECK_FAILS;
481 #endif
484 switch (isfatal)
486 case FATAL_IF_SANITY_CHECK_FAILS:
488 fstype = filesystem_type(newinfo, current_dir);
489 error (1, 0,
490 _("%s%s changed during execution of %s (old device number %ld, new device number %ld, file system type is %s) [ref %ld]"),
491 safely_quote_err_filename(0, specific_what),
492 parent ? "/.." : "",
493 safely_quote_err_filename(1, progname),
494 (long) old_dev,
495 (long) newinfo->st_dev,
496 fstype,
497 (long)line_no);
498 /*NOTREACHED*/
499 return false;
502 case NON_FATAL_IF_SANITY_CHECK_FAILS:
504 /* Since the device has changed under us, the inode number
505 * will almost certainly also be different. However, we have
506 * already decided that this is not a problem. Hence we return
507 * without checking the inode number.
509 free(specific_what);
510 return true;
513 case RETRY_IF_SANITY_CHECK_FAILS:
514 return false;
518 /* Device number was the same, check if the inode has changed. */
519 if (old_ino != newinfo->st_ino)
521 *changed = true;
522 specific_what = specific_dirname(what);
523 fstype = filesystem_type(newinfo, current_dir);
525 error ((isfatal == FATAL_IF_SANITY_CHECK_FAILS) ? 1 : 0,
526 0, /* no relevant errno value */
527 _("%s%s changed during execution of %s (old inode number %ld, new inode number %ld, file system type is %s) [ref %ld]"),
528 safely_quote_err_filename(0, specific_what),
529 parent ? "/.." : "",
530 safely_quote_err_filename(1, progname),
531 (long) old_ino,
532 (long) newinfo->st_ino,
533 fstype,
534 (long)line_no);
535 free(specific_what);
536 return false;
539 return true;
542 enum SafeChdirStatus
544 SafeChdirOK,
545 SafeChdirFailSymlink,
546 SafeChdirFailNotDir,
547 SafeChdirFailStat,
548 SafeChdirFailWouldBeUnableToReturn,
549 SafeChdirFailChdirFailed,
550 SafeChdirFailNonexistent,
551 SafeChdirFailDestUnreadable
554 /* Safely perform a change in directory. We do this by calling
555 * lstat() on the subdirectory, using chdir() to move into it, and
556 * then lstat()ing ".". We compare the results of the two stat calls
557 * to see if they are consistent. If not, we sound the alarm.
559 * If following_links() is true, we do follow symbolic links.
561 static enum SafeChdirStatus
562 safely_chdir_lstat(const char *dest,
563 enum TraversalDirection direction,
564 struct stat *statbuf_dest,
565 enum ChdirSymlinkHandling symlink_follow_option,
566 boolean *did_stat)
568 struct stat statbuf_arrived;
569 int rv, dotfd=-1;
570 int saved_errno; /* specific_dirname() changes errno. */
571 boolean rv_set = false;
572 boolean statflag = false;
573 int tries = 0;
574 enum WdSanityCheckFatality isfatal = RETRY_IF_SANITY_CHECK_FAILS;
576 saved_errno = errno = 0;
578 dotfd = open(".", O_RDONLY
579 #if defined O_LARGEFILE
580 |O_LARGEFILE
581 #endif
584 /* We jump back to here if wd_sanity_check()
585 * recoverably triggers an alert.
587 retry:
588 ++tries;
590 if (dotfd >= 0)
592 /* Stat the directory we're going to. */
593 set_stat_placeholders(statbuf_dest);
594 if (0 == options.xstat(dest, statbuf_dest))
596 statflag = true;
598 #ifdef S_ISLNK
599 /* symlink_follow_option might be set to SymlinkFollowOk, which
600 * would allow us to chdir() into a symbolic link. This is
601 * only useful for the case where the directory we're
602 * chdir()ing into is the basename of a command line
603 * argument, for example where "foo/bar/baz" is specified on
604 * the command line. When -P is in effect (the default),
605 * baz will not be followed if it is a symlink, but if bar
606 * is a symlink, it _should_ be followed. Hence we need the
607 * ability to override the policy set by following_links().
609 if (!following_links() && S_ISLNK(statbuf_dest->st_mode))
611 /* We're not supposed to be following links, but this is
612 * a link. Check symlink_follow_option to see if we should
613 * make a special exception.
615 if (symlink_follow_option == SymlinkFollowOk)
617 /* We need to re-stat() the file so that the
618 * sanity check can pass.
620 if (0 != stat(dest, statbuf_dest))
622 rv = SafeChdirFailNonexistent;
623 rv_set = true;
624 saved_errno = errno;
625 goto fail;
627 statflag = true;
629 else
631 /* Not following symlinks, so the attempt to
632 * chdir() into a symlink should be prevented.
634 rv = SafeChdirFailSymlink;
635 rv_set = true;
636 saved_errno = 0; /* silence the error message */
637 goto fail;
640 #endif
641 #ifdef S_ISDIR
642 /* Although the immediately following chdir() would detect
643 * the fact that this is not a directory for us, this would
644 * result in an extra system call that fails. Anybody
645 * examining the system-call trace should ideally not be
646 * concerned that something is actually failing.
648 if (!S_ISDIR(statbuf_dest->st_mode))
650 rv = SafeChdirFailNotDir;
651 rv_set = true;
652 saved_errno = 0; /* silence the error message */
653 goto fail;
655 #endif
657 if (options.debug_options & DebugSearch)
658 fprintf(stderr, "safely_chdir(): chdir(\"%s\")\n", dest);
660 if (0 == chdir(dest))
662 /* check we ended up where we wanted to go */
663 boolean changed = false;
664 if (!wd_sanity_check(".", program_name, ".",
665 statbuf_dest->st_dev,
666 statbuf_dest->st_ino,
667 &statbuf_arrived,
668 0, __LINE__, direction,
669 isfatal,
670 &changed))
672 /* Only allow one failure. */
673 if (RETRY_IF_SANITY_CHECK_FAILS == isfatal)
675 if (0 == fchdir(dotfd))
677 isfatal = FATAL_IF_SANITY_CHECK_FAILS;
678 goto retry;
680 else
682 /* Failed to return to original directory,
683 * but we know that the current working
684 * directory is not the one that we intend
685 * to be in. Since fchdir() failed, we
686 * can't recover from this and so this error
687 * is fatal.
689 error(1, errno,
690 "failed to return to parent directory");
693 else
695 /* XXX: not sure what to use as an excuse here. */
696 rv = SafeChdirFailNonexistent;
697 rv_set = true;
698 saved_errno = 0;
699 goto fail;
703 close(dotfd);
704 return SafeChdirOK;
706 else
708 saved_errno = errno;
709 if (ENOENT == saved_errno)
711 rv = SafeChdirFailNonexistent;
712 rv_set = true;
713 if (options.ignore_readdir_race)
714 errno = 0; /* don't issue err msg */
716 else if (ENOTDIR == saved_errno)
718 /* This can happen if the we stat a directory,
719 * and then file system activity changes it into
720 * a non-directory.
722 saved_errno = 0; /* don't issue err msg */
723 rv = SafeChdirFailNotDir;
724 rv_set = true;
726 else
728 rv = SafeChdirFailChdirFailed;
729 rv_set = true;
731 goto fail;
734 else
736 saved_errno = errno;
737 rv = SafeChdirFailStat;
738 rv_set = true;
740 if ( (ENOENT == saved_errno) || (0 == state.curdepth))
741 saved_errno = 0; /* don't issue err msg */
742 goto fail;
745 else
747 /* We do not have read permissions on "." */
748 rv = SafeChdirFailWouldBeUnableToReturn;
749 rv_set = true;
750 goto fail;
753 /* This is the success path, so we clear errno. The caller probably
754 * won't be calling error() anyway.
756 saved_errno = 0;
758 /* We use the same exit path for success or failure.
759 * which has occurred is recorded in RV.
761 fail:
762 /* We do not call error() as this would result in a duplicate error
763 * message when the caller does the same thing.
765 if (saved_errno)
766 errno = saved_errno;
768 if (dotfd >= 0)
770 close(dotfd);
771 dotfd = -1;
774 *did_stat = statflag;
775 assert (rv_set);
776 return rv;
779 #if defined O_NOFOLLOW
780 /* Safely change working directory to the specified subdirectory. If
781 * we are not allowed to follow symbolic links, we use open() with
782 * O_NOFOLLOW, followed by fchdir(). This ensures that we don't
783 * follow symbolic links (of course, we do follow them if the -L
784 * option is in effect).
786 static enum SafeChdirStatus
787 safely_chdir_nofollow(const char *dest,
788 enum TraversalDirection direction,
789 struct stat *statbuf_dest,
790 enum ChdirSymlinkHandling symlink_follow_option,
791 boolean *did_stat)
793 int extraflags, fd;
795 (void) direction;
796 (void) statbuf_dest;
798 extraflags = 0;
799 *did_stat = false;
801 switch (symlink_follow_option)
803 case SymlinkFollowOk:
804 extraflags = 0;
805 break;
807 case SymlinkHandleDefault:
808 if (following_links())
809 extraflags = 0;
810 else
811 extraflags = O_NOFOLLOW;
812 break;
815 errno = 0;
816 fd = open(dest, O_RDONLY
817 #if defined O_LARGEFILE
818 |O_LARGEFILE
819 #endif
820 |extraflags);
821 if (fd < 0)
823 switch (errno)
825 case ELOOP:
826 return SafeChdirFailSymlink; /* This is why we use O_NOFOLLOW */
827 case ENOENT:
828 return SafeChdirFailNonexistent;
829 default:
830 return SafeChdirFailDestUnreadable;
834 errno = 0;
835 if (0 == fchdir(fd))
837 close(fd);
838 return SafeChdirOK;
840 else
842 int saved_errno = errno;
843 close(fd);
844 errno = saved_errno;
846 switch (errno)
848 case ENOTDIR:
849 return SafeChdirFailNotDir;
851 case EACCES:
852 case EBADF: /* Shouldn't happen */
853 case EINTR:
854 case EIO:
855 default:
856 return SafeChdirFailChdirFailed;
860 #endif
862 static enum SafeChdirStatus
863 safely_chdir(const char *dest,
864 enum TraversalDirection direction,
865 struct stat *statbuf_dest,
866 enum ChdirSymlinkHandling symlink_follow_option,
867 boolean *did_stat)
869 enum SafeChdirStatus result;
871 /* We're about to leave a directory. If there are any -execdir
872 * argument lists which have been built but have not yet been
873 * processed, do them now because they must be done in the same
874 * directory.
876 complete_pending_execdirs(get_current_dirfd());
878 #if !defined(O_NOFOLLOW)
879 options.open_nofollow_available = false;
880 #endif
881 if (options.open_nofollow_available)
883 result = safely_chdir_nofollow(dest, direction, statbuf_dest,
884 symlink_follow_option, did_stat);
885 if (SafeChdirFailDestUnreadable != result)
887 return result;
889 else
891 /* Savannah bug #15384: fall through to use safely_chdir_lstat
892 * if the directory is not readable.
894 /* Do nothing. */
897 /* Even if O_NOFOLLOW is available, we may need to use the alternative
898 * method, since parent of the start point may be executable but not
899 * readable.
901 return safely_chdir_lstat(dest, direction, statbuf_dest,
902 symlink_follow_option, did_stat);
907 /* Safely go back to the starting directory. */
908 static void
909 chdir_back (void)
911 struct stat stat_buf;
912 boolean dummy;
914 if (starting_desc < 0)
916 if (options.debug_options & DebugSearch)
917 fprintf(stderr, "chdir_back(): chdir(\"%s\")\n", starting_dir);
919 #ifdef STAT_MOUNTPOINTS
920 /* We will need the mounted device list. Get it now if we don't
921 * already have it.
923 if (NULL == mounted_devices)
924 init_mounted_dev_list(1);
925 #endif
927 if (chdir (starting_dir) != 0)
928 fatal_file_error(starting_dir);
930 wd_sanity_check(starting_dir,
931 program_name,
932 starting_dir,
933 starting_stat_buf.st_dev,
934 starting_stat_buf.st_ino,
935 &stat_buf, 0, __LINE__,
936 TraversingUp,
937 FATAL_IF_SANITY_CHECK_FAILS,
938 &dummy);
940 else
942 if (options.debug_options & DebugSearch)
943 fprintf(stderr, "chdir_back(): chdir(<starting-point>)\n");
945 if (fchdir (starting_desc) != 0)
947 fatal_file_error(starting_dir);
952 /* Move to the parent of a given directory and then call a function,
953 * restoring the cwd. Don't bother changing directory if the
954 * specified directory is a child of "." or is the root directory.
956 static void
957 at_top (char *pathname,
958 mode_t mode,
959 struct stat *pstat,
960 void (*action)(char *pathname,
961 char *basename,
962 int mode,
963 struct stat *pstat))
965 int dirchange;
966 char *parent_dir = dir_name (pathname);
967 char *base = last_component (pathname);
969 state.curdepth = 0;
970 state.starting_path_length = strlen (pathname);
972 if (0 == *base
973 || 0 == strcmp(parent_dir, "."))
975 dirchange = 0;
976 base = pathname;
978 else
980 enum TraversalDirection direction;
981 enum SafeChdirStatus chdir_status;
982 struct stat st;
983 boolean did_stat = false;
985 dirchange = 1;
986 if (0 == strcmp(base, ".."))
987 direction = TraversingUp;
988 else
989 direction = TraversingDown;
991 /* We pass SymlinkFollowOk to safely_chdir(), which allows it to
992 * chdir() into a symbolic link. This is only useful for the
993 * case where the directory we're chdir()ing into is the
994 * basename of a command line argument, for example where
995 * "foo/bar/baz" is specified on the command line. When -P is
996 * in effect (the default), baz will not be followed if it is a
997 * symlink, but if bar is a symlink, it _should_ be followed.
998 * Hence we need the ability to override the policy set by
999 * following_links().
1001 chdir_status = safely_chdir(parent_dir, direction, &st, SymlinkFollowOk, &did_stat);
1002 if (SafeChdirOK != chdir_status)
1004 const char *what = (SafeChdirFailWouldBeUnableToReturn == chdir_status) ? "." : parent_dir;
1005 if (errno)
1006 error (0, errno, "%s",
1007 safely_quote_err_filename(0, what));
1008 else
1009 error (0, 0, _("Failed to safely change directory into %s"),
1010 safely_quote_err_filename(0, parent_dir));
1012 /* We can't process this command-line argument. */
1013 state.exit_status = 1;
1014 return;
1018 free (parent_dir);
1019 parent_dir = NULL;
1021 action(pathname, base, mode, pstat);
1023 if (dirchange)
1025 chdir_back();
1030 static void do_process_top_dir(char *pathname,
1031 char *base,
1032 int mode,
1033 struct stat *pstat)
1035 (void) pstat;
1037 process_path (pathname, base, false, ".", mode);
1038 complete_pending_execdirs(get_current_dirfd());
1041 static void do_process_predicate(char *pathname,
1042 char *base,
1043 int mode,
1044 struct stat *pstat)
1046 (void) mode;
1048 state.rel_pathname = base; /* cwd_dir_fd was already set by safely_chdir */
1049 apply_predicate (pathname, pstat, get_eval_tree());
1055 /* Descend PATHNAME, which is a command-line argument.
1057 Actions like -execdir assume that we are in the
1058 parent directory of the file we're examining,
1059 and on entry to this function our working directory
1060 is whatever it was when find was invoked. Therefore
1061 If PATHNAME is "." we just leave things as they are.
1062 Otherwise, we figure out what the parent directory is,
1063 and move to that.
1065 static void
1066 process_top_path (char *pathname, mode_t mode)
1068 at_top(pathname, mode, NULL, do_process_top_dir);
1072 /* Info on each directory in the current tree branch, to avoid
1073 getting stuck in symbolic link loops. */
1074 static struct dir_id *dir_ids = NULL;
1075 /* Entries allocated in `dir_ids'. */
1076 static int dir_alloc = 0;
1077 /* Index in `dir_ids' of directory currently being searched.
1078 This is always the last valid entry. */
1079 static int dir_curr = -1;
1080 /* (Arbitrary) number of entries to grow `dir_ids' by. */
1081 #define DIR_ALLOC_STEP 32
1085 /* We've detected a file system loop. This is caused by one of
1086 * two things:
1088 * 1. Option -L is in effect and we've hit a symbolic link that
1089 * points to an ancestor. This is harmless. We won't traverse the
1090 * symbolic link.
1092 * 2. We have hit a real cycle in the directory hierarchy. In this
1093 * case, we issue a diagnostic message (POSIX requires this) and we
1094 * skip that directory entry.
1096 static void
1097 issue_loop_warning(const char *name, const char *pathname, int level)
1099 struct stat stbuf_link;
1100 if (lstat(name, &stbuf_link) != 0)
1101 stbuf_link.st_mode = S_IFREG;
1103 if (S_ISLNK(stbuf_link.st_mode))
1105 error(0, 0,
1106 _("Symbolic link %s is part of a loop in the directory hierarchy; we have already visited the directory to which it points."),
1107 safely_quote_err_filename(0, pathname));
1108 /* XXX: POSIX appears to require that the exit status be non-zero if a
1109 * diagnostic is issued.
1112 else
1114 int distance = 1 + (dir_curr-level);
1115 /* We have found an infinite loop. POSIX requires us to
1116 * issue a diagnostic. Usually we won't get to here
1117 * because when the leaf optimisation is on, it will cause
1118 * the subdirectory to be skipped. If /a/b/c/d is a hard
1119 * link to /a/b, then the link count of /a/b/c is 2,
1120 * because the ".." entry of /b/b/c/d points to /a, not
1121 * to /a/b/c.
1123 error(0, 0,
1124 _("Filesystem loop detected; %s has the same device number and inode as a directory which is %d %s."),
1125 safely_quote_err_filename(0, pathname),
1126 distance,
1127 (distance == 1 ?
1128 _("level higher in the file system hierarchy") :
1129 _("levels higher in the file system hierarchy")));
1135 /* Recursively descend path PATHNAME, applying the predicates.
1136 LEAF is true if PATHNAME is known to be in a directory that has no
1137 more unexamined subdirectories, and therefore it is not a directory.
1138 Knowing this allows us to avoid calling stat as long as possible for
1139 leaf files.
1141 NAME is PATHNAME relative to the current directory. We access NAME
1142 but print PATHNAME.
1144 PARENT is the path of the parent of NAME, relative to find's
1145 starting directory.
1147 Return nonzero iff PATHNAME is a directory. */
1149 static int
1150 process_path (char *pathname, char *name, boolean leaf, char *parent,
1151 mode_t mode)
1153 struct stat stat_buf;
1154 static dev_t root_dev; /* Device ID of current argument pathname. */
1155 int i;
1156 struct predicate *eval_tree;
1158 eval_tree = get_eval_tree();
1159 /* Assume it is a non-directory initially. */
1160 stat_buf.st_mode = 0;
1161 state.rel_pathname = name;
1162 state.type = 0;
1163 state.have_stat = false;
1164 state.have_type = false;
1166 if (!digest_mode(mode, pathname, name, &stat_buf, leaf))
1167 return 0;
1169 if (!S_ISDIR (state.type))
1171 if (state.curdepth >= options.mindepth)
1172 apply_predicate (pathname, &stat_buf, eval_tree);
1173 return 0;
1176 /* From here on, we're working on a directory. */
1179 /* Now we really need to stat the directory, even if we know the
1180 * type, because we need information like struct stat.st_rdev.
1182 if (get_statinfo(pathname, name, &stat_buf) != 0)
1183 return 0;
1185 state.have_stat = true;
1186 mode = state.type = stat_buf.st_mode; /* use full info now that we have it. */
1187 state.stop_at_current_level =
1188 options.maxdepth >= 0
1189 && state.curdepth >= options.maxdepth;
1191 /* If we've already seen this directory on this branch,
1192 don't descend it again. */
1193 for (i = 0; i <= dir_curr; i++)
1194 if (stat_buf.st_ino == dir_ids[i].ino &&
1195 stat_buf.st_dev == dir_ids[i].dev)
1197 state.stop_at_current_level = true;
1198 issue_loop_warning(name, pathname, i);
1201 if (dir_alloc <= ++dir_curr)
1203 dir_alloc += DIR_ALLOC_STEP;
1204 dir_ids = (struct dir_id *)
1205 xrealloc ((char *) dir_ids, dir_alloc * sizeof (struct dir_id));
1207 dir_ids[dir_curr].ino = stat_buf.st_ino;
1208 dir_ids[dir_curr].dev = stat_buf.st_dev;
1210 if (options.stay_on_filesystem)
1212 if (state.curdepth == 0)
1213 root_dev = stat_buf.st_dev;
1214 else if (stat_buf.st_dev != root_dev)
1215 state.stop_at_current_level = true;
1218 if (options.do_dir_first && state.curdepth >= options.mindepth)
1219 apply_predicate (pathname, &stat_buf, eval_tree);
1221 if (options.debug_options & DebugSearch)
1222 fprintf(stderr, "pathname = %s, stop_at_current_level = %d\n",
1223 pathname, state.stop_at_current_level);
1225 if (state.stop_at_current_level == false)
1227 /* Scan directory on disk. */
1228 process_dir (pathname, name, strlen (pathname), &stat_buf, parent);
1231 if (options.do_dir_first == false && state.curdepth >= options.mindepth)
1233 /* The fields in 'state' are now out of date. Correct them.
1235 if (!digest_mode(mode, pathname, name, &stat_buf, leaf))
1236 return 0;
1238 if (0 == dir_curr)
1240 at_top(pathname, mode, &stat_buf, do_process_predicate);
1242 else
1244 do_process_predicate(pathname, name, mode, &stat_buf);
1248 dir_curr--;
1250 return 1;
1254 /* Scan directory PATHNAME and recurse through process_path for each entry.
1256 PATHLEN is the length of PATHNAME.
1258 NAME is PATHNAME relative to the current directory.
1260 STATP is the results of *options.xstat on it.
1262 PARENT is the path of the parent of NAME, relative to find's
1263 starting directory. */
1265 static void
1266 process_dir (char *pathname, char *name, int pathlen, const struct stat *statp, char *parent)
1268 int subdirs_left; /* Number of unexamined subdirs in PATHNAME. */
1269 boolean subdirs_unreliable; /* if true, cannot use dir link count as subdir limif (if false, it may STILL be unreliable) */
1270 unsigned int idx; /* Which entry are we on? */
1271 struct stat stat_buf;
1272 size_t dircount = 0u;
1273 struct savedir_dirinfo *dirinfo;
1274 #if 0
1275 printf("process_dir: pathname=%s name=%s statp->st_nlink=%d st_ino=%d\n",
1276 pathname,
1277 name,
1278 (int)statp->st_nlink,
1279 (int)statp->st_ino);
1280 #endif
1281 if (statp->st_nlink < 2)
1283 subdirs_unreliable = true;
1284 subdirs_left = 0;
1286 else
1288 subdirs_unreliable = false; /* not necessarily right */
1289 subdirs_left = statp->st_nlink - 2; /* Account for name and ".". */
1292 errno = 0;
1293 dirinfo = xsavedir(name, 0);
1296 if (dirinfo == NULL)
1298 assert (errno != 0);
1299 error (0, errno, "%s", safely_quote_err_filename(0, pathname));
1300 state.exit_status = 1;
1302 else
1304 register char *namep; /* Current point in `name_space'. */
1305 char *cur_path; /* Full path of each file to process. */
1306 char *cur_name; /* Base name of each file to process. */
1307 unsigned cur_path_size; /* Bytes allocated for `cur_path'. */
1308 register unsigned file_len; /* Length of each path to process. */
1309 register unsigned pathname_len; /* PATHLEN plus trailing '/'. */
1310 boolean did_stat = false;
1312 if (pathname[pathlen - 1] == '/')
1313 pathname_len = pathlen + 1; /* For '\0'; already have '/'. */
1314 else
1315 pathname_len = pathlen + 2; /* For '/' and '\0'. */
1316 cur_path_size = 0;
1317 cur_path = NULL;
1319 /* We're about to leave the directory. If there are any
1320 * -execdir argument lists which have been built but have not
1321 * yet been processed, do them now because they must be done in
1322 * the same directory.
1324 complete_pending_execdirs(get_current_dirfd());
1326 if (strcmp (name, "."))
1328 enum SafeChdirStatus status = safely_chdir (name, TraversingDown, &stat_buf, SymlinkHandleDefault, &did_stat);
1329 switch (status)
1331 case SafeChdirOK:
1332 /* If there had been a change but wd_sanity_check()
1333 * accepted it, we need to accept that on the
1334 * way back up as well, so modify our record
1335 * of what we think we should see later.
1336 * If there was no change, the assignments are a no-op.
1338 * However, before performing the assignment, we need to
1339 * check that we have the stat information. If O_NOFOLLOW
1340 * is available, safely_chdir() will not have needed to use
1341 * stat(), and so stat_buf will just contain random data.
1343 if (!did_stat)
1345 /* If there is a link we need to follow it. Hence
1346 * the direct call to stat() not through (options.xstat)
1348 set_stat_placeholders(&stat_buf);
1349 if (0 != stat(".", &stat_buf))
1350 break; /* skip the assignment. */
1352 dir_ids[dir_curr].dev = stat_buf.st_dev;
1353 dir_ids[dir_curr].ino = stat_buf.st_ino;
1355 break;
1357 case SafeChdirFailWouldBeUnableToReturn:
1358 error (0, errno, ".");
1359 state.exit_status = 1;
1360 break;
1362 case SafeChdirFailNonexistent:
1363 case SafeChdirFailDestUnreadable:
1364 case SafeChdirFailStat:
1365 case SafeChdirFailNotDir:
1366 case SafeChdirFailChdirFailed:
1367 error (0, errno, "%s",
1368 safely_quote_err_filename(0, pathname));
1369 state.exit_status = 1;
1370 return;
1372 case SafeChdirFailSymlink:
1373 error (0, 0,
1374 _("warning: not following the symbolic link %s"),
1375 safely_quote_err_filename(0, pathname));
1376 state.exit_status = 1;
1377 return;
1381 for (idx=0; idx < dirinfo->size; ++idx)
1383 /* savedirinfo() may return dirinfo=NULL if extended information
1384 * is not available.
1386 mode_t mode = (dirinfo->entries[idx].flags & SavedirHaveFileType) ?
1387 dirinfo->entries[idx].type_info : 0;
1388 namep = dirinfo->entries[idx].name;
1390 /* Append this directory entry's name to the path being searched. */
1391 file_len = pathname_len + strlen (namep);
1392 if (file_len > cur_path_size)
1394 while (file_len > cur_path_size)
1395 cur_path_size += 1024;
1396 if (cur_path)
1397 free (cur_path);
1398 cur_path = xmalloc (cur_path_size);
1399 strcpy (cur_path, pathname);
1400 cur_path[pathname_len - 2] = '/';
1402 cur_name = cur_path + pathname_len - 1;
1403 strcpy (cur_name, namep);
1405 state.curdepth++;
1406 if (!options.no_leaf_check && !subdirs_unreliable)
1408 if (mode && S_ISDIR(mode) && (subdirs_left == 0))
1410 /* This is a subdirectory, but the number of directories we
1411 * have found now exceeds the number we would expect given
1412 * the hard link count on the parent. This is likely to be
1413 * a bug in the file system driver (e.g. Linux's
1414 * /proc file system) or may just be a fact that the OS
1415 * doesn't really handle hard links with Unix semantics.
1416 * In the latter case, -noleaf should be used routinely.
1418 error(0, 0, _("WARNING: Hard link count is wrong for %s (saw only st_nlink=%d but we already saw %d subdirectories): this may be a bug in your file system driver. Automatically turning on find's -noleaf option. Earlier results may have failed to include directories that should have been searched."),
1419 safely_quote_err_filename(0, pathname),
1420 statp->st_nlink,
1421 dircount);
1422 state.exit_status = 1; /* We know the result is wrong, now */
1423 options.no_leaf_check = true; /* Don't make same
1424 mistake again */
1425 subdirs_unreliable = 1;
1426 subdirs_left = 1; /* band-aid for this iteration. */
1429 /* Normal case optimization. On normal Unix
1430 file systems, a directory that has no subdirectories
1431 has two links: its name, and ".". Any additional
1432 links are to the ".." entries of its subdirectories.
1433 Once we have processed as many subdirectories as
1434 there are additional links, we know that the rest of
1435 the entries are non-directories -- in other words,
1436 leaf files. */
1438 int count;
1439 count = process_path (cur_path, cur_name,
1440 subdirs_left == 0, pathname,
1441 mode);
1442 subdirs_left -= count;
1443 dircount += count;
1446 else
1448 /* There might be weird (e.g., CD-ROM or MS-DOS) file systems
1449 mounted, which don't have Unix-like directory link counts. */
1450 process_path (cur_path, cur_name, false, pathname, mode);
1453 state.curdepth--;
1457 /* We're about to leave the directory. If there are any
1458 * -execdir argument lists which have been built but have not
1459 * yet been processed, do them now because they must be done in
1460 * the same directory.
1462 complete_pending_execdirs(get_current_dirfd());
1464 if (strcmp (name, "."))
1466 enum SafeChdirStatus status;
1467 struct dir_id did;
1469 /* We could go back and do the next command-line arg
1470 instead, maybe using longjmp. */
1471 char const *dir;
1472 boolean deref = following_links() ? true : false;
1474 if ( (state.curdepth>0) && !deref)
1475 dir = "..";
1476 else
1478 chdir_back ();
1479 dir = parent;
1482 did_stat = false;
1483 status = safely_chdir (dir, TraversingUp, &stat_buf, SymlinkHandleDefault, &did_stat);
1484 switch (status)
1486 case SafeChdirOK:
1487 break;
1489 case SafeChdirFailWouldBeUnableToReturn:
1490 error (1, errno, ".");
1491 return;
1493 case SafeChdirFailNonexistent:
1494 case SafeChdirFailDestUnreadable:
1495 case SafeChdirFailStat:
1496 case SafeChdirFailSymlink:
1497 case SafeChdirFailNotDir:
1498 case SafeChdirFailChdirFailed:
1499 error (1, errno, "%s", safely_quote_err_filename(0, pathname));
1500 return;
1503 if (dir_curr > 0)
1505 did.dev = dir_ids[dir_curr-1].dev;
1506 did.ino = dir_ids[dir_curr-1].ino;
1508 else
1510 did.dev = starting_stat_buf.st_dev;
1511 did.ino = starting_stat_buf.st_ino;
1515 if (cur_path)
1516 free (cur_path);
1517 free_dirinfo(dirinfo);
1520 if (subdirs_unreliable)
1522 /* Make sure we hasn't used the variable subdirs_left if we knew
1523 * we shouldn't do so.
1525 assert (0 == subdirs_left || options.no_leaf_check);