xapian-core  1.4.21
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, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20  * USA
21  */
22 
23 #include <config.h>
24 
25 #include <xapian.h>
26 
27 #include <cstdlib>
28 #include <cstring>
29 #include <stdexcept>
30 #include <iostream>
31 
32 using namespace std;
33 
34 #define PROG_NAME "xapian-check"
35 #define PROG_DESC "Check the consistency of a database or table"
36 
37 static void show_usage() {
38  cout << "Usage: " PROG_NAME " DATABASE_DIRECTORY|PATH_TO_BTREE [[F][t][f][b][v][+]]\n\n"
39 "If a whole database is checked, then additional cross-checks between\n"
40 "the tables are performed.\n\n"
41 "The btree(s) is/are always checked - control the output verbosity with:\n"
42 " F = attempt to fix a broken database (implemented for chert currently)\n"
43 " t = short tree printing\n"
44 " f = full tree printing\n"
45 " b = show free blocks\n"
46 " v = show stats about B-tree (default)\n"
47 " + = same as tbv\n"
48 " e.g. " PROG_NAME " /var/lib/xapian/data/default\n"
49 " " PROG_NAME " /var/lib/xapian/data/default/postlist fbv" << endl;
50 }
51 
52 int
53 main(int argc, char **argv)
54 {
55  if (argc > 1 && argv[1][0] == '-') {
56  if (strcmp(argv[1], "--help") == 0) {
57  cout << PROG_NAME " - " PROG_DESC "\n\n";
58  show_usage();
59  exit(0);
60  }
61  if (strcmp(argv[1], "--version") == 0) {
62  cout << PROG_NAME " - " PACKAGE_STRING << endl;
63  exit(0);
64  }
65  }
66  if (argc < 2 || argc > 3) {
67  show_usage();
68  exit(1);
69  }
70 
71  int opts = 0;
72  const char * opt_string = argv[2];
73  if (!opt_string) opt_string = "v";
74  for (const char *p = opt_string; *p; ++p) {
75  switch (*p) {
76  case 't': opts |= Xapian::DBCHECK_SHORT_TREE; break;
77  case 'f': opts |= Xapian::DBCHECK_FULL_TREE; break;
78  case 'b': opts |= Xapian::DBCHECK_SHOW_FREELIST; break;
79  case 'v': opts |= Xapian::DBCHECK_SHOW_STATS; break;
80  case '+':
84  break;
85  case 'F':
86  opts |= Xapian::DBCHECK_FIX;
87  break;
88  default:
89  cerr << "option " << opt_string << " unknown\n";
90  cerr << "use t,f,b,v and/or + in the option string\n";
91  exit(1);
92  }
93  }
94 
95  try {
96  size_t errors = Xapian::Database::check(argv[1], opts, &cout);
97  if (errors > 0) {
98  cout << "Total errors found: " << errors << endl;
99  exit(1);
100  }
101  cout << "No errors found" << endl;
102  } catch (const Xapian::Error &error) {
103  cerr << argv[0] << ": " << error.get_description() << endl;
104  exit(1);
105  } catch (...) {
106  cerr << argv[0] << ": Unknown exception" << endl;
107  exit(1);
108  }
109 }
static size_t check(const std::string &path, int opts=0, std::ostream *out=NULL)
Check the integrity of a database or database table.
Definition: database.h:560
static const char * opts
STL namespace.
const int DBCHECK_SHOW_FREELIST
Show the bitmap for the B-tree.
Definition: constants.h:222
const int DBCHECK_FULL_TREE
Show a full display of the B-tree contents.
Definition: constants.h:216
Public interfaces for the Xapian library.
#define PROG_NAME
Definition: xapian-check.cc:34
const int DBCHECK_FIX
Fix problems.
Definition: constants.h:243
static void show_usage()
Definition: xapian-check.cc:37
std::string get_description() const
Return a string describing this object.
Definition: error.cc:93
All exceptions thrown by Xapian are subclasses of Xapian::Error.
Definition: error.h:43
int main(int argc, char **argv)
Definition: xapian-check.cc:53
#define PACKAGE_STRING
Definition: config.h:337
#define PROG_DESC
Definition: xapian-check.cc:35
const int DBCHECK_SHORT_TREE
Show a short-format display of the B-tree contents.
Definition: constants.h:210
const int DBCHECK_SHOW_STATS
Show statistics for the B-tree.
Definition: constants.h:228