diff options
author | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2007-03-29 10:30:50 +0000 |
---|---|---|
committer | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2007-03-29 10:30:50 +0000 |
commit | 3e816c1252cc55e3763f946622129d31ea1f0f20 (patch) | |
tree | 5031fd816b1df09eaa897530a37ce814bba95011 | |
parent | 3d43edb28c80ee9cb54335f593d42d5d0471e15a (diff) | |
download | busybox-w32-3e816c1252cc55e3763f946622129d31ea1f0f20.tar.gz busybox-w32-3e816c1252cc55e3763f946622129d31ea1f0f20.tar.bz2 busybox-w32-3e816c1252cc55e3763f946622129d31ea1f0f20.zip |
- fold recurse, depthFirst and dereference params into one param flags.
Minor size improvement (-16b for size, -24b according to bloat-o-meter).
-rw-r--r-- | archival/tar.c | 5 | ||||
-rw-r--r-- | coreutils/chmod.c | 2 | ||||
-rw-r--r-- | coreutils/chown.c | 13 | ||||
-rw-r--r-- | coreutils/diff.c | 3 | ||||
-rw-r--r-- | debianutils/run_parts.c | 4 | ||||
-rw-r--r-- | findutils/find.c | 14 | ||||
-rw-r--r-- | findutils/grep.c | 6 | ||||
-rw-r--r-- | include/libbb.h | 9 | ||||
-rw-r--r-- | libbb/recursive_action.c | 41 | ||||
-rw-r--r-- | modutils/insmod.c | 4 | ||||
-rw-r--r-- | selinux/chcon.c | 4 |
11 files changed, 50 insertions, 55 deletions
diff --git a/archival/tar.c b/archival/tar.c index 176a7e22d..5a6ef60db 100644 --- a/archival/tar.c +++ b/archival/tar.c | |||
@@ -562,8 +562,9 @@ static int writeTarFile(const int tar_fd, const int verboseFlag, | |||
562 | 562 | ||
563 | /* Read the directory/files and iterate over them one at a time */ | 563 | /* Read the directory/files and iterate over them one at a time */ |
564 | while (include) { | 564 | while (include) { |
565 | if (!recursive_action(include->data, TRUE, dereferenceFlag, | 565 | if (!recursive_action(include->data, (action_recurse | |
566 | FALSE, writeFileToTarball, writeFileToTarball, &tbInfo, 0)) | 566 | dereferenceFlag ? action_followLinks : 0), |
567 | writeFileToTarball, writeFileToTarball, &tbInfo, 0)) | ||
567 | { | 568 | { |
568 | errorFlag = TRUE; | 569 | errorFlag = TRUE; |
569 | } | 570 | } |
diff --git a/coreutils/chmod.c b/coreutils/chmod.c index 11c6731a1..9a73218a1 100644 --- a/coreutils/chmod.c +++ b/coreutils/chmod.c | |||
@@ -101,8 +101,6 @@ int chmod_main(int argc, char **argv) | |||
101 | do { | 101 | do { |
102 | if (!recursive_action(*argv, | 102 | if (!recursive_action(*argv, |
103 | OPT_RECURSE, // recurse | 103 | OPT_RECURSE, // recurse |
104 | FALSE, // follow links: coreutils doesn't | ||
105 | FALSE, // depth first | ||
106 | fileAction, // file action | 104 | fileAction, // file action |
107 | fileAction, // dir action | 105 | fileAction, // dir action |
108 | smode, // user data | 106 | smode, // user data |
diff --git a/coreutils/chown.c b/coreutils/chown.c index f92299e36..09b1a595b 100644 --- a/coreutils/chown.c +++ b/coreutils/chown.c | |||
@@ -92,13 +92,12 @@ int chown_main(int argc, char **argv) | |||
92 | } | 92 | } |
93 | 93 | ||
94 | if (!recursive_action(arg, | 94 | if (!recursive_action(arg, |
95 | OPT_RECURSE, // recurse | 95 | (OPT_RECURSE ? action_recurse : 0 | /* recurse */ |
96 | OPT_TRAVERSE, // follow links if -L | 96 | OPT_TRAVERSE ? action_followLinks : 0),/* follow links if -L */ |
97 | FALSE, // depth first | 97 | fileAction, /* file action */ |
98 | fileAction, // file action | 98 | fileAction, /* dir action */ |
99 | fileAction, // dir action | 99 | chown_func, /* user data */ |
100 | chown_func, // user data | 100 | 0) /* depth */ |
101 | 0) // depth | ||
102 | ) { | 101 | ) { |
103 | retval = EXIT_FAILURE; | 102 | retval = EXIT_FAILURE; |
104 | } | 103 | } |
diff --git a/coreutils/diff.c b/coreutils/diff.c index 911bfcf4d..1903bb151 100644 --- a/coreutils/diff.c +++ b/coreutils/diff.c | |||
@@ -1079,7 +1079,8 @@ static char **get_dir(char *path) | |||
1079 | * add_to_dirlist then removes root dir prefix. */ | 1079 | * add_to_dirlist then removes root dir prefix. */ |
1080 | 1080 | ||
1081 | if (option_mask32 & FLAG_r) { | 1081 | if (option_mask32 & FLAG_r) { |
1082 | recursive_action(path, TRUE, TRUE, FALSE, add_to_dirlist, NULL, | 1082 | recursive_action(path, action_recurse|action_followLinks, |
1083 | add_to_dirlist, NULL, | ||
1083 | (void*)(strlen(path)+1), 0); | 1084 | (void*)(strlen(path)+1), 0); |
1084 | } else { | 1085 | } else { |
1085 | DIR *dp; | 1086 | DIR *dp; |
diff --git a/debianutils/run_parts.c b/debianutils/run_parts.c index b41045a36..c67072730 100644 --- a/debianutils/run_parts.c +++ b/debianutils/run_parts.c | |||
@@ -138,9 +138,7 @@ int run_parts_main(int argc, char **argv) | |||
138 | G.cmd[tmp] = arg_list->data; | 138 | G.cmd[tmp] = arg_list->data; |
139 | /* G.cmd[tmp] = NULL; - G is already zeroed out */ | 139 | /* G.cmd[tmp] = NULL; - G is already zeroed out */ |
140 | if (!recursive_action(argv[argc - 1], | 140 | if (!recursive_action(argv[argc - 1], |
141 | TRUE, /* recurse */ | 141 | action_recurse|action_followLinks, |
142 | TRUE, /* follow links */ | ||
143 | FALSE, /* depth first */ | ||
144 | act, /* file action */ | 142 | act, /* file action */ |
145 | act, /* dir action */ | 143 | act, /* dir action */ |
146 | NULL, /* user data */ | 144 | NULL, /* user data */ |
diff --git a/findutils/find.c b/findutils/find.c index e98d995a4..c043fbc7d 100644 --- a/findutils/find.c +++ b/findutils/find.c | |||
@@ -574,7 +574,7 @@ static action*** parse_params(char **argv) | |||
574 | int find_main(int argc, char **argv); | 574 | int find_main(int argc, char **argv); |
575 | int find_main(int argc, char **argv) | 575 | int find_main(int argc, char **argv) |
576 | { | 576 | { |
577 | int dereference = FALSE; | 577 | bool dereference = FALSE; |
578 | char *arg; | 578 | char *arg; |
579 | char **argp; | 579 | char **argp; |
580 | int i, firstopt, status = EXIT_SUCCESS; | 580 | int i, firstopt, status = EXIT_SUCCESS; |
@@ -632,13 +632,11 @@ int find_main(int argc, char **argv) | |||
632 | 632 | ||
633 | for (i = 1; i < firstopt; i++) { | 633 | for (i = 1; i < firstopt; i++) { |
634 | if (!recursive_action(argv[i], | 634 | if (!recursive_action(argv[i], |
635 | TRUE, // recurse | 635 | action_recurse|(1<<dereference), /* flags */ |
636 | dereference, // follow links | 636 | fileAction, /* file action */ |
637 | FALSE, // depth first | 637 | fileAction, /* dir action */ |
638 | fileAction, // file action | 638 | NULL, /* user data */ |
639 | fileAction, // dir action | 639 | 0)) /* depth */ |
640 | NULL, // user data | ||
641 | 0)) // depth | ||
642 | status = EXIT_FAILURE; | 640 | status = EXIT_FAILURE; |
643 | } | 641 | } |
644 | return status; | 642 | return status; |
diff --git a/findutils/grep.c b/findutils/grep.c index f6ea54ed2..43810ea84 100644 --- a/findutils/grep.c +++ b/findutils/grep.c | |||
@@ -336,9 +336,9 @@ static int grep_dir(const char *dir) | |||
336 | { | 336 | { |
337 | int matched = 0; | 337 | int matched = 0; |
338 | recursive_action(dir, | 338 | recursive_action(dir, |
339 | /* recurse= */ 1, | 339 | /* recurse= */ action_recurse | |
340 | /* followLinks= */ 0, | 340 | /* followLinks= */ /* no. 0 | */ |
341 | /* depthFirst= */ 1, | 341 | /* depthFirst= */ action_depthFirst, |
342 | /* fileAction= */ file_action_grep, | 342 | /* fileAction= */ file_action_grep, |
343 | /* dirAction= */ NULL, | 343 | /* dirAction= */ NULL, |
344 | /* userData= */ &matched, | 344 | /* userData= */ &matched, |
diff --git a/include/libbb.h b/include/libbb.h index d734abed6..7f0ad2c25 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -231,11 +231,14 @@ extern const char *bb_mode_string(mode_t mode); | |||
231 | extern int is_directory(const char *name, int followLinks, struct stat *statBuf); | 231 | extern int is_directory(const char *name, int followLinks, struct stat *statBuf); |
232 | extern int remove_file(const char *path, int flags); | 232 | extern int remove_file(const char *path, int flags); |
233 | extern int copy_file(const char *source, const char *dest, int flags); | 233 | extern int copy_file(const char *source, const char *dest, int flags); |
234 | extern int recursive_action(const char *fileName, int recurse, | 234 | #define action_recurse (1<<0) |
235 | int followLinks, int depthFirst, | 235 | #define action_followLinks (1<<1) |
236 | #define action_depthFirst (1<<2) | ||
237 | #define action_reverse (1<<3) | ||
238 | extern int recursive_action(const char *fileName, unsigned flags, | ||
236 | int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData, int depth), | 239 | int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData, int depth), |
237 | int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData, int depth), | 240 | int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData, int depth), |
238 | void* userData, int depth); | 241 | void* userData, const unsigned depth); |
239 | extern int device_open(const char *device, int mode); | 242 | extern int device_open(const char *device, int mode); |
240 | extern int get_console_fd(void); | 243 | extern int get_console_fd(void); |
241 | extern char *find_block_device(const char *path); | 244 | extern char *find_block_device(const char *path); |
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 @@ | |||
22 | * is so stinking huge. | 22 | * is so stinking huge. |
23 | */ | 23 | */ |
24 | 24 | ||
25 | static int true_action(const char *fileName, struct stat *statbuf, void* userData, int depth) | 25 | static int true_action(const char *fileName, struct stat *statbuf, |
26 | void* userData, int depth) | ||
26 | { | 27 | { |
27 | return TRUE; | 28 | return TRUE; |
28 | } | 29 | } |
@@ -41,11 +42,11 @@ static int true_action(const char *fileName, struct stat *statbuf, void* userDat | |||
41 | */ | 42 | */ |
42 | 43 | ||
43 | int recursive_action(const char *fileName, | 44 | int recursive_action(const char *fileName, |
44 | int recurse, int followLinks, int depthFirst, | 45 | unsigned flags, |
45 | int (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth), | 46 | int (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth), |
46 | int (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth), | 47 | int (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth), |
47 | void* userData, | 48 | void* userData, |
48 | int depth) | 49 | const unsigned depth) |
49 | { | 50 | { |
50 | struct stat statbuf; | 51 | struct stat statbuf; |
51 | int status; | 52 | int status; |
@@ -54,22 +55,20 @@ int recursive_action(const char *fileName, | |||
54 | 55 | ||
55 | if (!fileAction) fileAction = true_action; | 56 | if (!fileAction) fileAction = true_action; |
56 | if (!dirAction) dirAction = true_action; | 57 | if (!dirAction) dirAction = true_action; |
57 | 58 | status = (flags & action_followLinks ? stat : lstat)(fileName, &statbuf); | |
58 | status = (followLinks ? stat : lstat)(fileName, &statbuf); | ||
59 | 59 | ||
60 | if (status < 0) { | 60 | if (status < 0) { |
61 | #ifdef DEBUG_RECURS_ACTION | 61 | #ifdef DEBUG_RECURS_ACTION |
62 | bb_error_msg("status=%d followLinks=%d TRUE=%d", | 62 | bb_error_msg("status=%d followLinks=%d TRUE=%d", |
63 | status, followLinks, TRUE); | 63 | status, flags & action_followLinks, TRUE); |
64 | #endif | 64 | #endif |
65 | bb_perror_msg("%s", fileName); | 65 | goto done_nak_warn; |
66 | return FALSE; | ||
67 | } | 66 | } |
68 | 67 | ||
69 | /* If S_ISLNK(m), then we know that !S_ISDIR(m). | 68 | /* If S_ISLNK(m), then we know that !S_ISDIR(m). |
70 | * Then we can skip checking first part: if it is true, then | 69 | * Then we can skip checking first part: if it is true, then |
71 | * (!dir) is also true! */ | 70 | * (!dir) is also true! */ |
72 | if ( /* (!followLinks && S_ISLNK(statbuf.st_mode)) || */ | 71 | if ( /* (!(flags & action_followLinks) && S_ISLNK(statbuf.st_mode)) || */ |
73 | !S_ISDIR(statbuf.st_mode) | 72 | !S_ISDIR(statbuf.st_mode) |
74 | ) { | 73 | ) { |
75 | return fileAction(fileName, &statbuf, userData, depth); | 74 | return fileAction(fileName, &statbuf, userData, depth); |
@@ -77,15 +76,14 @@ int recursive_action(const char *fileName, | |||
77 | 76 | ||
78 | /* It's a directory (or a link to one, and followLinks is set) */ | 77 | /* It's a directory (or a link to one, and followLinks is set) */ |
79 | 78 | ||
80 | if (!recurse) { | 79 | if (!(flags & action_recurse)) { |
81 | return dirAction(fileName, &statbuf, userData, depth); | 80 | return dirAction(fileName, &statbuf, userData, depth); |
82 | } | 81 | } |
83 | 82 | ||
84 | if (!depthFirst) { | 83 | if (!(flags & action_depthFirst)) { |
85 | status = dirAction(fileName, &statbuf, userData, depth); | 84 | status = dirAction(fileName, &statbuf, userData, depth); |
86 | if (!status) { | 85 | if (!status) { |
87 | bb_perror_msg("%s", fileName); | 86 | goto done_nak_warn; |
88 | return FALSE; | ||
89 | } | 87 | } |
90 | if (status == SKIP) | 88 | if (status == SKIP) |
91 | return TRUE; | 89 | return TRUE; |
@@ -96,8 +94,7 @@ int recursive_action(const char *fileName, | |||
96 | /* findutils-4.1.20 reports this */ | 94 | /* findutils-4.1.20 reports this */ |
97 | /* (i.e. it doesn't silently return with exit code 1) */ | 95 | /* (i.e. it doesn't silently return with exit code 1) */ |
98 | /* To trigger: "find -exec rm -rf {} \;" */ | 96 | /* To trigger: "find -exec rm -rf {} \;" */ |
99 | bb_perror_msg("%s", fileName); | 97 | goto done_nak_warn; |
100 | return FALSE; | ||
101 | } | 98 | } |
102 | status = TRUE; | 99 | status = TRUE; |
103 | while ((next = readdir(dir)) != NULL) { | 100 | while ((next = readdir(dir)) != NULL) { |
@@ -106,21 +103,23 @@ int recursive_action(const char *fileName, | |||
106 | nextFile = concat_subpath_file(fileName, next->d_name); | 103 | nextFile = concat_subpath_file(fileName, next->d_name); |
107 | if (nextFile == NULL) | 104 | if (nextFile == NULL) |
108 | continue; | 105 | continue; |
109 | if (!recursive_action(nextFile, TRUE, followLinks, depthFirst, | 106 | /* now descend into it, forcing recursion. */ |
107 | if (!recursive_action(nextFile, flags | action_recurse, | ||
110 | fileAction, dirAction, userData, depth+1)) { | 108 | fileAction, dirAction, userData, depth+1)) { |
111 | status = FALSE; | 109 | status = FALSE; |
112 | } | 110 | } |
113 | free(nextFile); | 111 | free(nextFile); |
114 | } | 112 | } |
115 | closedir(dir); | 113 | closedir(dir); |
116 | if (depthFirst) { | 114 | if (flags & action_depthFirst && |
117 | if (!dirAction(fileName, &statbuf, userData, depth)) { | 115 | !dirAction(fileName, &statbuf, userData, depth)) { |
118 | bb_perror_msg("%s", fileName); | 116 | goto done_nak_warn; |
119 | return FALSE; | ||
120 | } | ||
121 | } | 117 | } |
122 | 118 | ||
123 | if (!status) | 119 | if (!status) |
124 | return FALSE; | 120 | return FALSE; |
125 | return TRUE; | 121 | return TRUE; |
122 | done_nak_warn: | ||
123 | bb_perror_msg("%s", fileName); | ||
124 | return FALSE; | ||
126 | } | 125 | } |
diff --git a/modutils/insmod.c b/modutils/insmod.c index 57092f79a..075969dcb 100644 --- a/modutils/insmod.c +++ b/modutils/insmod.c | |||
@@ -4044,7 +4044,7 @@ int insmod_main( int argc, char **argv) | |||
4044 | module_dir = tmdn; | 4044 | module_dir = tmdn; |
4045 | else | 4045 | else |
4046 | module_dir = real_module_dir; | 4046 | module_dir = real_module_dir; |
4047 | recursive_action(module_dir, TRUE, FALSE, FALSE, | 4047 | recursive_action(module_dir, action_recurse, |
4048 | check_module_name_match, 0, m_fullName, 0); | 4048 | check_module_name_match, 0, m_fullName, 0); |
4049 | free(tmdn); | 4049 | free(tmdn); |
4050 | } | 4050 | } |
@@ -4059,7 +4059,7 @@ int insmod_main( int argc, char **argv) | |||
4059 | strcpy(module_dir, _PATH_MODULES); | 4059 | strcpy(module_dir, _PATH_MODULES); |
4060 | /* No module found under /lib/modules/`uname -r`, this | 4060 | /* No module found under /lib/modules/`uname -r`, this |
4061 | * time cast the net a bit wider. Search /lib/modules/ */ | 4061 | * time cast the net a bit wider. Search /lib/modules/ */ |
4062 | if (!recursive_action(module_dir, TRUE, FALSE, FALSE, | 4062 | if (!recursive_action(module_dir, action_recurse, |
4063 | check_module_name_match, 0, m_fullName, 0) | 4063 | check_module_name_match, 0, m_fullName, 0) |
4064 | ) { | 4064 | ) { |
4065 | if (m_filename == 0 | 4065 | if (m_filename == 0 |
diff --git a/selinux/chcon.c b/selinux/chcon.c index 72cfa93c3..0eab6864e 100644 --- a/selinux/chcon.c +++ b/selinux/chcon.c | |||
@@ -163,9 +163,7 @@ int chcon_main(int argc, char *argv[]) | |||
163 | fname[fname_len] = '\0'; | 163 | fname[fname_len] = '\0'; |
164 | 164 | ||
165 | if (recursive_action(fname, | 165 | if (recursive_action(fname, |
166 | option_mask32 & OPT_RECURSIVE, | 166 | 1<<option_mask32 & OPT_RECURSIVE, |
167 | FALSE, /* followLinks */ | ||
168 | FALSE, /* depthFirst */ | ||
169 | change_filedir_context, | 167 | change_filedir_context, |
170 | change_filedir_context, | 168 | change_filedir_context, |
171 | NULL, 0) != TRUE) | 169 | NULL, 0) != TRUE) |