-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind.cpp
More file actions
313 lines (280 loc) · 10.4 KB
/
Copy pathfind.cpp
File metadata and controls
313 lines (280 loc) · 10.4 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
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
// find — search for files in a directory hierarchy
// Predicates: -name PATTERN, -type [f|d|l|b|c|p|s], -maxdepth N, -exec CMD {} ;
// Boolean: -a (implicit), -o, ! / -not, ( ) grouping.
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <string>
#include <string_view>
#include <sys/wait.h>
#include <unistd.h>
#include <vector>
#include <cfbox/error.hpp>
#include <cfbox/help.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "find",
.version = CFBOX_VERSION_STRING,
.one_line = "search for files in a directory hierarchy",
.usage = "find [PATH] [PREDICATE]...",
.options = " Predicates:\n"
" -name PATTERN match filename (glob * and ?)\n"
" -type [f|d|l|b|c|p|s] match file type\n"
" -maxdepth N descend at most N levels (global option)\n"
" -exec CMD {} ; execute command on matches\n"
" Operators (descending precedence):\n"
" -o logical OR -a / implicit logical AND\n"
" ! / -not logical NOT ( ) grouping",
.extra = "",
};
// fnmatch-style glob: supports * ? and literal chars.
auto glob_match(std::string_view pattern, std::string_view text) -> bool {
std::size_t pi = 0, ti = 0;
std::size_t star_pi = std::string_view::npos, star_ti = std::string_view::npos;
while (ti < text.size()) {
if (pi < pattern.size()) {
if (pattern[pi] == '?') { ++pi; ++ti; continue; }
if (pattern[pi] == '*') { star_pi = pi; star_ti = ti; ++pi; continue; }
if (pattern[pi] == text[ti]) { ++pi; ++ti; continue; }
}
if (star_pi != std::string_view::npos) {
pi = star_pi + 1;
ti = star_ti + 1;
star_ti = ti;
continue;
}
return false;
}
while (pi < pattern.size() && pattern[pi] == '*') ++pi;
return pi == pattern.size();
}
auto file_type_char(const std::filesystem::directory_entry& entry) -> char {
auto st = entry.symlink_status();
switch (st.type()) {
case std::filesystem::file_type::regular: return 'f';
case std::filesystem::file_type::directory: return 'd';
case std::filesystem::file_type::symlink: return 'l';
case std::filesystem::file_type::block: return 'b';
case std::filesystem::file_type::character: return 'c';
case std::filesystem::file_type::fifo: return 'p';
case std::filesystem::file_type::socket: return 's';
default: return '?';
}
}
auto run_exec(const std::vector<std::string>& cmd_template,
const std::string& filepath) -> void {
std::vector<std::string> args;
args.reserve(cmd_template.size());
for (const auto& part : cmd_template) {
args.push_back(part == "{}" ? filepath : part);
}
std::vector<char*> argv_arr;
argv_arr.reserve(args.size() + 1);
for (auto& a : args) argv_arr.push_back(a.data());
argv_arr.push_back(nullptr);
pid_t pid = fork();
if (pid == 0) {
execvp(argv_arr[0], argv_arr.data());
_Exit(127);
} else if (pid > 0) {
int status;
waitpid(pid, &status, 0);
}
}
// Expression tree. And/Or hold N children; Not holds 1; Name/Type/Exec are
// leaves; True is a no-op (used for the empty expression and -maxdepth, which
// is a global option that does not participate in evaluation).
struct Node {
enum Kind { And, Or, Not, Name, Type, Exec, True };
Kind kind = True;
std::string value; // Name pattern / Type char
std::vector<std::string> exec_cmd; // Exec command template
std::vector<Node> children; // And/Or/Not sub-expressions
Node() = default;
explicit Node(Kind k) : kind(k) {}
};
auto eval(const Node& n, const std::filesystem::directory_entry& entry) -> bool {
switch (n.kind) {
case Node::True:
return true;
case Node::Name:
return glob_match(n.value, entry.path().filename().string());
case Node::Type:
return n.value.size() == 1 && file_type_char(entry) == n.value[0];
case Node::Exec:
run_exec(n.exec_cmd, entry.path().string());
return true; // actions always evaluate true
case Node::Not:
return !eval(n.children[0], entry);
case Node::And:
for (const auto& c : n.children)
if (!eval(c, entry)) return false;
return true;
case Node::Or:
for (const auto& c : n.children)
if (eval(c, entry)) return true;
return false;
}
return false;
}
auto has_exec_node(const Node& n) -> bool {
if (n.kind == Node::Exec) return true;
for (const auto& c : n.children)
if (has_exec_node(c)) return true;
return false;
}
// Recursive-descent parser over the predicate token stream. Sets `failed` on a
// syntax error (unknown predicate, missing operand, unbalanced paren).
struct Parser {
const std::vector<std::string>& toks;
int maxdepth = -1; // global -maxdepth (out)
std::size_t pos = 0;
bool failed = false;
[[nodiscard]] auto at_end() const -> bool { return pos >= toks.size(); }
auto peek() const -> const std::string& { return toks[pos]; }
auto parse_or() -> Node {
Node left = parse_and();
if (failed || at_end() || peek() != "-o") return left;
Node node{Node::Or};
node.children.push_back(std::move(left));
while (!failed && !at_end() && peek() == "-o") {
++pos;
node.children.push_back(parse_and());
}
return node;
}
auto parse_and() -> Node {
Node left = parse_not();
if (failed || at_end() || peek() == "-o" || peek() == ")") return left;
Node node{Node::And};
node.children.push_back(std::move(left));
while (!failed && !at_end() && peek() != "-o" && peek() != ")") {
if (peek() == "-a") ++pos; // explicit; otherwise implicit AND
node.children.push_back(parse_not());
}
return node;
}
auto parse_not() -> Node {
if (!at_end() && (peek() == "!" || peek() == "-not")) {
++pos;
Node node{Node::Not};
node.children.push_back(parse_not());
return node;
}
return parse_primary();
}
auto parse_primary() -> Node {
if (at_end()) { failed = true; return Node{Node::True}; }
const std::string& t = peek();
if (t == "(") {
++pos;
Node inner = parse_or();
if (failed) return inner;
if (at_end() || peek() != ")") { failed = true; return inner; }
++pos;
return inner;
}
if (t == "-name") {
++pos;
if (at_end()) { failed = true; return Node{Node::True}; }
Node n{Node::Name};
n.value = peek();
++pos;
return n;
}
if (t == "-type") {
++pos;
if (at_end()) { failed = true; return Node{Node::True}; }
Node n{Node::Type};
n.value = peek();
++pos;
return n;
}
if (t == "-maxdepth") {
++pos;
if (at_end()) { failed = true; return Node{Node::True}; }
maxdepth = std::atoi(peek().c_str());
++pos;
return Node{Node::True}; // global option: no-op in the tree
}
if (t == "-exec") {
++pos;
Node n{Node::Exec};
while (!at_end() && peek() != ";" && peek() != "\\;") {
n.exec_cmd.push_back(peek());
++pos;
}
if (!at_end()) ++pos; // consume the terminating ;
return n;
}
failed = true;
CFBOX_ERR("find", "unknown primary or operator '%s'", t.c_str());
return Node{Node::True};
}
};
auto do_find(const std::filesystem::path& root, const Node& expr, int maxdepth) -> int {
const bool print = !has_exec_node(expr);
std::error_code ec;
auto it = std::filesystem::recursive_directory_iterator(
root, std::filesystem::directory_options::follow_directory_symlink, ec);
if (ec) {
CFBOX_ERR("find", "'%s': %s", root.string().c_str(), ec.message().c_str());
return 1;
}
auto emit = [&](const std::filesystem::directory_entry& entry, int depth) {
if (maxdepth >= 0 && depth > maxdepth) return;
if (eval(expr, entry) && print) {
std::printf("%s\n", entry.path().string().c_str());
}
// When -exec is present, eval() already ran it as a side effect; the
// default -print is suppressed.
};
// Evaluate the root entry itself (depth 0).
{
std::error_code ec2;
std::filesystem::directory_entry root_entry(root, ec2);
if (!ec2) emit(root_entry, 0);
}
int rc = 0;
for (const auto& entry : it) {
int depth = it.depth() + 1; // root is depth 0
if (maxdepth >= 0 && depth > maxdepth) {
it.disable_recursion_pending();
continue;
}
emit(entry, depth);
}
return rc;
}
} // namespace
auto find_main(int argc, char* argv[]) -> int {
for (int i = 1; i < argc; ++i) {
std::string_view arg{argv[i]};
if (arg == "--help") { cfbox::help::print_help(HELP); return 0; }
if (arg == "--version") { cfbox::help::print_version(HELP); return 0; }
}
// First non-option argument is the PATH, unless it begins an expression
// (a predicate, '(', ')', or '!'). Default root is ".".
int start = 1;
std::filesystem::path root = ".";
auto is_expr_start = [](const char* s) -> bool {
return s[0] == '-' || s[0] == '(' || s[0] == ')' ||
(s[0] == '!' && s[1] == '\0');
};
if (argc > 1 && !is_expr_start(argv[1])) {
root = argv[1];
start = 2;
}
std::vector<std::string> tokens;
for (int i = start; i < argc; ++i) tokens.emplace_back(argv[i]);
// Empty expression prints everything (root + descendants).
if (tokens.empty()) {
return do_find(root, Node{Node::True}, -1);
}
Parser parser{tokens};
Node expr = parser.parse_or();
if (parser.failed || !parser.at_end()) {
return 1; // GNU find: invalid expression -> exit 1
}
return do_find(root, expr, parser.maxdepth);
}