1 /* vi: set sw=4 ts=4: */
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.
12 #undef DEBUG_RECURS_ACTION
15 * Walk down all the directories under the specified
16 * location, and do something (something specified
17 * by the fileAction and dirAction function pointers).
19 * Unfortunately, while nftw(3) could replace this and reduce
20 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
21 * and so isn't sufficiently portable to take over since glibc2.1
22 * is so stinking huge.
25 static int FAST_FUNC true_action(const char *fileName UNUSED_PARAM,
26 struct stat *statbuf UNUSED_PARAM,
27 void* userData UNUSED_PARAM,
28 int depth UNUSED_PARAM)
33 /* fileAction return value of 0 on any file in directory will make
34 * recursive_action() return 0, but it doesn't stop directory traversal
35 * (fileAction/dirAction will be called on each file).
37 * If !ACTION_RECURSE, dirAction is called on the directory and its
38 * return value is returned from recursive_action(). No recursion.
40 * If ACTION_RECURSE, recursive_action() is called on each directory.
41 * If any one of these calls returns 0, current recursive_action() returns 0.
43 * If ACTION_DEPTHFIRST, dirAction is called after recurse.
44 * If it returns 0, the warning is printed and recursive_action() returns 0.
46 * If !ACTION_DEPTHFIRST, dirAction is called before we recurse.
47 * Return value of 0 (FALSE) or 2 (SKIP) prevents recursion
48 * into that directory, instead recursive_action() returns 0 (if FALSE)
51 * followLinks=0/1 differs mainly in handling of links to dirs.
52 * 0: lstat(statbuf). Calls fileAction on link name even if points to dir.
53 * 1: stat(statbuf). Calls dirAction and optionally recurse on link to dir.
56 int FAST_FUNC recursive_action(const char *fileName,
58 int FAST_FUNC (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
59 int FAST_FUNC (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
68 if (!fileAction) fileAction = true_action;
69 if (!dirAction) dirAction = true_action;
71 status = ACTION_FOLLOWLINKS; /* hijack a variable for bitmask... */
73 status = ACTION_FOLLOWLINKS | ACTION_FOLLOWLINKS_L0;
74 status = ((flags & status) ? stat : lstat)(fileName, &statbuf);
76 #ifdef DEBUG_RECURS_ACTION
77 bb_error_msg("status=%d flags=%x", status, flags);
82 /* If S_ISLNK(m), then we know that !S_ISDIR(m).
83 * Then we can skip checking first part: if it is true, then
84 * (!dir) is also true! */
85 if ( /* (!(flags & ACTION_FOLLOWLINKS) && S_ISLNK(statbuf.st_mode)) || */
86 !S_ISDIR(statbuf.st_mode)
88 return fileAction(fileName, &statbuf, userData, depth);
91 /* It's a directory (or a link to one, and followLinks is set) */
93 if (!(flags & ACTION_RECURSE)) {
94 return dirAction(fileName, &statbuf, userData, depth);
97 if (!(flags & ACTION_DEPTHFIRST)) {
98 status = dirAction(fileName, &statbuf, userData, depth);
105 dir = opendir(fileName);
107 /* findutils-4.1.20 reports this */
108 /* (i.e. it doesn't silently return with exit code 1) */
109 /* To trigger: "find -exec rm -rf {} \;" */
113 while ((next = readdir(dir)) != NULL) {
116 nextFile = concat_subpath_file(fileName, next->d_name);
117 if (nextFile == NULL)
119 /* process every file (NB: ACTION_RECURSE is set in flags) */
120 if (!recursive_action(nextFile, flags, fileAction, dirAction,
121 userData, depth + 1))
123 // s = recursive_action(nextFile, flags, fileAction, dirAction,
124 // userData, depth + 1);
126 //#define RECURSE_RESULT_ABORT 3
127 // if (s == RECURSE_RESULT_ABORT) {
136 if (flags & ACTION_DEPTHFIRST) {
137 if (!dirAction(fileName, &statbuf, userData, depth))
144 if (!(flags & ACTION_QUIET))
145 bb_simple_perror_msg(fileName);