-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdscanner_linux.cpp
More file actions
199 lines (157 loc) · 5.55 KB
/
dscanner_linux.cpp
File metadata and controls
199 lines (157 loc) · 5.55 KB
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
#ifdef __linux__
#include "directory_scanner.hpp"
#include "dscanner_helper.hpp"
#include "config.hpp"
#include <atomic>
#include <cstddef>
#include <queue>
#include <mutex>
#include <sys/syscall.h>
#include <sys/stat.h>
#include <thread>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <cstdint>
#include <string_view>
#include <unordered_set>
#include <utility>
#include <vector>
namespace directory_scanner {
struct linux_dirent64 {
ino64_t d_ino;
off64_t d_off;
unsigned short d_reclen;
unsigned char d_type;
char d_name[];
};
static bool is_dot(const char* name) {
return (name[0] == '.' &&
(name[1] == '\0' ||
(name[1] == '.' &&
name[2] == '\0')));
}
static inline std::string_view get_extension(const char* name) {
const char* dot = nullptr;
for (const char* p =name; *p; ++p)
if (*p == '.')
dot = p;
if (!dot)
return {};
return std::string_view(dot + 1);
}
static bool excluded_extension(const char* name,
const std::unordered_set<std::string_view>& excluded_ext) {
std::string_view ext = get_extension(name);
if (ext.empty()) return false;
return excluded_ext.find(ext) != excluded_ext.end();
}
std::vector<FileEntry> scan(const Config& config) {
std::vector<FileEntry> files;
files.reserve(16384);
std::queue<std::string> dir_queue;
dir_queue.push(config.PATH);
std::mutex queue_mutex;
std::mutex file_mutex;
std::atomic<int> active_workers{0};
// excluded dirs
std::unordered_set<std::string_view> excluded;
for (auto d : config.DEFAULT_EXCLUDED_DIRS)
excluded.insert(d);
for (auto& d : config.USER_EXCLUDED_DIRS)
excluded.insert(d);
// exlcuded extensions
std::unordered_set<std::string_view> excluded_ext;
for (auto e : config.DEFAULT_EXCLUDED_EXTENSIONS)
excluded_ext.insert(e);
for (auto& e : config.USER_EXCLUDED_EXTENSIONS)
excluded_ext.insert(e);
// gitignore support
GitIgnore gitignore;
if (config.USE_GITIGNORE)
load_gitignore(config.PATH, gitignore);
for (auto& d : gitignore.ignored_dirs)
excluded.insert(d);
for (auto& e : gitignore.ignored_ext)
excluded_ext.insert(e);
// size filtering
uint64_t min_size = config.DEFAULT_MIN_FILE_SIZE * FILE_SIZE;
uint64_t max_size = config.DEFAULT_MAX_FILE_SIZE * FILE_SIZE;
bool use_size_filter = (min_size > 0) || (max_size < CAP_FILE_SIZE);
unsigned workers = config.DEFAULT_WALKER_THREADS;
std::vector<std::thread> threads;
auto worker = [&]() {
constexpr int BUF_SIZE = 262144;
char buf[BUF_SIZE];
std::string path;
path.reserve(4096);
while (true) {
std::string dir;
{
std::lock_guard<std::mutex> lock(queue_mutex);
if (dir_queue.empty()) {
if (active_workers == 0) return;
else continue;
}
dir = std::move(dir_queue.front());
dir_queue.pop();
active_workers++;
}
int fd = open (dir.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
if (fd < 0) {
active_workers--;
continue;
}
path = dir;
for (;;) {
int nread = syscall(SYS_getdents64, fd, buf, BUF_SIZE);
if (nread <= 0) break;
for (int bpos = 0; bpos < nread;) {
linux_dirent64* d = (linux_dirent64*)(buf + bpos);
const char* name = d->d_name;
if (!is_dot(name)) {
size_t old_len = path.size();
path.push_back('/');
path.append(name);
if (d->d_type == DT_DIR) {
if (excluded.find(name) == excluded.end()){
std::lock_guard<std::mutex> lock(queue_mutex);
dir_queue.emplace(path);
}
} else if (d->d_type == DT_REG) { // TODO: '|| d->d_type==DT_UNKNOWN'
if (!excluded_extension(name, excluded_ext)) {
struct stat st{};
if (fstatat(fd, name, &st, AT_SYMLINK_NOFOLLOW) != 0)
continue;
if (!S_ISREG(st.st_mode))
continue;
if (st.st_size < 0)
continue;
size_t file_size = static_cast<size_t>(st.st_size);
if (use_size_filter) {
if (file_size < min_size || file_size > max_size)
continue;
}
{
std::lock_guard<std::mutex> lock(file_mutex);
files.push_back({path, file_size});
}
}
}
path.resize(old_len);
}
bpos += d->d_reclen;
}
}
close(fd);
active_workers--;
}
};
for (unsigned i = 0; i < workers; i++)
threads.emplace_back(worker);
for (auto& t : threads)
t.join();
return files;
}
}
#endif