-
Notifications
You must be signed in to change notification settings - Fork 7
/
fd_traversal.cc
355 lines (295 loc) · 7.34 KB
/
fd_traversal.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* Copyright (c) 2011 Akamai Technologies, Inc. */
#include "system.hh"
#include "cgen.h"
#include "log.h"
#include "encode.h"
#include "decode.h"
#include "decider.h"
#include "scanner_mode.h"
#include "config.h"
#include "path_dir_pair.h"
#include "counter.h"
#include "traversal.h"
#include "fd_traversal.h"
#include "scanner.h"
#include "scannable.h"
#include "fd_scannable.h"
#include "fake_fdopendir.h"
FDTraversal::FDTraversal()
:
root_path_(""),
worklist_(),
decider_(NULL),
prev_dir_(NULL),
counter_(NULL),
scanner_(NULL)
{}
FDTraversal::~FDTraversal() {}
int FDTraversal::Init(Counter* counter,
struct path_dir_pair root,
const Decider * decider,
Scanner* scanner)
{
CHK(worklist_.size() != 0, "non-empty traversal", out_error);
CHK(counter == NULL, "null counter", out_error);
CHK(root.dir == NULL, "null root.dir", out_error);
CHK(decider == NULL, "null decider", out_error);
CHK(scanner == NULL, "null scanner", out_error);
counter_ = counter;
worklist_.push_back(root);
decider_ = decider;
scanner_ = scanner;
root_path_ = root.path;
counter_->num_dirs = 1; // for the root dir
counter_->num_files = 0;
counter_->num_interesting = 0;
return 0;
out_error:
return 1;
}
int FDTraversal::get_next_dirent(int* dfd, struct dirent** dip)
{
int ret; /* ret is for recording errors encountered by readdir_r */
struct dirent* di; /* di is for restoring *dip after readdir_r sets
*dip = NULL. */
while(worklist_.size() > 0)
{
/* 0. Get a directory */
struct path_dir_pair p = worklist_.front();
/* 1. If it's new, make it current. */
if (prev_dir_ != p.dir)
{
*dfd = dirfd(p.dir);
prev_dir_ = p.dir;
}
/* 2. Sanity-check dip and p.dir, save *dip, and reset p.dir. */
TST(dip == NULL, errno = EINVAL,
"tried use a null dip", out_error);
TST(p.dir == NULL, errno = EINVAL,
"tried to read a null dir", out_error);
di = *dip;
/* 3. readdir_r() and handle errors. */
ret = readdir_r(p.dir, *dip, dip); // alternately, scandir
TST(ret > 0, errno = ret, "readdir_r failed", out_error);
TST(ret < 0, errno = EINVAL, "readdir_r went crazy", out_error);
/* 4. If you finished the current directory, try again;
otherwise, return. */
if (*dip == NULL)
{
worklist_.pop_front();
closedir(p.dir);
*dip = di;
}
else
{
*dfd = dirfd(p.dir);
return 0;
}
}
/* 5. Signal that iteration is complete by setting *dip = NULL. */
*dip = NULL;
return 0;
out_error:
return 1;
}
int FDTraversal::go(bool* done)
{
struct dirent di;
struct dirent* dip = &di;
int fd, dfd;
struct stat st;
CHK(get_next_dirent(&dfd, &dip) != 0,
"unable to get next dirent", out_error);
if (dip == NULL)
{
ostringstream oss;
oss << uri_encode(root_path_);
string qpath = oss.str();
write_c_line(&cout, qpath, counter_);
*done = true;
return 0;
}
else
{
*done = false;
}
// TRACE:
// dbg_print_current_path("now", di.d_name);
if (di.d_type != DT_DIR &&
di.d_type != DT_REG &&
di.d_type != DT_LNK &&
di.d_type != DT_UNKNOWN)
return 0;
if (strlen(di.d_name) > 0 && di.d_name[0] == '.')
return 0;
fd = -1;
counter_->num_files += 1;
if (di.d_type == DT_DIR)
return go_dir(dfd, di, fd, st);
else if (di.d_type == DT_REG)
return go_reg(dfd, di, fd, st);
else if (di.d_type == DT_LNK)
return go_lnk(dfd, di, false, st);
if (di.d_type == DT_UNKNOWN)
{
CHK(fstatat(dfd, di.d_name, &st, AT_SYMLINK_NOFOLLOW) == -1,
"unable to stat fd", out_error);
if (S_ISDIR(st.st_mode))
return go_dir(dfd, di, fd, st);
if (S_ISLNK(st.st_mode))
return go_lnk(dfd, di, true, st);
if (S_ISREG(st.st_mode))
return go_reg(dfd, di, fd, st);
}
return 0;
out_error:
return 1;
}
int FDTraversal::go_dir(int dfd,
const struct dirent& di,
int fd,
const struct stat&)
{
struct path_dir_pair p;
DIR* dir;
int newfd;
counter_->num_files -= 1;
counter_->num_dirs += 1;
if (fd == -1)
{
LET(fd = openat(dfd, di.d_name, O_RDONLY), fd == -1,
"unable to open dir; skipping", skip,
"path: ^^ %d\n",
dbg_print_current_path("eio-go-dir", di.d_name));
LET(dir = fake_fdopendir(fd), dir == NULL,
"unable to fake_fdopendir", out_error);
} else {
LET(dir = fake_fdopendir(fd), dir == NULL,
"unable to fake_fdopendir", out_error);
}
LET(newfd = dirfd(dir), newfd == -1,
"unable to dirfd new dir", out_error);
CHK(newfd != fd && fd != -1 && close(fd) == -1,
"unable to close old fd", out_error);
p.path = string(di.d_name),
p.dir = dir;
worklist_.push_front(p);
skip:
return 0;
out_error:
return 1;
}
int FDTraversal::go_reg(int dfd,
const struct dirent& di,
int fd,
const struct stat& st)
{
int ret = 0;
bool should_scan;
struct stat st2 = st;
FDScannable sc_fd;
StringPiece frag = di.d_name;
CHK(decider_->Decide(di, fd != -1, st2, &should_scan) != 0,
"unable to decide whether to scan di", out_error);
if ( ! should_scan)
goto cleanup;
if (fd == -1)
{
LET(fd = openat(dfd, di.d_name, O_RDONLY | O_NOFOLLOW), fd == -1,
"unable to open reg file", cleanup,
"Path: ^^ %d\n",
dbg_print_current_path("eopen-reg-file", di.d_name));
CHK(fstat(fd, &st2) == -1,
"unable to stat fd", out_error);
CHK(decider_->Decide(di, fd != -1, st2, &should_scan) != 0,
"unable to decide whether to scan di", out_error);
if ( ! should_scan)
goto cleanup;
}
CHK(sc_fd.Init(this, &frag, (uint64_t)st2.st_size, fd) != 0,
"unable to init fd-scannable", out_error);
CHK(scanner_->scan(&sc_fd) != 0,
"unable to scan fd-scannable", out_error);
CHK(sc_fd.Destroy() != 0,
"unable to destroy fd-scannable", out_error);
ret = 0;
goto cleanup;
out_error:
ret = 1;
goto cleanup;
cleanup:
if (fd != -1)
{
TST(close(fd) == -1, fd = -1,
"unable to close fd", out_error);
}
return ret;
}
int FDTraversal::go_lnk(int dfd,
const struct dirent& di,
bool has_st,
struct stat& st)
{
bool should_scan;
StringPiece frag;
string path2;
int fd = -1;
frag = di.d_name;
if (!has_st)
{
CHK(fstatat(dfd, di.d_name, &st, AT_SYMLINK_NOFOLLOW) == -1,
"unable to lstat link", out_error);
}
CHK(decider_->Decide(di, true, st, &should_scan) != 0,
"unable to decide whether to scan di", out_error);
if ( ! should_scan)
goto skip;
LET(fd = openat(dfd, di.d_name, O_RDONLY),
fd == -1,
"unable to open reg file", skip,
"Path: ^^ %d\n",
dbg_print_current_path("eopen-reg-file", di.d_name));
CHK(fstat(fd, &st) == -1,
"unable to stat fd", out_error);
if (S_ISREG(st.st_mode))
return go_reg(dfd, di, fd, st);
return 0;
out_error:
if (fd != -1)
{
TST(close(fd) == -1, fd = -1,
"unable to close fd", out_error);
}
return 1;
skip:
return 0;
}
int FDTraversal::dbg_print_current_path(const char* msg, const char* frag) const
{
string qpath;
StringPiece spfrag(frag);
CHK(get_quoted_path(&spfrag, &qpath) != 0,
"unable to get quoted path", out_error);
fprintf(stderr, "E %s path %s\n", msg, qpath.c_str());
return 0;
out_error:
return 1;
}
int FDTraversal::get_quoted_path(void* traversal_datum, string* qpath) const
{
StringPiece* frag = reinterpret_cast<StringPiece*>(traversal_datum);
ostringstream oss;
list<struct path_dir_pair>::const_reverse_iterator it, ie, first;
first = worklist_.rbegin();
for(it = worklist_.rbegin(), ie = worklist_.rend();
it != ie;
++it)
{
if (it != first)
oss << "/";
oss << uri_encode(it->path);
}
oss << "/" << uri_encode(*frag);
*qpath = oss.str();
return 0;
}