find/tree.c (get_pred_cost): Eliminate unused variable
[findutils.git] / find / ftsfind.c
blobcce7e3381ebf15f6c6a772489d1e2678e067c27e
1 /* find -- search for files in a directory hierarchy (fts version)
2 Copyright (C) 1990, 91, 92, 93, 94, 2000,
3 2003, 2004, 2005, 2006, 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 2, or (at your option)
8 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, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18 USA.*/
20 /* This file was written by James Youngman, based on find.c.
22 GNU find was written by Eric Decker <cire@cisco.com>,
23 with enhancements by David MacKenzie <djm@gnu.org>,
24 Jay Plett <jay@silence.princeton.nj.us>,
25 and Tim Wood <axolotl!tim@toad.com>.
26 The idea for -print0 and xargs -0 came from
27 Dan Bernstein <brnstnd@kramden.acf.nyu.edu>.
31 #include <config.h>
32 #include "defs.h"
35 #define USE_SAFE_CHDIR 1
36 #undef STAT_MOUNTPOINTS
39 #include <errno.h>
40 #include <assert.h>
42 #ifdef HAVE_FCNTL_H
43 #include <fcntl.h>
44 #else
45 #include <sys/file.h>
46 #endif
47 #include <sys/stat.h>
49 #include <unistd.h>
51 #include "xalloc.h"
52 #include "closeout.h"
53 #include <modetype.h>
54 #include "quotearg.h"
55 #include "quote.h"
56 #include "fts_.h"
57 #include "openat.h"
58 #include "save-cwd.h"
59 #include "xgetcwd.h"
60 #include "error.h"
61 #include "dircallback.h"
63 #ifdef HAVE_LOCALE_H
64 #include <locale.h>
65 #endif
67 #if ENABLE_NLS
68 # include <libintl.h>
69 # define _(Text) gettext (Text)
70 #else
71 # define _(Text) Text
72 #define textdomain(Domain)
73 #define bindtextdomain(Package, Directory)
74 #endif
75 #ifdef gettext_noop
76 # define N_(String) gettext_noop (String)
77 #else
78 /* See locate.c for explanation as to why not use (String) */
79 # define N_(String) String
80 #endif
83 static void set_close_on_exec(int fd)
85 #if defined F_GETFD && defined FD_CLOEXEC
86 int flags;
87 flags = fcntl(fd, F_GETFD);
88 if (flags >= 0)
90 flags |= FD_CLOEXEC;
91 fcntl(fd, F_SETFD, flags);
93 #endif
98 /* FTS_TIGHT_CYCLE_CHECK tries to work around Savannah bug #17877
99 * (but actually using it doesn't fix the bug).
101 static int ftsoptions = FTS_NOSTAT|FTS_TIGHT_CYCLE_CHECK;
103 static int prev_depth = INT_MIN; /* fts_level can be < 0 */
104 static int curr_fd = -1;
106 int get_current_dirfd(void)
108 if (ftsoptions & FTS_CWDFD)
110 assert (curr_fd != -1);
111 assert ( (AT_FDCWD == curr_fd) || (curr_fd >= 0) );
113 if (AT_FDCWD == curr_fd)
114 return starting_desc;
115 else
116 return curr_fd;
118 else
120 return AT_FDCWD;
124 static void left_dir(void)
126 if (ftsoptions & FTS_CWDFD)
128 if (curr_fd >= 0)
130 close(curr_fd);
131 curr_fd = -1;
134 else
136 /* do nothing. */
141 * Signal that we are now inside a directory pointed to by dirfd.
142 * The caller can't tell if this is the first time this happens, so
143 * we have to be careful not to call dup() more than once
145 static void inside_dir(int dirfd)
147 if (ftsoptions & FTS_CWDFD)
149 assert (dirfd == AT_FDCWD || dirfd >= 0);
151 state.cwd_dir_fd = dirfd;
152 if (curr_fd < 0)
154 if (AT_FDCWD == dirfd)
156 curr_fd = AT_FDCWD;
158 else if (dirfd >= 0)
160 curr_fd = dup(dirfd);
161 set_close_on_exec(curr_fd);
163 else
165 /* curr_fd is invalid, but dirfd is also invalid.
166 * This should not have happened.
168 assert (curr_fd >= 0 || dirfd >= 0);
172 else
174 /* FTS_CWDFD is not in use. We can always assume that
175 * AT_FDCWD refers to the directory we are currentl searching.
177 * Therefore there is nothing to do.
184 #ifdef STAT_MOUNTPOINTS
185 static void init_mounted_dev_list(void);
186 #endif
188 /* We have encountered an error which should affect the exit status.
189 * This is normally used to change the exit status from 0 to 1.
190 * However, if the exit status is already 2 for example, we don't want to
191 * reduce it to 1.
193 static void
194 error_severity(int level)
196 if (state.exit_status < level)
197 state.exit_status = level;
201 #define STRINGIFY(X) #X
202 #define HANDLECASE(N) case N: return #N;
204 static char *
205 get_fts_info_name(int info)
207 static char buf[10];
208 switch (info)
210 HANDLECASE(FTS_D);
211 HANDLECASE(FTS_DC);
212 HANDLECASE(FTS_DEFAULT);
213 HANDLECASE(FTS_DNR);
214 HANDLECASE(FTS_DOT);
215 HANDLECASE(FTS_DP);
216 HANDLECASE(FTS_ERR);
217 HANDLECASE(FTS_F);
218 HANDLECASE(FTS_INIT);
219 HANDLECASE(FTS_NS);
220 HANDLECASE(FTS_NSOK);
221 HANDLECASE(FTS_SL);
222 HANDLECASE(FTS_SLNONE);
223 HANDLECASE(FTS_W);
224 default:
225 sprintf(buf, "[%d]", info);
226 return buf;
230 static void
231 visit(FTS *p, FTSENT *ent, struct stat *pstat)
233 struct predicate *eval_tree;
235 state.curdepth = ent->fts_level;
236 state.have_stat = (ent->fts_info != FTS_NS) && (ent->fts_info != FTS_NSOK);
237 state.rel_pathname = ent->fts_accpath;
238 state.cwd_dir_fd = p->fts_cwd_fd;
240 /* Apply the predicates to this path. */
241 eval_tree = get_eval_tree();
242 apply_predicate(ent->fts_path, pstat, eval_tree);
244 /* Deal with any side effects of applying the predicates. */
245 if (state.stop_at_current_level)
247 fts_set(p, ent, FTS_SKIP);
251 static const char*
252 partial_quotearg_n(int n, char *s, size_t len, enum quoting_style style)
254 if (0 == len)
256 return quotearg_n_style(n, style, "");
258 else
260 char saved;
261 const char *result;
263 saved = s[len];
264 s[len] = 0;
265 result = quotearg_n_style(n, style, s);
266 s[len] = saved;
267 return result;
272 /* We've detected a file system loop. This is caused by one of
273 * two things:
275 * 1. Option -L is in effect and we've hit a symbolic link that
276 * points to an ancestor. This is harmless. We won't traverse the
277 * symbolic link.
279 * 2. We have hit a real cycle in the directory hierarchy. In this
280 * case, we issue a diagnostic message (POSIX requires this) and we
281 * skip that directory entry.
283 static void
284 issue_loop_warning(FTSENT * ent)
286 if (S_ISLNK(ent->fts_statp->st_mode))
288 error(0, 0,
289 _("Symbolic link %s is part of a loop in the directory hierarchy; we have already visited the directory to which it points."),
290 safely_quote_err_filename(0, ent->fts_path));
292 else
294 /* We have found an infinite loop. POSIX requires us to
295 * issue a diagnostic. Usually we won't get to here
296 * because when the leaf optimisation is on, it will cause
297 * the subdirectory to be skipped. If /a/b/c/d is a hard
298 * link to /a/b, then the link count of /a/b/c is 2,
299 * because the ".." entry of /a/b/c/d points to /a, not
300 * to /a/b/c.
302 error(0, 0,
303 _("File system loop detected; "
304 "%s is part of the same file system loop as %s."),
305 safely_quote_err_filename(0, ent->fts_path),
306 partial_quotearg_n(1,
307 ent->fts_cycle->fts_path,
308 ent->fts_cycle->fts_pathlen,
309 options.err_quoting_style));
314 * Return true if NAME corresponds to a file which forms part of a
315 * symbolic link loop. The command
316 * rm -f a b; ln -s a b; ln -s b a
317 * produces such a loop.
319 static boolean
320 symlink_loop(const char *name)
322 struct stat stbuf;
323 int rv;
324 if (following_links())
325 rv = stat(name, &stbuf);
326 else
327 rv = lstat(name, &stbuf);
328 return (0 != rv) && (ELOOP == errno);
332 static int
333 complete_execdirs_cb(void *context)
335 (void) context;
336 /* By the tme this callback is called, the current directory is correct. */
337 complete_pending_execdirs(AT_FDCWD);
338 return 0;
341 static void
342 show_outstanding_execdirs(FILE *fp)
344 if (options.debug_options & DebugExec)
346 int seen=0;
347 struct predicate *p;
348 p = get_eval_tree();
349 fprintf(fp, "Outstanding execdirs:");
351 while (p)
353 const char *pfx;
355 if (pred_is(p, pred_execdir))
356 pfx = "-execdir";
357 else if (pred_is(p, pred_okdir))
358 pfx = "-okdir";
359 else
360 pfx = NULL;
361 if (pfx)
363 int i;
364 const struct exec_val *execp = &p->args.exec_vec;
365 ++seen;
367 fprintf(fp, "%s ", pfx);
368 if (execp->multiple)
369 fprintf(fp, "multiple ");
370 fprintf(fp, "%d args: ", execp->state.cmd_argc);
371 for (i=0; i<execp->state.cmd_argc; ++i)
373 fprintf(fp, "%s ", execp->state.cmd_argv[i]);
375 fprintf(fp, "\n");
377 p = p->pred_next;
379 if (!seen)
380 fprintf(fp, " none\n");
382 else
384 /* No debug output is wanted. */
391 static void
392 consider_visiting(FTS *p, FTSENT *ent)
394 struct stat statbuf;
395 mode_t mode;
396 int ignore, isdir;
398 if (options.debug_options & DebugSearch)
399 fprintf(stderr,
400 "consider_visiting: fts_info=%-6s, fts_level=%2d, prev_depth=%d "
401 "fts_path=%s, fts_accpath=%s\n",
402 get_fts_info_name(ent->fts_info),
403 (int)ent->fts_level, prev_depth,
404 quotearg_n_style(0, options.err_quoting_style, ent->fts_path),
405 quotearg_n_style(1, options.err_quoting_style, ent->fts_accpath));
407 if (ent->fts_info == FTS_DP)
409 left_dir();
411 else if (ent->fts_level > prev_depth || ent->fts_level==0)
413 left_dir();
415 inside_dir(p->fts_cwd_fd);
416 prev_depth = ent->fts_level;
419 /* Cope with various error conditions. */
420 if (ent->fts_info == FTS_ERR
421 || ent->fts_info == FTS_DNR)
423 error(0, ent->fts_errno, "%s",
424 safely_quote_err_filename(0, ent->fts_path));
425 error_severity(1);
426 return;
428 else if (ent->fts_info == FTS_DC)
430 issue_loop_warning(ent);
431 error_severity(1);
432 return;
434 else if (ent->fts_info == FTS_SLNONE)
436 /* fts_read() claims that ent->fts_accpath is a broken symbolic
437 * link. That would be fine, but if this is part of a symbolic
438 * link loop, we diagnose the problem and also ensure that the
439 * eventual return value is nonzero. Note that while the path
440 * we stat is local (fts_accpath), we print the fill path name
441 * of the file (fts_path) in the error message.
443 if (symlink_loop(ent->fts_accpath))
445 error(0, ELOOP, "%s", safely_quote_err_filename(0, ent->fts_path));
446 error_severity(1);
447 return;
450 else if (ent->fts_info == FTS_NS)
452 if (ent->fts_level == 0)
454 /* e.g., nonexistent starting point */
455 error(0, ent->fts_errno, "%s",
456 safely_quote_err_filename(0, ent->fts_path));
457 error_severity(1); /* remember problem */
458 return;
460 else
462 /* The following if statement fixes Savannah bug #19605
463 * (failure to diagnose a symbolic link loop)
465 if (symlink_loop(ent->fts_accpath))
467 error(0, ELOOP, "%s",
468 safely_quote_err_filename(0, ent->fts_path));
469 error_severity(1);
470 return;
475 /* Cope with the usual cases. */
476 if (ent->fts_info == FTS_NSOK
477 || ent->fts_info == FTS_NS /* e.g. symlink loop */)
479 assert (!state.have_stat);
480 assert (!state.have_type);
481 state.type = mode = 0;
483 else
485 state.have_stat = true;
486 state.have_type = true;
487 statbuf = *(ent->fts_statp);
488 state.type = mode = statbuf.st_mode;
490 if (00000 == mode)
492 /* Savannah bug #16378. */
493 error(0, 0, _("Warning: file %s appears to have mode 0000"),
494 quotearg_n_style(0, options.err_quoting_style, ent->fts_path));
498 if (mode)
500 if (!digest_mode(mode, ent->fts_path, ent->fts_name, &statbuf, 0))
501 return;
504 /* examine this item. */
505 ignore = 0;
506 isdir = S_ISDIR(statbuf.st_mode)
507 || (FTS_D == ent->fts_info)
508 || (FTS_DP == ent->fts_info)
509 || (FTS_DC == ent->fts_info);
511 if (isdir && (ent->fts_info == FTS_NSOK))
513 /* This is a directory, but fts did not stat it, so
514 * presumably would not be planning to search its
515 * children. Force a stat of the file so that the
516 * children can be checked.
518 fts_set(p, ent, FTS_AGAIN);
519 return;
522 if (options.maxdepth >= 0)
524 if (ent->fts_level >= options.maxdepth)
526 fts_set(p, ent, FTS_SKIP); /* descend no further */
528 if (ent->fts_level > options.maxdepth)
529 ignore = 1; /* don't even look at this one */
533 if ( (ent->fts_info == FTS_D) && !options.do_dir_first )
535 /* this is the preorder visit, but user said -depth */
536 ignore = 1;
538 else if ( (ent->fts_info == FTS_DP) && options.do_dir_first )
540 /* this is the postorder visit, but user didn't say -depth */
541 ignore = 1;
543 else if (ent->fts_level < options.mindepth)
545 ignore = 1;
548 if (!ignore)
550 visit(p, ent, &statbuf);
553 /* XXX: if we allow a build-up of pending arguments for "-execdir foo {} +"
554 * we need to execute them in the same directory as we found the item.
555 * If we are trying to do "find a -execdir echo {} +", we will need to
556 * echo
557 * a while in the original working directory
558 * b while in a
559 * c while in b (just before leaving b)
561 * These restrictions are hard to satisfy while using fts(). The reason is
562 * that it doesn't tell us just before we leave a directory. For the moment,
563 * we punt and don't allow the arguments to build up.
565 if (state.execdirs_outstanding)
567 show_outstanding_execdirs(stderr);
568 run_in_dir(p->fts_cwd_fd, complete_execdirs_cb, NULL);
571 if (ent->fts_info == FTS_DP)
573 /* we're leaving a directory. */
574 state.stop_at_current_level = false;
580 static void
581 find(char *arg)
583 char * arglist[2];
584 FTS *p;
585 FTSENT *ent;
588 state.starting_path_length = strlen(arg);
589 inside_dir(AT_FDCWD);
591 arglist[0] = arg;
592 arglist[1] = NULL;
594 switch (options.symlink_handling)
596 case SYMLINK_ALWAYS_DEREF:
597 ftsoptions |= FTS_COMFOLLOW|FTS_LOGICAL;
598 break;
600 case SYMLINK_DEREF_ARGSONLY:
601 ftsoptions |= FTS_COMFOLLOW|FTS_PHYSICAL;
602 break;
604 case SYMLINK_NEVER_DEREF:
605 ftsoptions |= FTS_PHYSICAL;
606 break;
609 if (options.stay_on_filesystem)
610 ftsoptions |= FTS_XDEV;
612 p = fts_open(arglist, ftsoptions, NULL);
613 if (NULL == p)
615 error (0, errno, _("cannot search %s"),
616 safely_quote_err_filename(0, arg));
618 else
620 while ( (ent=fts_read(p)) != NULL )
622 state.have_stat = false;
623 state.have_type = false;
624 state.type = 0;
625 consider_visiting(p, ent);
627 fts_close(p);
628 p = NULL;
633 static void
634 process_all_startpoints(int argc, char *argv[])
636 int i;
638 /* figure out how many start points there are */
639 for (i = 0; i < argc && !looks_like_expression(argv[i], true); i++)
641 state.starting_path_length = strlen(argv[i]); /* TODO: is this redundant? */
642 find(argv[i]);
645 if (i == 0)
648 * We use a temporary variable here because some actions modify
649 * the path temporarily. Hence if we use a string constant,
650 * we get a coredump. The best example of this is if we say
651 * "find -printf %H" (note, not "find . -printf %H").
653 char defaultpath[2] = ".";
654 find(defaultpath);
662 main (int argc, char **argv)
664 int end_of_leading_options = 0; /* First arg after any -H/-L etc. */
665 struct predicate *eval_tree;
667 program_name = argv[0];
668 state.exit_status = 0;
669 state.execdirs_outstanding = false;
670 state.cwd_dir_fd = AT_FDCWD;
672 /* Set the option defaults before we do the locale initialisation as
673 * check_nofollow() needs to be executed in the POSIX locale.
675 set_option_defaults(&options);
677 #ifdef HAVE_SETLOCALE
678 setlocale (LC_ALL, "");
679 #endif
681 bindtextdomain (PACKAGE, LOCALEDIR);
682 textdomain (PACKAGE);
683 atexit (close_stdout);
685 /* Check for -P, -H or -L options. Also -D and -O, which are
686 * both GNU extensions.
688 end_of_leading_options = process_leading_options(argc, argv);
690 if (options.debug_options & DebugStat)
691 options.xstat = debug_stat;
693 #ifdef DEBUG
694 fprintf (stderr, "cur_day_start = %s", ctime (&options.cur_day_start));
695 #endif /* DEBUG */
698 /* We are now processing the part of the "find" command line
699 * after the -H/-L options (if any).
701 eval_tree = build_expression_tree(argc, argv, end_of_leading_options);
703 /* safely_chdir() needs to check that it has ended up in the right place.
704 * To avoid bailing out when something gets automounted, it checks if
705 * the target directory appears to have had a directory mounted on it as
706 * we chdir()ed. The problem with this is that in order to notice that
707 * a file system was mounted, we would need to lstat() all the mount points.
708 * That strategy loses if our machine is a client of a dead NFS server.
710 * Hence if safely_chdir() and wd_sanity_check() can manage without needing
711 * to know the mounted device list, we do that.
713 if (!options.open_nofollow_available)
715 #ifdef STAT_MOUNTPOINTS
716 init_mounted_dev_list();
717 #endif
721 starting_desc = open (".", O_RDONLY
722 #if defined O_LARGEFILE
723 |O_LARGEFILE
724 #endif
726 if (0 <= starting_desc && fchdir (starting_desc) != 0)
728 close (starting_desc);
729 starting_desc = -1;
731 if (starting_desc < 0)
733 starting_dir = xgetcwd ();
734 if (! starting_dir)
735 error (1, errno, _("cannot get current directory"));
739 process_all_startpoints(argc-end_of_leading_options, 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);
746 cleanup();
747 return state.exit_status;
750 boolean
751 is_fts_enabled(int *fts_options)
753 /* this version of find (i.e. this main()) uses fts. */
754 *fts_options = ftsoptions;
755 return true;