-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
286 lines (255 loc) · 10.3 KB
/
main.cpp
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
// MIT License
//
// Copyright (c) 2023 Piotr Pszczółkowski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <tbb/tbb.h>
#include <string>
#include "share/share.h"
#include <fmt/core.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <atomic>
#include <regex>
#include <system_error>
#include <optional>
#include <variant>
#include <fstream>
#include <sstream>
#include "clap/clap.h"
#include "pointer_wrapper.h"
tbb::task_group tg;
std::atomic_uint64_t total_dir_counter{};
std::atomic_uint64_t total_file_counter{};
std::atomic_uint64_t matched_dir_counter{};
std::atomic_uint64_t matched_file_counter{};
std::regex rgx_dir, rgx_file;
std::optional<std::regex> rgx_text{};
auto quiet{false};
auto clap = Clap("dirscanner v. 0.1",
Arg()
.marker("-i")
.promarker("--icase")
.help("regex with ignore case"),
Arg()
.marker("-r")
.promarker("--recursive")
.help("recursive scan"),
Arg()
.marker("-d")
.promarker("--dir"),
Arg()
.marker("-t")
.promarker("--text"),
Arg()
.marker("-n")
.promarker("--name")
.help("file/directory name"),
Arg()
.marker("-w")
.help("regex with word boundaries"),
Arg()
.marker("-e")
.promarker("--ext"),
Arg()
.marker("-q")
.promarker("--quiet")
);
/// Parsing a file without reading it, we use file-to-memory mapping.
bool parse_file2(std::string const& fp) noexcept {
if (auto fd = open(fp.c_str(), O_RDONLY); fd != -1) {
struct stat sb{};
if (fstat(fd, &sb) != -1 && sb.st_size > 0) {
char* addr = reinterpret_cast<char*>(mmap(nullptr, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0));
if (addr == MAP_FAILED) {
auto const err = std::make_error_code(std::errc{errno});
fmt::print(stderr, "Can't map file to memory: {} ({}) {}\n", err.message(), err.value(), fp);
return false;
}
if (rgx_text) {
auto const rgx = *rgx_text;
std::smatch smatch;
std::string const& str = addr;
// TODO: second copy of data (not efficient in every aspect)
// std::pmr::string text{addr, uint64_t(sb.st_size)};
// TODO: how to run regex with iterators/pointers to maped memory area
// mapping works, but now we have two copies of data,
// pmr doesn't help at all (or I'm doing something wrong)
if (std::regex_search(str, smatch, rgx) && smatch[0].matched) {
fmt::print("{}\n", fp);
return true;
}
}
if (munmap(reinterpret_cast<void*>(addr), sb.st_size) == -1) {
auto const err = std::make_error_code(std::errc{errno});
fmt::print(stderr, "Can't unmap file to memory.: {} ({})\n", err.message(), err.value());
}
}
}
return false;
}
bool parse_file(std::string const& fp) {
if (rgx_text) {
std::ifstream f;
f.open(fp);
std::stringstream ss;
ss << f.rdbuf();
std::string str = ss.str();
f.close();
std::smatch smatch;
if (std::regex_search(str, smatch, *rgx_text) && smatch[0].matched) {
fmt::print("{}\n", fp);
return true;
}
}
return false;
}
bool ends_with(std::string const& text, char c) {
return *std::prev(std::end(text)) == c;
}
void iterate_dir(std::string const& dir) noexcept {
total_dir_counter++;
if (auto dirp = opendir(dir.c_str()); dirp) {
auto* entry_prev = reinterpret_cast<struct dirent*>(malloc(offsetof(struct dirent, d_name) + NAME_MAX + 1));
struct dirent* entry;
for (;;) {
if (readdir_r(dirp, entry_prev, &entry) != 0) {
auto const err = std::make_error_code(std::errc{errno});
fmt::print(stderr, "({}) {}\n", err.value(), err.message());
break;
}
if (!entry) break;
std::string name{entry->d_name};
// no hidden, no . (current) and no .. (parent)
if (name[0] == '.') continue;
auto fp{dir};
if (!ends_with(fp, '/')) fp.append("/");
fp = fp.append(name);
struct stat fstat{};
if (0 == lstat(fp.c_str(), &fstat)) {
if ((fstat.st_mode & S_IFREG) == S_IFREG) {
// Perform in a dedicated tbb-task
tg.run([fp] {
total_file_counter++;
std::smatch smatch;
if (std::regex_search(fp, smatch, rgx_file) && smatch[0].matched) {
matched_file_counter++;
if (parse_file(fp))
return; // return from task
if (!quiet)
fmt::print("{}\n", fp);
}
});
}
else if ((fstat.st_mode & S_IFDIR) == S_IFDIR) {
// Perform in a dedicated tbb-task
tg.run([fp] {
std::smatch smatch;
if (std::regex_search(fp, smatch, rgx_dir) && smatch[0].matched) {
matched_dir_counter++;
if (!quiet)
fmt::print("{}\n", fp);
}
iterate_dir(fp);
});
}
}
}
closedir(dirp);
}
}
std::string strip_str(std::string text) noexcept {
if (text[0] == '\'' && text[text.size() - 1] == '\'')
return text.substr(1, text.size() -2);
return text;
}
int main(int argn, char* argv[]) {
clap.parse(argn, argv);
auto word_boundary{false};
auto ignore_case{false};
std::string name{};
std::string extension{};
std::string text{};
std::string fpath{};
// fetch directory
if (auto arg = clap["--dir"]; arg)
if (auto value = std::get_if<std::string>(&arg->value()); value)
fpath = strip_str(*value);
// during operation, regex runs in ignore case mode
if (auto arg = clap["--icase"]; arg)
if (auto flag = std::get_if<bool>(&arg->value()); flag)
ignore_case = *flag;
// search for a file/directory that contains 'name' in its name
if (auto arg = clap["--name"]; arg)
if (auto value = std::get_if<std::string>(&arg->value()); value)
name = strip_str(*value);
// regex searches for text at a word boundary
if (auto arg = clap["-w"]; arg)
if (auto value = std::get_if<bool>(&arg->value()); value)
word_boundary = *value;
// don't display directories and files
if (auto arg = clap["--quiet"]; arg)
if (auto value = std::get_if<bool>(&arg->value()); value)
quiet = *value;
// file extension
if (auto arg = clap["--ext"]; arg)
if (auto value = std::get_if<std::string>(&arg->value()); value)
extension = strip_str(*value);
// text to search in file
if (auto arg = clap["--text"]; arg)
if (auto value = std::get_if<std::string>(&arg->value()); value)
text = strip_str(*value);
std::string express_dir{};
if (!name.empty())
express_dir = word_boundary ? fmt::format("\\b{}\\b", name) : name;
auto express_file = express_dir;
if (!extension.empty())
express_file += fmt::format("\\w*\\.({})$", extension);
fmt::print("------- settings --------------------------------\n");
fmt::print(" initial dir: {}\n", fpath);
fmt::print("file/directory name provided: {}\n", name);
fmt::print(" file extension: {}\n", extension);
fmt::print(" text: {}\n", text);
fmt::print(" ignore case: {}\n", ignore_case);
fmt::print(" word boundaries: {}\n", word_boundary);
fmt::print(" quiet: {}\n", quiet);
fmt::print("-------------------------------------------------\n");
auto rgx_flags = std::regex_constants::ECMAScript;
if (ignore_case)
rgx_flags |= std::regex_constants::icase;
rgx_dir = std::regex(express_dir, rgx_flags);
rgx_file = std::regex(express_file, rgx_flags);
if (!text.empty()) {
auto pattern = fmt::format("\\b{}\\b", text);
// fmt::print("{}\n", pattern);
rgx_text = std::regex(pattern, rgx_flags);
}
tg.run([fpath] {
iterate_dir(fpath);
});
auto dt = share::execution_timer([&] {
tg.wait();
}, 1);
fmt::print("\nexecution time: {}\n", dt);
fmt::print(" dir total: {}, matched: {}\n", share::number2str(total_dir_counter.load()), share::number2str(matched_dir_counter.load()));
fmt::print(" files total: {}, matched: {}\n", share::number2str(total_file_counter.load()), share::number2str(matched_file_counter.load()));
return 0;
}