diff options
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/recursive_action.c | 41 |
1 files changed, 20 insertions, 21 deletions
diff --git a/libbb/recursive_action.c b/libbb/recursive_action.c index 25a87b88e..0c0531575 100644 --- a/libbb/recursive_action.c +++ b/libbb/recursive_action.c @@ -22,7 +22,8 @@ * is so stinking huge. */ -static int true_action(const char *fileName, struct stat *statbuf, void* userData, int depth) +static int true_action(const char *fileName, struct stat *statbuf, + void* userData, int depth) { return TRUE; } @@ -41,11 +42,11 @@ static int true_action(const char *fileName, struct stat *statbuf, void* userDat */ int recursive_action(const char *fileName, - int recurse, int followLinks, int depthFirst, + unsigned flags, int (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth), int (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth), void* userData, - int depth) + const unsigned depth) { struct stat statbuf; int status; @@ -54,22 +55,20 @@ int recursive_action(const char *fileName, if (!fileAction) fileAction = true_action; if (!dirAction) dirAction = true_action; - - status = (followLinks ? stat : lstat)(fileName, &statbuf); + status = (flags & action_followLinks ? stat : lstat)(fileName, &statbuf); if (status < 0) { #ifdef DEBUG_RECURS_ACTION bb_error_msg("status=%d followLinks=%d TRUE=%d", - status, followLinks, TRUE); + status, flags & action_followLinks, TRUE); #endif - bb_perror_msg("%s", fileName); - return FALSE; + goto done_nak_warn; } /* If S_ISLNK(m), then we know that !S_ISDIR(m). * Then we can skip checking first part: if it is true, then * (!dir) is also true! */ - if ( /* (!followLinks && S_ISLNK(statbuf.st_mode)) || */ + if ( /* (!(flags & action_followLinks) && S_ISLNK(statbuf.st_mode)) || */ !S_ISDIR(statbuf.st_mode) ) { return fileAction(fileName, &statbuf, userData, depth); @@ -77,15 +76,14 @@ int recursive_action(const char *fileName, /* It's a directory (or a link to one, and followLinks is set) */ - if (!recurse) { + if (!(flags & action_recurse)) { return dirAction(fileName, &statbuf, userData, depth); } - if (!depthFirst) { + if (!(flags & action_depthFirst)) { status = dirAction(fileName, &statbuf, userData, depth); if (!status) { - bb_perror_msg("%s", fileName); - return FALSE; + goto done_nak_warn; } if (status == SKIP) return TRUE; @@ -96,8 +94,7 @@ int recursive_action(const char *fileName, /* findutils-4.1.20 reports this */ /* (i.e. it doesn't silently return with exit code 1) */ /* To trigger: "find -exec rm -rf {} \;" */ - bb_perror_msg("%s", fileName); - return FALSE; + goto done_nak_warn; } status = TRUE; while ((next = readdir(dir)) != NULL) { @@ -106,21 +103,23 @@ int recursive_action(const char *fileName, nextFile = concat_subpath_file(fileName, next->d_name); if (nextFile == NULL) continue; - if (!recursive_action(nextFile, TRUE, followLinks, depthFirst, + /* now descend into it, forcing recursion. */ + if (!recursive_action(nextFile, flags | action_recurse, fileAction, dirAction, userData, depth+1)) { status = FALSE; } free(nextFile); } closedir(dir); - if (depthFirst) { - if (!dirAction(fileName, &statbuf, userData, depth)) { - bb_perror_msg("%s", fileName); - return FALSE; - } + if (flags & action_depthFirst && + !dirAction(fileName, &statbuf, userData, depth)) { + goto done_nak_warn; } if (!status) return FALSE; return TRUE; +done_nak_warn: + bb_perror_msg("%s", fileName); + return FALSE; } |