did make dist
[findutils.git] / find / ftsfind.c
blobd66ec9124f231fd4c5ab87b12ffbffe6224387c9
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 "defs.h"
34 #define USE_SAFE_CHDIR 1
35 #undef STAT_MOUNTPOINTS
38 #include <errno.h>
39 #include <assert.h>
41 #ifdef HAVE_FCNTL_H
42 #include <fcntl.h>
43 #else
44 #include <sys/file.h>
45 #endif
47 #include <unistd.h>
48 #if defined(HAVE_FCNTL_H)
49 #include <fcntl.h>
50 #endif
52 #include "../gnulib/lib/xalloc.h"
53 #include "closeout.h"
54 #include <modetype.h>
55 #include "quotearg.h"
56 #include "quote.h"
57 #include "fts_.h"
58 #include "openat.h"
59 #include "save-cwd.h"
60 #include "xgetcwd.h"
61 #include "error.h"
62 #include "dircallback.h"
64 #ifdef HAVE_LOCALE_H
65 #include <locale.h>
66 #endif
68 #if ENABLE_NLS
69 # include <libintl.h>
70 # define _(Text) gettext (Text)
71 #else
72 # define _(Text) Text
73 #define textdomain(Domain)
74 #define bindtextdomain(Package, Directory)
75 #endif
76 #ifdef gettext_noop
77 # define N_(String) gettext_noop (String)
78 #else
79 /* See locate.c for explanation as to why not use (String) */
80 # define N_(String) String
81 #endif
84 static void set_close_on_exec(int fd)
86 #if defined(F_GETFD) && defined(FD_CLOEXEC)
87 int flags;
88 flags = fcntl(fd, F_GETFD);
89 if (flags >= 0)
91 flags |= FD_CLOEXEC;
92 fcntl(fd, F_SETFD, flags);
94 #endif
99 /* FTS_TIGHT_CYCLE_CHECK tries to work around Savannah bug #17877
100 * (but actually using it doesn't fix the bug).
102 static int ftsoptions = FTS_NOSTAT|FTS_TIGHT_CYCLE_CHECK;
104 static int prev_depth = INT_MIN; /* fts_level can be < 0 */
105 static int curr_fd = -1;
107 int get_current_dirfd(void)
109 if (ftsoptions & FTS_CWDFD)
111 assert(curr_fd != -1);
112 assert( (AT_FDCWD == curr_fd) || (curr_fd >= 0) );
114 if (AT_FDCWD == curr_fd)
115 return starting_desc;
116 else
117 return curr_fd;
119 else
121 return AT_FDCWD;
125 static void left_dir(void)
127 if (ftsoptions & FTS_CWDFD)
129 if (curr_fd >= 0)
131 close(curr_fd);
132 curr_fd = -1;
135 else
137 /* do nothing. */
142 * Signal that we are now inside a directory pointed to by dirfd.
143 * The caller can't tell if this is the first time this happens, so
144 * we have to be careful not to call dup() more than once
146 static void inside_dir(int dirfd)
148 if (ftsoptions & FTS_CWDFD)
150 assert(dirfd == AT_FDCWD || dirfd >= 0);
152 state.cwd_dir_fd = dirfd;
153 if (curr_fd < 0)
155 if (AT_FDCWD == dirfd)
157 curr_fd = AT_FDCWD;
159 else if (dirfd >= 0)
161 curr_fd = dup(dirfd);
162 set_close_on_exec(curr_fd);
164 else
166 /* curr_fd is invalid, but dirfd is also invalid.
167 * This should not have happened.
169 assert(curr_fd >= 0 || dirfd >= 0);
173 else
175 /* FTS_CWDFD is not in use. We can always assume that
176 * AT_FDCWD refers to the directory we are currentl searching.
178 * Therefore there is nothing to do.
185 #ifdef STAT_MOUNTPOINTS
186 static void init_mounted_dev_list(void);
187 #endif
189 /* We have encountered an error which should affect the exit status.
190 * This is normally used to change the exit status from 0 to 1.
191 * However, if the exit status is already 2 for example, we don't want to
192 * reduce it to 1.
194 static void
195 error_severity(int level)
197 if (state.exit_status < level)
198 state.exit_status = level;
202 #define STRINGIFY(X) #X
203 #define HANDLECASE(N) case N: return #N;
205 static char *
206 get_fts_info_name(int info)
208 static char buf[10];
209 switch (info)
211 HANDLECASE(FTS_D);
212 HANDLECASE(FTS_DC);
213 HANDLECASE(FTS_DEFAULT);
214 HANDLECASE(FTS_DNR);
215 HANDLECASE(FTS_DOT);
216 HANDLECASE(FTS_DP);
217 HANDLECASE(FTS_ERR);
218 HANDLECASE(FTS_F);
219 HANDLECASE(FTS_INIT);
220 HANDLECASE(FTS_NS);
221 HANDLECASE(FTS_NSOK);
222 HANDLECASE(FTS_SL);
223 HANDLECASE(FTS_SLNONE);
224 HANDLECASE(FTS_W);
225 default:
226 sprintf(buf, "[%d]", info);
227 return buf;
231 static void
232 visit(FTS *p, FTSENT *ent, struct stat *pstat)
234 struct predicate *eval_tree;
236 state.curdepth = ent->fts_level;
237 state.have_stat = (ent->fts_info != FTS_NS) && (ent->fts_info != FTS_NSOK);
238 state.rel_pathname = ent->fts_accpath;
239 state.cwd_dir_fd = p->fts_cwd_fd;
241 /* Apply the predicates to this path. */
242 eval_tree = get_eval_tree();
243 apply_predicate(ent->fts_path, pstat, eval_tree);
245 /* Deal with any side effects of applying the predicates. */
246 if (state.stop_at_current_level)
248 fts_set(p, ent, FTS_SKIP);
252 static const char*
253 partial_quotearg_n(int n, char *s, size_t len, enum quoting_style style)
255 if (0 == len)
257 return quotearg_n_style(n, style, "");
259 else
261 char saved;
262 const char *result;
264 saved = s[len];
265 s[len] = 0;
266 result = quotearg_n_style(n, style, s);
267 s[len] = saved;
268 return result;
273 /* We've detected a filesystem loop. This is caused by one of
274 * two things:
276 * 1. Option -L is in effect and we've hit a symbolic link that
277 * points to an ancestor. This is harmless. We won't traverse the
278 * symbolic link.
280 * 2. We have hit a real cycle in the directory hierarchy. In this
281 * case, we issue a diagnostic message (POSIX requires this) and we
282 * skip that directory entry.
284 static void
285 issue_loop_warning(FTSENT * ent)
287 if (S_ISLNK(ent->fts_statp->st_mode))
289 error(0, 0,
290 _("Symbolic link %s is part of a loop in the directory hierarchy; we have already visited the directory to which it points."),
291 safely_quote_err_filename(0, ent->fts_path));
293 else
295 /* We have found an infinite loop. POSIX requires us to
296 * issue a diagnostic. Usually we won't get to here
297 * because when the leaf optimisation is on, it will cause
298 * the subdirectory to be skipped. If /a/b/c/d is a hard
299 * link to /a/b, then the link count of /a/b/c is 2,
300 * because the ".." entry of /a/b/c/d points to /a, not
301 * to /a/b/c.
303 error(0, 0,
304 _("Filesystem loop detected; "
305 "%s is part of the same filesystem loop as %s."),
306 safely_quote_err_filename(0, ent->fts_path),
307 partial_quotearg_n(1,
308 ent->fts_cycle->fts_path,
309 ent->fts_cycle->fts_pathlen,
310 options.err_quoting_style));
315 * Return true if NAME corresponds to a file which forms part of a
316 * symbolic link loop. The command
317 * rm -f a b; ln -s a b; ln -s b a
318 * produces such a loop.
320 static boolean
321 symlink_loop(const char *name)
323 struct stat stbuf;
324 int rv;
325 if (following_links())
326 rv = stat(name, &stbuf);
327 else
328 rv = lstat(name, &stbuf);
329 return (0 != rv) && (ELOOP == errno);
333 static int
334 complete_execdirs_cb(void *context)
336 (void) context;
337 /* By the tme this callback is called, the current directory is correct. */
338 complete_pending_execdirs(AT_FDCWD);
339 return 0;
342 static void
343 show_outstanding_execdirs(FILE *fp)
345 if (options.debug_options & DebugExec)
347 int seen=0;
348 struct predicate *p;
349 p = get_eval_tree();
350 fprintf(fp, "Outstanding execdirs:");
352 while (p)
354 const char *pfx;
356 if (pred_is(p, pred_execdir))
357 pfx = "-execdir";
358 else if (pred_is(p, pred_okdir))
359 pfx = "-okdir";
360 else
361 pfx = NULL;
362 if (pfx)
364 int i;
365 const struct exec_val *execp = &p->args.exec_vec;
366 ++seen;
368 fprintf(fp, "%s ", pfx);
369 if (execp->multiple)
370 fprintf(fp, "multiple ");
371 fprintf(fp, "%d args: ", execp->state.cmd_argc);
372 for (i=0; i<execp->state.cmd_argc; ++i)
374 fprintf(fp, "%s ", execp->state.cmd_argv[i]);
376 fprintf(fp, "\n");
378 p = p->pred_next;
380 if (!seen)
381 fprintf(fp, " none\n");
383 else
385 /* No debug output is wanted. */
392 static void
393 consider_visiting(FTS *p, FTSENT *ent)
395 struct stat statbuf;
396 mode_t mode;
397 int ignore, isdir;
399 if (options.debug_options & DebugSearch)
400 fprintf(stderr,
401 "consider_visiting: fts_info=%-6s, fts_level=%2d, prev_depth=%d "
402 "fts_path=%s, fts_accpath=%s\n",
403 get_fts_info_name(ent->fts_info),
404 (int)ent->fts_level, prev_depth,
405 quotearg_n_style(0, options.err_quoting_style, ent->fts_path),
406 quotearg_n_style(1, options.err_quoting_style, ent->fts_accpath));
408 if (ent->fts_info == FTS_DP)
410 left_dir();
412 else if (ent->fts_level > prev_depth || ent->fts_level==0)
414 left_dir();
416 inside_dir(p->fts_cwd_fd);
417 prev_depth = ent->fts_level;
420 /* Cope with various error conditions. */
421 if (ent->fts_info == FTS_ERR
422 || ent->fts_info == FTS_DNR)
424 error(0, ent->fts_errno, "%s",
425 safely_quote_err_filename(0, ent->fts_path));
426 error_severity(1);
427 return;
429 else if (ent->fts_info == FTS_DC)
431 issue_loop_warning(ent);
432 error_severity(1);
433 return;
435 else if (ent->fts_info == FTS_SLNONE)
437 /* fts_read() claims that ent->fts_accpath is a broken symbolic
438 * link. That would be fine, but if this is part of a symbolic
439 * link loop, we diagnose the problem and also ensure that the
440 * eventual return value is nonzero. Note that while the path
441 * we stat is local (fts_accpath), we print the fill path name
442 * of the file (fts_path) in the error message.
444 if (symlink_loop(ent->fts_accpath))
446 error(0, ELOOP, "%s", safely_quote_err_filename(0, ent->fts_path));
447 error_severity(1);
448 return;
451 else if (ent->fts_info == FTS_NS)
453 if (ent->fts_level == 0)
455 /* e.g., nonexistent starting point */
456 error(0, ent->fts_errno, "%s",
457 safely_quote_err_filename(0, ent->fts_path));
458 error_severity(1); /* remember problem */
459 return;
461 else
463 /* The following if statement fixes Savannah bug #19605
464 * (failure to diagnose a symbolic link loop)
466 if (symlink_loop(ent->fts_accpath))
468 error(0, ELOOP, "%s",
469 safely_quote_err_filename(0, ent->fts_path));
470 error_severity(1);
471 return;
476 /* Cope with the usual cases. */
477 if (ent->fts_info == FTS_NSOK
478 || ent->fts_info == FTS_NS /* e.g. symlink loop */)
480 assert(!state.have_stat);
481 assert(!state.have_type);
482 state.type = mode = 0;
484 else
486 state.have_stat = true;
487 state.have_type = true;
488 statbuf = *(ent->fts_statp);
489 state.type = mode = statbuf.st_mode;
491 if (00000 == mode)
493 /* Savannah bug #16378. */
494 error(0, 0, _("Warning: file %s appears to have mode 0000"),
495 quotearg_n_style(0, options.err_quoting_style, ent->fts_path));
499 if (mode)
501 if (!digest_mode(mode, ent->fts_path, ent->fts_name, &statbuf, 0))
502 return;
505 /* examine this item. */
506 ignore = 0;
507 isdir = S_ISDIR(statbuf.st_mode)
508 || (FTS_D == ent->fts_info)
509 || (FTS_DP == ent->fts_info)
510 || (FTS_DC == ent->fts_info);
512 if (isdir && (ent->fts_info == FTS_NSOK))
514 /* This is a directory, but fts did not stat it, so
515 * presumably would not be planning to search its
516 * children. Force a stat of the file so that the
517 * children can be checked.
519 fts_set(p, ent, FTS_AGAIN);
520 return;
523 if (options.maxdepth >= 0)
525 if (ent->fts_level >= options.maxdepth)
527 fts_set(p, ent, FTS_SKIP); /* descend no further */
529 if (ent->fts_level > options.maxdepth)
530 ignore = 1; /* don't even look at this one */
534 if ( (ent->fts_info == FTS_D) && !options.do_dir_first )
536 /* this is the preorder visit, but user said -depth */
537 ignore = 1;
539 else if ( (ent->fts_info == FTS_DP) && options.do_dir_first )
541 /* this is the postorder visit, but user didn't say -depth */
542 ignore = 1;
544 else if (ent->fts_level < options.mindepth)
546 ignore = 1;
549 if (!ignore)
551 visit(p, ent, &statbuf);
554 /* XXX: if we allow a build-up of pending arguments for "-execdir foo {} +"
555 * we need to execute them in the same directory as we found the item.
556 * If we are trying to do "find a -execdir echo {} +", we will need to
557 * echo
558 * a while in the original working directory
559 * b while in a
560 * c while in b (just before leaving b)
562 * These restrictions are hard to satisfy while using fts(). The reason is
563 * that it doesn't tell us just before we leave a directory. For the moment,
564 * we punt and don't allow the arguments to build up.
566 if (state.execdirs_outstanding)
568 show_outstanding_execdirs(stderr);
569 run_in_dir(p->fts_cwd_fd, complete_execdirs_cb, NULL);
572 if (ent->fts_info == FTS_DP)
574 /* we're leaving a directory. */
575 state.stop_at_current_level = false;
581 static void
582 find(char *arg)
584 char * arglist[2];
585 FTS *p;
586 FTSENT *ent;
589 state.starting_path_length = strlen(arg);
590 inside_dir(AT_FDCWD);
592 arglist[0] = arg;
593 arglist[1] = NULL;
595 switch (options.symlink_handling)
597 case SYMLINK_ALWAYS_DEREF:
598 ftsoptions |= FTS_COMFOLLOW|FTS_LOGICAL;
599 break;
601 case SYMLINK_DEREF_ARGSONLY:
602 ftsoptions |= FTS_COMFOLLOW|FTS_PHYSICAL;
603 break;
605 case SYMLINK_NEVER_DEREF:
606 ftsoptions |= FTS_PHYSICAL;
607 break;
610 if (options.stay_on_filesystem)
611 ftsoptions |= FTS_XDEV;
613 p = fts_open(arglist, ftsoptions, NULL);
614 if (NULL == p)
616 error (0, errno, _("cannot search %s"),
617 safely_quote_err_filename(0, arg));
619 else
621 while ( (ent=fts_read(p)) != NULL )
623 state.have_stat = false;
624 state.have_type = false;
625 state.type = 0;
626 consider_visiting(p, ent);
628 fts_close(p);
629 p = NULL;
634 static void
635 process_all_startpoints(int argc, char *argv[])
637 int i;
639 /* figure out how many start points there are */
640 for (i = 0; i < argc && !looks_like_expression(argv[i], true); i++)
642 state.starting_path_length = strlen(argv[i]); /* TODO: is this redundant? */
643 find(argv[i]);
646 if (i == 0)
649 * We use a temporary variable here because some actions modify
650 * the path temporarily. Hence if we use a string constant,
651 * we get a coredump. The best example of this is if we say
652 * "find -printf %H" (note, not "find . -printf %H").
654 char defaultpath[2] = ".";
655 find(defaultpath);
663 main (int argc, char **argv)
665 int end_of_leading_options = 0; /* First arg after any -H/-L etc. */
666 struct predicate *eval_tree;
668 program_name = argv[0];
669 state.exit_status = 0;
670 state.execdirs_outstanding = false;
671 state.cwd_dir_fd = AT_FDCWD;
673 /* Set the option defaults before we do the the locale
674 * initialisation as check_nofollow() needs to be executed in the
675 * POSIX locale.
677 set_option_defaults(&options);
679 #ifdef HAVE_SETLOCALE
680 setlocale (LC_ALL, "");
681 #endif
683 bindtextdomain (PACKAGE, LOCALEDIR);
684 textdomain (PACKAGE);
685 atexit (close_stdout);
687 /* Check for -P, -H or -L options. Also -D and -O, which are
688 * both GNU extensions.
690 end_of_leading_options = process_leading_options(argc, argv);
692 if (options.debug_options & DebugStat)
693 options.xstat = debug_stat;
695 #ifdef DEBUG
696 fprintf (stderr, "cur_day_start = %s", ctime (&options.cur_day_start));
697 #endif /* DEBUG */
700 /* We are now processing the part of the "find" command line
701 * after the -H/-L options (if any).
703 eval_tree = build_expression_tree(argc, argv, end_of_leading_options);
705 /* safely_chdir() needs to check that it has ended up in the right place.
706 * To avoid bailing out when something gets automounted, it checks if
707 * the target directory appears to have had a directory mounted on it as
708 * we chdir()ed. The problem with this is that in order to notice that
709 * a filesystem was mounted, we would need to lstat() all the mount points.
710 * That strategy loses if our machine is a client of a dead NFS server.
712 * Hence if safely_chdir() and wd_sanity_check() can manage without needing
713 * to know the mounted device list, we do that.
715 if (!options.open_nofollow_available)
717 #ifdef STAT_MOUNTPOINTS
718 init_mounted_dev_list();
719 #endif
723 starting_desc = open (".", O_RDONLY
724 #if defined O_LARGEFILE
725 |O_LARGEFILE
726 #endif
728 if (0 <= starting_desc && fchdir (starting_desc) != 0)
730 close (starting_desc);
731 starting_desc = -1;
733 if (starting_desc < 0)
735 starting_dir = xgetcwd ();
736 if (! starting_dir)
737 error (1, errno, _("cannot get current directory"));
741 process_all_startpoints(argc-end_of_leading_options, argv+end_of_leading_options);
743 /* If "-exec ... {} +" has been used, there may be some
744 * partially-full command lines which have been built,
745 * but which are not yet complete. Execute those now.
747 show_success_rates(eval_tree);
748 cleanup();
749 return state.exit_status;
752 boolean
753 is_fts_enabled(int *fts_options)
755 /* this version of find (i.e. this main()) uses fts. */
756 *fts_options = ftsoptions;
757 return true;