-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwalk_filesystem.h
More file actions
34 lines (31 loc) · 796 Bytes
/
walk_filesystem.h
File metadata and controls
34 lines (31 loc) · 796 Bytes
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
#ifndef WALK_FILESYSTEM_H_
#define WALK_FILESYSTEM_H_
#include <fts.h>
#include <functional>
#include <string>
bool WalkFilesystem(const std::string& dir,
std::function<void(FTSENT* entry)> cb) {
// fts wants a mutable directory name, why?
std::string mutable_dir(dir);
char* const paths[] = {&mutable_dir[0], nullptr};
FTS* f = fts_open(paths, FTS_PHYSICAL, nullptr /* use default ordering */);
if (!f) {
perror("fts_open");
return false;
}
FTSENT* entry;
while ((entry = fts_read(f)) != nullptr) {
cb(entry);
}
if (errno) {
perror("fts_read");
fts_close(f); // Ignore error here since we're already in error.
return false;
}
if (fts_close(f) == -1) {
perror("fts_close");
return false;
}
return true;
}
#endif