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 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
|
/* updatedb configuration.
Copyright (C) 2005, 2007, 2008 Red Hat, Inc. All rights reserved.
This copyrighted material is made available to anyone wishing to use, modify,
copy, or redistribute it subject to the terms and conditions of the GNU General
Public License v.2.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
Street, Fifth Floor, Boston, MA 02110-1301, USA.
Author: Miloslav Trmac <mitr@redhat.com>
plocate modifications: Copyright (C) 2020 Steinar H. Gunderson.
plocate parts and modifications are licensed under the GPLv2 or, at your option,
any later version.
*/
#include "conf.h"
#include "lib.h"
#include <algorithm>
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
using namespace std;
/* true if locate(1) should check whether files are visible before reporting
them */
bool conf_check_visibility = true;
/* Filesystems to skip, converted to uppercase and sorted by name */
vector<string> conf_prunefs;
/* Directory names to skip, sorted by name */
vector<string> conf_prunenames;
/* Paths to skip, sorted by name using dir_path_cmp () */
vector<string> conf_prunepaths;
/* true if bind mounts should be skipped */
bool conf_prune_bind_mounts; /* = false; */
/* true if pruning debug output was requested */
bool conf_debug_pruning; /* = false; */
/* Root of the directory tree to store in the database (canonical) */
char *conf_scan_root; /* = NULL; */
/* Absolute (not necessarily canonical) path to the database */
string conf_output;
/* 1 if file names should be written to stdout as they are found */
bool conf_verbose; /* = false; */
/* Configuration representation for the database configuration block */
string conf_block;
/* Custom configuration file path, or NULL to use default */
const char *conf_config_file = NULL;
int conf_block_size = 32;
bool use_debug = false;
/* Parse a STR, store the parsed boolean value to DEST;
return 0 if OK, -1 on error. */
static int
parse_bool(bool *dest, const char *str)
{
if (strcmp(str, "0") == 0 || strcmp(str, "no") == 0) {
*dest = false;
return 0;
}
if (strcmp(str, "1") == 0 || strcmp(str, "yes") == 0) {
*dest = true;
return 0;
}
return -1;
}
/* String list handling */
/* Add values from space-separated VAL to VAR and LIST */
static void
var_add_values(vector<string> *list, const char *val)
{
for (;;) {
const char *start;
while (isspace((unsigned char)*val))
val++;
if (*val == 0)
break;
start = val;
do
val++;
while (*val != 0 && !isspace((unsigned char)*val));
list->emplace_back(start, val - start);
}
}
/* Finish variable LIST, sort its contents, remove duplicates */
static void
var_finish(vector<string> *list)
{
sort(list->begin(), list->end());
auto new_end = unique(list->begin(), list->end());
list->erase(new_end, list->end());
}
/* Configuration file parsing */
/* Configuration file (locked) */
static FILE *uc_file;
/* Configuration file path */
static const char *uc_file_path;
/* Line number at token start; type matches error_at_line () */
static unsigned uc_line;
/* Current line number; type matches error_at_line () */
static unsigned uc_current_line;
/* Last string returned by uc_lex */
static string uc_lex_buf;
/* Token types */
enum {
UCT_EOF,
UCT_EOL,
UCT_IDENTIFIER,
UCT_EQUAL,
UCT_QUOTED,
UCT_OTHER,
UCT_PRUNE_BIND_MOUNTS,
UCT_PRUNEFS,
UCT_PRUNENAMES,
UCT_PRUNEPATHS
};
/* Return next token from uc_file; for UCT_IDENTIFIER, UCT_QUOTED or keywords,
store the data to uc_lex_buf (valid until next call). */
static int
uc_lex(void)
{
int c;
uc_lex_buf.clear();
uc_line = uc_current_line;
do {
c = getc_unlocked(uc_file);
if (c == EOF)
return UCT_EOF;
} while (c != '\n' && isspace((unsigned char)c));
switch (c) {
case '#':
do {
c = getc_unlocked(uc_file);
if (c == EOF)
return UCT_EOF;
} while (c != '\n');
/* Fall through */
case '\n':
uc_current_line++;
return UCT_EOL;
case '=':
return UCT_EQUAL;
case '"': {
while ((c = getc_unlocked(uc_file)) != '"') {
if (c == EOF || c == '\n') {
fprintf(stderr, "%s:%u: missing closing `\"'\n",
uc_file_path, uc_line);
exit(EXIT_FAILURE);
}
uc_lex_buf.push_back(c);
}
return UCT_QUOTED;
}
default: {
if (!isalpha((unsigned char)c) && c != '_')
return UCT_OTHER;
do {
uc_lex_buf.push_back(c);
c = getc_unlocked(uc_file);
} while (c != EOF && (isalnum((unsigned char)c) || c == '_'));
ungetc(c, uc_file);
if (uc_lex_buf == "PRUNE_BIND_MOUNTS")
return UCT_PRUNE_BIND_MOUNTS;
if (uc_lex_buf == "PRUNEFS")
return UCT_PRUNEFS;
if (uc_lex_buf == "PRUNENAMES")
return UCT_PRUNENAMES;
if (uc_lex_buf == "PRUNEPATHS")
return UCT_PRUNEPATHS;
return UCT_IDENTIFIER;
}
}
}
/* Parse configuration file. Exit on I/O or syntax error. */
static void
parse_updatedb_conf(void)
{
bool had_prune_bind_mounts, had_prunefs, had_prunenames, had_prunepaths;
uc_file_path = conf_config_file ? conf_config_file : UPDATEDB_CONF;
uc_file = fopen(uc_file_path, "r");
if (uc_file == NULL) {
if (errno != ENOENT || conf_config_file != NULL) {
perror(uc_file_path);
exit(EXIT_FAILURE);
}
return;
}
flockfile(uc_file);
uc_current_line = 1;
had_prune_bind_mounts = false;
had_prunefs = false;
had_prunenames = false;
had_prunepaths = false;
for (;;) {
bool *had_var;
int var_token, token;
token = uc_lex();
switch (token) {
case UCT_EOF:
goto eof;
case UCT_EOL:
continue;
case UCT_PRUNE_BIND_MOUNTS:
had_var = &had_prune_bind_mounts;
break;
case UCT_PRUNEFS:
had_var = &had_prunefs;
break;
case UCT_PRUNENAMES:
had_var = &had_prunenames;
break;
case UCT_PRUNEPATHS:
had_var = &had_prunepaths;
break;
case UCT_IDENTIFIER:
fprintf(stderr, "%s:%u: unknown variable: `%s'\n",
uc_file_path, uc_line, uc_lex_buf.c_str());
exit(EXIT_FAILURE);
default:
fprintf(stderr, "%s:%u: variable name expected\n",
uc_file_path, uc_line);
exit(EXIT_FAILURE);
}
if (*had_var != false) {
fprintf(stderr, "%s:%u: variable `%s' was already defined\n",
uc_file_path, uc_line, uc_lex_buf.c_str());
exit(EXIT_FAILURE);
}
*had_var = true;
var_token = token;
token = uc_lex();
if (token != UCT_EQUAL) {
fprintf(stderr, "%s:%u: `=' expected after variable name\n",
uc_file_path, uc_line);
exit(EXIT_FAILURE);
}
token = uc_lex();
if (token != UCT_QUOTED) {
fprintf(stderr, "%s:%u: value in quotes expected after `='\n",
uc_file_path, uc_line);
exit(EXIT_FAILURE);
}
if (var_token == UCT_PRUNE_BIND_MOUNTS) {
if (parse_bool(&conf_prune_bind_mounts, uc_lex_buf.c_str()) != 0) {
fprintf(stderr, "%s:%u: invalid value `%s' of PRUNE_BIND_MOUNTS\n",
uc_file_path, uc_line, uc_lex_buf.c_str());
exit(EXIT_FAILURE);
}
} else if (var_token == UCT_PRUNEFS)
var_add_values(&conf_prunefs, uc_lex_buf.c_str());
else if (var_token == UCT_PRUNENAMES)
var_add_values(&conf_prunenames, uc_lex_buf.c_str());
else if (var_token == UCT_PRUNEPATHS)
var_add_values(&conf_prunepaths, uc_lex_buf.c_str());
else
abort();
token = uc_lex();
if (token != UCT_EOL && token != UCT_EOF) {
fprintf(stderr, "%s:%u: unexpected data after variable value\n",
uc_file_path, uc_line);
exit(EXIT_FAILURE);
}
/* Fall through */
while (token != UCT_EOL) {
if (token == UCT_EOF)
goto eof;
token = uc_lex();
}
}
eof:
if (ferror(uc_file)) {
perror(uc_file_path);
exit(EXIT_FAILURE);
}
funlockfile(uc_file);
fclose(uc_file);
}
/* Command-line argument parsing */
/* Output --help text */
static void
help(void)
{
printf("Usage: updatedb [OPTION]...\n"
"Update a plocate database.\n"
"\n"
" -f, --add-prunefs FS omit also FS (space-separated)\n"
" -n, --add-prunenames NAMES omit also NAMES (space-separated)\n"
" -e, --add-prunepaths PATHS omit also PATHS (space-separated)\n"
" --add-single-prunepath PATH omit also PATH\n"
" -U, --database-root PATH the subtree to store in "
"database (default \"/\")\n"
" -h, --help print this help\n"
" -o, --output FILE database to update (default\n"
" `%s')\n"
" -b, --block-size SIZE number of filenames to store\n"
" in each block (default 32)\n"
" --config-file FILE configuration file (default `%s')\n"
" --prune-bind-mounts FLAG omit bind mounts (default "
"\"no\")\n"
" --prunefs FS filesystems to omit from "
"database\n"
" --prunenames NAMES directory names to omit from "
"database\n"
" --prunepaths PATHS paths to omit from database\n"
" -l, --require-visibility FLAG check visibility before "
"reporting files\n"
" (default \"yes\")\n"
" -v, --verbose print paths of files as they "
"are found\n"
" -V, --version print version information\n"
"\n"
"The configuration defaults to values read from `%s',\n"
"or from a file specified with --config-file.\n",
DBFILE, UPDATEDB_CONF, UPDATEDB_CONF);
printf("\n"
"Report bugs to %s.\n",
PACKAGE_BUGREPORT);
}
/* Prepend current working directory to PATH;
return resulting path */
static string
prepend_cwd(const string &path)
{
const char *res;
string buf;
buf.resize(BUFSIZ); /* Not PATH_MAX because it is not defined on some platforms. */
do
buf.resize(buf.size() * 1.5);
while ((res = getcwd(buf.data(), buf.size())) == NULL && errno == ERANGE);
if (res == NULL) {
fprintf(stderr, "%s: %s: can not get current working directory\n",
program_invocation_name, strerror(errno));
exit(EXIT_FAILURE);
}
buf.resize(strlen(buf.data()));
return buf + '/' + path;
}
/* Command line options definitions, shared by parse_initial_arguments and parse_arguments */
enum { OPT_DEBUG_PRUNING = CHAR_MAX + 1,
OPT_ADD_SINGLE_PRUNEPATH = CHAR_MAX + 2,
OPT_CONFIG_FILE = CHAR_MAX + 3 };
static const struct option options[] = {
{ "add-prunefs", required_argument, NULL, 'f' },
{ "add-prunenames", required_argument, NULL, 'n' },
{ "add-prunepaths", required_argument, NULL, 'e' },
{ "add-single-prunepath", required_argument, NULL, OPT_ADD_SINGLE_PRUNEPATH },
{ "config-file", required_argument, NULL, OPT_CONFIG_FILE },
{ "database-root", required_argument, NULL, 'U' },
{ "debug-pruning", no_argument, NULL, OPT_DEBUG_PRUNING },
{ "help", no_argument, NULL, 'h' },
{ "output", required_argument, NULL, 'o' },
{ "prune-bind-mounts", required_argument, NULL, 'B' },
{ "prunefs", required_argument, NULL, 'F' },
{ "prunenames", required_argument, NULL, 'N' },
{ "prunepaths", required_argument, NULL, 'P' },
{ "require-visibility", required_argument, NULL, 'l' },
{ "verbose", no_argument, NULL, 'v' },
{ "version", no_argument, NULL, 'V' },
{ "block-size", required_argument, 0, 'b' },
{ "debug", no_argument, 0, 'D' }, // Not documented.
{ NULL, 0, NULL, 0 }
};
static auto short_options = "U:Ve:f:hl:n:o:vb:D";
/* Parse ARGC, ARGV for options that need to be processed before config file parsing. */
static void
parse_initial_arguments(int argc, char *argv[])
{
for (;;) {
int idx;
auto opt = getopt_long(argc, argv, short_options, options, &idx);
switch (opt) {
case -1:
goto options_done;
case '?':
exit(EXIT_FAILURE);
case OPT_CONFIG_FILE:
if (conf_config_file != NULL) {
fprintf(stderr, "%s: --%s specified twice\n",
program_invocation_name, "config-file");
exit(EXIT_FAILURE);
}
conf_config_file = optarg;
break;
default:
break; // Ignore other options
}
}
options_done:
if (optind != argc) {
fprintf(stderr, "%s: unexpected operand on command line",
program_invocation_name);
exit(EXIT_FAILURE);
}
}
/* Parse ARGC, ARGV. Exit on error or --help, --version. */
static void
parse_arguments(int argc, char *argv[])
{
bool prunefs_changed, prunenames_changed, prunepaths_changed;
bool got_prune_bind_mounts, got_visibility;
prunefs_changed = false;
prunenames_changed = false;
prunepaths_changed = false;
got_prune_bind_mounts = false;
got_visibility = false;
optind = 1; // Reset for second pass
for (;;) {
int opt, idx;
opt = getopt_long(argc, argv, short_options, options, &idx);
switch (opt) {
case -1:
goto options_done;
case '?':
exit(EXIT_FAILURE);
case 'B':
if (got_prune_bind_mounts != false) {
fprintf(stderr, "%s: --%s would override earlier command-line argument\n",
program_invocation_name, "prune-bind-mounts");
exit(EXIT_FAILURE);
}
got_prune_bind_mounts = true;
if (parse_bool(&conf_prune_bind_mounts, optarg) != 0) {
fprintf(stderr, "%s: invalid value %s of --%s\n",
program_invocation_name, optarg, "prune-bind-mounts");
exit(EXIT_FAILURE);
}
break;
case 'F':
if (prunefs_changed != false) {
fprintf(stderr, "%s: --%s would override earlier command-line argument\n",
program_invocation_name, "prunefs");
exit(EXIT_FAILURE);
}
prunefs_changed = true;
conf_prunefs.clear();
var_add_values(&conf_prunefs, optarg);
break;
case 'N':
if (prunenames_changed != false) {
fprintf(stderr, "%s: --%s would override earlier command-line argument\n",
program_invocation_name, "prunenames");
exit(EXIT_FAILURE);
}
prunenames_changed = true;
conf_prunenames.clear();
var_add_values(&conf_prunenames, optarg);
break;
case 'P':
if (prunepaths_changed != false) {
fprintf(stderr, "%s: --%s would override earlier command-line argument\n",
program_invocation_name, "prunepaths");
exit(EXIT_FAILURE);
}
prunepaths_changed = true;
conf_prunepaths.clear();
var_add_values(&conf_prunepaths, optarg);
break;
case 'U':
if (conf_scan_root != NULL) {
fprintf(stderr, "%s: --%s specified twice\n",
program_invocation_name, "database-root");
exit(EXIT_FAILURE);
}
conf_scan_root = realpath(optarg, nullptr);
if (conf_scan_root == NULL) {
fprintf(stderr, "%s: %s: invalid value `%s' of --%s\n",
program_invocation_name, strerror(errno), optarg, "database-root");
exit(EXIT_FAILURE);
}
break;
case 'V':
puts("updatedb (" PACKAGE_NAME ") " PACKAGE_VERSION);
puts("Copyright (C) 2007 Red Hat, Inc. All rights reserved.\n"
"This software is distributed under the GPL v.2.\n"
"\n"
"This program is provided with NO WARRANTY, to the extent "
"permitted by law.");
exit(EXIT_SUCCESS);
case 'e':
prunepaths_changed = true;
var_add_values(&conf_prunepaths, optarg);
break;
case OPT_ADD_SINGLE_PRUNEPATH:
prunepaths_changed = true;
conf_prunepaths.push_back(optarg);
break;
case 'f':
prunefs_changed = true;
var_add_values(&conf_prunefs, optarg);
break;
case 'h':
help();
exit(EXIT_SUCCESS);
case 'l':
if (got_visibility != false) {
fprintf(stderr, "%s: --%s specified twice\n",
program_invocation_name, "require-visibility");
exit(EXIT_FAILURE);
}
got_visibility = true;
if (parse_bool(&conf_check_visibility, optarg) != 0) {
fprintf(stderr, "%s: invalid value `%s' of --%s",
program_invocation_name, optarg, "require-visibility");
exit(EXIT_FAILURE);
}
break;
case 'n':
prunenames_changed = true;
var_add_values(&conf_prunenames, optarg);
break;
case 'o':
if (!conf_output.empty()) {
fprintf(stderr, "%s: --%s specified twice",
program_invocation_name, "output");
exit(EXIT_FAILURE);
}
conf_output = optarg;
break;
case 'v':
conf_verbose = true;
break;
case 'b':
conf_block_size = atoi(optarg);
break;
case 'D':
use_debug = true;
break;
case OPT_DEBUG_PRUNING:
conf_debug_pruning = true;
break;
case OPT_CONFIG_FILE:
break; // Was handled in parse_initial_arguments
default:
abort();
}
}
options_done:
if (optind != argc) {
fprintf(stderr, "%s: unexpected operand on command line",
program_invocation_name);
exit(EXIT_FAILURE);
}
if (conf_scan_root == NULL) {
static char root[] = "/";
conf_scan_root = root;
}
if (conf_output.empty())
conf_output = DBFILE;
if (conf_output[0] != '/')
conf_output = prepend_cwd(conf_output);
}
/* Conversion of configuration for main code */
/* Store a string list to OBSTACK */
static void
gen_conf_block_string_list(string *obstack,
const vector<string> *strings)
{
for (const string &str : *strings) {
*obstack += str;
*obstack += '\0';
}
*obstack += '\0';
}
/* Generate conf_block */
static void
gen_conf_block(void)
{
conf_block.clear();
#define CONST(S) conf_block.append(S, sizeof(S))
/* conf_check_visibility value is stored in the header */
CONST("prune_bind_mounts");
/* Add two NUL bytes after the value */
conf_block.append(conf_prune_bind_mounts != false ? "1\0" : "0\0", 3);
CONST("prunefs");
gen_conf_block_string_list(&conf_block, &conf_prunefs);
CONST("prunenames");
gen_conf_block_string_list(&conf_block, &conf_prunenames);
CONST("prunepaths");
gen_conf_block_string_list(&conf_block, &conf_prunepaths);
/* scan_root is contained directly in the header */
/* conf_output, conf_verbose are not relevant */
#undef CONST
}
/* Parse /etc/updatedb.conf and command-line arguments ARGC, ARGV.
Exit on error or --help, --version. */
void conf_prepare(int argc, char *argv[])
{
parse_initial_arguments(argc, argv);
parse_updatedb_conf();
parse_arguments(argc, argv);
for (string &str : conf_prunefs) {
/* Assuming filesystem names are ASCII-only */
for (char &c : str)
c = toupper(c);
}
/* Finish the variable only after converting filesystem names to upper case
to avoid keeping duplicates that originally differed in case and to sort
them correctly. */
var_finish(&conf_prunefs);
var_finish(&conf_prunenames);
var_finish(&conf_prunepaths);
gen_conf_block();
string_list_dir_path_sort(&conf_prunepaths);
if (conf_debug_pruning) {
fprintf(stderr, "conf_block:\n");
for (char c : conf_block) {
if (isascii((unsigned char)c) && isprint((unsigned char)c) && c != '\\')
putc(c, stderr);
else {
fprintf(stderr, "\\%03o", (unsigned)(unsigned char)c);
if (c == 0)
putc('\n', stderr);
}
}
fprintf(stderr, "\n-----------------------\n");
}
}
|