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,63 +54,54 @@ int recursive_action(const char *fileName,
} }
if (!followLinks && (S_ISLNK(statbuf.st_mode))) { if (!followLinks && (S_ISLNK(statbuf.st_mode))) {
if (fileAction == NULL) return fileAction(fileName, &statbuf, userData);
return TRUE;
else
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;
} else if (status == SKIP)
return TRUE;
}
dir = opendir(fileName);
if (!dir) {
return FALSE; return FALSE;
} }
status = TRUE; if (status == SKIP)
while ((next = readdir(dir)) != NULL) {
char *nextFile;
nextFile = concat_subpath_file(fileName, next->d_name);
if(nextFile == NULL)
continue;
if (!recursive_action(nextFile, TRUE, followLinks, depthFirst,
fileAction, dirAction, userData)) {
status = FALSE;
}
free(nextFile);
}
closedir(dir);
if (dirAction != NULL && depthFirst) {
if (!dirAction(fileName, &statbuf, userData)) {
bb_perror_msg("%s", fileName);
return FALSE;
}
}
if (!status)
return FALSE;
} else {
if (fileAction == NULL)
return TRUE; return TRUE;
else
return fileAction(fileName, &statbuf, userData);
} }
dir = opendir(fileName);
if (!dir) {
return FALSE;
}
status = TRUE;
while ((next = readdir(dir)) != NULL) {
char *nextFile;
nextFile = concat_subpath_file(fileName, next->d_name);
if (nextFile == NULL)
continue;
if (!recursive_action(nextFile, TRUE, followLinks, depthFirst,
fileAction, dirAction, userData)) {
status = FALSE;
}
free(nextFile);
}
closedir(dir);
if (depthFirst) {
if (!dirAction(fileName, &statbuf, userData)) {
bb_perror_msg("%s", fileName);
return FALSE;
}
}
if (!status)
return FALSE;
return TRUE; return TRUE;
} }