- fold recurse, depthFirst and dereference params into one param flags.

Minor size improvement (-16b for size, -24b according to bloat-o-meter).
1_6_stable
Bernhard Reutner-Fischer 2007-03-29 10:30:50 +00:00
parent 3d43edb28c
commit 3e816c1252
11 changed files with 50 additions and 55 deletions

View File

@ -562,8 +562,9 @@ static int writeTarFile(const int tar_fd, const int verboseFlag,
/* Read the directory/files and iterate over them one at a time */ /* Read the directory/files and iterate over them one at a time */
while (include) { while (include) {
if (!recursive_action(include->data, TRUE, dereferenceFlag, if (!recursive_action(include->data, (action_recurse |
FALSE, writeFileToTarball, writeFileToTarball, &tbInfo, 0)) dereferenceFlag ? action_followLinks : 0),
writeFileToTarball, writeFileToTarball, &tbInfo, 0))
{ {
errorFlag = TRUE; errorFlag = TRUE;
} }

View File

@ -101,8 +101,6 @@ int chmod_main(int argc, char **argv)
do { do {
if (!recursive_action(*argv, if (!recursive_action(*argv,
OPT_RECURSE, // recurse OPT_RECURSE, // recurse
FALSE, // follow links: coreutils doesn't
FALSE, // depth first
fileAction, // file action fileAction, // file action
fileAction, // dir action fileAction, // dir action
smode, // user data smode, // user data

View File

@ -92,13 +92,12 @@ int chown_main(int argc, char **argv)
} }
if (!recursive_action(arg, if (!recursive_action(arg,
OPT_RECURSE, // recurse (OPT_RECURSE ? action_recurse : 0 | /* recurse */
OPT_TRAVERSE, // follow links if -L OPT_TRAVERSE ? action_followLinks : 0),/* follow links if -L */
FALSE, // depth first fileAction, /* file action */
fileAction, // file action fileAction, /* dir action */
fileAction, // dir action chown_func, /* user data */
chown_func, // user data 0) /* depth */
0) // depth
) { ) {
retval = EXIT_FAILURE; retval = EXIT_FAILURE;
} }

View File

@ -1079,7 +1079,8 @@ static char **get_dir(char *path)
* add_to_dirlist then removes root dir prefix. */ * add_to_dirlist then removes root dir prefix. */
if (option_mask32 & FLAG_r) { if (option_mask32 & FLAG_r) {
recursive_action(path, TRUE, TRUE, FALSE, add_to_dirlist, NULL, recursive_action(path, action_recurse|action_followLinks,
add_to_dirlist, NULL,
(void*)(strlen(path)+1), 0); (void*)(strlen(path)+1), 0);
} else { } else {
DIR *dp; DIR *dp;

View File

@ -138,9 +138,7 @@ int run_parts_main(int argc, char **argv)
G.cmd[tmp] = arg_list->data; G.cmd[tmp] = arg_list->data;
/* G.cmd[tmp] = NULL; - G is already zeroed out */ /* G.cmd[tmp] = NULL; - G is already zeroed out */
if (!recursive_action(argv[argc - 1], if (!recursive_action(argv[argc - 1],
TRUE, /* recurse */ action_recurse|action_followLinks,
TRUE, /* follow links */
FALSE, /* depth first */
act, /* file action */ act, /* file action */
act, /* dir action */ act, /* dir action */
NULL, /* user data */ NULL, /* user data */

View File

@ -574,7 +574,7 @@ static action*** parse_params(char **argv)
int find_main(int argc, char **argv); int find_main(int argc, char **argv);
int find_main(int argc, char **argv) int find_main(int argc, char **argv)
{ {
int dereference = FALSE; bool dereference = FALSE;
char *arg; char *arg;
char **argp; char **argp;
int i, firstopt, status = EXIT_SUCCESS; int i, firstopt, status = EXIT_SUCCESS;
@ -632,13 +632,11 @@ int find_main(int argc, char **argv)
for (i = 1; i < firstopt; i++) { for (i = 1; i < firstopt; i++) {
if (!recursive_action(argv[i], if (!recursive_action(argv[i],
TRUE, // recurse action_recurse|(1<<dereference), /* flags */
dereference, // follow links fileAction, /* file action */
FALSE, // depth first fileAction, /* dir action */
fileAction, // file action NULL, /* user data */
fileAction, // dir action 0)) /* depth */
NULL, // user data
0)) // depth
status = EXIT_FAILURE; status = EXIT_FAILURE;
} }
return status; return status;

View File

@ -336,9 +336,9 @@ static int grep_dir(const char *dir)
{ {
int matched = 0; int matched = 0;
recursive_action(dir, recursive_action(dir,
/* recurse= */ 1, /* recurse= */ action_recurse |
/* followLinks= */ 0, /* followLinks= */ /* no. 0 | */
/* depthFirst= */ 1, /* depthFirst= */ action_depthFirst,
/* fileAction= */ file_action_grep, /* fileAction= */ file_action_grep,
/* dirAction= */ NULL, /* dirAction= */ NULL,
/* userData= */ &matched, /* userData= */ &matched,

View File

@ -231,11 +231,14 @@ extern const char *bb_mode_string(mode_t mode);
extern int is_directory(const char *name, int followLinks, struct stat *statBuf); extern int is_directory(const char *name, int followLinks, struct stat *statBuf);
extern int remove_file(const char *path, int flags); extern int remove_file(const char *path, int flags);
extern int copy_file(const char *source, const char *dest, int flags); extern int copy_file(const char *source, const char *dest, int flags);
extern int recursive_action(const char *fileName, int recurse, #define action_recurse (1<<0)
int followLinks, int depthFirst, #define action_followLinks (1<<1)
#define action_depthFirst (1<<2)
#define action_reverse (1<<3)
extern int recursive_action(const char *fileName, unsigned flags,
int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData, int depth), int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData, int depth),
int (*dirAction) (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); void* userData, const unsigned depth);
extern int device_open(const char *device, int mode); extern int device_open(const char *device, int mode);
extern int get_console_fd(void); extern int get_console_fd(void);
extern char *find_block_device(const char *path); extern char *find_block_device(const char *path);

View File

@ -22,7 +22,8 @@
* is so stinking huge. * 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; 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 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 (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
int (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth), int (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
void* userData, void* userData,
int depth) const unsigned depth)
{ {
struct stat statbuf; struct stat statbuf;
int status; int status;
@ -54,22 +55,20 @@ int recursive_action(const char *fileName,
if (!fileAction) fileAction = true_action; if (!fileAction) fileAction = true_action;
if (!dirAction) dirAction = true_action; if (!dirAction) dirAction = true_action;
status = (flags & action_followLinks ? stat : lstat)(fileName, &statbuf);
status = (followLinks ? stat : lstat)(fileName, &statbuf);
if (status < 0) { if (status < 0) {
#ifdef DEBUG_RECURS_ACTION #ifdef DEBUG_RECURS_ACTION
bb_error_msg("status=%d followLinks=%d TRUE=%d", bb_error_msg("status=%d followLinks=%d TRUE=%d",
status, followLinks, TRUE); status, flags & action_followLinks, TRUE);
#endif #endif
bb_perror_msg("%s", fileName); goto done_nak_warn;
return FALSE;
} }
/* If S_ISLNK(m), then we know that !S_ISDIR(m). /* If S_ISLNK(m), then we know that !S_ISDIR(m).
* Then we can skip checking first part: if it is true, then * Then we can skip checking first part: if it is true, then
* (!dir) is also true! */ * (!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) !S_ISDIR(statbuf.st_mode)
) { ) {
return fileAction(fileName, &statbuf, userData, depth); 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) */ /* 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); return dirAction(fileName, &statbuf, userData, depth);
} }
if (!depthFirst) { if (!(flags & action_depthFirst)) {
status = dirAction(fileName, &statbuf, userData, depth); status = dirAction(fileName, &statbuf, userData, depth);
if (!status) { if (!status) {
bb_perror_msg("%s", fileName); goto done_nak_warn;
return FALSE;
} }
if (status == SKIP) if (status == SKIP)
return TRUE; return TRUE;
@ -96,8 +94,7 @@ int recursive_action(const char *fileName,
/* findutils-4.1.20 reports this */ /* findutils-4.1.20 reports this */
/* (i.e. it doesn't silently return with exit code 1) */ /* (i.e. it doesn't silently return with exit code 1) */
/* To trigger: "find -exec rm -rf {} \;" */ /* To trigger: "find -exec rm -rf {} \;" */
bb_perror_msg("%s", fileName); goto done_nak_warn;
return FALSE;
} }
status = TRUE; status = TRUE;
while ((next = readdir(dir)) != NULL) { while ((next = readdir(dir)) != NULL) {
@ -106,21 +103,23 @@ int recursive_action(const char *fileName,
nextFile = concat_subpath_file(fileName, next->d_name); nextFile = concat_subpath_file(fileName, next->d_name);
if (nextFile == NULL) if (nextFile == NULL)
continue; 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)) { fileAction, dirAction, userData, depth+1)) {
status = FALSE; status = FALSE;
} }
free(nextFile); free(nextFile);
} }
closedir(dir); closedir(dir);
if (depthFirst) { if (flags & action_depthFirst &&
if (!dirAction(fileName, &statbuf, userData, depth)) { !dirAction(fileName, &statbuf, userData, depth)) {
bb_perror_msg("%s", fileName); goto done_nak_warn;
return FALSE;
}
} }
if (!status) if (!status)
return FALSE; return FALSE;
return TRUE; return TRUE;
done_nak_warn:
bb_perror_msg("%s", fileName);
return FALSE;
} }

View File

@ -4044,7 +4044,7 @@ int insmod_main( int argc, char **argv)
module_dir = tmdn; module_dir = tmdn;
else else
module_dir = real_module_dir; module_dir = real_module_dir;
recursive_action(module_dir, TRUE, FALSE, FALSE, recursive_action(module_dir, action_recurse,
check_module_name_match, 0, m_fullName, 0); check_module_name_match, 0, m_fullName, 0);
free(tmdn); free(tmdn);
} }
@ -4059,7 +4059,7 @@ int insmod_main( int argc, char **argv)
strcpy(module_dir, _PATH_MODULES); strcpy(module_dir, _PATH_MODULES);
/* No module found under /lib/modules/`uname -r`, this /* No module found under /lib/modules/`uname -r`, this
* time cast the net a bit wider. Search /lib/modules/ */ * time cast the net a bit wider. Search /lib/modules/ */
if (!recursive_action(module_dir, TRUE, FALSE, FALSE, if (!recursive_action(module_dir, action_recurse,
check_module_name_match, 0, m_fullName, 0) check_module_name_match, 0, m_fullName, 0)
) { ) {
if (m_filename == 0 if (m_filename == 0

View File

@ -163,9 +163,7 @@ int chcon_main(int argc, char *argv[])
fname[fname_len] = '\0'; fname[fname_len] = '\0';
if (recursive_action(fname, if (recursive_action(fname,
option_mask32 & OPT_RECURSIVE, 1<<option_mask32 & OPT_RECURSIVE,
FALSE, /* followLinks */
FALSE, /* depthFirst */
change_filedir_context, change_filedir_context,
change_filedir_context, change_filedir_context,
NULL, 0) != TRUE) NULL, 0) != TRUE)