forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrules.c
328 lines (282 loc) · 8.33 KB
/
rules.c
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
/* Copyright 2013-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman.h"
/* These are the legacy rules functions.
* They're going to be replaced by equivalent functionality
* in the query engine */
void w_free_rules(struct watchman_rule *head)
{
struct watchman_rule *r;
while (head) {
r = head;
head = r->next;
#ifdef HAVE_PCRE_H
if (r->re) {
pcre_free(r->re);
}
if (r->re_extra) {
pcre_free(r->re_extra);
}
#endif
free((char*)r->pattern);
free(r);
}
}
static struct watchman_rule_match *add_match(
struct watchman_rule_match **results_ptr,
uint32_t *num_allocd_ptr,
uint32_t *num_matches_ptr)
{
uint32_t num_allocd = *num_allocd_ptr;
uint32_t num_matches = *num_matches_ptr;
struct watchman_rule_match *results = *results_ptr;
struct watchman_rule_match *m;
if (num_matches + 1 > num_allocd) {
num_allocd = num_allocd ? num_allocd * 2 : 64;
results = realloc(results,
num_allocd * sizeof(struct watchman_rule_match));
if (!results) {
w_log(W_LOG_DBG, "out of memory while running rules!\n");
return false;
}
*results_ptr = results;
*num_allocd_ptr = num_allocd;
}
m = &results[num_matches++];
*num_matches_ptr = num_matches;
return m;
}
// must be called with root locked
uint32_t w_rules_match(w_root_t *root,
struct watchman_file *oldest_file,
struct watchman_rule_match **results,
struct watchman_rule *head,
struct w_clockspec *spec)
{
struct watchman_file *file;
struct watchman_rule *rule;
w_string_t *full_name;
w_string_t *relname;
uint32_t num_matches = 0;
uint32_t name_start;
struct watchman_rule_match *res = NULL;
uint32_t num_allocd = 0;
struct w_query_since since;
w_clockspec_eval(root, spec, &since);
name_start = root->root_path->len + 1;
for (file = oldest_file; file; file = file->prev) {
// no rules means return everything
bool matched = (head == NULL) ? true : false;
full_name = w_string_path_cat(file->parent->path, file->name);
// Record the name relative to the root
relname = w_string_slice(full_name, name_start,
full_name->len - name_start);
w_string_delref(full_name);
// Work through the rules; we stop as soon as we get a match.
for (rule = head; rule && !matched; rule = rule->next) {
// In theory, relname->buf may not be NUL terminated in
// the right spot if it was created as a slice.
// In practice, we don't see those, but if we do, we should
// probably make a copy of the string into a stack buffer :-/
switch (rule->rule_type) {
case RT_FNMATCH:
matched = fnmatch(rule->pattern, relname->buf, rule->flags) == 0;
break;
case RT_PCRE:
#ifdef HAVE_PCRE_H
{
int rc = pcre_exec(rule->re, rule->re_extra, relname->buf,
relname->len, 0, 0, NULL, 0);
if (rc == PCRE_ERROR_NOMATCH) {
matched = false;
} else if (rc >= 0) {
matched = true;
} else {
w_log(W_LOG_ERR, "pcre match %s against %s failed: %d\n",
rule->pattern, relname->buf, rc);
matched = false;
}
}
#else
matched = false;
#endif
break;
}
// If the rule is negated, we negate the sense of the
// match result
if (rule->negated) {
matched = !matched;
}
// If the pattern matched then we're going to include the file
// in our result set, but only if it is set to include.
// If we're not including, we explicitly don't want to know
// about the file, so pretend it didn't match and stop processing
// rules for the file.
if (matched && !rule->include) {
matched = false;
break;
}
}
if (matched) {
struct watchman_rule_match *m;
m = add_match(&res, &num_allocd, &num_matches);
if (!m) {
w_log(W_LOG_ERR, "out of memory while running rules!\n");
w_string_delref(relname);
free(res);
return 0;
}
m->root_number = root->number;
m->relname = relname;
m->file = file;
if (since.is_timestamp) {
m->is_new = w_timeval_compare(since.timestamp, file->ctime.tv) > 0;
} else if (since.clock.is_fresh_instance) {
m->is_new = true;
} else {
m->is_new = file->ctime.ticks > since.clock.ticks;
}
} else {
w_string_delref(relname);
}
}
*results = res;
return num_matches;
}
void w_match_results_free(uint32_t num_matches,
struct watchman_rule_match *matches)
{
uint32_t i;
for (i = 0; i < num_matches; i++) {
w_string_delref(matches[i].relname);
}
free(matches);
}
/* Parses filename match rules.
* By default, we want to include items that positively match
* the set of fnmatch(3) patterns specified.
* If -X is specified, we switch to exclude mode; any patterns
* that are encountered after -X are excluded from the result set.
* If -I is specified, we switch to include mode, so you can use
* -I to turn on include mode again after using -X.
* If "!" is specified, the following pattern is negated.
* We switch back out of negation mode after that pattern.
* If -p is specified, the following pattern is interpreted as a PCRE.
* If -P is specified, the following pattern is interpreted as a PCRE
* with the PCRE_CASELESS flag set.
*
* We stop processing args when we find "--" and update
* *next_arg to the argv index after that argument.
*/
bool parse_watch_params(int start, json_t *args,
struct watchman_rule **head_ptr,
uint32_t *next_arg,
char *errbuf, int errbuflen)
{
bool include = true;
bool negated = false;
#ifdef HAVE_PCRE_H
int is_pcre = 0;
#endif
struct watchman_rule *rule, *prior = NULL;
uint32_t i;
if (!json_is_array(args)) {
return false;
}
*head_ptr = NULL;
for (i = start; i < json_array_size(args); i++) {
const char *arg = json_string_value(json_array_get(args, i));
if (!arg) {
/* not a string value! */
w_free_rules(*head_ptr);
*head_ptr = NULL;
snprintf(errbuf, errbuflen,
"rule @ position %d is not a string value", i);
return false;
}
if (!strcmp(arg, "--")) {
i++;
break;
}
if (!strcmp(arg, "-X")) {
include = false;
continue;
}
if (!strcmp(arg, "-I")) {
include = true;
continue;
}
if (!strcmp(arg, "!")) {
negated = true;
continue;
}
if (!strcmp(arg, "-P") || !strcmp(arg, "-p")) {
#ifdef HAVE_PCRE_H
is_pcre = arg[1];
continue;
#else
snprintf(errbuf, errbuflen,
"this watchman was not built with pcre support");
return false;
#endif
}
rule = calloc(1, sizeof(*rule));
if (!rule) {
w_free_rules(*head_ptr);
*head_ptr = NULL;
snprintf(errbuf, errbuflen, "out of memory");
return false;
}
rule->include = include;
rule->negated = negated;
// We default the fnmatch so that we can match against paths that include
// slashes.
// To recursively match the contents of a dir, use "dir/*". To match all
// "C" source files, use "*.c". To match all makefiles, use
// "*/Makefile" + "Makefile" (include the latter if the Makefile might
// be at the top level).
rule->rule_type = RT_FNMATCH;
rule->pattern = strdup(arg);
rule->flags = FNM_PERIOD;
#ifdef HAVE_PCRE_H
if (is_pcre) {
const char *errptr = NULL;
int erroff = 0;
int errcode = 0;
rule->re = pcre_compile2(rule->pattern,
is_pcre == 'P' ? PCRE_CASELESS : 0,
&errcode, &errptr, &erroff, NULL);
if (!rule->re) {
snprintf(errbuf, errbuflen,
"invalid pcre: `%s' at offset %d: code %d %s",
rule->pattern, erroff, errcode, errptr);
w_free_rules(rule);
w_free_rules(*head_ptr);
*head_ptr = NULL;
return false;
}
if (rule->re) {
rule->re_extra = pcre_study(rule->re, 0, &errptr);
}
rule->rule_type = RT_PCRE;
}
#endif
if (!prior) {
*head_ptr = rule;
} else {
prior->next = rule;
}
prior = rule;
// Reset negated flag
negated = false;
#ifdef HAVE_PCRE_H
is_pcre = 0;
#endif
}
if (next_arg) {
*next_arg = i;
}
return true;
}
/* vim:ts=2:sw=2:et:
*/