1 /* backendmanager.cc: manage backends for testsuite
3 * Copyright 1999,2000,2001 BrightStation PLC
4 * Copyright 2002 Ananova Ltd
5 * Copyright 2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2016 Olly Betts
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.
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.
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
28 # include <valgrind/memcheck.h>
31 #include "safeerrno.h"
38 #include <sys/types.h>
39 #include "safesysstat.h"
41 #include "index_utils.h"
42 #include "backendmanager.h"
48 BackendManager::index_files_to_database(Xapian::WritableDatabase
& database
,
49 const vector
<string
> & files
)
51 FileIndexer(datadir
, files
).index_to(database
);
54 /** Create the directory dirname if needed. Returns true if the
55 * directory was created and false if it was already there. Throws
56 * an exception if there was an error (eg not a directory).
59 BackendManager::create_dir_if_needed(const string
&dirname
)
61 // create a directory if not present
63 int result
= stat(dirname
.c_str(), &sbuf
);
66 throw Xapian::DatabaseOpeningError("Can't stat directory");
67 if (mkdir(dirname
.c_str(), 0700) < 0)
68 throw Xapian::DatabaseOpeningError("Can't create directory");
69 return true; // Successfully created a directory.
71 if (!S_ISDIR(sbuf
.st_mode
))
72 throw Xapian::DatabaseOpeningError("Is not a directory.");
73 return false; // Already a directory.
76 #ifdef XAPIAN_HAS_INMEMORY_BACKEND
77 Xapian::WritableDatabase
78 BackendManager::getwritedb_inmemory(const vector
<string
> &files
)
80 Xapian::WritableDatabase
db(string(), Xapian::DB_BACKEND_INMEMORY
);
81 index_files_to_database(db
, files
);
86 #ifdef XAPIAN_HAS_GLASS_BACKEND
88 BackendManager::createdb_glass(const vector
<string
> &files
)
90 string parent_dir
= ".glass";
91 create_dir_if_needed(parent_dir
);
93 string dbdir
= parent_dir
+ "/db";
94 for (vector
<string
>::const_iterator i
= files
.begin();
95 i
!= files
.end(); ++i
) {
99 // If the database is readonly, we can reuse it if it exists.
100 if (create_dir_if_needed(dbdir
)) {
101 // Directory was created, so do the indexing.
102 Xapian::WritableDatabase
db(dbdir
,
103 Xapian::DB_CREATE
|Xapian::DB_BACKEND_GLASS
, 2048);
104 index_files_to_database(db
, files
);
110 Xapian::WritableDatabase
111 BackendManager::getwritedb_glass(const string
& name
,
112 const vector
<string
> & files
)
114 string dbdir
= getwritedb_glass_path(name
);
116 // For a writable database we need to start afresh each time.
118 (void)create_dir_if_needed(dbdir
);
120 // directory was created, so do the indexing.
121 Xapian::WritableDatabase
db(dbdir
,
122 Xapian::DB_CREATE
|Xapian::DB_BACKEND_GLASS
, 2048);
123 index_files_to_database(db
, files
);
128 BackendManager::getwritedb_glass_path(const string
& name
)
130 string parent_dir
= ".glass";
131 create_dir_if_needed(parent_dir
);
133 string dbdir
= parent_dir
;
140 #ifdef XAPIAN_HAS_CHERT_BACKEND
142 BackendManager::createdb_chert(const vector
<string
> &files
)
144 string parent_dir
= ".chert";
145 create_dir_if_needed(parent_dir
);
147 string dbdir
= parent_dir
+ "/db";
148 for (vector
<string
>::const_iterator i
= files
.begin();
149 i
!= files
.end(); ++i
) {
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(dbdir
,
157 Xapian::DB_CREATE
|Xapian::DB_BACKEND_CHERT
, 2048);
158 index_files_to_database(db
, files
);
164 Xapian::WritableDatabase
165 BackendManager::getwritedb_chert(const string
& name
,
166 const vector
<string
> & files
)
168 string dbdir
= getwritedb_chert_path(name
);
170 // For a writable database we need to start afresh each time.
172 (void)create_dir_if_needed(dbdir
);
174 // directory was created, so do the indexing.
175 Xapian::WritableDatabase
db(dbdir
,
176 Xapian::DB_CREATE
|Xapian::DB_BACKEND_CHERT
, 2048);
177 index_files_to_database(db
, files
);
182 BackendManager::getwritedb_chert_path(const string
& name
)
184 string parent_dir
= ".chert";
185 create_dir_if_needed(parent_dir
);
187 string dbdir
= parent_dir
;
195 BackendManager::~BackendManager() { }
198 BackendManager::get_dbtype() const
204 BackendManager::do_get_database_path(const vector
<string
> &)
206 throw Xapian::InvalidArgumentError("Path isn't meaningful for this database type");
210 BackendManager::do_get_database(const vector
<string
> & files
)
212 return Xapian::Database(do_get_database_path(files
));
216 BackendManager::get_database(const vector
<string
> & files
)
218 return do_get_database(files
);
222 BackendManager::get_database(const string
& file
)
224 return do_get_database(vector
<string
>(1, file
));
228 BackendManager::get_database(const std::string
&dbname
,
229 void (*gen
)(Xapian::WritableDatabase
&,
230 const std::string
&),
231 const std::string
&arg
)
233 string dbleaf
= "db__";
235 const string
& path
= get_writable_database_path(dbleaf
);
237 return Xapian::Database(path
);
238 } catch (const Xapian::DatabaseOpeningError
&) {
242 string
tmp_dbleaf(dbleaf
);
244 string
tmp_path(path
);
248 Xapian::WritableDatabase wdb
= get_writable_database(tmp_dbleaf
,
252 rename(tmp_path
.c_str(), path
.c_str());
254 return Xapian::Database(path
);
258 BackendManager::get_database_path(const std::string
&dbname
,
259 void (*gen
)(Xapian::WritableDatabase
&,
260 const std::string
&),
261 const std::string
&arg
)
263 string dbleaf
= "db__";
265 const string
& path
= get_writable_database_path(dbleaf
);
267 (void)Xapian::Database(path
);
269 } catch (const Xapian::DatabaseOpeningError
&) {
273 string
tmp_dbleaf(dbleaf
);
275 string
tmp_path(path
);
279 Xapian::WritableDatabase wdb
= get_writable_database(tmp_dbleaf
,
283 rename(tmp_path
.c_str(), path
.c_str());
289 BackendManager::get_database_path(const vector
<string
> & files
)
291 return do_get_database_path(files
);
295 BackendManager::get_database_path(const string
& file
)
297 return do_get_database_path(vector
<string
>(1, file
));
300 Xapian::WritableDatabase
301 BackendManager::get_writable_database(const string
&, const string
&)
303 throw Xapian::InvalidArgumentError("Attempted to open a disabled database");
307 BackendManager::get_writable_database_path(const std::string
&)
309 throw Xapian::InvalidArgumentError("Path isn't meaningful for this database type");
313 BackendManager::get_remote_database(const vector
<string
> &, unsigned int)
315 string msg
= "BackendManager::get_remote_database() called for non-remote database (type is ";
318 throw Xapian::InvalidOperationError(msg
);
322 BackendManager::get_writable_database_as_database()
324 string msg
= "Backend ";
326 msg
+= " doesn't support get_writable_database_as_database()";
327 throw Xapian::InvalidOperationError(msg
);
330 Xapian::WritableDatabase
331 BackendManager::get_writable_database_again()
333 string msg
= "Backend ";
335 msg
+= " doesn't support get_writable_database_again()";
336 throw Xapian::InvalidOperationError(msg
);
340 BackendManager::clean_up()
345 BackendManager::get_xapian_progsrv_command()
348 if (RUNNING_ON_VALGRIND
) {
349 return "./runsrv " XAPIAN_PROGSRV
;
352 return XAPIAN_PROGSRV
;