summaryrefslogtreecommitdiff
path: root/libbb/recursive_action.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-10-27 17:59:14 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-10-27 17:59:14 +0000
commit3b8fc1c582884d356df04b4984db3963bc129c48 (patch)
tree418835c710efda550d251084822b51eeba4fdeb0 /libbb/recursive_action.c
parent51b4c92f80dc232b9d34985f99a4479393644433 (diff)
downloadbusybox-w32-3b8fc1c582884d356df04b4984db3963bc129c48.tar.gz
busybox-w32-3b8fc1c582884d356df04b4984db3963bc129c48.tar.bz2
busybox-w32-3b8fc1c582884d356df04b4984db3963bc129c48.zip
recursive_action: preparatory changes. will introduce "int level".
Diffstat (limited to 'libbb/recursive_action.c')
-rw-r--r--libbb/recursive_action.c98
1 files changed, 48 insertions, 50 deletions
diff --git a/libbb/recursive_action.c b/libbb/recursive_action.c
index 6949e34f3..0d6567dda 100644
--- a/libbb/recursive_action.c
+++ b/libbb/recursive_action.c
@@ -22,20 +22,27 @@
22 * and so isn't sufficiently portable to take over since glibc2.1 22 * and so isn't sufficiently portable to take over since glibc2.1
23 * is so stinking huge. 23 * is so stinking huge.
24 */ 24 */
25
26static int true_action(const char *fileName, struct stat *statbuf, void* userData)
27{
28 return TRUE;
29}
30
25int recursive_action(const char *fileName, 31int recursive_action(const char *fileName,
26 int recurse, int followLinks, int depthFirst, 32 int recurse, int followLinks, int depthFirst,
27 int (*fileAction) (const char *fileName, struct stat * statbuf, void* userData), 33 int (*fileAction) (const char *fileName, struct stat * statbuf, void* userData),
28 int (*dirAction) (const char *fileName, struct stat * statbuf, void* userData), 34 int (*dirAction) (const char *fileName, struct stat * statbuf, void* userData),
29 void* userData) 35 void* userData)
30{ 36{
31 int status;
32 struct stat statbuf; 37 struct stat statbuf;
38 int status;
39 DIR *dir;
33 struct dirent *next; 40 struct dirent *next;
34 41
35 if (followLinks) 42 if (!fileAction) fileAction = true_action;
36 status = stat(fileName, &statbuf); 43 if (!dirAction) dirAction = true_action;
37 else 44
38 status = lstat(fileName, &statbuf); 45 status = (followLinks ? stat : lstat)(fileName, &statbuf);
39 46
40 if (status < 0) { 47 if (status < 0) {
41#ifdef DEBUG_RECURS_ACTION 48#ifdef DEBUG_RECURS_ACTION
@@ -47,63 +54,54 @@ int recursive_action(const char *fileName,
47 } 54 }
48 55
49 if (!followLinks && (S_ISLNK(statbuf.st_mode))) { 56 if (!followLinks && (S_ISLNK(statbuf.st_mode))) {
50 if (fileAction == NULL) 57 return fileAction(fileName, &statbuf, userData);
51 return TRUE;
52 else
53 return fileAction(fileName, &statbuf, userData);
54 } 58 }
55 59
56 if (!recurse) { 60 if (!recurse) {
57 if (S_ISDIR(statbuf.st_mode)) { 61 if (S_ISDIR(statbuf.st_mode)) {
58 if (dirAction != NULL) 62 return dirAction(fileName, &statbuf, userData);
59 return (dirAction(fileName, &statbuf, userData));
60 else
61 return TRUE;
62 } 63 }
63 } 64 }
64 65
65 if (S_ISDIR(statbuf.st_mode)) { 66 if (!S_ISDIR(statbuf.st_mode))
66 DIR *dir; 67 return fileAction(fileName, &statbuf, userData);
67 68
68 if (dirAction != NULL && !depthFirst) { 69 if (!depthFirst) {
69 status = dirAction(fileName, &statbuf, userData); 70 status = dirAction(fileName, &statbuf, userData);
70 if (!status) { 71 if (!status) {
71 bb_perror_msg("%s", fileName); 72 bb_perror_msg("%s", fileName);
72 return FALSE;
73 } else if (status == SKIP)
74 return TRUE;
75 }
76 dir = opendir(fileName);
77 if (!dir) {
78 return FALSE; 73 return FALSE;
79 } 74 }
80 status = TRUE; 75 if (status == SKIP)
81 while ((next = readdir(dir)) != NULL) { 76 return TRUE;
82 char *nextFile; 77 }
83 78
84 nextFile = concat_subpath_file(fileName, next->d_name); 79 dir = opendir(fileName);
85 if(nextFile == NULL) 80 if (!dir) {
86 continue; 81 return FALSE;
87 if (!recursive_action(nextFile, TRUE, followLinks, depthFirst, 82 }
88 fileAction, dirAction, userData)) { 83 status = TRUE;
89 status = FALSE; 84 while ((next = readdir(dir)) != NULL) {
90 } 85 char *nextFile;
91 free(nextFile); 86
92 } 87 nextFile = concat_subpath_file(fileName, next->d_name);
93 closedir(dir); 88 if (nextFile == NULL)
94 if (dirAction != NULL && depthFirst) { 89 continue;
95 if (!dirAction(fileName, &statbuf, userData)) { 90 if (!recursive_action(nextFile, TRUE, followLinks, depthFirst,
96 bb_perror_msg("%s", fileName); 91 fileAction, dirAction, userData)) {
97 return FALSE; 92 status = FALSE;
98 }
99 } 93 }
100 if (!status) 94 free(nextFile);
95 }
96 closedir(dir);
97 if (depthFirst) {
98 if (!dirAction(fileName, &statbuf, userData)) {
99 bb_perror_msg("%s", fileName);
101 return FALSE; 100 return FALSE;
102 } else { 101 }
103 if (fileAction == NULL)
104 return TRUE;
105 else
106 return fileAction(fileName, &statbuf, userData);
107 } 102 }
103
104 if (!status)
105 return FALSE;
108 return TRUE; 106 return TRUE;
109} 107}