recursive_action: preparatory changes. will introduce "int level".

This commit is contained in:
Denis Vlasenko 2006-10-27 17:59:14 +00:00
parent 51b4c92f80
commit 3b8fc1c582

View File

@ -22,20 +22,27 @@
* and so isn't sufficiently portable to take over since glibc2.1 * and so isn't sufficiently portable to take over since glibc2.1
* is so stinking huge. * is so stinking huge.
*/ */
static int true_action(const char *fileName, struct stat *statbuf, void* userData)
{
return TRUE;
}
int recursive_action(const char *fileName, int recursive_action(const char *fileName,
int recurse, int followLinks, int depthFirst, int recurse, int followLinks, int depthFirst,
int (*fileAction) (const char *fileName, struct stat * statbuf, void* userData), int (*fileAction) (const char *fileName, struct stat * statbuf, void* userData),
int (*dirAction) (const char *fileName, struct stat * statbuf, void* userData), int (*dirAction) (const char *fileName, struct stat * statbuf, void* userData),
void* userData) void* userData)
{ {
int status;
struct stat statbuf; struct stat statbuf;
int status;
DIR *dir;
struct dirent *next; struct dirent *next;
if (followLinks) if (!fileAction) fileAction = true_action;
status = stat(fileName, &statbuf); if (!dirAction) dirAction = true_action;
else
status = lstat(fileName, &statbuf); status = (followLinks ? stat : lstat)(fileName, &statbuf);
if (status < 0) { if (status < 0) {
#ifdef DEBUG_RECURS_ACTION #ifdef DEBUG_RECURS_ACTION
@ -47,32 +54,28 @@ int recursive_action(const char *fileName,
} }
if (!followLinks && (S_ISLNK(statbuf.st_mode))) { if (!followLinks && (S_ISLNK(statbuf.st_mode))) {
if (fileAction == NULL)
return TRUE;
else
return fileAction(fileName, &statbuf, userData); return fileAction(fileName, &statbuf, userData);
} }
if (!recurse) { if (!recurse) {
if (S_ISDIR(statbuf.st_mode)) { if (S_ISDIR(statbuf.st_mode)) {
if (dirAction != NULL) return dirAction(fileName, &statbuf, userData);
return (dirAction(fileName, &statbuf, userData));
else
return TRUE;
} }
} }
if (S_ISDIR(statbuf.st_mode)) { if (!S_ISDIR(statbuf.st_mode))
DIR *dir; return fileAction(fileName, &statbuf, userData);
if (dirAction != NULL && !depthFirst) { if (!depthFirst) {
status = dirAction(fileName, &statbuf, userData); status = dirAction(fileName, &statbuf, userData);
if (!status) { if (!status) {
bb_perror_msg("%s", fileName); bb_perror_msg("%s", fileName);
return FALSE; return FALSE;
} else if (status == SKIP) }
if (status == SKIP)
return TRUE; return TRUE;
} }
dir = opendir(fileName); dir = opendir(fileName);
if (!dir) { if (!dir) {
return FALSE; return FALSE;
@ -91,19 +94,14 @@ int recursive_action(const char *fileName,
free(nextFile); free(nextFile);
} }
closedir(dir); closedir(dir);
if (dirAction != NULL && depthFirst) { if (depthFirst) {
if (!dirAction(fileName, &statbuf, userData)) { if (!dirAction(fileName, &statbuf, userData)) {
bb_perror_msg("%s", fileName); bb_perror_msg("%s", fileName);
return FALSE; return FALSE;
} }
} }
if (!status) if (!status)
return FALSE; return FALSE;
} else {
if (fileAction == NULL)
return TRUE;
else
return fileAction(fileName, &statbuf, userData);
}
return TRUE; return TRUE;
} }