xapian-core  2.0.0
xapian-check.cc
Go to the documentation of this file.
1 
4 /* Copyright 1999,2000,2001 BrightStation PLC
5  * Copyright 2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2014,2016 Olly Betts
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see
19  * <https://www.gnu.org/licenses/>.
20  */
21 
22 #include <config.h>
23 
24 #include <xapian.h>
25 
26 #include <cstdlib>
27 #include <cstring>
28 #include <stdexcept>
29 #include <iostream>
30 
31 using namespace std;
32 
33 #define PROG_NAME "xapian-check"
34 #define PROG_DESC "Check the consistency of a database or table"
35 
36 static void show_usage() {
37  cout << "Usage: " PROG_NAME " DATABASE_DIRECTORY|PATH_TO_BTREE [[F][t][f][b][v][+]]\n\n"
38 "If a whole database is checked, then additional cross-checks between\n"
39 "the tables are performed.\n\n"
40 "The btree(s) is/are always checked - control the output verbosity with:\n"
41 " F = attempt to fix a broken database (implemented for glass currently)\n"
42 " t = short tree printing\n"
43 " f = full tree printing\n"
44 " b = show free blocks\n"
45 " v = show stats about B-tree (default)\n"
46 " + = same as tbv\n"
47 " e.g. " PROG_NAME " /var/lib/xapian/data/default\n"
48 " " PROG_NAME " /var/lib/xapian/data/default/postlist fbv\n";
49 }
50 
51 int
52 main(int argc, char **argv)
53 {
54  if (argc > 1 && argv[1][0] == '-') {
55  if (strcmp(argv[1], "--help") == 0) {
56  cout << PROG_NAME " - " PROG_DESC "\n\n";
57  show_usage();
58  exit(0);
59  }
60  if (strcmp(argv[1], "--version") == 0) {
61  cout << PROG_NAME " - " PACKAGE_STRING "\n";
62  exit(0);
63  }
64  }
65  if (argc < 2 || argc > 3) {
66  show_usage();
67  exit(1);
68  }
69 
70  int opts = 0;
71  const char * opt_string = argv[2];
72  if (!opt_string) opt_string = "v";
73  for (const char *p = opt_string; *p; ++p) {
74  switch (*p) {
75  case 't': opts |= Xapian::DBCHECK_SHORT_TREE; break;
76  case 'f': opts |= Xapian::DBCHECK_FULL_TREE; break;
77  case 'b': opts |= Xapian::DBCHECK_SHOW_FREELIST; break;
78  case 'v': opts |= Xapian::DBCHECK_SHOW_STATS; break;
79  case '+':
83  break;
84  case 'F':
86  break;
87  default:
88  cerr << "option " << opt_string << " unknown\n";
89  cerr << "use t,f,b,v and/or + in the option string\n";
90  exit(1);
91  }
92  }
93 
94  try {
95  size_t errors = Xapian::Database::check(argv[1], opts, &cout);
96  if (errors > 0) {
97  cout << "Total errors found: " << errors << '\n';
98  exit(1);
99  }
100  cout << "No errors found\n";
101  } catch (const Xapian::Error &error) {
102  cerr << argv[0] << ": " << error.get_description() << '\n';
103  exit(1);
104  } catch (...) {
105  cerr << argv[0] << ": Unknown exception\n";
106  exit(1);
107  }
108 }
static size_t check(std::string_view path, int opts=0, std::ostream *out=NULL)
Check the integrity of a database or database table.
Definition: database.h:669
All exceptions thrown by Xapian are subclasses of Xapian::Error.
Definition: error.h:41
std::string get_description() const
Return a string describing this object.
Definition: error.cc:93
#define PACKAGE_STRING
Definition: config.h:361
PositionList * p
const int DBCHECK_SHOW_FREELIST
Show the bitmap for the B-tree.
Definition: constants.h:224
const int DBCHECK_SHOW_STATS
Show statistics for the B-tree.
Definition: constants.h:230
const int DBCHECK_SHORT_TREE
Show a short-format display of the B-tree contents.
Definition: constants.h:212
const int DBCHECK_FIX
Fix problems.
Definition: constants.h:238
const int DBCHECK_FULL_TREE
Show a full display of the B-tree contents.
Definition: constants.h:218
static void show_usage()
Definition: xapian-check.cc:36
int main(int argc, char **argv)
Definition: xapian-check.cc:52
#define PROG_NAME
Definition: xapian-check.cc:33
#define PROG_DESC
Definition: xapian-check.cc:34
static const char * opts
Public interfaces for the Xapian library.