Migrated from GPL version 2 to GPL version 3
[findutils.git] / find / ftsfind.c
blobe50896a3edd97896af2540a82f33a3d681a6f489
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 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@cisco.com>,
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>.
30 #include <config.h>
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
46 #include <sys/stat.h>
48 #include <unistd.h>
50 #include "xalloc.h"
51 #include "closeout.h"
52 #include <modetype.h>
53 #include "quotearg.h"
54 #include "quote.h"
55 #include "fts_.h"
56 #include "openat.h"
57 #include "save-cwd.h"
58 #include "xgetcwd.h"
59 #include "error.h"
60 #include "dircallback.h"
62 #ifdef HAVE_LOCALE_H
63 #include <locale.h>
64 #endif
66 #if ENABLE_NLS
67 # include <libintl.h>
68 # define _(Text) gettext (Text)
69 #else
70 # define _(Text) Text
71 #define textdomain(Domain)
72 #define bindtextdomain(Package, Directory)
73 #endif
74 #ifdef gettext_noop
75 # define N_(String) gettext_noop (String)
76 #else
77 /* See locate.c for explanation as to why not use (String) */
78 # define N_(String) String
79 #endif
82 static void set_close_on_exec(int fd)
84 #if defined F_GETFD && defined FD_CLOEXEC
85 int flags;
86 flags = fcntl(fd, F_GETFD);
87 if (flags >= 0)
89 flags |= FD_CLOEXEC;
90 fcntl(fd, F_SETFD, flags);
92 #endif
97 /* FTS_TIGHT_CYCLE_CHECK tries to work around Savannah bug #17877
98 * (but actually using it doesn't fix the bug).
100 static int ftsoptions = FTS_NOSTAT|FTS_TIGHT_CYCLE_CHECK;
102 static int prev_depth = INT_MIN; /* fts_level can be < 0 */
103 static int curr_fd = -1;
105 int get_current_dirfd(void)
107 if (ftsoptions & FTS_CWDFD)
109 assert (curr_fd != -1);
110 assert ( (AT_FDCWD == curr_fd) || (curr_fd >= 0) );
112 if (AT_FDCWD == curr_fd)
113 return starting_desc;
114 else
115 return curr_fd;
117 else
119 return AT_FDCWD;
123 static void left_dir(void)
125 if (ftsoptions & FTS_CWDFD)
127 if (curr_fd >= 0)
129 close(curr_fd);
130 curr_fd = -1;
133 else
135 /* do nothing. */
140 * Signal that we are now inside a directory pointed to by dirfd.
141 * The caller can't tell if this is the first time this happens, so
142 * we have to be careful not to call dup() more than once
144 static void inside_dir(int dirfd)
146 if (ftsoptions & FTS_CWDFD)
148 assert (dirfd == AT_FDCWD || dirfd >= 0);
150 state.cwd_dir_fd = dirfd;
151 if (curr_fd < 0)
153 if (AT_FDCWD == dirfd)
155 curr_fd = AT_FDCWD;
157 else if (dirfd >= 0)
159 curr_fd = dup(dirfd);
160 set_close_on_exec(curr_fd);
162 else
164 /* curr_fd is invalid, but dirfd is also invalid.
165 * This should not have happened.
167 assert (curr_fd >= 0 || dirfd >= 0);
171 else
173 /* FTS_CWDFD is not in use. We can always assume that
174 * AT_FDCWD refers to the directory we are currentl searching.
176 * Therefore there is nothing to do.
183 #ifdef STAT_MOUNTPOINTS
184 static void init_mounted_dev_list(void);
185 #endif
187 /* We have encountered an error which should affect the exit status.
188 * This is normally used to change the exit status from 0 to 1.
189 * However, if the exit status is already 2 for example, we don't want to
190 * reduce it to 1.
192 static void
193 error_severity(int level)
195 if (state.exit_status < level)
196 state.exit_status = level;
200 #define STRINGIFY(X) #X
201 #define HANDLECASE(N) case N: return #N;
203 static char *
204 get_fts_info_name(int info)
206 static char buf[10];
207 switch (info)
209 HANDLECASE(FTS_D);
210 HANDLECASE(FTS_DC);
211 HANDLECASE(FTS_DEFAULT);
212 HANDLECASE(FTS_DNR);
213 HANDLECASE(FTS_DOT);
214 HANDLECASE(FTS_DP);
215 HANDLECASE(FTS_ERR);
216 HANDLECASE(FTS_F);
217 HANDLECASE(FTS_INIT);
218 HANDLECASE(FTS_NS);
219 HANDLECASE(FTS_NSOK);
220 HANDLECASE(FTS_SL);
221 HANDLECASE(FTS_SLNONE);
222 HANDLECASE(FTS_W);
223 default:
224 sprintf(buf, "[%d]", info);
225 return buf;
229 static void
230 visit(FTS *p, FTSENT *ent, struct stat *pstat)
232 struct predicate *eval_tree;
234 state.curdepth = ent->fts_level;
235 state.have_stat = (ent->fts_info != FTS_NS) && (ent->fts_info != FTS_NSOK);
236 state.rel_pathname = ent->fts_accpath;
237 state.cwd_dir_fd = p->fts_cwd_fd;
239 /* Apply the predicates to this path. */
240 eval_tree = get_eval_tree();
241 apply_predicate(ent->fts_path, pstat, eval_tree);
243 /* Deal with any side effects of applying the predicates. */
244 if (state.stop_at_current_level)
246 fts_set(p, ent, FTS_SKIP);
250 static const char*
251 partial_quotearg_n(int n, char *s, size_t len, enum quoting_style style)
253 if (0 == len)
255 return quotearg_n_style(n, style, "");
257 else
259 char saved;
260 const char *result;
262 saved = s[len];
263 s[len] = 0;
264 result = quotearg_n_style(n, style, s);
265 s[len] = saved;
266 return result;
271 /* We've detected a file system loop. This is caused by one of
272 * two things:
274 * 1. Option -L is in effect and we've hit a symbolic link that
275 * points to an ancestor. This is harmless. We won't traverse the
276 * symbolic link.
278 * 2. We have hit a real cycle in the directory hierarchy. In this
279 * case, we issue a diagnostic message (POSIX requires this) and we
280 * skip that directory entry.
282 static void
283 issue_loop_warning(FTSENT * ent)
285 if (S_ISLNK(ent->fts_statp->st_mode))
287 error(0, 0,
288 _("Symbolic link %s is part of a loop in the directory hierarchy; we have already visited the directory to which it points."),
289 safely_quote_err_filename(0, ent->fts_path));
291 else
293 /* We have found an infinite loop. POSIX requires us to
294 * issue a diagnostic. Usually we won't get to here
295 * because when the leaf optimisation is on, it will cause
296 * the subdirectory to be skipped. If /a/b/c/d is a hard
297 * link to /a/b, then the link count of /a/b/c is 2,
298 * because the ".." entry of /a/b/c/d points to /a, not
299 * to /a/b/c.
301 error(0, 0,
302 _("File system loop detected; "
303 "%s is part of the same file system loop as %s."),
304 safely_quote_err_filename(0, ent->fts_path),
305 partial_quotearg_n(1,
306 ent->fts_cycle->fts_path,
307 ent->fts_cycle->fts_pathlen,
308 options.err_quoting_style));
313 * Return true if NAME corresponds to a file which forms part of a
314 * symbolic link loop. The command
315 * rm -f a b; ln -s a b; ln -s b a
316 * produces such a loop.
318 static boolean
319 symlink_loop(const char *name)
321 struct stat stbuf;
322 int rv;
323 if (following_links())
324 rv = stat(name, &stbuf);
325 else
326 rv = lstat(name, &stbuf);
327 return (0 != rv) && (ELOOP == errno);
331 static int
332 complete_execdirs_cb(void *context)
334 (void) context;
335 /* By the tme this callback is called, the current directory is correct. */
336 complete_pending_execdirs(AT_FDCWD);
337 return 0;
340 static void
341 show_outstanding_execdirs(FILE *fp)
343 if (options.debug_options & DebugExec)
345 int seen=0;
346 struct predicate *p;
347 p = get_eval_tree();
348 fprintf(fp, "Outstanding execdirs:");
350 while (p)
352 const char *pfx;
354 if (pred_is(p, pred_execdir))
355 pfx = "-execdir";
356 else if (pred_is(p, pred_okdir))
357 pfx = "-okdir";
358 else
359 pfx = NULL;
360 if (pfx)
362 int i;
363 const struct exec_val *execp = &p->args.exec_vec;
364 ++seen;
366 fprintf(fp, "%s ", pfx);
367 if (execp->multiple)
368 fprintf(fp, "multiple ");
369 fprintf(fp, "%d args: ", execp->state.cmd_argc);
370 for (i=0; i<execp->state.cmd_argc; ++i)
372 fprintf(fp, "%s ", execp->state.cmd_argv[i]);
374 fprintf(fp, "\n");
376 p = p->pred_next;
378 if (!seen)
379 fprintf(fp, " none\n");
381 else
383 /* No debug output is wanted. */
390 static void
391 consider_visiting(FTS *p, FTSENT *ent)
393 struct stat statbuf;
394 mode_t mode;
395 int ignore, isdir;
397 if (options.debug_options & DebugSearch)
398 fprintf(stderr,
399 "consider_visiting: fts_info=%-6s, fts_level=%2d, prev_depth=%d "
400 "fts_path=%s, fts_accpath=%s\n",
401 get_fts_info_name(ent->fts_info),
402 (int)ent->fts_level, prev_depth,
403 quotearg_n_style(0, options.err_quoting_style, ent->fts_path),
404 quotearg_n_style(1, options.err_quoting_style, ent->fts_accpath));
406 if (ent->fts_info == FTS_DP)
408 left_dir();
410 else if (ent->fts_level > prev_depth || ent->fts_level==0)
412 left_dir();
414 inside_dir(p->fts_cwd_fd);
415 prev_depth = ent->fts_level;
418 /* Cope with various error conditions. */
419 if (ent->fts_info == FTS_ERR
420 || ent->fts_info == FTS_DNR)
422 error(0, ent->fts_errno, "%s",
423 safely_quote_err_filename(0, ent->fts_path));
424 error_severity(1);
425 return;
427 else if (ent->fts_info == FTS_DC)
429 issue_loop_warning(ent);
430 error_severity(1);
431 return;
433 else if (ent->fts_info == FTS_SLNONE)
435 /* fts_read() claims that ent->fts_accpath is a broken symbolic
436 * link. That would be fine, but if this is part of a symbolic
437 * link loop, we diagnose the problem and also ensure that the
438 * eventual return value is nonzero. Note that while the path
439 * we stat is local (fts_accpath), we print the fill path name
440 * of the file (fts_path) in the error message.
442 if (symlink_loop(ent->fts_accpath))
444 error(0, ELOOP, "%s", safely_quote_err_filename(0, ent->fts_path));
445 error_severity(1);
446 return;
449 else if (ent->fts_info == FTS_NS)
451 if (ent->fts_level == 0)
453 /* e.g., nonexistent starting point */
454 error(0, ent->fts_errno, "%s",
455 safely_quote_err_filename(0, ent->fts_path));
456 error_severity(1); /* remember problem */
457 return;
459 else
461 /* The following if statement fixes Savannah bug #19605
462 * (failure to diagnose a symbolic link loop)
464 if (symlink_loop(ent->fts_accpath))
466 error(0, ELOOP, "%s",
467 safely_quote_err_filename(0, ent->fts_path));
468 error_severity(1);
469 return;
474 /* Cope with the usual cases. */
475 if (ent->fts_info == FTS_NSOK
476 || ent->fts_info == FTS_NS /* e.g. symlink loop */)
478 assert (!state.have_stat);
479 assert (!state.have_type);
480 state.type = mode = 0;
482 else
484 state.have_stat = true;
485 state.have_type = true;
486 statbuf = *(ent->fts_statp);
487 state.type = mode = statbuf.st_mode;
489 if (00000 == mode)
491 /* Savannah bug #16378. */
492 error(0, 0, _("Warning: file %s appears to have mode 0000"),
493 quotearg_n_style(0, options.err_quoting_style, ent->fts_path));
497 if (mode)
499 if (!digest_mode(mode, ent->fts_path, ent->fts_name, &statbuf, 0))
500 return;
503 /* examine this item. */
504 ignore = 0;
505 isdir = S_ISDIR(statbuf.st_mode)
506 || (FTS_D == ent->fts_info)
507 || (FTS_DP == ent->fts_info)
508 || (FTS_DC == ent->fts_info);
510 if (isdir && (ent->fts_info == FTS_NSOK))
512 /* This is a directory, but fts did not stat it, so
513 * presumably would not be planning to search its
514 * children. Force a stat of the file so that the
515 * children can be checked.
517 fts_set(p, ent, FTS_AGAIN);
518 return;
521 if (options.maxdepth >= 0)
523 if (ent->fts_level >= options.maxdepth)
525 fts_set(p, ent, FTS_SKIP); /* descend no further */
527 if (ent->fts_level > options.maxdepth)
528 ignore = 1; /* don't even look at this one */
532 if ( (ent->fts_info == FTS_D) && !options.do_dir_first )
534 /* this is the preorder visit, but user said -depth */
535 ignore = 1;
537 else if ( (ent->fts_info == FTS_DP) && options.do_dir_first )
539 /* this is the postorder visit, but user didn't say -depth */
540 ignore = 1;
542 else if (ent->fts_level < options.mindepth)
544 ignore = 1;
547 if (!ignore)
549 visit(p, ent, &statbuf);
552 /* XXX: if we allow a build-up of pending arguments for "-execdir foo {} +"
553 * we need to execute them in the same directory as we found the item.
554 * If we are trying to do "find a -execdir echo {} +", we will need to
555 * echo
556 * a while in the original working directory
557 * b while in a
558 * c while in b (just before leaving b)
560 * These restrictions are hard to satisfy while using fts(). The reason is
561 * that it doesn't tell us just before we leave a directory. For the moment,
562 * we punt and don't allow the arguments to build up.
564 if (state.execdirs_outstanding)
566 show_outstanding_execdirs(stderr);
567 run_in_dir(p->fts_cwd_fd, complete_execdirs_cb, NULL);
570 if (ent->fts_info == FTS_DP)
572 /* we're leaving a directory. */
573 state.stop_at_current_level = false;
579 static void
580 find(char *arg)
582 char * arglist[2];
583 FTS *p;
584 FTSENT *ent;
587 state.starting_path_length = strlen(arg);
588 inside_dir(AT_FDCWD);
590 arglist[0] = arg;
591 arglist[1] = NULL;
593 switch (options.symlink_handling)
595 case SYMLINK_ALWAYS_DEREF:
596 ftsoptions |= FTS_COMFOLLOW|FTS_LOGICAL;
597 break;
599 case SYMLINK_DEREF_ARGSONLY:
600 ftsoptions |= FTS_COMFOLLOW|FTS_PHYSICAL;
601 break;
603 case SYMLINK_NEVER_DEREF:
604 ftsoptions |= FTS_PHYSICAL;
605 break;
608 if (options.stay_on_filesystem)
609 ftsoptions |= FTS_XDEV;
611 p = fts_open(arglist, ftsoptions, NULL);
612 if (NULL == p)
614 error (0, errno, _("cannot search %s"),
615 safely_quote_err_filename(0, arg));
617 else
619 while ( (ent=fts_read(p)) != NULL )
621 state.have_stat = false;
622 state.have_type = false;
623 state.type = 0;
624 consider_visiting(p, ent);
626 fts_close(p);
627 p = NULL;
632 static void
633 process_all_startpoints(int argc, char *argv[])
635 int i;
637 /* figure out how many start points there are */
638 for (i = 0; i < argc && !looks_like_expression(argv[i], true); i++)
640 state.starting_path_length = strlen(argv[i]); /* TODO: is this redundant? */
641 find(argv[i]);
644 if (i == 0)
647 * We use a temporary variable here because some actions modify
648 * the path temporarily. Hence if we use a string constant,
649 * we get a coredump. The best example of this is if we say
650 * "find -printf %H" (note, not "find . -printf %H").
652 char defaultpath[2] = ".";
653 find(defaultpath);
661 main (int argc, char **argv)
663 int end_of_leading_options = 0; /* First arg after any -H/-L etc. */
664 struct predicate *eval_tree;
666 program_name = argv[0];
667 state.exit_status = 0;
668 state.execdirs_outstanding = false;
669 state.cwd_dir_fd = AT_FDCWD;
671 /* Set the option defaults before we do the locale initialisation as
672 * check_nofollow() needs to be executed in the POSIX locale.
674 set_option_defaults(&options);
676 #ifdef HAVE_SETLOCALE
677 setlocale (LC_ALL, "");
678 #endif
680 bindtextdomain (PACKAGE, LOCALEDIR);
681 textdomain (PACKAGE);
682 atexit (close_stdout);
684 /* Check for -P, -H or -L options. Also -D and -O, which are
685 * both GNU extensions.
687 end_of_leading_options = process_leading_options(argc, argv);
689 if (options.debug_options & DebugStat)
690 options.xstat = debug_stat;
692 #ifdef DEBUG
693 fprintf (stderr, "cur_day_start = %s", ctime (&options.cur_day_start));
694 #endif /* DEBUG */
697 /* We are now processing the part of the "find" command line
698 * after the -H/-L options (if any).
700 eval_tree = build_expression_tree(argc, argv, end_of_leading_options);
702 /* safely_chdir() needs to check that it has ended up in the right place.
703 * To avoid bailing out when something gets automounted, it checks if
704 * the target directory appears to have had a directory mounted on it as
705 * we chdir()ed. The problem with this is that in order to notice that
706 * a file system was mounted, we would need to lstat() all the mount points.
707 * That strategy loses if our machine is a client of a dead NFS server.
709 * Hence if safely_chdir() and wd_sanity_check() can manage without needing
710 * to know the mounted device list, we do that.
712 if (!options.open_nofollow_available)
714 #ifdef STAT_MOUNTPOINTS
715 init_mounted_dev_list();
716 #endif
720 starting_desc = open (".", O_RDONLY
721 #if defined O_LARGEFILE
722 |O_LARGEFILE
723 #endif
725 if (0 <= starting_desc && fchdir (starting_desc) != 0)
727 close (starting_desc);
728 starting_desc = -1;
730 if (starting_desc < 0)
732 starting_dir = xgetcwd ();
733 if (! starting_dir)
734 error (1, errno, _("cannot get current directory"));
738 process_all_startpoints(argc-end_of_leading_options, argv+end_of_leading_options);
740 /* If "-exec ... {} +" has been used, there may be some
741 * partially-full command lines which have been built,
742 * but which are not yet complete. Execute those now.
744 show_success_rates(eval_tree);
745 cleanup();
746 return state.exit_status;
749 boolean
750 is_fts_enabled(int *fts_options)
752 /* this version of find (i.e. this main()) uses fts. */
753 *fts_options = ftsoptions;
754 return true;