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 /libbb/recursive_action.c | |
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).
Diffstat (limited to 'libbb/recursive_action.c')
-rw-r--r-- | libbb/recursive_action.c | 41 |
1 files changed, 20 insertions, 21 deletions
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 | } |