1 /* find -- search for files in a directory hierarchy (fts version)
2 Copyright (C) 1990, 1091, 1992, 1993, 1994, 2000, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010, 2011 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/>.
19 /* This file was written by James Youngman, based on find.c.
21 GNU find was written by Eric Decker <cire@soe.ucsc.edu>,
22 with enhancements by David MacKenzie <djm@gnu.org>,
23 Jay Plett <jay@silence.princeton.nj.us>,
24 and Tim Wood <axolotl!tim@toad.com>.
25 The idea for -print0 and xargs -0 came from
26 Dan Bernstein <brnstnd@kramden.acf.nyu.edu>.
29 /* config.h must always be included first. */
55 #include "dircallback.h"
57 #include "unused-result.h"
59 #define USE_SAFE_CHDIR 1
60 #undef STAT_MOUNTPOINTS
65 # define _(Text) gettext (Text)
68 #define textdomain(Domain)
69 #define bindtextdomain(Package, Directory)
72 # define N_(String) gettext_noop (String)
74 /* See locate.c for explanation as to why not use (String) */
75 # define N_(String) String
79 /* FTS_TIGHT_CYCLE_CHECK tries to work around Savannah bug #17877
80 * (but actually using it doesn't fix the bug).
82 static int ftsoptions = FTS_NOSTAT|FTS_TIGHT_CYCLE_CHECK|FTS_CWDFD|FTS_VERBATIM;
84 static int prev_depth = INT_MIN; /* fts_level can be < 0 */
85 static int curr_fd = -1;
88 static bool find (char *arg) __attribute_warn_unused_result__;
89 static bool process_all_startpoints (int argc, char *argv[]) __attribute_warn_unused_result__;
96 if (ftsoptions & FTS_CWDFD)
111 * Signal that we are now inside a directory pointed to by dir_fd.
112 * The caller can't tell if this is the first time this happens, so
113 * we have to be careful not to call dup() more than once
116 inside_dir (int dir_fd)
118 if (ftsoptions & FTS_CWDFD)
120 assert (dir_fd == AT_FDCWD || dir_fd >= 0);
122 state.cwd_dir_fd = dir_fd;
125 if (AT_FDCWD == dir_fd)
129 else if (dir_fd >= 0)
131 curr_fd = dup_cloexec (dir_fd);
135 /* curr_fd is invalid, but dir_fd is also invalid.
136 * This should not have happened.
138 assert (curr_fd >= 0 || dir_fd >= 0);
144 /* FTS_CWDFD is not in use. We can always assume that
145 * AT_FDCWD refers to the directory we are currentl searching.
147 * Therefore there is nothing to do.
154 #ifdef STAT_MOUNTPOINTS
155 static void init_mounted_dev_list (void);
158 #define STRINGIFY(X) #X
159 #define HANDLECASE(N) case N: return #N;
162 get_fts_info_name (int info)
169 HANDLECASE(FTS_DEFAULT);
175 HANDLECASE(FTS_INIT);
177 HANDLECASE(FTS_NSOK);
179 HANDLECASE(FTS_SLNONE);
182 sprintf (buf, "[%d]", info);
188 visit (FTS *p, FTSENT *ent, struct stat *pstat)
190 struct predicate *eval_tree;
192 state.have_stat = (ent->fts_info != FTS_NS) && (ent->fts_info != FTS_NSOK);
193 state.rel_pathname = ent->fts_accpath;
194 state.cwd_dir_fd = p->fts_cwd_fd;
196 /* Apply the predicates to this path. */
197 eval_tree = get_eval_tree ();
198 apply_predicate (ent->fts_path, pstat, eval_tree);
200 /* Deal with any side effects of applying the predicates. */
201 if (state.stop_at_current_level)
203 fts_set (p, ent, FTS_SKIP);
208 partial_quotearg_n (int n, char *s, size_t len, enum quoting_style style)
212 return quotearg_n_style (n, style, "");
221 result = quotearg_n_style (n, style, s);
228 /* We've detected a file system loop. This is caused by one of
231 * 1. Option -L is in effect and we've hit a symbolic link that
232 * points to an ancestor. This is harmless. We won't traverse the
235 * 2. We have hit a real cycle in the directory hierarchy. In this
236 * case, we issue a diagnostic message (POSIX requires this) and we
237 * skip that directory entry.
240 issue_loop_warning (FTSENT * ent)
242 if (S_ISLNK(ent->fts_statp->st_mode))
245 _("Symbolic link %s is part of a loop in the directory hierarchy; we have already visited the directory to which it points."),
246 safely_quote_err_filename (0, ent->fts_path));
250 /* We have found an infinite loop. POSIX requires us to
251 * issue a diagnostic. Usually we won't get to here
252 * because when the leaf optimisation is on, it will cause
253 * the subdirectory to be skipped. If /a/b/c/d is a hard
254 * link to /a/b, then the link count of /a/b/c is 2,
255 * because the ".." entry of /a/b/c/d points to /a, not
259 _("File system loop detected; "
260 "%s is part of the same file system loop as %s."),
261 safely_quote_err_filename (0, ent->fts_path),
262 partial_quotearg_n (1,
263 ent->fts_cycle->fts_path,
264 ent->fts_cycle->fts_pathlen,
265 options.err_quoting_style));
270 * Return true if NAME corresponds to a file which forms part of a
271 * symbolic link loop. The command
272 * rm -f a b; ln -s a b; ln -s b a
273 * produces such a loop.
276 symlink_loop (const char *name)
279 const int rv = options.xstat (name, &stbuf);
280 return (0 != rv) && (ELOOP == errno);
285 show_outstanding_execdirs (FILE *fp)
287 if (options.debug_options & DebugExec)
291 p = get_eval_tree ();
292 fprintf (fp, "Outstanding execdirs:");
298 if (pred_is (p, pred_execdir))
300 else if (pred_is (p, pred_okdir))
307 const struct exec_val *execp = &p->args.exec_vec;
310 fprintf (fp, "%s ", pfx);
312 fprintf (fp, "multiple ");
313 fprintf (fp, "%" PRIuMAX " args: ", (uintmax_t) execp->state.cmd_argc);
314 for (i=0; i<execp->state.cmd_argc; ++i)
316 fprintf (fp, "%s ", execp->state.cmd_argv[i]);
323 fprintf (fp, " none\n");
327 /* No debug output is wanted. */
332 consider_visiting (FTS *p, FTSENT *ent)
338 if (options.debug_options & DebugSearch)
340 "consider_visiting (early): %s: "
341 "fts_info=%-6s, fts_level=%2d, prev_depth=%d "
342 "fts_path=%s, fts_accpath=%s\n",
343 quotearg_n_style (0, options.err_quoting_style, ent->fts_path),
344 get_fts_info_name (ent->fts_info),
345 (int)ent->fts_level, prev_depth,
346 quotearg_n_style (1, options.err_quoting_style, ent->fts_path),
347 quotearg_n_style (2, options.err_quoting_style, ent->fts_accpath));
349 if (ent->fts_info == FTS_DP)
353 else if (ent->fts_level > prev_depth || ent->fts_level==0)
357 inside_dir (p->fts_cwd_fd);
358 prev_depth = ent->fts_level;
360 statbuf.st_ino = ent->fts_statp->st_ino;
362 /* Cope with various error conditions. */
363 if (ent->fts_info == FTS_ERR
364 || ent->fts_info == FTS_DNR)
366 nonfatal_target_file_error (ent->fts_errno, ent->fts_path);
369 else if (ent->fts_info == FTS_DC)
371 issue_loop_warning (ent);
372 error_severity (EXIT_FAILURE);
375 else if (ent->fts_info == FTS_SLNONE)
377 /* fts_read() claims that ent->fts_accpath is a broken symbolic
378 * link. That would be fine, but if this is part of a symbolic
379 * link loop, we diagnose the problem and also ensure that the
380 * eventual return value is nonzero. Note that while the path
381 * we stat is local (fts_accpath), we print the full path name
382 * of the file (fts_path) in the error message.
384 if (symlink_loop (ent->fts_accpath))
386 nonfatal_target_file_error (ELOOP, ent->fts_path);
390 else if (ent->fts_info == FTS_NS)
392 if (ent->fts_level == 0)
394 /* e.g., nonexistent starting point */
395 nonfatal_target_file_error (ent->fts_errno, ent->fts_path);
400 /* The following if statement fixes Savannah bug #19605
401 * (failure to diagnose a symbolic link loop)
403 if (symlink_loop (ent->fts_accpath))
405 nonfatal_target_file_error (ELOOP, ent->fts_path);
410 nonfatal_target_file_error (ent->fts_errno, ent->fts_path);
411 /* Continue despite the error, as file name without stat info
412 * might be better than not even processing the file name. This
413 * can lead to repeated error messages later on, though, if a
414 * predicate requires stat information.
416 * Not printing an error message here would be even more wrong,
417 * though, as this could cause the contents of a directory to be
418 * silently ignored, as the directory wouldn't be identified as
426 /* Cope with the usual cases. */
427 if (ent->fts_info == FTS_NSOK
428 || ent->fts_info == FTS_NS /* e.g. symlink loop */)
430 assert (!state.have_stat);
431 assert (ent->fts_info == FTS_NSOK || state.type == 0);
436 state.have_stat = true;
437 state.have_type = true;
438 statbuf = *(ent->fts_statp);
439 state.type = mode = statbuf.st_mode;
443 /* Savannah bug #16378. */
444 error (0, 0, _("WARNING: file %s appears to have mode 0000"),
445 quotearg_n_style (0, options.err_quoting_style, ent->fts_path));
449 /* update state.curdepth before calling digest_mode(), because digest_mode
450 * may call following_links().
452 state.curdepth = ent->fts_level;
455 if (!digest_mode (&mode, ent->fts_path, ent->fts_name, &statbuf, 0))
459 /* examine this item. */
461 isdir = S_ISDIR(mode)
462 || (FTS_D == ent->fts_info)
463 || (FTS_DP == ent->fts_info)
464 || (FTS_DC == ent->fts_info);
466 if (isdir && (ent->fts_info == FTS_NSOK))
468 /* This is a directory, but fts did not stat it, so
469 * presumably would not be planning to search its
470 * children. Force a stat of the file so that the
471 * children can be checked.
473 fts_set (p, ent, FTS_AGAIN);
477 if (options.maxdepth >= 0)
479 if (ent->fts_level >= options.maxdepth)
481 fts_set (p, ent, FTS_SKIP); /* descend no further */
483 if (ent->fts_level > options.maxdepth)
484 ignore = 1; /* don't even look at this one */
488 if ( (ent->fts_info == FTS_D) && !options.do_dir_first )
490 /* this is the preorder visit, but user said -depth */
493 else if ( (ent->fts_info == FTS_DP) && options.do_dir_first )
495 /* this is the postorder visit, but user didn't say -depth */
498 else if (ent->fts_level < options.mindepth)
503 if (options.debug_options & DebugSearch)
505 "consider_visiting (late): %s: "
506 "fts_info=%-6s, isdir=%d ignore=%d have_stat=%d have_type=%d \n",
507 quotearg_n_style (0, options.err_quoting_style, ent->fts_path),
508 get_fts_info_name (ent->fts_info),
509 isdir, ignore, state.have_stat, state.have_type);
513 visit (p, ent, &statbuf);
516 if (ent->fts_info == FTS_DP)
518 /* we're leaving a directory. */
519 state.stop_at_current_level = false;
532 state.starting_path_length = strlen (arg);
533 inside_dir (AT_FDCWD);
538 switch (options.symlink_handling)
540 case SYMLINK_ALWAYS_DEREF:
541 ftsoptions |= FTS_COMFOLLOW|FTS_LOGICAL;
544 case SYMLINK_DEREF_ARGSONLY:
545 ftsoptions |= FTS_COMFOLLOW|FTS_PHYSICAL;
548 case SYMLINK_NEVER_DEREF:
549 ftsoptions |= FTS_PHYSICAL;
553 if (options.stay_on_filesystem)
554 ftsoptions |= FTS_XDEV;
556 p = fts_open (arglist, ftsoptions, NULL);
559 error (0, errno, _("cannot search %s"),
560 safely_quote_err_filename (0, arg));
561 error_severity (EXIT_FAILURE);
567 while ( (errno=0, ent=fts_read (p)) != NULL )
569 if (state.execdirs_outstanding)
571 /* If we changed level, perform any outstanding
572 * execdirs. If we see a sequence of directory entries
573 * like this: fffdfffdfff, we could build a command line
574 * of 9 files, but this simple-minded implementation
575 * builds a command line for only 3 files at a time
576 * (since fts descends into the directories).
578 if ((int)ent->fts_level != level)
580 show_outstanding_execdirs (stderr);
581 complete_pending_execdirs ();
584 level = (int)ent->fts_level;
586 state.already_issued_stat_error_msg = false;
587 state.have_stat = false;
588 state.have_type = !!ent->fts_statp->st_mode;
589 state.type = state.have_type ? ent->fts_statp->st_mode : 0;
590 consider_visiting (p, ent);
592 /* fts_read returned NULL; distinguish between "finished" and "error". */
596 "failed to read file names from file system at or below %s",
597 safely_quote_err_filename (0, arg));
598 error_severity (EXIT_FAILURE);
602 if (0 != fts_close (p))
604 /* Here we break the abstraction of fts_close a bit, because we
605 * are going to skip the rest of the start points, and return with
606 * nonzero exit status. Hence we need to issue a diagnostic on
609 _("failed to restore working directory after searching %s"),
611 error_severity (EXIT_FAILURE);
621 process_all_startpoints (int argc, char *argv[])
625 /* figure out how many start points there are */
626 for (i = 0; i < argc && !looks_like_expression (argv[i], true); i++)
628 state.starting_path_length = strlen (argv[i]); /* TODO: is this redundant? */
636 * We use a temporary variable here because some actions modify
637 * the path temporarily. Hence if we use a string constant,
638 * we get a coredump. The best example of this is if we say
639 * "find -printf %H" (note, not "find . -printf %H").
641 char defaultpath[2] = ".";
642 return find (defaultpath);
651 main (int argc, char **argv)
653 int end_of_leading_options = 0; /* First arg after any -H/-L etc. */
654 struct predicate *eval_tree;
657 set_program_name (argv[0]);
659 set_program_name ("find");
661 record_initial_cwd ();
663 state.already_issued_stat_error_msg = false;
664 state.exit_status = 0;
665 state.execdirs_outstanding = false;
666 state.cwd_dir_fd = AT_FDCWD;
668 if (fd_leak_check_is_enabled ())
670 remember_non_cloexec_fds ();
673 state.shared_files = sharefile_init ("w");
674 if (NULL == state.shared_files)
676 error (EXIT_FAILURE, errno,
677 _("Failed initialize shared-file hash table"));
680 /* Set the option defaults before we do the locale initialisation as
681 * check_nofollow() needs to be executed in the POSIX locale.
683 set_option_defaults (&options);
685 #ifdef HAVE_SETLOCALE
686 setlocale (LC_ALL, "");
689 bindtextdomain (PACKAGE, LOCALEDIR);
690 textdomain (PACKAGE);
691 if (atexit (close_stdout))
693 error (EXIT_FAILURE, errno, _("The atexit library function failed"));
696 /* Check for -P, -H or -L options. Also -D and -O, which are
697 * both GNU extensions.
699 end_of_leading_options = process_leading_options (argc, argv);
701 if (options.debug_options & DebugStat)
702 options.xstat = debug_stat;
705 fprintf (stderr, "cur_day_start = %s", ctime (&options.cur_day_start));
709 /* We are now processing the part of the "find" command line
710 * after the -H/-L options (if any).
712 eval_tree = build_expression_tree (argc, argv, end_of_leading_options);
714 /* safely_chdir() needs to check that it has ended up in the right place.
715 * To avoid bailing out when something gets automounted, it checks if
716 * the target directory appears to have had a directory mounted on it as
717 * we chdir()ed. The problem with this is that in order to notice that
718 * a file system was mounted, we would need to lstat() all the mount points.
719 * That strategy loses if our machine is a client of a dead NFS server.
721 * Hence if safely_chdir() and wd_sanity_check() can manage without needing
722 * to know the mounted device list, we do that.
724 if (!options.open_nofollow_available)
726 #ifdef STAT_MOUNTPOINTS
727 init_mounted_dev_list ();
732 /* process_all_startpoints processes the starting points named on
733 * the command line. A false return value from it means that we
734 * failed to restore the original context. That means it would not
735 * be safe to call cleanup() since we might complete an execdir in
736 * the wrong directory for example.
738 if (process_all_startpoints (argc-end_of_leading_options,
739 argv+end_of_leading_options))
741 /* If "-exec ... {} +" has been used, there may be some
742 * partially-full command lines which have been built,
743 * but which are not yet complete. Execute those now.
745 show_success_rates (eval_tree);
748 return state.exit_status;
752 is_fts_enabled (int *fts_options)
754 /* this version of find (i.e. this main()) uses fts. */
755 *fts_options = ftsoptions;