1
2
3
4
5
6
7
8
9
10#include "libbb.h"
11
12#undef DEBUG_RECURS_ACTION
13
14
15
16
17
18
19
20
21
22
23
24
25static int FAST_FUNC true_action(const char *fileName UNUSED_PARAM,
26 struct stat *statbuf UNUSED_PARAM,
27 void* userData UNUSED_PARAM,
28 int depth UNUSED_PARAM)
29{
30 return TRUE;
31}
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56int FAST_FUNC recursive_action(const char *fileName,
57 unsigned flags,
58 int FAST_FUNC (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
59 int FAST_FUNC (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
60 void* userData,
61 unsigned depth)
62{
63 struct stat statbuf;
64 unsigned follow;
65 int status;
66 DIR *dir;
67 struct dirent *next;
68
69 if (!fileAction) fileAction = true_action;
70 if (!dirAction) dirAction = true_action;
71
72 follow = ACTION_FOLLOWLINKS;
73 if (depth == 0)
74 follow = ACTION_FOLLOWLINKS | ACTION_FOLLOWLINKS_L0;
75 follow &= flags;
76 status = (follow ? stat : lstat)(fileName, &statbuf);
77 if (status < 0) {
78#ifdef DEBUG_RECURS_ACTION
79 bb_error_msg("status=%d flags=%x", status, flags);
80#endif
81 if ((flags & ACTION_DANGLING_OK)
82 && errno == ENOENT
83 && lstat(fileName, &statbuf) == 0
84 ) {
85
86 return fileAction(fileName, &statbuf, userData, depth);
87 }
88 goto done_nak_warn;
89 }
90
91
92
93
94 if (
95 !S_ISDIR(statbuf.st_mode)
96 ) {
97 return fileAction(fileName, &statbuf, userData, depth);
98 }
99
100
101
102 if (!(flags & ACTION_RECURSE)) {
103 return dirAction(fileName, &statbuf, userData, depth);
104 }
105
106 if (!(flags & ACTION_DEPTHFIRST)) {
107 status = dirAction(fileName, &statbuf, userData, depth);
108 if (!status)
109 goto done_nak_warn;
110 if (status == SKIP)
111 return TRUE;
112 }
113
114 dir = opendir(fileName);
115 if (!dir) {
116
117
118
119 goto done_nak_warn;
120 }
121 status = TRUE;
122 while ((next = readdir(dir)) != NULL) {
123 char *nextFile;
124
125 nextFile = concat_subpath_file(fileName, next->d_name);
126 if (nextFile == NULL)
127 continue;
128
129 if (!recursive_action(nextFile, flags, fileAction, dirAction,
130 userData, depth + 1))
131 status = FALSE;
132
133
134 free(nextFile);
135
136
137
138
139
140
141
142 }
143 closedir(dir);
144
145 if (flags & ACTION_DEPTHFIRST) {
146 if (!dirAction(fileName, &statbuf, userData, depth))
147 goto done_nak_warn;
148 }
149
150 return status;
151
152 done_nak_warn:
153 if (!(flags & ACTION_QUIET))
154 bb_simple_perror_msg(fileName);
155 return FALSE;
156}
157