123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- #include "libbb.h"
- #undef DEBUG_RECURS_ACTION
- static int FAST_FUNC true_action(const char *fileName UNUSED_PARAM,
- struct stat *statbuf UNUSED_PARAM,
- void* userData UNUSED_PARAM,
- int depth UNUSED_PARAM)
- {
- return TRUE;
- }
- int FAST_FUNC recursive_action(const char *fileName,
- unsigned flags,
- int FAST_FUNC (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
- int FAST_FUNC (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
- void* userData,
- unsigned depth)
- {
- struct stat statbuf;
- unsigned follow;
- int status;
- DIR *dir;
- struct dirent *next;
- if (!fileAction) fileAction = true_action;
- if (!dirAction) dirAction = true_action;
- follow = ACTION_FOLLOWLINKS;
- if (depth == 0)
- follow = ACTION_FOLLOWLINKS | ACTION_FOLLOWLINKS_L0;
- follow &= flags;
- status = (follow ? stat : lstat)(fileName, &statbuf);
- if (status < 0) {
- #ifdef DEBUG_RECURS_ACTION
- bb_error_msg("status=%d flags=%x", status, flags);
- #endif
- if ((flags & ACTION_DANGLING_OK)
- && errno == ENOENT
- && lstat(fileName, &statbuf) == 0
- ) {
-
- return fileAction(fileName, &statbuf, userData, depth);
- }
- goto done_nak_warn;
- }
-
- if (
- !S_ISDIR(statbuf.st_mode)
- ) {
- return fileAction(fileName, &statbuf, userData, depth);
- }
-
- if (!(flags & ACTION_RECURSE)) {
- return dirAction(fileName, &statbuf, userData, depth);
- }
- if (!(flags & ACTION_DEPTHFIRST)) {
- status = dirAction(fileName, &statbuf, userData, depth);
- if (status == FALSE)
- goto done_nak_warn;
- if (status == SKIP)
- return TRUE;
- }
- dir = opendir(fileName);
- if (!dir) {
-
-
-
- goto done_nak_warn;
- }
- status = TRUE;
- while ((next = readdir(dir)) != NULL) {
- char *nextFile;
- int s;
- nextFile = concat_subpath_file(fileName, next->d_name);
- if (nextFile == NULL)
- continue;
-
- s = recursive_action(nextFile, flags, fileAction, dirAction,
- userData, depth + 1);
- if (s == FALSE)
- status = FALSE;
- free(nextFile);
- }
- closedir(dir);
- if (flags & ACTION_DEPTHFIRST) {
- if (!dirAction(fileName, &statbuf, userData, depth))
- goto done_nak_warn;
- }
- return status;
- done_nak_warn:
- if (!(flags & ACTION_QUIET))
- bb_simple_perror_msg(fileName);
- return FALSE;
- }
|