1 /* vi: set sw=8 ts=8: */
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
13 #include <linux/stat.h>
20 * Walk down all the directories under the specified
21 * location, and do something (something specified
22 * by the fileAction and dirAction function pointers).
24 * Unfortunately, while nftw(3) could replace this and reduce
25 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
26 * and so isn't sufficiently portable to take over since glibc2.1
27 * is so stinking huge.
30 static int true_action(const char *fileName
, struct stat
*statbuf
,
31 void* userData
, int depth
)
36 /* fileAction return value of 0 on any file in directory will make
37 * recursive_action() return 0, but it doesn't stop directory traversal
38 * (fileAction/dirAction will be called on each file).
40 * if !depthFirst, dirAction return value of 0 (FALSE) or 2 (SKIP)
41 * prevents recursion into that directory, instead
42 * recursive_action() returns 0 (if FALSE) or 1 (if SKIP).
44 * followLinks=0/1 differs mainly in handling of links to dirs.
45 * 0: lstat(statbuf). Calls fileAction on link name even if points to dir.
46 * 1: stat(statbuf). Calls dirAction and optionally recurse on link to dir.
49 int recursive_action(const char *fileName
,
51 int (*fileAction
)(const char *fileName
, struct stat
*statbuf
, void* userData
, int depth
),
52 int (*dirAction
)(const char *fileName
, struct stat
*statbuf
, void* userData
, int depth
),
61 if (!fileAction
) fileAction
= true_action
;
62 if (!dirAction
) dirAction
= true_action
;
63 status
= stat(fileName
, &statbuf
);
66 #ifdef DEBUG_RECURS_ACTION
67 bb_error_msg("status=%d followLinks=%d TRUE=%d",
68 status
, flags
& ACTION_FOLLOWLINKS
, TRUE
);
73 /* If S_ISLNK(m), then we know that !S_ISDIR(m).
74 * Then we can skip checking first part: if it is true, then
75 * (!dir) is also true! */
76 if ( /* (!(flags & ACTION_FOLLOWLINKS) && S_ISLNK(statbuf.st_mode)) || */
77 !S_ISDIR(statbuf
.st_mode
)
79 return fileAction(fileName
, &statbuf
, userData
, depth
);
82 /* It's a directory (or a link to one, and followLinks is set) */
84 if (!(flags
& ACTION_RECURSE
)) {
85 return dirAction(fileName
, &statbuf
, userData
, depth
);
88 if (!(flags
& ACTION_DEPTHFIRST
)) {
89 status
= dirAction(fileName
, &statbuf
, userData
, depth
);
97 dir
= opendir(fileName
);
99 /* findutils-4.1.20 reports this */
100 /* (i.e. it doesn't silently return with exit code 1) */
101 /* To trigger: "find -exec rm -rf {} \;" */
105 while ((next
= readdir(dir
)) != NULL
) {
108 nextFile
= concat_subpath_file(fileName
, next
->d_name
);
109 if (nextFile
== NULL
)
111 /* now descend into it, forcing recursion. */
112 if (!recursive_action(nextFile
, flags
| ACTION_RECURSE
,
113 fileAction
, dirAction
, userData
, depth
+1)) {
119 if ((flags
& ACTION_DEPTHFIRST
) &&
120 !dirAction(fileName
, &statbuf
, userData
, depth
)) {
128 printf("%s", fileName
);
133 EXPORT_SYMBOL(recursive_action
);