1 /* backendmanager.cc: manage backends for testsuite
2 *
3 * Copyright 1999,2000,2001 BrightStation PLC
4 * Copyright 2002 Ananova Ltd
5 * Copyright 2002,2003,2004,2005,2006,2007,2008,2009,2010,2012 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 #ifdef HAVE_VALGRIND
28 # include <valgrind/memcheck.h>
29 #endif
30
31 #include "safeerrno.h"
32
33 #include <cstdio>
34 #include <fstream>
35 #include <string>
36 #include <vector>
37
38 #include <sys/types.h>
39 #include "safesysstat.h"
40
41 #include "index_utils.h"
42 #include "backendmanager.h"
43 #include "unixcmds.h"
44 #include "utils.h"
45
46 using namespace std;
47
48 void
index_files_to_database(Xapian::WritableDatabase & database,const vector<string> & files)49 BackendManager::index_files_to_database(Xapian::WritableDatabase & database,
50 const vector<string> & files)
51 {
52 FileIndexer f(datadir, files);
53 while (f) database.add_document(f.next());
54 }
55
56 /** Create the directory dirname if needed. Returns true if the
57 * directory was created and false if it was already there. Throws
58 * an exception if there was an error (eg not a directory).
59 */
60 bool
create_dir_if_needed(const string & dirname)61 BackendManager::create_dir_if_needed(const string &dirname)
62 {
63 // create a directory if not present
64 struct stat sbuf;
65 int result = stat(dirname, &sbuf);
66 if (result < 0) {
67 if (errno != ENOENT)
68 throw Xapian::DatabaseOpeningError("Can't stat directory");
69 if (mkdir(dirname, 0700) < 0)
70 throw Xapian::DatabaseOpeningError("Can't create directory");
71 return true; // Successfully created a directory.
72 }
73 if (!S_ISDIR(sbuf.st_mode))
74 throw Xapian::DatabaseOpeningError("Is not a directory.");
75 return false; // Already a directory.
76 }
77
78 #ifdef XAPIAN_HAS_INMEMORY_BACKEND
79 Xapian::WritableDatabase
getwritedb_inmemory(const vector<string> & files)80 BackendManager::getwritedb_inmemory(const vector<string> &files)
81 {
82 Xapian::WritableDatabase db(Xapian::InMemory::open());
83 index_files_to_database(db, files);
84 return db;
85 }
86 #endif
87
88 #ifdef XAPIAN_HAS_BRASS_BACKEND
89 string
createdb_brass(const vector<string> & files)90 BackendManager::createdb_brass(const vector<string> &files)
91 {
92 string parent_dir = ".brass";
93 create_dir_if_needed(parent_dir);
94
95 string dbdir = parent_dir + "/db";
96 for (vector<string>::const_iterator i = files.begin();
97 i != files.end(); ++i) {
98 dbdir += '=';
99 dbdir += *i;
100 }
101 // If the database is readonly, we can reuse it if it exists.
102 if (create_dir_if_needed(dbdir)) {
103 // Directory was created, so do the indexing.
104 Xapian::WritableDatabase db(Xapian::Brass::open(dbdir, Xapian::DB_CREATE, 2048));
105 index_files_to_database(db, files);
106 db.commit();
107 }
108 return dbdir;
109 }
110
111 Xapian::WritableDatabase
getwritedb_brass(const string & name,const vector<string> & files)112 BackendManager::getwritedb_brass(const string & name,
113 const vector<string> & files)
114 {
115 string dbdir = getwritedb_brass_path(name);
116
117 // For a writable database we need to start afresh each time.
118 rm_rf(dbdir);
119 (void)create_dir_if_needed(dbdir);
120
121 // directory was created, so do the indexing.
122 Xapian::WritableDatabase db(Xapian::Brass::open(dbdir, Xapian::DB_CREATE, 2048));
123 index_files_to_database(db, files);
124 return db;
125 }
126
127 std::string
getwritedb_brass_path(const string & name)128 BackendManager::getwritedb_brass_path(const string & name)
129 {
130 string parent_dir = ".brass";
131 create_dir_if_needed(parent_dir);
132
133 string dbdir = parent_dir;
134 dbdir += '/';
135 dbdir += name;
136 return dbdir;
137 }
138 #endif
139
140 #ifdef XAPIAN_HAS_CHERT_BACKEND
141 string
createdb_chert(const vector<string> & files)142 BackendManager::createdb_chert(const vector<string> &files)
143 {
144 string parent_dir = ".chert";
145 create_dir_if_needed(parent_dir);
146
147 string dbdir = parent_dir + "/db";
148 for (vector<string>::const_iterator i = files.begin();
149 i != files.end(); ++i) {
150 dbdir += '=';
151 dbdir += *i;
152 }
153 // If the database is readonly, we can reuse it if it exists.
154 if (create_dir_if_needed(dbdir)) {
155 // Directory was created, so do the indexing.
156 Xapian::WritableDatabase db(Xapian::Chert::open(dbdir, Xapian::DB_CREATE, 2048));
157 index_files_to_database(db, files);
158 db.commit();
159 }
160 return dbdir;
161 }
162
163 Xapian::WritableDatabase
getwritedb_chert(const string & name,const vector<string> & files)164 BackendManager::getwritedb_chert(const string & name,
165 const vector<string> & files)
166 {
167 string dbdir = getwritedb_chert_path(name);
168
169 // For a writable database we need to start afresh each time.
170 rm_rf(dbdir);
171 (void)create_dir_if_needed(dbdir);
172
173 // directory was created, so do the indexing.
174 Xapian::WritableDatabase db(Xapian::Chert::open(dbdir, Xapian::DB_CREATE, 2048));
175 index_files_to_database(db, files);
176 return db;
177 }
178
179 std::string
getwritedb_chert_path(const string & name)180 BackendManager::getwritedb_chert_path(const string & name)
181 {
182 string parent_dir = ".chert";
183 create_dir_if_needed(parent_dir);
184
185 string dbdir = parent_dir;
186 dbdir += '/';
187 dbdir += name;
188 return dbdir;
189 }
190
191 #endif
192
193 #ifdef XAPIAN_HAS_FLINT_BACKEND
194 string
createdb_flint(const vector<string> & files)195 BackendManager::createdb_flint(const vector<string> &files)
196 {
197 string parent_dir = ".flint";
198 create_dir_if_needed(parent_dir);
199
200 string dbdir = parent_dir + "/db";
201 for (vector<string>::const_iterator i = files.begin();
202 i != files.end(); i++) {
203 dbdir += '=';
204 dbdir += *i;
205 }
206 // If the database is readonly, we can reuse it if it exists.
207 if (create_dir_if_needed(dbdir)) {
208 // Directory was created, so do the indexing.
209 Xapian::WritableDatabase db(Xapian::Flint::open(dbdir, Xapian::DB_CREATE, 2048));
210 index_files_to_database(db, files);
211 db.commit();
212 }
213 return dbdir;
214 }
215
216 Xapian::WritableDatabase
getwritedb_flint(const string & name,const vector<string> & files)217 BackendManager::getwritedb_flint(const string & name,
218 const vector<string> & files)
219 {
220 string dbdir = getwritedb_flint_path(name);
221
222 // For a writable database we need to start afresh each time.
223 rm_rf(dbdir);
224 (void)create_dir_if_needed(dbdir);
225
226 // directory was created, so do the indexing.
227 Xapian::WritableDatabase db(Xapian::Flint::open(dbdir, Xapian::DB_CREATE, 2048));
228 index_files_to_database(db, files);
229 return db;
230 }
231
232 std::string
getwritedb_flint_path(const string & name)233 BackendManager::getwritedb_flint_path(const string & name)
234 {
235 string parent_dir = ".flint";
236 create_dir_if_needed(parent_dir);
237
238 string dbdir = parent_dir;
239 dbdir += '/';
240 dbdir += name;
241 return dbdir;
242 }
243
244 #endif
245
~BackendManager()246 BackendManager::~BackendManager() { }
247
248 std::string
get_dbtype() const249 BackendManager::get_dbtype() const
250 {
251 return "none";
252 }
253
254 string
do_get_database_path(const vector<string> &)255 BackendManager::do_get_database_path(const vector<string> &)
256 {
257 throw Xapian::InvalidArgumentError("Path isn't meaningful for this database type");
258 }
259
260 Xapian::Database
do_get_database(const vector<string> & files)261 BackendManager::do_get_database(const vector<string> & files)
262 {
263 return Xapian::Database(do_get_database_path(files));
264 }
265
266 Xapian::Database
get_database(const vector<string> & files)267 BackendManager::get_database(const vector<string> & files)
268 {
269 return do_get_database(files);
270 }
271
272 Xapian::Database
get_database(const string & file)273 BackendManager::get_database(const string & file)
274 {
275 return do_get_database(vector<string>(1, file));
276 }
277
278 Xapian::Database
get_database(const std::string & dbname,void (* gen)(Xapian::WritableDatabase &,const std::string &),const std::string & arg)279 BackendManager::get_database(const std::string &dbname,
280 void (*gen)(Xapian::WritableDatabase&,
281 const std::string &),
282 const std::string &arg)
283 {
284 string dbleaf = "db__";
285 dbleaf += dbname;
286 const string & path = get_writable_database_path(dbleaf);
287 try {
288 return Xapian::Database(path);
289 } catch (const Xapian::DatabaseOpeningError &) {
290 }
291 rm_rf(path);
292
293 string tmp_dbleaf(dbleaf);
294 tmp_dbleaf += '~';
295 string tmp_path(path);
296 tmp_path += '~';
297
298 {
299 Xapian::WritableDatabase wdb = get_writable_database(tmp_dbleaf,
300 string());
301 gen(wdb, arg);
302 }
303 rename(tmp_path.c_str(), path.c_str());
304
305 return Xapian::Database(path);
306 }
307
308 std::string
get_database_path(const std::string & dbname,void (* gen)(Xapian::WritableDatabase &,const std::string &),const std::string & arg)309 BackendManager::get_database_path(const std::string &dbname,
310 void (*gen)(Xapian::WritableDatabase&,
311 const std::string &),
312 const std::string &arg)
313 {
314 string dbleaf = "db__";
315 dbleaf += dbname;
316 const string & path = get_writable_database_path(dbleaf);
317 try {
318 (void)Xapian::Database(path);
319 return path;
320 } catch (const Xapian::DatabaseOpeningError &) {
321 }
322 rm_rf(path);
323
324 string tmp_dbleaf(dbleaf);
325 tmp_dbleaf += '~';
326 string tmp_path(path);
327 tmp_path += '~';
328
329 {
330 Xapian::WritableDatabase wdb = get_writable_database(tmp_dbleaf,
331 string());
332 gen(wdb, arg);
333 }
334 rename(tmp_path.c_str(), path.c_str());
335
336 return path;
337 }
338
339 string
get_database_path(const vector<string> & files)340 BackendManager::get_database_path(const vector<string> & files)
341 {
342 return do_get_database_path(files);
343 }
344
345 string
get_database_path(const string & file)346 BackendManager::get_database_path(const string & file)
347 {
348 return do_get_database_path(vector<string>(1, file));
349 }
350
351 Xapian::WritableDatabase
get_writable_database(const string &,const string &)352 BackendManager::get_writable_database(const string &, const string &)
353 {
354 throw Xapian::InvalidArgumentError("Attempted to open a disabled database");
355 }
356
357 string
get_writable_database_path(const std::string &)358 BackendManager::get_writable_database_path(const std::string &)
359 {
360 throw Xapian::InvalidArgumentError("Path isn't meaningful for this database type");
361 }
362
363 Xapian::Database
get_remote_database(const vector<string> &,unsigned int)364 BackendManager::get_remote_database(const vector<string> &, unsigned int)
365 {
366 string msg = "BackendManager::get_remote_database() called for non-remote database (type is ";
367 msg += get_dbtype();
368 msg += ')';
369 throw Xapian::InvalidOperationError(msg);
370 }
371
372 Xapian::Database
get_writable_database_as_database()373 BackendManager::get_writable_database_as_database()
374 {
375 string msg = "Backend ";
376 msg += get_dbtype();
377 msg += " doesn't support get_writable_database_as_database()";
378 throw Xapian::InvalidOperationError(msg);
379 }
380
381 Xapian::WritableDatabase
get_writable_database_again()382 BackendManager::get_writable_database_again()
383 {
384 string msg = "Backend ";
385 msg += get_dbtype();
386 msg += " doesn't support get_writable_database_again()";
387 throw Xapian::InvalidOperationError(msg);
388 }
389
390 void
clean_up()391 BackendManager::clean_up()
392 {
393 }
394
395 const char *
get_xapian_progsrv_command()396 BackendManager::get_xapian_progsrv_command()
397 {
398 #ifdef HAVE_VALGRIND
399 if (RUNNING_ON_VALGRIND) {
400 return "./runsrv " XAPIAN_PROGSRV;
401 }
402 #endif
403 return XAPIAN_PROGSRV;
404 }
405