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 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
|
/* Copyright 2013-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman.h"
using CaseSensitivity = watchman::CaseSensitivity;
// This can't be a simple global because other compilation units
// may try to mutate it before this compilation unit has had its
// constructors run, leading to SIOF.
static std::unordered_map<w_string, w_query_expr_parser>& term_hash() {
static std::unordered_map<w_string, w_query_expr_parser> hash;
return hash;
}
QueryExpr::~QueryExpr() {}
bool w_query_register_expression_parser(
const char *term,
w_query_expr_parser parser)
{
char capname[128];
w_string name(term, W_STRING_UNICODE);
snprintf(capname, sizeof(capname), "term-%s", term);
w_capability_register(capname);
term_hash()[name] = parser;
return true;
}
/* parse an expression term. It can be one of:
* "term"
* ["term" <parameters>]
*/
std::unique_ptr<QueryExpr> w_query_expr_parse(
w_query* query,
const json_ref& exp) {
w_string name;
if (json_is_string(exp)) {
name = json_to_w_string(exp);
} else if (json_is_array(exp) && json_array_size(exp) > 0) {
const auto& first = exp.at(0);
if (!json_is_string(first)) {
throw QueryParseError("first element of an expression must be a string");
}
name = json_to_w_string(first);
} else {
throw QueryParseError("expected array or string for an expression");
}
auto it = term_hash().find(name);
if (it == term_hash().end()) {
throw QueryParseError(
watchman::to<std::string>("unknown expression term '", name, "'"));
}
return it->second(query, exp);
}
static bool parse_since(w_query* res, const json_ref& query) {
auto since = query.get_default("since");
if (!since) {
return true;
}
auto spec = ClockSpec::parseOptionalClockSpec(since);
if (spec) {
// res owns the ref to spec
res->since_spec = std::move(spec);
return true;
}
throw QueryParseError("invalid value for 'since'");
}
static w_string parse_suffix(const json_ref& ele) {
if (!json_is_string(ele)) {
throw QueryParseError("'suffix' must be a string or an array of strings");
}
auto str = json_to_w_string(ele);
return w_string(w_string_new_lower_typed(str.c_str(), str.type()), false);
}
static void parse_suffixes(w_query* res, const json_ref& query) {
size_t i;
auto suffixes = query.get_default("suffix");
if (!suffixes) {
return;
}
if (json_is_string(suffixes)) {
auto suff = parse_suffix(suffixes);
res->suffixes.emplace_back(std::move(suff));
return;
}
if (!json_is_array(suffixes)) {
throw QueryParseError("'suffix' must be a string or an array of strings");
}
res->suffixes.reserve(json_array_size(suffixes));
for (i = 0; i < json_array_size(suffixes); i++) {
const auto& ele = suffixes.at(i);
if (!json_is_string(ele)) {
throw QueryParseError("'suffix' must be a string or an array of strings");
}
auto suff = parse_suffix(ele);
res->suffixes.emplace_back(std::move(suff));
}
}
static bool parse_paths(w_query* res, const json_ref& query) {
size_t i;
auto paths = query.get_default("path");
if (!paths) {
return true;
}
if (!json_is_array(paths)) {
throw QueryParseError("'path' must be an array");
}
res->paths.resize(json_array_size(paths));
for (i = 0; i < json_array_size(paths); i++) {
const auto& ele = paths.at(i);
const char *name = NULL;
res->paths[i].depth = -1;
if (json_is_string(ele)) {
name = json_string_value(ele);
} else if (json_unpack(ele, "{s:s, s:i}",
"path", &name,
"depth", &res->paths[i].depth
) != 0) {
throw QueryParseError(
"expected object with 'path' and 'depth' properties");
}
auto nameCopy = w_string_new_typed(name, W_STRING_BYTE);
w_string_in_place_normalize_separators(&nameCopy);
res->paths[i].name = nameCopy;
}
return true;
}
W_CAP_REG("relative_root")
static void parse_relative_root(
const std::shared_ptr<w_root_t>& root,
w_query* res,
const json_ref& query) {
auto relative_root = query.get_default("relative_root");
if (!relative_root) {
return;
}
if (!json_is_string(relative_root)) {
throw QueryParseError("'relative_root' must be a string");
}
w_string path = json_to_w_string(relative_root);
w_string canon_path = w_string_canon_path(path);
res->relative_root = w_string::pathCat({root->root_path, canon_path});
res->relative_root_slash =
w_string::printf("%s/", res->relative_root.c_str());
}
static void parse_query_expression(w_query* res, const json_ref& query) {
auto exp = query.get_default("expression");
if (!exp) {
// Empty expression means that we emit all generated files
return;
}
res->expr = w_query_expr_parse(res, exp);
return;
}
static void parse_sync(w_query* res, const json_ref& query) {
int value = DEFAULT_QUERY_SYNC_MS.count();
if (query &&
json_unpack(query, "{s?:i*}", "sync_timeout", &value) != 0) {
throw QueryParseError("sync_timeout must be an integer value >= 0");
}
if (value < 0) {
throw QueryParseError("sync_timeout must be an integer value >= 0");
}
res->sync_timeout = std::chrono::milliseconds(value);
}
static void parse_lock_timeout(w_query* res, const json_ref& query) {
int value = DEFAULT_QUERY_SYNC_MS.count();
if (query &&
json_unpack(query, "{s?:i*}", "lock_timeout", &value) != 0) {
throw QueryParseError("lock_timeout must be an integer value >= 0");
}
if (value < 0) {
throw QueryParseError("lock_timeout must be an integer value >= 0");
}
res->lock_timeout = value;
}
W_CAP_REG("dedup_results")
static void parse_dedup(w_query* res, const json_ref& query) {
int value = 0;
if (query &&
json_unpack(query, "{s?:b*}", "dedup_results", &value) != 0) {
throw QueryParseError("dedup_results must be a boolean");
}
res->dedup_results = (bool) value;
}
static void parse_empty_on_fresh_instance(w_query* res, const json_ref& query) {
int value = 0;
if (query &&
json_unpack(query, "{s?:b*}", "empty_on_fresh_instance", &value) != 0) {
throw QueryParseError("empty_on_fresh_instance must be a boolean");
}
res->empty_on_fresh_instance = (bool) value;
}
static void parse_case_sensitive(
w_query* res,
const std::shared_ptr<w_root_t>& root,
const json_ref& query) {
int value = root->case_sensitive == CaseSensitivity::CaseSensitive;
if (query && json_unpack(query, "{s?:b*}", "case_sensitive", &value) != 0) {
throw QueryParseError("case_sensitive must be a boolean");
}
res->case_sensitive = value ? CaseSensitivity::CaseSensitive
: CaseSensitivity::CaseInSensitive;
}
std::shared_ptr<w_query> w_query_parse(
const std::shared_ptr<w_root_t>& root,
const json_ref& query) {
auto result = std::make_shared<w_query>();
auto res = result.get();
parse_case_sensitive(res, root, query);
parse_sync(res, query);
parse_dedup(res, query);
parse_lock_timeout(res, query);
parse_relative_root(root, res, query);
parse_empty_on_fresh_instance(res, query);
/* Look for path generators */
parse_paths(res, query);
/* Look for glob generators */
parse_globs(res, query);
/* Look for suffix generators */
parse_suffixes(res, query);
/* Look for since generator */
parse_since(res, query);
parse_query_expression(res, query);
parse_field_list(query.get_default("fields"), &res->fieldList);
for (auto& f : res->fieldList) {
if (f->futureMake) {
res->renderUsesFutures = true;
break;
}
}
res->query_spec = query;
return result;
}
void w_query_legacy_field_list(w_query_field_list* flist) {
static const char *names[] = {
"name", "exists", "size", "mode", "uid", "gid", "mtime",
"ctime", "ino", "dev", "nlink", "new", "cclock", "oclock"
};
uint8_t i;
auto list = json_array();
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
json_array_append_new(list, typed_string_to_json(names[i],
W_STRING_UNICODE));
}
parse_field_list(list, flist);
}
// Translate from the legacy array into the new style, then
// delegate to the main parser.
// We build a big anyof expression
std::shared_ptr<w_query> w_query_parse_legacy(
const std::shared_ptr<w_root_t>& root,
const json_ref& args,
int start,
uint32_t* next_arg,
const char* clockspec,
json_ref* expr_p) {
bool include = true;
bool negated = false;
uint32_t i;
const char *term_name = "match";
json_ref included, excluded;
auto query_obj = json_object();
if (!json_is_array(args)) {
throw QueryParseError("Expected an array");
}
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! */
throw QueryParseError(watchman::to<std::string>(
"rule @ position ", i, " is not a string value"));
}
}
for (i = start; i < json_array_size(args); i++) {
const char *arg = json_string_value(json_array_get(args, i));
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")) {
term_name = "ipcre";
continue;
}
if (!strcmp(arg, "-p")) {
term_name = "pcre";
continue;
}
// Which group are we going to file it into
json_ref container;
if (include) {
if (!included) {
included =
json_array({typed_string_to_json("anyof", W_STRING_UNICODE)});
}
container = included;
} else {
if (!excluded) {
excluded =
json_array({typed_string_to_json("anyof", W_STRING_UNICODE)});
}
container = excluded;
}
auto term =
json_array({typed_string_to_json(term_name, W_STRING_UNICODE),
typed_string_to_json(arg),
typed_string_to_json("wholename", W_STRING_UNICODE)});
if (negated) {
term = json_array({typed_string_to_json("not", W_STRING_UNICODE), term});
}
json_array_append_new(container, std::move(term));
// Reset negated flag
negated = false;
term_name = "match";
}
if (excluded) {
excluded =
json_array({typed_string_to_json("not", W_STRING_UNICODE), excluded});
}
json_ref query_array;
if (included && excluded) {
query_array = json_array(
{typed_string_to_json("allof", W_STRING_UNICODE), excluded, included});
} else if (included) {
query_array = included;
} else {
query_array = excluded;
}
// query_array may be NULL, which means find me all files.
// Otherwise, it is the expression we want to use.
if (query_array) {
json_object_set_new_nocheck(
query_obj, "expression", std::move(query_array));
}
// For trigger
if (next_arg) {
*next_arg = i;
}
if (clockspec) {
json_object_set_new_nocheck(query_obj,
"since", typed_string_to_json(clockspec, W_STRING_UNICODE));
}
/* compose the query with the field list */
auto query = w_query_parse(root, query_obj);
if (expr_p) {
*expr_p = query_obj;
}
if (query) {
w_query_legacy_field_list(&query->fieldList);
}
return query;
}
/* vim:ts=2:sw=2:et:
*/
|