1 /* util.c -- functions for initializing new tree elements, and other things.
2 Copyright (C) 1990, 91, 92, 93, 94, 2000, 2003, 2004, 2005 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
27 #ifdef HAVE_SYS_UTSNAME_H
28 #include <sys/utsname.h>
39 # define _(Text) gettext (Text)
44 # define N_(String) gettext_noop (String)
46 /* See locate.c for explanation as to why not use (String) */
47 # define N_(String) String
57 struct debug_option_assoc
63 static struct debug_option_assoc debugassoc
[] =
65 { "help", DebugHelp
, "Explain the various -D options" },
66 { "tree", DebugExpressionTree
, "Display the expression tree" },
67 { "search",DebugSearch
, "Navigate the directory tree verbosely" },
68 { "stat", DebugStat
, "Trace calls to stat(2) and lstat(2)" },
69 { "opt", DebugExpressionTree
|DebugTreeOpt
, "Show diagnostic information relating to optimisation" },
70 { "exec", DebugExec
, "Show diagnostic information relating to -exec, -execdir, -ok and -okdir" }
72 #define N_DEBUGASSOC (sizeof(debugassoc)/sizeof(debugassoc[0]))
77 /* Add a primary of predicate type PRED_FUNC (described by ENTRY) to the predicate input list.
79 Return a pointer to the predicate node just inserted.
81 Fills in the following cells of the new predicate node:
88 Other cells that need to be filled in are defaulted by
89 get_new_pred_chk_op, which is used to insure that the prior node is
90 either not there at all (we are the very first node) or is an
94 insert_primary_withpred (const struct parser_table
*entry
, PRED_FUNC pred_func
)
96 struct predicate
*new_pred
;
98 new_pred
= get_new_pred_chk_op (entry
);
99 new_pred
->pred_func
= pred_func
;
100 new_pred
->p_name
= entry
->parser_name
;
101 new_pred
->args
.str
= NULL
;
102 new_pred
->p_type
= PRIMARY_TYPE
;
103 new_pred
->p_prec
= NO_PREC
;
107 /* Add a primary described by ENTRY to the predicate input list.
109 Return a pointer to the predicate node just inserted.
111 Fills in the following cells of the new predicate node:
118 Other cells that need to be filled in are defaulted by
119 get_new_pred_chk_op, which is used to insure that the prior node is
120 either not there at all (we are the very first node) or is an
123 insert_primary (const struct parser_table
*entry
)
125 assert(entry
->pred_func
!= NULL
);
126 return insert_primary_withpred(entry
, entry
->pred_func
);
132 show_valid_debug_options(FILE *fp
, int full
)
137 fprintf(fp
, "Valid arguments for -D:\n");
138 for (i
=0; i
<N_DEBUGASSOC
; ++i
)
140 fprintf(fp
, "%-10s %s\n",
142 debugassoc
[i
].docstring
);
147 for (i
=0; i
<N_DEBUGASSOC
; ++i
)
149 fprintf(fp
, "%s%s", (i
>0 ? "|" : ""), debugassoc
[i
].name
);
155 usage (FILE *fp
, int status
, char *msg
)
158 fprintf (fp
, "%s: %s\n", program_name
, msg
);
160 fprintf (fp
, _("Usage: %s [-H] [-L] [-P] [-Olevel] [-D "), program_name
);
161 show_valid_debug_options(fp
, 0);
162 fprintf (fp
, _("] [path...] [expression]\n"));
168 set_stat_placeholders(struct stat
*p
)
170 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
173 #if HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
174 p
->st_birthtimensec
= 0;
176 #if HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
177 p
->st_birthtimespec
.tv_nsec
= -1;
179 #if HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_SEC
180 p
->st_birthtimespec
.tv_sec
= 0;
185 /* Get the stat information for a file, if it is
189 get_statinfo (const char *pathname
, const char *name
, struct stat
*p
)
191 /* Set markers in fields so we have a good idea if the implementation
192 * didn't bother to set them (e.g., NetBSD st_birthtimespec for MS-DOS
195 if (!state
.have_stat
)
197 set_stat_placeholders(p
);
198 if ((*options
.xstat
) (name
, p
) != 0)
200 if (!options
.ignore_readdir_race
|| (errno
!= ENOENT
) )
202 error (0, errno
, "%s", pathname
);
203 state
.exit_status
= 1;
208 state
.have_stat
= true;
209 state
.have_type
= true;
210 state
.type
= p
->st_mode
;
215 /* Get the stat/type information for a file, if it is
219 get_info (const char *pathname
,
221 struct predicate
*pred_ptr
)
223 boolean todo
= false;
225 /* If we need the full stat info, or we need the type info but don't
226 * already have it, stat the file now.
228 if (pred_ptr
->need_stat
)
230 else if ((pred_ptr
->need_type
&& (0 == state
.have_type
)))
234 return get_statinfo(pathname
, state
.rel_pathname
, p
);
239 /* Determine if we can use O_NOFOLLOW.
241 #if defined(O_NOFOLLOW)
248 if (0 == uname(&uts
))
250 /* POSIX requires that atof() ignore "unrecognised suffixes". */
251 release
= atof(uts
.release
);
253 if (0 == strcmp("Linux", uts
.sysname
))
255 /* Linux kernels 2.1.126 and earlier ignore the O_NOFOLLOW flag. */
256 return release
>= 2.2; /* close enough */
258 else if (0 == strcmp("FreeBSD", uts
.sysname
))
260 /* FreeBSD 3.0-CURRENT and later support it */
261 return release
>= 3.1;
265 /* Well, O_NOFOLLOW was defined, so we'll try to use it. */
272 /* Examine the predicate list for instances of -execdir or -okdir
273 * which have been terminated with '+' (build argument list) rather
274 * than ';' (singles only). If there are any, run them (this will
275 * have no effect if there are no arguments waiting).
278 do_complete_pending_execdirs(struct predicate
*p
, int dirfd
)
283 assert(state
.execdirs_outstanding
);
285 do_complete_pending_execdirs(p
->pred_left
, dirfd
);
287 if (p
->pred_func
== pred_execdir
|| p
->pred_func
== pred_okdir
)
289 /* It's an exec-family predicate. p->args.exec_val is valid. */
290 if (p
->args
.exec_vec
.multiple
)
292 struct exec_val
*execp
= &p
->args
.exec_vec
;
294 /* This one was terminated by '+' and so might have some
295 * left... Run it if necessary.
297 if (execp
->state
.todo
)
299 /* There are not-yet-executed arguments. */
300 launch (&execp
->ctl
, &execp
->state
);
305 do_complete_pending_execdirs(p
->pred_right
, dirfd
);
309 complete_pending_execdirs(int dirfd
)
311 if (state
.execdirs_outstanding
)
313 do_complete_pending_execdirs(get_eval_tree(), dirfd
);
314 state
.execdirs_outstanding
= false;
320 /* Examine the predicate list for instances of -exec which have been
321 * terminated with '+' (build argument list) rather than ';' (singles
322 * only). If there are any, run them (this will have no effect if
323 * there are no arguments waiting).
326 complete_pending_execs(struct predicate
*p
)
331 complete_pending_execs(p
->pred_left
);
333 /* It's an exec-family predicate then p->args.exec_val is valid
334 * and we can check it.
336 if (p
->pred_func
== pred_exec
&& p
->args
.exec_vec
.multiple
)
338 struct exec_val
*execp
= &p
->args
.exec_vec
;
340 /* This one was terminated by '+' and so might have some
341 * left... Run it if necessary. Set state.exit_status if
342 * there are any problems.
344 if (execp
->state
.todo
)
346 /* There are not-yet-executed arguments. */
347 launch (&execp
->ctl
, &execp
->state
);
351 complete_pending_execs(p
->pred_right
);
355 /* Complete any outstanding commands.
360 struct predicate
*eval_tree
= get_eval_tree();
363 complete_pending_execs(eval_tree
);
364 complete_pending_execdirs(get_current_dirfd());
370 fallback_stat(const char *name
, struct stat
*p
, int prev_rv
)
372 /* Our original stat() call failed. Perhaps we can't follow a
373 * symbolic link. If that might be the problem, lstat() the link.
374 * Otherwise, admit defeat.
380 if (options
.debug_options
& DebugStat
)
381 fprintf(stderr
, "fallback_stat(): stat(%s) failed; falling back on lstat()\n", name
);
382 return fstatat(state
.cwd_dir_fd
, name
, p
, AT_SYMLINK_NOFOLLOW
);
389 case EOVERFLOW
: /* EOVERFLOW is not #defined on UNICOS. */
397 /* optionh_stat() implements the stat operation when the -H option is
400 * If the item to be examined is a command-line argument, we follow
401 * symbolic links. If the stat() call fails on the command-line item,
402 * we fall back on the properties of the symbolic link.
404 * If the item to be examined is not a command-line argument, we
405 * examine the link itself.
408 optionh_stat(const char *name
, struct stat
*p
)
410 set_stat_placeholders(p
);
411 if (0 == state
.curdepth
)
413 /* This file is from the command line; deference the link (if it
417 rv
= fstatat(state
.cwd_dir_fd
, name
, p
, 0);
419 return 0; /* success */
421 return fallback_stat(name
, p
, rv
);
425 /* Not a file on the command line; do not dereference the link.
427 return fstatat(state
.cwd_dir_fd
, name
, p
, AT_SYMLINK_NOFOLLOW
);
431 /* optionl_stat() implements the stat operation when the -L option is
432 * in effect. That option makes us examine the thing the symbolic
433 * link points to, not the symbolic link itself.
436 optionl_stat(const char *name
, struct stat
*p
)
439 set_stat_placeholders(p
);
440 rv
= fstatat(state
.cwd_dir_fd
, name
, p
, 0);
442 return 0; /* normal case. */
444 return fallback_stat(name
, p
, rv
);
447 /* optionp_stat() implements the stat operation when the -P option is
448 * in effect (this is also the default). That option makes us examine
449 * the symbolic link itself, not the thing it points to.
452 optionp_stat(const char *name
, struct stat
*p
)
454 set_stat_placeholders(p
);
455 return fstatat(state
.cwd_dir_fd
, name
, p
, AT_SYMLINK_NOFOLLOW
);
459 static uintmax_t stat_count
= 0u;
462 debug_stat (const char *file
, struct stat
*bufp
)
465 fprintf (stderr
, "debug_stat (%s)\n", file
);
467 switch (options
.symlink_handling
)
469 case SYMLINK_ALWAYS_DEREF
:
470 return optionl_stat(file
, bufp
);
471 case SYMLINK_DEREF_ARGSONLY
:
472 return optionh_stat(file
, bufp
);
473 case SYMLINK_NEVER_DEREF
:
474 return optionp_stat(file
, bufp
);
483 following_links(void)
485 switch (options
.symlink_handling
)
487 case SYMLINK_ALWAYS_DEREF
:
489 case SYMLINK_DEREF_ARGSONLY
:
490 return (state
.curdepth
== 0);
491 case SYMLINK_NEVER_DEREF
:
498 /* Take a "mode" indicator and fill in the files of 'state'.
501 digest_mode(mode_t mode
,
502 const char *pathname
,
507 /* If we know the type of the directory entry, and it is not a
508 * symbolic link, we may be able to avoid a stat() or lstat() call.
512 if (S_ISLNK(mode
) && following_links())
514 /* mode is wrong because we should have followed the symlink. */
515 if (get_statinfo(pathname
, name
, pstat
) != 0)
517 mode
= state
.type
= pstat
->st_mode
;
518 state
.have_type
= true;
522 state
.have_type
= true;
523 pstat
->st_mode
= state
.type
= mode
;
528 /* Mode is not yet known; may have to stat the file unless we
529 * can deduce that it is not a directory (which is all we need to
530 * know at this stage)
534 state
.have_stat
= false;
535 state
.have_type
= false;;
540 if (get_statinfo(pathname
, name
, pstat
) != 0)
543 /* If -L is in effect and we are dealing with a symlink,
544 * st_mode is the mode of the pointed-to file, while mode is
545 * the mode of the directory entry (S_IFLNK). Hence now
546 * that we have the stat information, override "mode".
548 state
.type
= pstat
->st_mode
;
549 state
.have_type
= true;
558 /* Return true if there are no predicates with no_default_print in
559 predicate list PRED, false if there are any.
560 Returns true if default print should be performed */
563 default_prints (struct predicate
*pred
)
567 if (pred
->no_default_print
)
569 pred
= pred
->pred_next
;
575 looks_like_expression(const char *arg
, boolean leading
)
580 if (arg
[1]) /* "-foo" is an expression. */
583 return false; /* Just "-" is a filename. */
589 return false; /* )x and ,z are not expressions */
591 return !leading
; /* A leading ) or , is not either */
593 /* ( and ! are part of an expression, but (2 and !foo are
609 process_debug_options(char *arg
)
612 char *token_context
= NULL
;
613 const char delimiters
[] = ",";
614 boolean empty
= true;
617 p
= strtok_r(arg
, delimiters
, &token_context
);
622 for (i
=0; i
<N_DEBUGASSOC
; ++i
)
624 if (0 == strcmp(debugassoc
[i
].name
, p
))
626 options
.debug_options
|= debugassoc
[i
].val
;
630 if (i
>= N_DEBUGASSOC
)
632 error(0, 0, _("Ignoring unrecognised debug flag %s"),
633 quotearg_n_style(0, locale_quoting_style
, arg
));
635 p
= strtok_r(NULL
, delimiters
, &token_context
);
639 error(1, 0, _("Empty argument to the -D option."));
641 else if (options
.debug_options
& DebugHelp
)
643 show_valid_debug_options(stdout
, 1);
649 process_optimisation_option(const char *arg
)
653 error(1, 0, _("The -O option must be immediately followed by a decimal integer"));
657 unsigned long opt_level
;
660 if (!isdigit( (unsigned char) arg
[0] ))
662 error(1, 0, _("Please specify a decimal number immediately after -O"));
666 int prev_errno
= errno
;
669 opt_level
= strtoul(arg
, &end
, 10);
670 if ( (0==opt_level
) && (end
==arg
) )
672 error(1, 0, _("Please specify a decimal number immediately after -O"));
676 /* unwanted trailing characters. */
677 error(1, 0, _("Invalid optimisation level %s"), arg
);
679 else if ( (ULONG_MAX
==opt_level
) && errno
)
681 error(1, errno
, _("Invalid optimisation level %s"), arg
);
683 else if (opt_level
> USHRT_MAX
)
685 /* tricky to test, as on some platforms USHORT_MAX and ULONG_MAX
686 * can have the same value, though this is unusual.
688 error(1, 0, _("Optimisation level %lu is too high. "
689 "If you want to find files very quickly, "
690 "consider using GNU locate."),
695 options
.optimisation_level
= opt_level
;
703 process_leading_options(int argc
, char *argv
[])
705 int i
, end_of_leading_options
;
707 for (i
=1; (end_of_leading_options
= i
) < argc
; ++i
)
709 if (0 == strcmp("-H", argv
[i
]))
711 /* Meaning: dereference symbolic links on command line, but nowhere else. */
712 set_follow_state(SYMLINK_DEREF_ARGSONLY
);
714 else if (0 == strcmp("-L", argv
[i
]))
716 /* Meaning: dereference all symbolic links. */
717 set_follow_state(SYMLINK_ALWAYS_DEREF
);
719 else if (0 == strcmp("-P", argv
[i
]))
721 /* Meaning: never dereference symbolic links (default). */
722 set_follow_state(SYMLINK_NEVER_DEREF
);
724 else if (0 == strcmp("--", argv
[i
]))
726 /* -- signifies the end of options. */
727 end_of_leading_options
= i
+1; /* Next time start with the next option */
730 else if (0 == strcmp("-D", argv
[i
]))
732 process_debug_options(argv
[i
+1]);
733 ++i
; /* skip the argument too. */
735 else if (0 == strncmp("-O", argv
[i
], 2))
737 process_optimisation_option(argv
[i
]+2);
741 /* Hmm, must be one of
745 end_of_leading_options
= i
; /* Next time start with this option */
749 return end_of_leading_options
;
752 static struct timespec
755 struct timespec retval
;
759 if (0 == gettimeofday(&tv
, NULL
))
761 retval
.tv_sec
= tv
.tv_sec
;
762 retval
.tv_nsec
= tv
.tv_usec
* 1000; /* convert unit from microseconds to nanoseconds */
766 assert(t
!= (time_t)-1);
773 set_option_defaults(struct options
*p
)
775 /* We call check_nofollow() before setlocale() because the numbers
776 * for which we check (in the results of uname) definitiely have "."
777 * as the decimal point indicator even under locales for which that
778 * is not normally true. Hence atof() would do the wrong thing
779 * if we call it after setlocale().
782 p
->open_nofollow_available
= check_nofollow();
784 p
->open_nofollow_available
= false;
787 p
->regex_options
= RE_SYNTAX_EMACS
;
792 p
->literal_control_chars
= false;
797 p
->literal_control_chars
= false; /* may change */
801 p
->do_dir_first
= true;
802 p
->maxdepth
= p
->mindepth
= -1;
803 p
->start_time
= now();
804 p
->cur_day_start
= p
->start_time
.tv_sec
- DAYSECS
;
805 p
->full_days
= false;
806 p
->stay_on_filesystem
= false;
807 p
->ignore_readdir_race
= false;
809 if (getenv("POSIXLY_CORRECT"))
810 p
->output_block_size
= 512;
812 p
->output_block_size
= 1024;
814 p
->debug_options
= 0uL;
815 p
->optimisation_level
= 0;
817 if (getenv("FIND_BLOCK_SIZE"))
819 error (1, 0, _("The environment variable FIND_BLOCK_SIZE is not supported, the only thing that affects the block size is the POSIXLY_CORRECT environment variable"));
822 #if LEAF_OPTIMISATION
823 /* The leaf optimisation is enabled. */
824 p
->no_leaf_check
= false;
826 /* The leaf optimisation is disabled. */
827 p
->no_leaf_check
= true;
830 set_follow_state(SYMLINK_NEVER_DEREF
); /* The default is equivalent to -P. */
836 * Returns the fd for the directory we started in.
838 int get_start_dirfd(void)
840 return starting_desc
;