1 | /* vi: set sw=4 ts=4: */
|
---|
2 | /*
|
---|
3 | * Mini find implementation for busybox
|
---|
4 | *
|
---|
5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
---|
6 | *
|
---|
7 | * Reworked by David Douthitt <n9ubh@callsign.net> and
|
---|
8 | * Matt Kraai <kraai@alumni.carnegiemellon.edu>.
|
---|
9 | *
|
---|
10 | * Licensed under GPLv2, see file LICENSE in this source tree.
|
---|
11 | */
|
---|
12 |
|
---|
13 | /* findutils-4.1.20:
|
---|
14 | *
|
---|
15 | * # find file.txt -exec 'echo {}' '{} {}' ';'
|
---|
16 | * find: echo file.txt: No such file or directory
|
---|
17 | * # find file.txt -exec 'echo' '{} {}' '; '
|
---|
18 | * find: missing argument to `-exec'
|
---|
19 | * # find file.txt -exec 'echo {}' '{} {}' ';' junk
|
---|
20 | * find: paths must precede expression
|
---|
21 | * # find file.txt -exec 'echo {}' '{} {}' ';' junk ';'
|
---|
22 | * find: paths must precede expression
|
---|
23 | * # find file.txt -exec 'echo' '{} {}' ';'
|
---|
24 | * file.txt file.txt
|
---|
25 | * (strace: execve("/bin/echo", ["echo", "file.txt file.txt"], [ 30 vars ]))
|
---|
26 | * # find file.txt -exec 'echo' '{} {}' ';' -print -exec pwd ';'
|
---|
27 | * file.txt file.txt
|
---|
28 | * file.txt
|
---|
29 | * /tmp
|
---|
30 | * # find -name '*.c' -o -name '*.h'
|
---|
31 | * [shows files, *.c and *.h intermixed]
|
---|
32 | * # find file.txt -name '*f*' -o -name '*t*'
|
---|
33 | * file.txt
|
---|
34 | * # find file.txt -name '*z*' -o -name '*t*'
|
---|
35 | * file.txt
|
---|
36 | * # find file.txt -name '*f*' -o -name '*z*'
|
---|
37 | * file.txt
|
---|
38 | *
|
---|
39 | * # find t z -name '*t*' -print -o -name '*z*'
|
---|
40 | * t
|
---|
41 | * # find t z t z -name '*t*' -o -name '*z*' -print
|
---|
42 | * z
|
---|
43 | * z
|
---|
44 | * # find t z t z '(' -name '*t*' -o -name '*z*' ')' -o -print
|
---|
45 | * (no output)
|
---|
46 | */
|
---|
47 |
|
---|
48 | /* Testing script
|
---|
49 | * ./busybox find "$@" | tee /tmp/bb_find
|
---|
50 | * echo ==================
|
---|
51 | * /path/to/gnu/find "$@" | tee /tmp/std_find
|
---|
52 | * echo ==================
|
---|
53 | * diff -u /tmp/std_find /tmp/bb_find && echo Identical
|
---|
54 | */
|
---|
55 |
|
---|
56 | //config:config FIND
|
---|
57 | //config: bool "find"
|
---|
58 | //config: default y
|
---|
59 | //config: help
|
---|
60 | //config: find is used to search your system to find specified files.
|
---|
61 | //config:
|
---|
62 | //config:config FEATURE_FIND_PRINT0
|
---|
63 | //config: bool "Enable -print0: NUL-terminated output"
|
---|
64 | //config: default y
|
---|
65 | //config: depends on FIND
|
---|
66 | //config: help
|
---|
67 | //config: Causes output names to be separated by a NUL character
|
---|
68 | //config: rather than a newline. This allows names that contain
|
---|
69 | //config: newlines and other whitespace to be more easily
|
---|
70 | //config: interpreted by other programs.
|
---|
71 | //config:
|
---|
72 | //config:config FEATURE_FIND_MTIME
|
---|
73 | //config: bool "Enable -mtime: modified time matching"
|
---|
74 | //config: default y
|
---|
75 | //config: depends on FIND
|
---|
76 | //config: help
|
---|
77 | //config: Allow searching based on the modification time of
|
---|
78 | //config: files, in days.
|
---|
79 | //config:
|
---|
80 | //config:config FEATURE_FIND_MMIN
|
---|
81 | //config: bool "Enable -mmin: modified time matching by minutes"
|
---|
82 | //config: default y
|
---|
83 | //config: depends on FIND
|
---|
84 | //config: help
|
---|
85 | //config: Allow searching based on the modification time of
|
---|
86 | //config: files, in minutes.
|
---|
87 | //config:
|
---|
88 | //config:config FEATURE_FIND_PERM
|
---|
89 | //config: bool "Enable -perm: permissions matching"
|
---|
90 | //config: default y
|
---|
91 | //config: depends on FIND
|
---|
92 | //config: help
|
---|
93 | //config: Enable searching based on file permissions.
|
---|
94 | //config:
|
---|
95 | //config:config FEATURE_FIND_TYPE
|
---|
96 | //config: bool "Enable -type: file type matching (file/dir/link/...)"
|
---|
97 | //config: default y
|
---|
98 | //config: depends on FIND
|
---|
99 | //config: help
|
---|
100 | //config: Enable searching based on file type (file,
|
---|
101 | //config: directory, socket, device, etc.).
|
---|
102 | //config:
|
---|
103 | //config:config FEATURE_FIND_XDEV
|
---|
104 | //config: bool "Enable -xdev: 'stay in filesystem'"
|
---|
105 | //config: default y
|
---|
106 | //config: depends on FIND
|
---|
107 | //config: help
|
---|
108 | //config: This option allows find to restrict searches to a single filesystem.
|
---|
109 | //config:
|
---|
110 | //config:config FEATURE_FIND_MAXDEPTH
|
---|
111 | //config: bool "Enable -mindepth N and -maxdepth N"
|
---|
112 | //config: default y
|
---|
113 | //config: depends on FIND
|
---|
114 | //config: help
|
---|
115 | //config: This option enables -mindepth N and -maxdepth N option.
|
---|
116 | //config:
|
---|
117 | //config:config FEATURE_FIND_NEWER
|
---|
118 | //config: bool "Enable -newer: compare file modification times"
|
---|
119 | //config: default y
|
---|
120 | //config: depends on FIND
|
---|
121 | //config: help
|
---|
122 | //config: Support the 'find -newer' option for finding any files which have
|
---|
123 | //config: modification time that is more recent than the specified FILE.
|
---|
124 | //config:
|
---|
125 | //config:config FEATURE_FIND_INUM
|
---|
126 | //config: bool "Enable -inum: inode number matching"
|
---|
127 | //config: default y
|
---|
128 | //config: depends on FIND
|
---|
129 | //config: help
|
---|
130 | //config: Support the 'find -inum' option for searching by inode number.
|
---|
131 | //config:
|
---|
132 | //config:config FEATURE_FIND_EXEC
|
---|
133 | //config: bool "Enable -exec: execute commands"
|
---|
134 | //config: default y
|
---|
135 | //config: depends on FIND
|
---|
136 | //config: help
|
---|
137 | //config: Support the 'find -exec' option for executing commands based upon
|
---|
138 | //config: the files matched.
|
---|
139 | //config:
|
---|
140 | //config:config FEATURE_FIND_USER
|
---|
141 | //config: bool "Enable -user: username/uid matching"
|
---|
142 | //config: default y
|
---|
143 | //config: depends on FIND
|
---|
144 | //config: help
|
---|
145 | //config: Support the 'find -user' option for searching by username or uid.
|
---|
146 | //config:
|
---|
147 | //config:config FEATURE_FIND_GROUP
|
---|
148 | //config: bool "Enable -group: group/gid matching"
|
---|
149 | //config: default y
|
---|
150 | //config: depends on FIND
|
---|
151 | //config: help
|
---|
152 | //config: Support the 'find -group' option for searching by group name or gid.
|
---|
153 | //config:
|
---|
154 | //config:config FEATURE_FIND_NOT
|
---|
155 | //config: bool "Enable the 'not' (!) operator"
|
---|
156 | //config: default y
|
---|
157 | //config: depends on FIND
|
---|
158 | //config: help
|
---|
159 | //config: Support the '!' operator to invert the test results.
|
---|
160 | //config: If 'Enable full-blown desktop' is enabled, then will also support
|
---|
161 | //config: the non-POSIX notation '-not'.
|
---|
162 | //config:
|
---|
163 | //config:config FEATURE_FIND_DEPTH
|
---|
164 | //config: bool "Enable -depth"
|
---|
165 | //config: default y
|
---|
166 | //config: depends on FIND
|
---|
167 | //config: help
|
---|
168 | //config: Process each directory's contents before the directory itself.
|
---|
169 | //config:
|
---|
170 | //config:config FEATURE_FIND_PAREN
|
---|
171 | //config: bool "Enable parens in options"
|
---|
172 | //config: default y
|
---|
173 | //config: depends on FIND
|
---|
174 | //config: help
|
---|
175 | //config: Enable usage of parens '(' to specify logical order of arguments.
|
---|
176 | //config:
|
---|
177 | //config:config FEATURE_FIND_SIZE
|
---|
178 | //config: bool "Enable -size: file size matching"
|
---|
179 | //config: default y
|
---|
180 | //config: depends on FIND
|
---|
181 | //config: help
|
---|
182 | //config: Support the 'find -size' option for searching by file size.
|
---|
183 | //config:
|
---|
184 | //config:config FEATURE_FIND_PRUNE
|
---|
185 | //config: bool "Enable -prune: exclude subdirectories"
|
---|
186 | //config: default y
|
---|
187 | //config: depends on FIND
|
---|
188 | //config: help
|
---|
189 | //config: If the file is a directory, dont descend into it. Useful for
|
---|
190 | //config: exclusion .svn and CVS directories.
|
---|
191 | //config:
|
---|
192 | //config:config FEATURE_FIND_DELETE
|
---|
193 | //config: bool "Enable -delete: delete files/dirs"
|
---|
194 | //config: default y
|
---|
195 | //config: depends on FIND && FEATURE_FIND_DEPTH
|
---|
196 | //config: help
|
---|
197 | //config: Support the 'find -delete' option for deleting files and directories.
|
---|
198 | //config: WARNING: This option can do much harm if used wrong. Busybox will not
|
---|
199 | //config: try to protect the user from doing stupid things. Use with care.
|
---|
200 | //config:
|
---|
201 | //config:config FEATURE_FIND_PATH
|
---|
202 | //config: bool "Enable -path: match pathname with shell pattern"
|
---|
203 | //config: default y
|
---|
204 | //config: depends on FIND
|
---|
205 | //config: help
|
---|
206 | //config: The -path option matches whole pathname instead of just filename.
|
---|
207 | //config:
|
---|
208 | //config:config FEATURE_FIND_REGEX
|
---|
209 | //config: bool "Enable -regex: match pathname with regex"
|
---|
210 | //config: default y
|
---|
211 | //config: depends on FIND
|
---|
212 | //config: help
|
---|
213 | //config: The -regex option matches whole pathname against regular expression.
|
---|
214 | //config:
|
---|
215 | //config:config FEATURE_FIND_CONTEXT
|
---|
216 | //config: bool "Enable -context: security context matching"
|
---|
217 | //config: default n
|
---|
218 | //config: depends on FIND && SELINUX
|
---|
219 | //config: help
|
---|
220 | //config: Support the 'find -context' option for matching security context.
|
---|
221 | //config:
|
---|
222 | //config:config FEATURE_FIND_LINKS
|
---|
223 | //config: bool "Enable -links: link count matching"
|
---|
224 | //config: default y
|
---|
225 | //config: depends on FIND
|
---|
226 | //config: help
|
---|
227 | //config: Support the 'find -links' option for matching number of links.
|
---|
228 |
|
---|
229 | //applet:IF_FIND(APPLET_NOEXEC(find, find, BB_DIR_USR_BIN, BB_SUID_DROP, find))
|
---|
230 |
|
---|
231 | //kbuild:lib-$(CONFIG_FIND) += find.o
|
---|
232 |
|
---|
233 | //usage:#define find_trivial_usage
|
---|
234 | //usage: "[PATH]... [OPTIONS] [ACTIONS]"
|
---|
235 | //usage:#define find_full_usage "\n\n"
|
---|
236 | //usage: "Search for files and perform actions on them.\n"
|
---|
237 | //usage: "First failed action stops processing of current file.\n"
|
---|
238 | //usage: "Defaults: PATH is current directory, action is '-print'\n"
|
---|
239 | //usage: "\n -follow Follow symlinks"
|
---|
240 | //usage: IF_FEATURE_FIND_XDEV(
|
---|
241 | //usage: "\n -xdev Don't descend directories on other filesystems"
|
---|
242 | //usage: )
|
---|
243 | //usage: IF_FEATURE_FIND_MAXDEPTH(
|
---|
244 | //usage: "\n -maxdepth N Descend at most N levels. -maxdepth 0 applies"
|
---|
245 | //usage: "\n actions to command line arguments only"
|
---|
246 | //usage: "\n -mindepth N Don't act on first N levels"
|
---|
247 | //usage: )
|
---|
248 | //usage: IF_FEATURE_FIND_DEPTH(
|
---|
249 | //usage: "\n -depth Act on directory *after* traversing it"
|
---|
250 | //usage: )
|
---|
251 | //usage: "\n"
|
---|
252 | //usage: "\nActions:"
|
---|
253 | //usage: IF_FEATURE_FIND_PAREN(
|
---|
254 | //usage: "\n ( ACTIONS ) Group actions for -o / -a"
|
---|
255 | //usage: )
|
---|
256 | //usage: IF_FEATURE_FIND_NOT(
|
---|
257 | //usage: "\n ! ACT Invert ACT's success/failure"
|
---|
258 | //usage: )
|
---|
259 | //usage: "\n ACT1 [-a] ACT2 If ACT1 fails, stop, else do ACT2"
|
---|
260 | //usage: "\n ACT1 -o ACT2 If ACT1 succeeds, stop, else do ACT2"
|
---|
261 | //usage: "\n Note: -a has higher priority than -o"
|
---|
262 | //usage: "\n -name PATTERN Match file name (w/o directory name) to PATTERN"
|
---|
263 | //usage: "\n -iname PATTERN Case insensitive -name"
|
---|
264 | //usage: IF_FEATURE_FIND_PATH(
|
---|
265 | //usage: "\n -path PATTERN Match path to PATTERN"
|
---|
266 | //usage: "\n -ipath PATTERN Case insensitive -path"
|
---|
267 | //usage: )
|
---|
268 | //usage: IF_FEATURE_FIND_REGEX(
|
---|
269 | //usage: "\n -regex PATTERN Match path to regex PATTERN"
|
---|
270 | //usage: )
|
---|
271 | //usage: IF_FEATURE_FIND_TYPE(
|
---|
272 | //usage: "\n -type X File type is X (one of: f,d,l,b,c,...)"
|
---|
273 | //usage: )
|
---|
274 | //usage: IF_FEATURE_FIND_PERM(
|
---|
275 | //usage: "\n -perm MASK At least one mask bit (+MASK), all bits (-MASK),"
|
---|
276 | //usage: "\n or exactly MASK bits are set in file's mode"
|
---|
277 | //usage: )
|
---|
278 | //usage: IF_FEATURE_FIND_MTIME(
|
---|
279 | //usage: "\n -mtime DAYS mtime is greater than (+N), less than (-N),"
|
---|
280 | //usage: "\n or exactly N days in the past"
|
---|
281 | //usage: )
|
---|
282 | //usage: IF_FEATURE_FIND_MMIN(
|
---|
283 | //usage: "\n -mmin MINS mtime is greater than (+N), less than (-N),"
|
---|
284 | //usage: "\n or exactly N minutes in the past"
|
---|
285 | //usage: )
|
---|
286 | //usage: IF_FEATURE_FIND_NEWER(
|
---|
287 | //usage: "\n -newer FILE mtime is more recent than FILE's"
|
---|
288 | //usage: )
|
---|
289 | //usage: IF_FEATURE_FIND_INUM(
|
---|
290 | //usage: "\n -inum N File has inode number N"
|
---|
291 | //usage: )
|
---|
292 | //usage: IF_FEATURE_FIND_USER(
|
---|
293 | //usage: "\n -user NAME/ID File is owned by given user"
|
---|
294 | //usage: )
|
---|
295 | //usage: IF_FEATURE_FIND_GROUP(
|
---|
296 | //usage: "\n -group NAME/ID File is owned by given group"
|
---|
297 | //usage: )
|
---|
298 | //usage: IF_FEATURE_FIND_SIZE(
|
---|
299 | //usage: "\n -size N[bck] File size is N (c:bytes,k:kbytes,b:512 bytes(def.))"
|
---|
300 | //usage: "\n +/-N: file size is bigger/smaller than N"
|
---|
301 | //usage: )
|
---|
302 | //usage: IF_FEATURE_FIND_LINKS(
|
---|
303 | //usage: "\n -links N Number of links is greater than (+N), less than (-N),"
|
---|
304 | //usage: "\n or exactly N"
|
---|
305 | //usage: )
|
---|
306 | //usage: IF_FEATURE_FIND_CONTEXT(
|
---|
307 | //usage: "\n -context CTX File has specified security context"
|
---|
308 | //usage: )
|
---|
309 | //usage: IF_FEATURE_FIND_PRUNE(
|
---|
310 | //usage: "\n -prune If current file is directory, don't descend into it"
|
---|
311 | //usage: )
|
---|
312 | //usage: "\nIf none of the following actions is specified, -print is assumed"
|
---|
313 | //usage: "\n -print Print file name"
|
---|
314 | //usage: IF_FEATURE_FIND_PRINT0(
|
---|
315 | //usage: "\n -print0 Print file name, NUL terminated"
|
---|
316 | //usage: )
|
---|
317 | //usage: IF_FEATURE_FIND_EXEC(
|
---|
318 | //usage: "\n -exec CMD ARG ; Run CMD with all instances of {} replaced by"
|
---|
319 | //usage: "\n file name. Fails if CMD exits with nonzero"
|
---|
320 | //usage: )
|
---|
321 | //usage: IF_FEATURE_FIND_DELETE(
|
---|
322 | //usage: "\n -delete Delete current file/directory. Turns on -depth option"
|
---|
323 | //usage: )
|
---|
324 | //usage:
|
---|
325 | //usage:#define find_example_usage
|
---|
326 | //usage: "$ find / -name passwd\n"
|
---|
327 | //usage: "/etc/passwd\n"
|
---|
328 |
|
---|
329 | #include <fnmatch.h>
|
---|
330 | #include "libbb.h"
|
---|
331 | #if ENABLE_FEATURE_FIND_REGEX
|
---|
332 | # include "xregex.h"
|
---|
333 | #endif
|
---|
334 | /* GNUism: */
|
---|
335 | #ifndef FNM_CASEFOLD
|
---|
336 | # define FNM_CASEFOLD 0
|
---|
337 | #endif
|
---|
338 |
|
---|
339 | #define dbg(...) ((void)0)
|
---|
340 | /* #define dbg(...) bb_error_msg(__VA_ARGS__) */
|
---|
341 |
|
---|
342 | /* This is a NOEXEC applet. Be very careful! */
|
---|
343 |
|
---|
344 |
|
---|
345 | typedef int (*action_fp)(const char *fileName, const struct stat *statbuf, void *) FAST_FUNC;
|
---|
346 |
|
---|
347 | typedef struct {
|
---|
348 | action_fp f;
|
---|
349 | #if ENABLE_FEATURE_FIND_NOT
|
---|
350 | bool invert;
|
---|
351 | #endif
|
---|
352 | } action;
|
---|
353 |
|
---|
354 | #define ACTS(name, ...) typedef struct { action a; __VA_ARGS__ } action_##name;
|
---|
355 | #define ACTF(name) \
|
---|
356 | static int FAST_FUNC func_##name(const char *fileName UNUSED_PARAM, \
|
---|
357 | const struct stat *statbuf UNUSED_PARAM, \
|
---|
358 | action_##name* ap UNUSED_PARAM)
|
---|
359 |
|
---|
360 | ACTS(print)
|
---|
361 | ACTS(name, const char *pattern; bool iname;)
|
---|
362 | IF_FEATURE_FIND_PATH( ACTS(path, const char *pattern; bool ipath;))
|
---|
363 | IF_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
|
---|
364 | IF_FEATURE_FIND_PRINT0( ACTS(print0))
|
---|
365 | IF_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
|
---|
366 | IF_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
|
---|
367 | IF_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
|
---|
368 | IF_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
|
---|
369 | IF_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
|
---|
370 | IF_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
|
---|
371 | IF_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
|
---|
372 | IF_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
|
---|
373 | IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
|
---|
374 | IF_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
|
---|
375 | IF_FEATURE_FIND_PRUNE( ACTS(prune))
|
---|
376 | IF_FEATURE_FIND_DELETE( ACTS(delete))
|
---|
377 | IF_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned *subst_count; int exec_argc;))
|
---|
378 | IF_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
|
---|
379 | IF_FEATURE_FIND_LINKS( ACTS(links, char links_char; int links_count;))
|
---|
380 |
|
---|
381 | struct globals {
|
---|
382 | IF_FEATURE_FIND_XDEV(dev_t *xdev_dev;)
|
---|
383 | IF_FEATURE_FIND_XDEV(int xdev_count;)
|
---|
384 | #if ENABLE_FEATURE_FIND_MAXDEPTH
|
---|
385 | int minmaxdepth[2];
|
---|
386 | #endif
|
---|
387 | action ***actions;
|
---|
388 | smallint need_print;
|
---|
389 | smallint xdev_on;
|
---|
390 | recurse_flags_t recurse_flags;
|
---|
391 | } FIX_ALIASING;
|
---|
392 | #define G (*(struct globals*)&bb_common_bufsiz1)
|
---|
393 | #define INIT_G() do { \
|
---|
394 | struct G_sizecheck { \
|
---|
395 | char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
|
---|
396 | }; \
|
---|
397 | /* we have to zero it out because of NOEXEC */ \
|
---|
398 | memset(&G, 0, sizeof(G)); \
|
---|
399 | IF_FEATURE_FIND_MAXDEPTH(G.minmaxdepth[1] = INT_MAX;) \
|
---|
400 | G.need_print = 1; \
|
---|
401 | G.recurse_flags = ACTION_RECURSE; \
|
---|
402 | } while (0)
|
---|
403 |
|
---|
404 | #if ENABLE_FEATURE_FIND_EXEC
|
---|
405 | static unsigned count_subst(const char *str)
|
---|
406 | {
|
---|
407 | unsigned count = 0;
|
---|
408 | while ((str = strstr(str, "{}")) != NULL) {
|
---|
409 | count++;
|
---|
410 | str++;
|
---|
411 | }
|
---|
412 | return count;
|
---|
413 | }
|
---|
414 |
|
---|
415 |
|
---|
416 | static char* subst(const char *src, unsigned count, const char* filename)
|
---|
417 | {
|
---|
418 | char *buf, *dst, *end;
|
---|
419 | size_t flen = strlen(filename);
|
---|
420 | /* we replace each '{}' with filename: growth by strlen-2 */
|
---|
421 | buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
|
---|
422 | while ((end = strstr(src, "{}"))) {
|
---|
423 | memcpy(dst, src, end - src);
|
---|
424 | dst += end - src;
|
---|
425 | src = end + 2;
|
---|
426 | memcpy(dst, filename, flen);
|
---|
427 | dst += flen;
|
---|
428 | }
|
---|
429 | strcpy(dst, src);
|
---|
430 | return buf;
|
---|
431 | }
|
---|
432 | #endif
|
---|
433 |
|
---|
434 | /* Return values of ACTFs ('action functions') are a bit mask:
|
---|
435 | * bit 1=1: prune (use SKIP constant for setting it)
|
---|
436 | * bit 0=1: matched successfully (TRUE)
|
---|
437 | */
|
---|
438 |
|
---|
439 | static int exec_actions(action ***appp, const char *fileName, const struct stat *statbuf)
|
---|
440 | {
|
---|
441 | int cur_group;
|
---|
442 | int cur_action;
|
---|
443 | int rc = 0;
|
---|
444 | action **app, *ap;
|
---|
445 |
|
---|
446 | /* "action group" is a set of actions ANDed together.
|
---|
447 | * groups are ORed together.
|
---|
448 | * We simply evaluate each group until we find one in which all actions
|
---|
449 | * succeed. */
|
---|
450 |
|
---|
451 | /* -prune is special: if it is encountered, then we won't
|
---|
452 | * descend into current directory. It doesn't matter whether
|
---|
453 | * action group (in which -prune sits) will succeed or not:
|
---|
454 | * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
|
---|
455 | * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
|
---|
456 | * not starting with 'f' */
|
---|
457 |
|
---|
458 | /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
|
---|
459 | * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
|
---|
460 | * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
|
---|
461 | * On return, bit is restored. */
|
---|
462 |
|
---|
463 | cur_group = -1;
|
---|
464 | while ((app = appp[++cur_group]) != NULL) {
|
---|
465 | rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
|
---|
466 | cur_action = -1;
|
---|
467 | while (1) {
|
---|
468 | ap = app[++cur_action];
|
---|
469 | if (!ap) /* all actions in group were successful */
|
---|
470 | return rc ^ TRUE; /* restore TRUE bit */
|
---|
471 | rc |= TRUE ^ ap->f(fileName, statbuf, ap);
|
---|
472 | #if ENABLE_FEATURE_FIND_NOT
|
---|
473 | if (ap->invert) rc ^= TRUE;
|
---|
474 | #endif
|
---|
475 | dbg("grp %d action %d rc:0x%x", cur_group, cur_action, rc);
|
---|
476 | if (rc & TRUE) /* current group failed, try next */
|
---|
477 | break;
|
---|
478 | }
|
---|
479 | }
|
---|
480 | dbg("returning:0x%x", rc ^ TRUE);
|
---|
481 | return rc ^ TRUE; /* restore TRUE bit */
|
---|
482 | }
|
---|
483 |
|
---|
484 |
|
---|
485 | #if !FNM_CASEFOLD
|
---|
486 | static char *strcpy_upcase(char *dst, const char *src)
|
---|
487 | {
|
---|
488 | char *d = dst;
|
---|
489 | while (1) {
|
---|
490 | unsigned char ch = *src++;
|
---|
491 | if (ch >= 'a' && ch <= 'z')
|
---|
492 | ch -= ('a' - 'A');
|
---|
493 | *d++ = ch;
|
---|
494 | if (ch == '\0')
|
---|
495 | break;
|
---|
496 | }
|
---|
497 | return dst;
|
---|
498 | }
|
---|
499 | #endif
|
---|
500 |
|
---|
501 | ACTF(name)
|
---|
502 | {
|
---|
503 | const char *tmp = bb_basename(fileName);
|
---|
504 | if (tmp != fileName && *tmp == '\0') {
|
---|
505 | /* "foo/bar/". Oh no... go back to 'b' */
|
---|
506 | tmp--;
|
---|
507 | while (tmp != fileName && *--tmp != '/')
|
---|
508 | continue;
|
---|
509 | if (*tmp == '/')
|
---|
510 | tmp++;
|
---|
511 | }
|
---|
512 | /* Was using FNM_PERIOD flag too,
|
---|
513 | * but somewhere between 4.1.20 and 4.4.0 GNU find stopped using it.
|
---|
514 | * find -name '*foo' should match .foo too:
|
---|
515 | */
|
---|
516 | #if FNM_CASEFOLD
|
---|
517 | return fnmatch(ap->pattern, tmp, (ap->iname ? FNM_CASEFOLD : 0)) == 0;
|
---|
518 | #else
|
---|
519 | if (ap->iname)
|
---|
520 | tmp = strcpy_upcase(alloca(strlen(tmp) + 1), tmp);
|
---|
521 | return fnmatch(ap->pattern, tmp, 0) == 0;
|
---|
522 | #endif
|
---|
523 | }
|
---|
524 |
|
---|
525 | #if ENABLE_FEATURE_FIND_PATH
|
---|
526 | ACTF(path)
|
---|
527 | {
|
---|
528 | # if FNM_CASEFOLD
|
---|
529 | return fnmatch(ap->pattern, fileName, (ap->ipath ? FNM_CASEFOLD : 0)) == 0;
|
---|
530 | # else
|
---|
531 | if (ap->ipath)
|
---|
532 | fileName = strcpy_upcase(alloca(strlen(fileName) + 1), fileName);
|
---|
533 | return fnmatch(ap->pattern, fileName, 0) == 0;
|
---|
534 | # endif
|
---|
535 | }
|
---|
536 | #endif
|
---|
537 | #if ENABLE_FEATURE_FIND_REGEX
|
---|
538 | ACTF(regex)
|
---|
539 | {
|
---|
540 | regmatch_t match;
|
---|
541 | if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
|
---|
542 | return 0; /* no match */
|
---|
543 | if (match.rm_so)
|
---|
544 | return 0; /* match doesn't start at pos 0 */
|
---|
545 | if (fileName[match.rm_eo])
|
---|
546 | return 0; /* match doesn't end exactly at end of pathname */
|
---|
547 | return 1;
|
---|
548 | }
|
---|
549 | #endif
|
---|
550 | #if ENABLE_FEATURE_FIND_TYPE
|
---|
551 | ACTF(type)
|
---|
552 | {
|
---|
553 | return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
|
---|
554 | }
|
---|
555 | #endif
|
---|
556 | #if ENABLE_FEATURE_FIND_PERM
|
---|
557 | ACTF(perm)
|
---|
558 | {
|
---|
559 | /* -perm +mode: at least one of perm_mask bits are set */
|
---|
560 | if (ap->perm_char == '+')
|
---|
561 | return (statbuf->st_mode & ap->perm_mask) != 0;
|
---|
562 | /* -perm -mode: all of perm_mask are set */
|
---|
563 | if (ap->perm_char == '-')
|
---|
564 | return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
|
---|
565 | /* -perm mode: file mode must match perm_mask */
|
---|
566 | return (statbuf->st_mode & 07777) == ap->perm_mask;
|
---|
567 | }
|
---|
568 | #endif
|
---|
569 | #if ENABLE_FEATURE_FIND_MTIME
|
---|
570 | ACTF(mtime)
|
---|
571 | {
|
---|
572 | time_t file_age = time(NULL) - statbuf->st_mtime;
|
---|
573 | time_t mtime_secs = ap->mtime_days * 24*60*60;
|
---|
574 | if (ap->mtime_char == '+')
|
---|
575 | return file_age >= mtime_secs + 24*60*60;
|
---|
576 | if (ap->mtime_char == '-')
|
---|
577 | return file_age < mtime_secs;
|
---|
578 | /* just numeric mtime */
|
---|
579 | return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
|
---|
580 | }
|
---|
581 | #endif
|
---|
582 | #if ENABLE_FEATURE_FIND_MMIN
|
---|
583 | ACTF(mmin)
|
---|
584 | {
|
---|
585 | time_t file_age = time(NULL) - statbuf->st_mtime;
|
---|
586 | time_t mmin_secs = ap->mmin_mins * 60;
|
---|
587 | if (ap->mmin_char == '+')
|
---|
588 | return file_age >= mmin_secs + 60;
|
---|
589 | if (ap->mmin_char == '-')
|
---|
590 | return file_age < mmin_secs;
|
---|
591 | /* just numeric mmin */
|
---|
592 | return file_age >= mmin_secs && file_age < (mmin_secs + 60);
|
---|
593 | }
|
---|
594 | #endif
|
---|
595 | #if ENABLE_FEATURE_FIND_NEWER
|
---|
596 | ACTF(newer)
|
---|
597 | {
|
---|
598 | return (ap->newer_mtime < statbuf->st_mtime);
|
---|
599 | }
|
---|
600 | #endif
|
---|
601 | #if ENABLE_FEATURE_FIND_INUM
|
---|
602 | ACTF(inum)
|
---|
603 | {
|
---|
604 | return (statbuf->st_ino == ap->inode_num);
|
---|
605 | }
|
---|
606 | #endif
|
---|
607 | #if ENABLE_FEATURE_FIND_EXEC
|
---|
608 | ACTF(exec)
|
---|
609 | {
|
---|
610 | int i, rc;
|
---|
611 | #if ENABLE_USE_PORTABLE_CODE
|
---|
612 | char **argv = alloca(sizeof(char*) * (ap->exec_argc + 1));
|
---|
613 | #else /* gcc 4.3.1 generates smaller code: */
|
---|
614 | char *argv[ap->exec_argc + 1];
|
---|
615 | #endif
|
---|
616 | for (i = 0; i < ap->exec_argc; i++)
|
---|
617 | argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
|
---|
618 | argv[i] = NULL; /* terminate the list */
|
---|
619 |
|
---|
620 | rc = spawn_and_wait(argv);
|
---|
621 | if (rc < 0)
|
---|
622 | bb_simple_perror_msg(argv[0]);
|
---|
623 |
|
---|
624 | i = 0;
|
---|
625 | while (argv[i])
|
---|
626 | free(argv[i++]);
|
---|
627 | return rc == 0; /* return 1 if exitcode 0 */
|
---|
628 | }
|
---|
629 | #endif
|
---|
630 | #if ENABLE_FEATURE_FIND_USER
|
---|
631 | ACTF(user)
|
---|
632 | {
|
---|
633 | return (statbuf->st_uid == ap->uid);
|
---|
634 | }
|
---|
635 | #endif
|
---|
636 | #if ENABLE_FEATURE_FIND_GROUP
|
---|
637 | ACTF(group)
|
---|
638 | {
|
---|
639 | return (statbuf->st_gid == ap->gid);
|
---|
640 | }
|
---|
641 | #endif
|
---|
642 | #if ENABLE_FEATURE_FIND_PRINT0
|
---|
643 | ACTF(print0)
|
---|
644 | {
|
---|
645 | printf("%s%c", fileName, '\0');
|
---|
646 | return TRUE;
|
---|
647 | }
|
---|
648 | #endif
|
---|
649 | ACTF(print)
|
---|
650 | {
|
---|
651 | puts(fileName);
|
---|
652 | return TRUE;
|
---|
653 | }
|
---|
654 | #if ENABLE_FEATURE_FIND_PAREN
|
---|
655 | ACTF(paren)
|
---|
656 | {
|
---|
657 | return exec_actions(ap->subexpr, fileName, statbuf);
|
---|
658 | }
|
---|
659 | #endif
|
---|
660 | #if ENABLE_FEATURE_FIND_SIZE
|
---|
661 | ACTF(size)
|
---|
662 | {
|
---|
663 | if (ap->size_char == '+')
|
---|
664 | return statbuf->st_size > ap->size;
|
---|
665 | if (ap->size_char == '-')
|
---|
666 | return statbuf->st_size < ap->size;
|
---|
667 | return statbuf->st_size == ap->size;
|
---|
668 | }
|
---|
669 | #endif
|
---|
670 | #if ENABLE_FEATURE_FIND_PRUNE
|
---|
671 | /*
|
---|
672 | * -prune: if -depth is not given, return true and do not descend
|
---|
673 | * current dir; if -depth is given, return false with no effect.
|
---|
674 | * Example:
|
---|
675 | * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
|
---|
676 | */
|
---|
677 | ACTF(prune)
|
---|
678 | {
|
---|
679 | return SKIP + TRUE;
|
---|
680 | }
|
---|
681 | #endif
|
---|
682 | #if ENABLE_FEATURE_FIND_DELETE
|
---|
683 | ACTF(delete)
|
---|
684 | {
|
---|
685 | int rc;
|
---|
686 | if (S_ISDIR(statbuf->st_mode)) {
|
---|
687 | rc = rmdir(fileName);
|
---|
688 | } else {
|
---|
689 | rc = unlink(fileName);
|
---|
690 | }
|
---|
691 | if (rc < 0)
|
---|
692 | bb_simple_perror_msg(fileName);
|
---|
693 | return TRUE;
|
---|
694 | }
|
---|
695 | #endif
|
---|
696 | #if ENABLE_FEATURE_FIND_CONTEXT
|
---|
697 | ACTF(context)
|
---|
698 | {
|
---|
699 | security_context_t con;
|
---|
700 | int rc;
|
---|
701 |
|
---|
702 | if (G.recurse_flags & ACTION_FOLLOWLINKS) {
|
---|
703 | rc = getfilecon(fileName, &con);
|
---|
704 | } else {
|
---|
705 | rc = lgetfilecon(fileName, &con);
|
---|
706 | }
|
---|
707 | if (rc < 0)
|
---|
708 | return FALSE;
|
---|
709 | rc = strcmp(ap->context, con);
|
---|
710 | freecon(con);
|
---|
711 | return rc == 0;
|
---|
712 | }
|
---|
713 | #endif
|
---|
714 | #if ENABLE_FEATURE_FIND_LINKS
|
---|
715 | ACTF(links)
|
---|
716 | {
|
---|
717 | switch(ap->links_char) {
|
---|
718 | case '-' : return (statbuf->st_nlink < ap->links_count);
|
---|
719 | case '+' : return (statbuf->st_nlink > ap->links_count);
|
---|
720 | default: return (statbuf->st_nlink == ap->links_count);
|
---|
721 | }
|
---|
722 | }
|
---|
723 | #endif
|
---|
724 |
|
---|
725 | static int FAST_FUNC fileAction(const char *fileName,
|
---|
726 | struct stat *statbuf,
|
---|
727 | void *userData UNUSED_PARAM,
|
---|
728 | int depth IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM))
|
---|
729 | {
|
---|
730 | int r;
|
---|
731 | int same_fs = 1;
|
---|
732 |
|
---|
733 | #if ENABLE_FEATURE_FIND_XDEV
|
---|
734 | if (S_ISDIR(statbuf->st_mode) && G.xdev_count) {
|
---|
735 | int i;
|
---|
736 | for (i = 0; i < G.xdev_count; i++) {
|
---|
737 | if (G.xdev_dev[i] == statbuf->st_dev)
|
---|
738 | goto found;
|
---|
739 | }
|
---|
740 | //bb_error_msg("'%s': not same fs", fileName);
|
---|
741 | same_fs = 0;
|
---|
742 | found: ;
|
---|
743 | }
|
---|
744 | #endif
|
---|
745 |
|
---|
746 | #if ENABLE_FEATURE_FIND_MAXDEPTH
|
---|
747 | if (depth < G.minmaxdepth[0]) {
|
---|
748 | if (same_fs)
|
---|
749 | return TRUE; /* skip this, continue recursing */
|
---|
750 | return SKIP; /* stop recursing */
|
---|
751 | }
|
---|
752 | if (depth > G.minmaxdepth[1])
|
---|
753 | return SKIP; /* stop recursing */
|
---|
754 | #endif
|
---|
755 |
|
---|
756 | r = exec_actions(G.actions, fileName, statbuf);
|
---|
757 | /* Had no explicit -print[0] or -exec? then print */
|
---|
758 | if ((r & TRUE) && G.need_print)
|
---|
759 | puts(fileName);
|
---|
760 |
|
---|
761 | #if ENABLE_FEATURE_FIND_MAXDEPTH
|
---|
762 | if (S_ISDIR(statbuf->st_mode)) {
|
---|
763 | if (depth == G.minmaxdepth[1])
|
---|
764 | return SKIP;
|
---|
765 | }
|
---|
766 | #endif
|
---|
767 | /* -xdev stops on mountpoints, but AFTER mountpoit itself
|
---|
768 | * is processed as usual */
|
---|
769 | if (!same_fs) {
|
---|
770 | return SKIP;
|
---|
771 | }
|
---|
772 |
|
---|
773 | /* Cannot return 0: our caller, recursive_action(),
|
---|
774 | * will perror() and skip dirs (if called on dir) */
|
---|
775 | return (r & SKIP) ? SKIP : TRUE;
|
---|
776 | }
|
---|
777 |
|
---|
778 |
|
---|
779 | #if ENABLE_FEATURE_FIND_TYPE
|
---|
780 | static int find_type(const char *type)
|
---|
781 | {
|
---|
782 | int mask = 0;
|
---|
783 |
|
---|
784 | if (*type == 'b')
|
---|
785 | mask = S_IFBLK;
|
---|
786 | else if (*type == 'c')
|
---|
787 | mask = S_IFCHR;
|
---|
788 | else if (*type == 'd')
|
---|
789 | mask = S_IFDIR;
|
---|
790 | else if (*type == 'p')
|
---|
791 | mask = S_IFIFO;
|
---|
792 | else if (*type == 'f')
|
---|
793 | mask = S_IFREG;
|
---|
794 | else if (*type == 'l')
|
---|
795 | mask = S_IFLNK;
|
---|
796 | else if (*type == 's')
|
---|
797 | mask = S_IFSOCK;
|
---|
798 |
|
---|
799 | if (mask == 0 || type[1] != '\0')
|
---|
800 | bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
|
---|
801 |
|
---|
802 | return mask;
|
---|
803 | }
|
---|
804 | #endif
|
---|
805 |
|
---|
806 | #if ENABLE_FEATURE_FIND_PERM \
|
---|
807 | || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
|
---|
808 | || ENABLE_FEATURE_FIND_SIZE || ENABLE_FEATURE_FIND_LINKS
|
---|
809 | static const char* plus_minus_num(const char* str)
|
---|
810 | {
|
---|
811 | if (*str == '-' || *str == '+')
|
---|
812 | str++;
|
---|
813 | return str;
|
---|
814 | }
|
---|
815 | #endif
|
---|
816 |
|
---|
817 | static action*** parse_params(char **argv)
|
---|
818 | {
|
---|
819 | enum {
|
---|
820 | OPT_FOLLOW ,
|
---|
821 | IF_FEATURE_FIND_XDEV( OPT_XDEV ,)
|
---|
822 | IF_FEATURE_FIND_DEPTH( OPT_DEPTH ,)
|
---|
823 | PARM_a ,
|
---|
824 | PARM_o ,
|
---|
825 | IF_FEATURE_FIND_NOT( PARM_char_not ,)
|
---|
826 | #if ENABLE_DESKTOP
|
---|
827 | PARM_and ,
|
---|
828 | PARM_or ,
|
---|
829 | IF_FEATURE_FIND_NOT( PARM_not ,)
|
---|
830 | #endif
|
---|
831 | PARM_print ,
|
---|
832 | IF_FEATURE_FIND_PRINT0( PARM_print0 ,)
|
---|
833 | IF_FEATURE_FIND_PRUNE( PARM_prune ,)
|
---|
834 | IF_FEATURE_FIND_DELETE( PARM_delete ,)
|
---|
835 | IF_FEATURE_FIND_EXEC( PARM_exec ,)
|
---|
836 | IF_FEATURE_FIND_PAREN( PARM_char_brace,)
|
---|
837 | /* All options/actions starting from here require argument */
|
---|
838 | PARM_name ,
|
---|
839 | PARM_iname ,
|
---|
840 | IF_FEATURE_FIND_PATH( PARM_path ,)
|
---|
841 | #if ENABLE_DESKTOP
|
---|
842 | /* -wholename is a synonym for -path */
|
---|
843 | /* We support it because Linux kernel's "make tags" uses it */
|
---|
844 | IF_FEATURE_FIND_PATH( PARM_wholename ,)
|
---|
845 | #endif
|
---|
846 | IF_FEATURE_FIND_PATH( PARM_ipath ,)
|
---|
847 | IF_FEATURE_FIND_REGEX( PARM_regex ,)
|
---|
848 | IF_FEATURE_FIND_TYPE( PARM_type ,)
|
---|
849 | IF_FEATURE_FIND_PERM( PARM_perm ,)
|
---|
850 | IF_FEATURE_FIND_MTIME( PARM_mtime ,)
|
---|
851 | IF_FEATURE_FIND_MMIN( PARM_mmin ,)
|
---|
852 | IF_FEATURE_FIND_NEWER( PARM_newer ,)
|
---|
853 | IF_FEATURE_FIND_INUM( PARM_inum ,)
|
---|
854 | IF_FEATURE_FIND_USER( PARM_user ,)
|
---|
855 | IF_FEATURE_FIND_GROUP( PARM_group ,)
|
---|
856 | IF_FEATURE_FIND_SIZE( PARM_size ,)
|
---|
857 | IF_FEATURE_FIND_CONTEXT(PARM_context ,)
|
---|
858 | IF_FEATURE_FIND_LINKS( PARM_links ,)
|
---|
859 | IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,OPT_MAXDEPTH,)
|
---|
860 | };
|
---|
861 |
|
---|
862 | static const char params[] ALIGN1 =
|
---|
863 | "-follow\0"
|
---|
864 | IF_FEATURE_FIND_XDEV( "-xdev\0" )
|
---|
865 | IF_FEATURE_FIND_DEPTH( "-depth\0" )
|
---|
866 | "-a\0"
|
---|
867 | "-o\0"
|
---|
868 | IF_FEATURE_FIND_NOT( "!\0" )
|
---|
869 | #if ENABLE_DESKTOP
|
---|
870 | "-and\0"
|
---|
871 | "-or\0"
|
---|
872 | IF_FEATURE_FIND_NOT( "-not\0" )
|
---|
873 | #endif
|
---|
874 | "-print\0"
|
---|
875 | IF_FEATURE_FIND_PRINT0( "-print0\0" )
|
---|
876 | IF_FEATURE_FIND_PRUNE( "-prune\0" )
|
---|
877 | IF_FEATURE_FIND_DELETE( "-delete\0" )
|
---|
878 | IF_FEATURE_FIND_EXEC( "-exec\0" )
|
---|
879 | IF_FEATURE_FIND_PAREN( "(\0" )
|
---|
880 | /* All options/actions starting from here require argument */
|
---|
881 | "-name\0"
|
---|
882 | "-iname\0"
|
---|
883 | IF_FEATURE_FIND_PATH( "-path\0" )
|
---|
884 | #if ENABLE_DESKTOP
|
---|
885 | IF_FEATURE_FIND_PATH( "-wholename\0")
|
---|
886 | #endif
|
---|
887 | IF_FEATURE_FIND_PATH( "-ipath\0" )
|
---|
888 | IF_FEATURE_FIND_REGEX( "-regex\0" )
|
---|
889 | IF_FEATURE_FIND_TYPE( "-type\0" )
|
---|
890 | IF_FEATURE_FIND_PERM( "-perm\0" )
|
---|
891 | IF_FEATURE_FIND_MTIME( "-mtime\0" )
|
---|
892 | IF_FEATURE_FIND_MMIN( "-mmin\0" )
|
---|
893 | IF_FEATURE_FIND_NEWER( "-newer\0" )
|
---|
894 | IF_FEATURE_FIND_INUM( "-inum\0" )
|
---|
895 | IF_FEATURE_FIND_USER( "-user\0" )
|
---|
896 | IF_FEATURE_FIND_GROUP( "-group\0" )
|
---|
897 | IF_FEATURE_FIND_SIZE( "-size\0" )
|
---|
898 | IF_FEATURE_FIND_CONTEXT("-context\0")
|
---|
899 | IF_FEATURE_FIND_LINKS( "-links\0" )
|
---|
900 | IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0")
|
---|
901 | ;
|
---|
902 |
|
---|
903 | action*** appp;
|
---|
904 | unsigned cur_group = 0;
|
---|
905 | unsigned cur_action = 0;
|
---|
906 | IF_FEATURE_FIND_NOT( bool invert_flag = 0; )
|
---|
907 |
|
---|
908 | /* This is the only place in busybox where we use nested function.
|
---|
909 | * So far more standard alternatives were bigger. */
|
---|
910 | /* Auto decl suppresses "func without a prototype" warning: */
|
---|
911 | auto action* alloc_action(int sizeof_struct, action_fp f);
|
---|
912 | action* alloc_action(int sizeof_struct, action_fp f)
|
---|
913 | {
|
---|
914 | action *ap;
|
---|
915 | appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
|
---|
916 | appp[cur_group][cur_action++] = ap = xzalloc(sizeof_struct);
|
---|
917 | appp[cur_group][cur_action] = NULL;
|
---|
918 | ap->f = f;
|
---|
919 | IF_FEATURE_FIND_NOT( ap->invert = invert_flag; )
|
---|
920 | IF_FEATURE_FIND_NOT( invert_flag = 0; )
|
---|
921 | return ap;
|
---|
922 | }
|
---|
923 |
|
---|
924 | #define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
|
---|
925 |
|
---|
926 | appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
|
---|
927 |
|
---|
928 | while (*argv) {
|
---|
929 | const char *arg = argv[0];
|
---|
930 | int parm = index_in_strings(params, arg);
|
---|
931 | const char *arg1 = argv[1];
|
---|
932 |
|
---|
933 | dbg("arg:'%s' arg1:'%s' parm:%d PARM_type:%d", arg, arg1, parm, PARM_type);
|
---|
934 |
|
---|
935 | if (parm >= PARM_name) {
|
---|
936 | /* All options/actions starting from -name require argument */
|
---|
937 | if (!arg1)
|
---|
938 | bb_error_msg_and_die(bb_msg_requires_arg, arg);
|
---|
939 | argv++;
|
---|
940 | }
|
---|
941 |
|
---|
942 | /* We can use big switch() here, but on i386
|
---|
943 | * it doesn't give smaller code. Other arches? */
|
---|
944 |
|
---|
945 | /* Options always return true. They always take effect
|
---|
946 | * rather than being processed only when their place in the
|
---|
947 | * expression is reached.
|
---|
948 | */
|
---|
949 | /* Options */
|
---|
950 | if (parm == OPT_FOLLOW) {
|
---|
951 | dbg("follow enabled: %d", __LINE__);
|
---|
952 | G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
|
---|
953 | }
|
---|
954 | #if ENABLE_FEATURE_FIND_XDEV
|
---|
955 | else if (parm == OPT_XDEV) {
|
---|
956 | dbg("%d", __LINE__);
|
---|
957 | G.xdev_on = 1;
|
---|
958 | }
|
---|
959 | #endif
|
---|
960 | #if ENABLE_FEATURE_FIND_MAXDEPTH
|
---|
961 | else if (parm == OPT_MINDEPTH || parm == OPT_MINDEPTH + 1) {
|
---|
962 | dbg("%d", __LINE__);
|
---|
963 | G.minmaxdepth[parm - OPT_MINDEPTH] = xatoi_positive(arg1);
|
---|
964 | }
|
---|
965 | #endif
|
---|
966 | #if ENABLE_FEATURE_FIND_DEPTH
|
---|
967 | else if (parm == OPT_DEPTH) {
|
---|
968 | dbg("%d", __LINE__);
|
---|
969 | G.recurse_flags |= ACTION_DEPTHFIRST;
|
---|
970 | }
|
---|
971 | #endif
|
---|
972 | /* Actions are grouped by operators
|
---|
973 | * ( expr ) Force precedence
|
---|
974 | * ! expr True if expr is false
|
---|
975 | * -not expr Same as ! expr
|
---|
976 | * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
|
---|
977 | * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
|
---|
978 | * expr1 , expr2 List; both expr1 and expr2 are always evaluated
|
---|
979 | * We implement: (), -a, -o
|
---|
980 | */
|
---|
981 | /* Operators */
|
---|
982 | else if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) {
|
---|
983 | dbg("%d", __LINE__);
|
---|
984 | /* no further special handling required */
|
---|
985 | }
|
---|
986 | else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) {
|
---|
987 | dbg("%d", __LINE__);
|
---|
988 | /* start new OR group */
|
---|
989 | cur_group++;
|
---|
990 | appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
|
---|
991 | /*appp[cur_group] = NULL; - already NULL */
|
---|
992 | appp[cur_group+1] = NULL;
|
---|
993 | cur_action = 0;
|
---|
994 | }
|
---|
995 | #if ENABLE_FEATURE_FIND_NOT
|
---|
996 | else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) {
|
---|
997 | /* also handles "find ! ! -name 'foo*'" */
|
---|
998 | invert_flag ^= 1;
|
---|
999 | dbg("invert_flag:%d", invert_flag);
|
---|
1000 | }
|
---|
1001 | #endif
|
---|
1002 | /* Actions */
|
---|
1003 | else if (parm == PARM_print) {
|
---|
1004 | dbg("%d", __LINE__);
|
---|
1005 | G.need_print = 0;
|
---|
1006 | (void) ALLOC_ACTION(print);
|
---|
1007 | }
|
---|
1008 | #if ENABLE_FEATURE_FIND_PRINT0
|
---|
1009 | else if (parm == PARM_print0) {
|
---|
1010 | dbg("%d", __LINE__);
|
---|
1011 | G.need_print = 0;
|
---|
1012 | (void) ALLOC_ACTION(print0);
|
---|
1013 | }
|
---|
1014 | #endif
|
---|
1015 | #if ENABLE_FEATURE_FIND_PRUNE
|
---|
1016 | else if (parm == PARM_prune) {
|
---|
1017 | dbg("%d", __LINE__);
|
---|
1018 | (void) ALLOC_ACTION(prune);
|
---|
1019 | }
|
---|
1020 | #endif
|
---|
1021 | #if ENABLE_FEATURE_FIND_DELETE
|
---|
1022 | else if (parm == PARM_delete) {
|
---|
1023 | dbg("%d", __LINE__);
|
---|
1024 | G.need_print = 0;
|
---|
1025 | G.recurse_flags |= ACTION_DEPTHFIRST;
|
---|
1026 | (void) ALLOC_ACTION(delete);
|
---|
1027 | }
|
---|
1028 | #endif
|
---|
1029 | #if ENABLE_FEATURE_FIND_EXEC
|
---|
1030 | else if (parm == PARM_exec) {
|
---|
1031 | int i;
|
---|
1032 | action_exec *ap;
|
---|
1033 | dbg("%d", __LINE__);
|
---|
1034 | G.need_print = 0;
|
---|
1035 | ap = ALLOC_ACTION(exec);
|
---|
1036 | ap->exec_argv = ++argv; /* first arg after -exec */
|
---|
1037 | /*ap->exec_argc = 0; - ALLOC_ACTION did it */
|
---|
1038 | while (1) {
|
---|
1039 | if (!*argv) /* did not see ';' or '+' until end */
|
---|
1040 | bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
|
---|
1041 | // find -exec echo Foo ">{}<" ";"
|
---|
1042 | // executes "echo Foo >FILENAME<",
|
---|
1043 | // find -exec echo Foo ">{}<" "+"
|
---|
1044 | // executes "echo Foo FILENAME1 FILENAME2 FILENAME3...".
|
---|
1045 | // TODO (so far we treat "+" just like ";")
|
---|
1046 | if ((argv[0][0] == ';' || argv[0][0] == '+')
|
---|
1047 | && argv[0][1] == '\0'
|
---|
1048 | ) {
|
---|
1049 | break;
|
---|
1050 | }
|
---|
1051 | argv++;
|
---|
1052 | ap->exec_argc++;
|
---|
1053 | }
|
---|
1054 | if (ap->exec_argc == 0)
|
---|
1055 | bb_error_msg_and_die(bb_msg_requires_arg, arg);
|
---|
1056 | ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
|
---|
1057 | i = ap->exec_argc;
|
---|
1058 | while (i--)
|
---|
1059 | ap->subst_count[i] = count_subst(ap->exec_argv[i]);
|
---|
1060 | }
|
---|
1061 | #endif
|
---|
1062 | #if ENABLE_FEATURE_FIND_PAREN
|
---|
1063 | else if (parm == PARM_char_brace) {
|
---|
1064 | action_paren *ap;
|
---|
1065 | char **endarg;
|
---|
1066 | unsigned nested = 1;
|
---|
1067 |
|
---|
1068 | dbg("%d", __LINE__);
|
---|
1069 | endarg = argv;
|
---|
1070 | while (1) {
|
---|
1071 | if (!*++endarg)
|
---|
1072 | bb_error_msg_and_die("unpaired '('");
|
---|
1073 | if (LONE_CHAR(*endarg, '('))
|
---|
1074 | nested++;
|
---|
1075 | else if (LONE_CHAR(*endarg, ')') && !--nested) {
|
---|
1076 | *endarg = NULL;
|
---|
1077 | break;
|
---|
1078 | }
|
---|
1079 | }
|
---|
1080 | ap = ALLOC_ACTION(paren);
|
---|
1081 | ap->subexpr = parse_params(argv + 1);
|
---|
1082 | *endarg = (char*) ")"; /* restore NULLed parameter */
|
---|
1083 | argv = endarg;
|
---|
1084 | }
|
---|
1085 | #endif
|
---|
1086 | else if (parm == PARM_name || parm == PARM_iname) {
|
---|
1087 | action_name *ap;
|
---|
1088 | dbg("%d", __LINE__);
|
---|
1089 | ap = ALLOC_ACTION(name);
|
---|
1090 | ap->pattern = arg1;
|
---|
1091 | ap->iname = (parm == PARM_iname);
|
---|
1092 | }
|
---|
1093 | #if ENABLE_FEATURE_FIND_PATH
|
---|
1094 | else if (parm == PARM_path IF_DESKTOP(|| parm == PARM_wholename) || parm == PARM_ipath) {
|
---|
1095 | action_path *ap;
|
---|
1096 | dbg("%d", __LINE__);
|
---|
1097 | ap = ALLOC_ACTION(path);
|
---|
1098 | ap->pattern = arg1;
|
---|
1099 | ap->ipath = (parm == PARM_ipath);
|
---|
1100 | }
|
---|
1101 | #endif
|
---|
1102 | #if ENABLE_FEATURE_FIND_REGEX
|
---|
1103 | else if (parm == PARM_regex) {
|
---|
1104 | action_regex *ap;
|
---|
1105 | dbg("%d", __LINE__);
|
---|
1106 | ap = ALLOC_ACTION(regex);
|
---|
1107 | xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
|
---|
1108 | }
|
---|
1109 | #endif
|
---|
1110 | #if ENABLE_FEATURE_FIND_TYPE
|
---|
1111 | else if (parm == PARM_type) {
|
---|
1112 | action_type *ap;
|
---|
1113 | ap = ALLOC_ACTION(type);
|
---|
1114 | ap->type_mask = find_type(arg1);
|
---|
1115 | dbg("created:type mask:%x", ap->type_mask);
|
---|
1116 | }
|
---|
1117 | #endif
|
---|
1118 | #if ENABLE_FEATURE_FIND_PERM
|
---|
1119 | /* -perm BITS File's mode bits are exactly BITS (octal or symbolic).
|
---|
1120 | * Symbolic modes use mode 0 as a point of departure.
|
---|
1121 | * -perm -BITS All of the BITS are set in file's mode.
|
---|
1122 | * -perm +BITS At least one of the BITS is set in file's mode.
|
---|
1123 | */
|
---|
1124 | else if (parm == PARM_perm) {
|
---|
1125 | action_perm *ap;
|
---|
1126 | dbg("%d", __LINE__);
|
---|
1127 | ap = ALLOC_ACTION(perm);
|
---|
1128 | ap->perm_char = arg1[0];
|
---|
1129 | arg1 = plus_minus_num(arg1);
|
---|
1130 | /*ap->perm_mask = 0; - ALLOC_ACTION did it */
|
---|
1131 | if (!bb_parse_mode(arg1, &ap->perm_mask))
|
---|
1132 | bb_error_msg_and_die("invalid mode '%s'", arg1);
|
---|
1133 | }
|
---|
1134 | #endif
|
---|
1135 | #if ENABLE_FEATURE_FIND_MTIME
|
---|
1136 | else if (parm == PARM_mtime) {
|
---|
1137 | action_mtime *ap;
|
---|
1138 | dbg("%d", __LINE__);
|
---|
1139 | ap = ALLOC_ACTION(mtime);
|
---|
1140 | ap->mtime_char = arg1[0];
|
---|
1141 | ap->mtime_days = xatoul(plus_minus_num(arg1));
|
---|
1142 | }
|
---|
1143 | #endif
|
---|
1144 | #if ENABLE_FEATURE_FIND_MMIN
|
---|
1145 | else if (parm == PARM_mmin) {
|
---|
1146 | action_mmin *ap;
|
---|
1147 | dbg("%d", __LINE__);
|
---|
1148 | ap = ALLOC_ACTION(mmin);
|
---|
1149 | ap->mmin_char = arg1[0];
|
---|
1150 | ap->mmin_mins = xatoul(plus_minus_num(arg1));
|
---|
1151 | }
|
---|
1152 | #endif
|
---|
1153 | #if ENABLE_FEATURE_FIND_NEWER
|
---|
1154 | else if (parm == PARM_newer) {
|
---|
1155 | struct stat stat_newer;
|
---|
1156 | action_newer *ap;
|
---|
1157 | dbg("%d", __LINE__);
|
---|
1158 | ap = ALLOC_ACTION(newer);
|
---|
1159 | xstat(arg1, &stat_newer);
|
---|
1160 | ap->newer_mtime = stat_newer.st_mtime;
|
---|
1161 | }
|
---|
1162 | #endif
|
---|
1163 | #if ENABLE_FEATURE_FIND_INUM
|
---|
1164 | else if (parm == PARM_inum) {
|
---|
1165 | action_inum *ap;
|
---|
1166 | dbg("%d", __LINE__);
|
---|
1167 | ap = ALLOC_ACTION(inum);
|
---|
1168 | ap->inode_num = xatoul(arg1);
|
---|
1169 | }
|
---|
1170 | #endif
|
---|
1171 | #if ENABLE_FEATURE_FIND_USER
|
---|
1172 | else if (parm == PARM_user) {
|
---|
1173 | action_user *ap;
|
---|
1174 | dbg("%d", __LINE__);
|
---|
1175 | ap = ALLOC_ACTION(user);
|
---|
1176 | ap->uid = bb_strtou(arg1, NULL, 10);
|
---|
1177 | if (errno)
|
---|
1178 | ap->uid = xuname2uid(arg1);
|
---|
1179 | }
|
---|
1180 | #endif
|
---|
1181 | #if ENABLE_FEATURE_FIND_GROUP
|
---|
1182 | else if (parm == PARM_group) {
|
---|
1183 | action_group *ap;
|
---|
1184 | dbg("%d", __LINE__);
|
---|
1185 | ap = ALLOC_ACTION(group);
|
---|
1186 | ap->gid = bb_strtou(arg1, NULL, 10);
|
---|
1187 | if (errno)
|
---|
1188 | ap->gid = xgroup2gid(arg1);
|
---|
1189 | }
|
---|
1190 | #endif
|
---|
1191 | #if ENABLE_FEATURE_FIND_SIZE
|
---|
1192 | else if (parm == PARM_size) {
|
---|
1193 | /* -size n[bckw]: file uses n units of space
|
---|
1194 | * b (default): units are 512-byte blocks
|
---|
1195 | * c: 1 byte
|
---|
1196 | * k: kilobytes
|
---|
1197 | * w: 2-byte words
|
---|
1198 | */
|
---|
1199 | #if ENABLE_LFS
|
---|
1200 | #define XATOU_SFX xatoull_sfx
|
---|
1201 | #else
|
---|
1202 | #define XATOU_SFX xatoul_sfx
|
---|
1203 | #endif
|
---|
1204 | static const struct suffix_mult find_suffixes[] = {
|
---|
1205 | { "c", 1 },
|
---|
1206 | { "w", 2 },
|
---|
1207 | { "", 512 },
|
---|
1208 | { "b", 512 },
|
---|
1209 | { "k", 1024 },
|
---|
1210 | { "", 0 }
|
---|
1211 | };
|
---|
1212 | action_size *ap;
|
---|
1213 | dbg("%d", __LINE__);
|
---|
1214 | ap = ALLOC_ACTION(size);
|
---|
1215 | ap->size_char = arg1[0];
|
---|
1216 | ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
|
---|
1217 | }
|
---|
1218 | #endif
|
---|
1219 | #if ENABLE_FEATURE_FIND_CONTEXT
|
---|
1220 | else if (parm == PARM_context) {
|
---|
1221 | action_context *ap;
|
---|
1222 | dbg("%d", __LINE__);
|
---|
1223 | ap = ALLOC_ACTION(context);
|
---|
1224 | /*ap->context = NULL; - ALLOC_ACTION did it */
|
---|
1225 | /* SELinux headers erroneously declare non-const parameter */
|
---|
1226 | if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
|
---|
1227 | bb_simple_perror_msg(arg1);
|
---|
1228 | }
|
---|
1229 | #endif
|
---|
1230 | #if ENABLE_FEATURE_FIND_LINKS
|
---|
1231 | else if (parm == PARM_links) {
|
---|
1232 | action_links *ap;
|
---|
1233 | dbg("%d", __LINE__);
|
---|
1234 | ap = ALLOC_ACTION(links);
|
---|
1235 | ap->links_char = arg1[0];
|
---|
1236 | ap->links_count = xatoul(plus_minus_num(arg1));
|
---|
1237 | }
|
---|
1238 | #endif
|
---|
1239 | else {
|
---|
1240 | bb_error_msg("unrecognized: %s", arg);
|
---|
1241 | bb_show_usage();
|
---|
1242 | }
|
---|
1243 | argv++;
|
---|
1244 | }
|
---|
1245 | dbg("exiting %s", __func__);
|
---|
1246 | return appp;
|
---|
1247 | #undef ALLOC_ACTION
|
---|
1248 | }
|
---|
1249 |
|
---|
1250 | int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
1251 | int find_main(int argc UNUSED_PARAM, char **argv)
|
---|
1252 | {
|
---|
1253 | int i, firstopt, status = EXIT_SUCCESS;
|
---|
1254 |
|
---|
1255 | INIT_G();
|
---|
1256 |
|
---|
1257 | argv++;
|
---|
1258 | for (firstopt = 0; argv[firstopt]; firstopt++) {
|
---|
1259 | if (argv[firstopt][0] == '-')
|
---|
1260 | break;
|
---|
1261 | if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
|
---|
1262 | break;
|
---|
1263 | if (ENABLE_FEATURE_FIND_PAREN && LONE_CHAR(argv[firstopt], '('))
|
---|
1264 | break;
|
---|
1265 | }
|
---|
1266 | if (firstopt == 0) {
|
---|
1267 | *--argv = (char*)".";
|
---|
1268 | firstopt++;
|
---|
1269 | }
|
---|
1270 |
|
---|
1271 | G.actions = parse_params(&argv[firstopt]);
|
---|
1272 | argv[firstopt] = NULL;
|
---|
1273 |
|
---|
1274 | #if ENABLE_FEATURE_FIND_XDEV
|
---|
1275 | if (G.xdev_on) {
|
---|
1276 | struct stat stbuf;
|
---|
1277 |
|
---|
1278 | G.xdev_count = firstopt;
|
---|
1279 | G.xdev_dev = xzalloc(G.xdev_count * sizeof(G.xdev_dev[0]));
|
---|
1280 | for (i = 0; argv[i]; i++) {
|
---|
1281 | /* not xstat(): shouldn't bomb out on
|
---|
1282 | * "find not_exist exist -xdev" */
|
---|
1283 | if (stat(argv[i], &stbuf) == 0)
|
---|
1284 | G.xdev_dev[i] = stbuf.st_dev;
|
---|
1285 | /* else G.xdev_dev[i] stays 0 and
|
---|
1286 | * won't match any real device dev_t
|
---|
1287 | */
|
---|
1288 | }
|
---|
1289 | }
|
---|
1290 | #endif
|
---|
1291 |
|
---|
1292 | for (i = 0; argv[i]; i++) {
|
---|
1293 | if (!recursive_action(argv[i],
|
---|
1294 | G.recurse_flags,/* flags */
|
---|
1295 | fileAction, /* file action */
|
---|
1296 | fileAction, /* dir action */
|
---|
1297 | NULL, /* user data */
|
---|
1298 | 0) /* depth */
|
---|
1299 | ) {
|
---|
1300 | status = EXIT_FAILURE;
|
---|
1301 | }
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | return status;
|
---|
1305 | }
|
---|