Report errno if harness fails to create directory
[xapian.git] / xapian-core / tests / harness / backendmanager.cc
blob6d3f533ff5bb9496a618154e15412e18ac32dadd
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,2017,2018 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
20 * USA
23 #include <config.h>
25 #include <xapian.h>
27 #ifdef HAVE_VALGRIND
28 # include <valgrind/memcheck.h>
29 #endif
31 #include "safeerrno.h"
33 #include <cstdio>
34 #include <fstream>
35 #include <string>
36 #include <vector>
38 #include <sys/types.h>
39 #include "safesysstat.h"
41 #include "filetests.h"
42 #include "index_utils.h"
43 #include "backendmanager.h"
44 #include "unixcmds.h"
46 using namespace std;
48 void
49 BackendManager::index_files_to_database(Xapian::WritableDatabase & database,
50 const vector<string> & files)
52 FileIndexer(datadir, files).index_to(database);
55 /** Create the directory dirname if needed. Returns true if the
56 * directory was created and false if it was already there. Throws
57 * an exception if there was an error (eg not a directory).
59 bool
60 BackendManager::create_dir_if_needed(const string &dirname)
62 // create a directory if not present
63 struct stat sbuf;
64 int result = stat(dirname.c_str(), &sbuf);
65 if (result < 0) {
66 if (errno != ENOENT)
67 throw Xapian::DatabaseOpeningError("Can't stat directory", errno);
68 if (mkdir(dirname.c_str(), 0700) < 0)
69 throw Xapian::DatabaseOpeningError("Can't create directory", errno);
70 return true; // Successfully created a directory.
72 if (!S_ISDIR(sbuf.st_mode))
73 throw Xapian::DatabaseOpeningError("Is not a directory.");
74 return false; // Already a directory.
77 #ifdef XAPIAN_HAS_INMEMORY_BACKEND
78 Xapian::WritableDatabase
79 BackendManager::getwritedb_inmemory(const vector<string> &files)
81 Xapian::WritableDatabase db(string(), Xapian::DB_BACKEND_INMEMORY);
82 index_files_to_database(db, files);
83 return db;
85 #endif
87 #ifdef XAPIAN_HAS_GLASS_BACKEND
88 string
89 BackendManager::createdb_glass(const vector<string> &files)
91 string parent_dir = ".glass";
92 create_dir_if_needed(parent_dir);
94 string dbdir = parent_dir + "/db";
95 for (vector<string>::const_iterator i = files.begin();
96 i != files.end(); ++i) {
97 dbdir += '=';
98 dbdir += *i;
100 // If the database is readonly, we can reuse it if it exists.
101 if (create_dir_if_needed(dbdir)) {
102 // Directory was created, so do the indexing.
103 Xapian::WritableDatabase db(dbdir,
104 Xapian::DB_CREATE|Xapian::DB_BACKEND_GLASS, 2048);
105 index_files_to_database(db, files);
106 db.commit();
108 return dbdir;
111 Xapian::WritableDatabase
112 BackendManager::getwritedb_glass(const string & name,
113 const vector<string> & files)
115 string dbdir = getwritedb_glass_path(name);
117 // For a writable database we need to start afresh each time.
118 rm_rf(dbdir);
119 (void)create_dir_if_needed(dbdir);
121 // directory was created, so do the indexing.
122 Xapian::WritableDatabase db(dbdir,
123 Xapian::DB_CREATE|Xapian::DB_BACKEND_GLASS, 2048);
124 index_files_to_database(db, files);
125 return db;
128 std::string
129 BackendManager::getwritedb_glass_path(const string & name)
131 string parent_dir = ".glass";
132 create_dir_if_needed(parent_dir);
134 string dbdir = parent_dir;
135 dbdir += '/';
136 dbdir += name;
137 return dbdir;
139 #endif
141 #ifdef XAPIAN_HAS_CHERT_BACKEND
142 string
143 BackendManager::createdb_chert(const vector<string> &files)
145 string parent_dir = ".chert";
146 create_dir_if_needed(parent_dir);
148 string dbdir = parent_dir + "/db";
149 for (vector<string>::const_iterator i = files.begin();
150 i != files.end(); ++i) {
151 dbdir += '=';
152 dbdir += *i;
154 // If the database is readonly, we can reuse it if it exists.
155 if (create_dir_if_needed(dbdir)) {
156 // Directory was created, so do the indexing.
157 Xapian::WritableDatabase db(dbdir,
158 Xapian::DB_CREATE|Xapian::DB_BACKEND_CHERT, 2048);
159 index_files_to_database(db, files);
160 db.commit();
162 return dbdir;
165 Xapian::WritableDatabase
166 BackendManager::getwritedb_chert(const string & name,
167 const vector<string> & files)
169 string dbdir = getwritedb_chert_path(name);
171 // For a writable database we need to start afresh each time.
172 rm_rf(dbdir);
173 (void)create_dir_if_needed(dbdir);
175 // directory was created, so do the indexing.
176 Xapian::WritableDatabase db(dbdir,
177 Xapian::DB_CREATE|Xapian::DB_BACKEND_CHERT, 2048);
178 index_files_to_database(db, files);
179 return db;
182 std::string
183 BackendManager::getwritedb_chert_path(const string & name)
185 string parent_dir = ".chert";
186 create_dir_if_needed(parent_dir);
188 string dbdir = parent_dir;
189 dbdir += '/';
190 dbdir += name;
191 return dbdir;
194 #endif
196 BackendManager::~BackendManager() { }
198 std::string
199 BackendManager::get_dbtype() const
201 return "none";
204 string
205 BackendManager::do_get_database_path(const vector<string> &)
207 throw Xapian::InvalidArgumentError("Path isn't meaningful for this database type");
210 Xapian::Database
211 BackendManager::do_get_database(const vector<string> & files)
213 return Xapian::Database(do_get_database_path(files));
216 Xapian::Database
217 BackendManager::get_database(const vector<string> & files)
219 return do_get_database(files);
222 Xapian::Database
223 BackendManager::get_database(const string & file)
225 return do_get_database(vector<string>(1, file));
228 Xapian::Database
229 BackendManager::get_database(const std::string &dbname,
230 void (*gen)(Xapian::WritableDatabase&,
231 const std::string &),
232 const std::string &arg)
234 string dbleaf = "db__";
235 dbleaf += dbname;
236 const string & path = get_writable_database_path(dbleaf);
237 if (path.empty()) {
238 // InMemory doesn't have a path but we want to support generated
239 // databases for it.
240 Xapian::WritableDatabase wdb = get_writable_database(path, path);
241 gen(wdb, arg);
242 return wdb;
245 if (path_exists(path)) {
246 try {
247 return Xapian::Database(path);
248 } catch (const Xapian::DatabaseOpeningError &) {
251 rm_rf(path);
253 string tmp_dbleaf(dbleaf);
254 tmp_dbleaf += '~';
255 string tmp_path(path);
256 tmp_path += '~';
259 Xapian::WritableDatabase wdb = get_writable_database(tmp_dbleaf,
260 string());
261 gen(wdb, arg);
263 rename(tmp_path.c_str(), path.c_str());
264 // For multi, the shards will use the temporary name, but that's not really
265 // a problem.
267 return Xapian::Database(path);
270 std::string
271 BackendManager::get_database_path(const std::string &dbname,
272 void (*gen)(Xapian::WritableDatabase&,
273 const std::string &),
274 const std::string &arg)
276 string dbleaf = "db__";
277 dbleaf += dbname;
278 const string & path = get_writable_database_path(dbleaf);
279 if (path_exists(path)) {
280 try {
281 (void)Xapian::Database(path);
282 return path;
283 } catch (const Xapian::DatabaseOpeningError &) {
286 rm_rf(path);
288 string tmp_dbleaf(dbleaf);
289 tmp_dbleaf += '~';
290 string tmp_path(path);
291 tmp_path += '~';
294 Xapian::WritableDatabase wdb = get_writable_database(tmp_dbleaf,
295 string());
296 gen(wdb, arg);
298 rename(tmp_path.c_str(), path.c_str());
300 return path;
303 string
304 BackendManager::get_database_path(const vector<string> & files)
306 return do_get_database_path(files);
309 string
310 BackendManager::get_database_path(const string & file)
312 return do_get_database_path(vector<string>(1, file));
315 Xapian::WritableDatabase
316 BackendManager::get_writable_database(const string &, const string &)
318 throw Xapian::InvalidArgumentError("Attempted to open a disabled database");
321 string
322 BackendManager::get_writable_database_path(const std::string &)
324 throw Xapian::InvalidArgumentError("Path isn't meaningful for this database type");
327 Xapian::Database
328 BackendManager::get_remote_database(const vector<string> &, unsigned int)
330 string msg = "BackendManager::get_remote_database() called for non-remote database (type is ";
331 msg += get_dbtype();
332 msg += ')';
333 throw Xapian::InvalidOperationError(msg);
336 Xapian::Database
337 BackendManager::get_writable_database_as_database()
339 string msg = "Backend ";
340 msg += get_dbtype();
341 msg += " doesn't support get_writable_database_as_database()";
342 throw Xapian::InvalidOperationError(msg);
345 Xapian::WritableDatabase
346 BackendManager::get_writable_database_again()
348 string msg = "Backend ";
349 msg += get_dbtype();
350 msg += " doesn't support get_writable_database_again()";
351 throw Xapian::InvalidOperationError(msg);
354 void
355 BackendManager::clean_up()
359 const char *
360 BackendManager::get_xapian_progsrv_command()
362 #ifdef HAVE_VALGRIND
363 if (RUNNING_ON_VALGRIND) {
364 return "./runsrv " XAPIAN_PROGSRV;
366 #endif
367 return XAPIAN_PROGSRV;