2 * @brief Database factories for non-remote databases.
4 /* Copyright 2002,2003,2004,2005,2006,2007,2008,2009,2011,2012,2013,2014,2015 Olly Betts
5 * Copyright 2008 Lemur Consulting Ltd
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
25 #include "xapian/dbfactory.h"
27 #include "xapian/constants.h"
28 #include "xapian/database.h"
29 #include "xapian/error.h"
30 #include "xapian/version.h" // For XAPIAN_HAS_XXX_BACKEND.
33 #include "filetests.h"
34 #include "fileutils.h"
35 #include "posixy_wrapper.h"
38 #include "safeerrno.h"
39 #include <cstdlib> // For atoi().
41 #ifdef XAPIAN_HAS_GLASS_BACKEND
42 # include "glass/glass_database.h"
44 #ifdef XAPIAN_HAS_CHERT_BACKEND
45 # include "chert/chert_database.h"
47 #ifdef XAPIAN_HAS_INMEMORY_BACKEND
48 # include "inmemory/inmemory_database.h"
50 // Even if none of the above get included, we still need a definition of
51 // Database::Internal.
52 #include "backends/database.h"
60 check_if_single_file_db(const struct stat
& sb
, const string
& path
,
63 #ifdef XAPIAN_HAS_GLASS_BACKEND
64 if (!S_ISREG(sb
.st_mode
)) return false;
65 // Look at the size as a clue - if it's 0 or not a multiple of 2048,
66 // then it's not a single-file glass database. If it is, peek at the start
67 // of the file to determine which it is.
68 if (sb
.st_size
== 0 || sb
.st_size
% 2048 != 0) return false;
69 int fd
= posixy_open(path
.c_str(), O_RDONLY
|O_BINARY
);
72 // FIXME: Don't duplicate magic check here...
73 if (io_read(fd
, magic_buf
, 14) == 14 &&
74 lseek(fd
, 0, SEEK_SET
) == 0 &&
75 memcmp(magic_buf
, "\x0f\x0dXapian Glass", 14) == 0) {
95 #ifdef XAPIAN_HAS_INMEMORY_BACKEND
98 LOGCALL_STATIC(API
, WritableDatabase
, "InMemory::open", NO_ARGS
);
99 RETURN(WritableDatabase(new InMemoryDatabase
));
104 open_stub(Database
&db
, const string
&file
)
106 // A stub database is a text file with one or more lines of this format:
107 // <dbtype> <serialised db object>
109 // Lines which start with a "#" character are ignored.
111 // Any paths specified in stub database files which are relative will be
112 // considered to be relative to the directory containing the stub database.
113 ifstream
stub(file
.c_str());
115 string msg
= "Couldn't open stub database file: ";
117 throw Xapian::DatabaseOpeningError(msg
, errno
);
120 unsigned int line_no
= 0;
121 while (getline(stub
, line
)) {
123 if (line
.empty() || line
[0] == '#')
125 string::size_type space
= line
.find(' ');
126 if (space
== string::npos
) space
= line
.size();
128 string
type(line
, 0, space
);
129 line
.erase(0, space
+ 1);
131 if (type
== "auto") {
132 resolve_relative_path(line
, file
);
133 db
.add_database(Database(line
));
137 #ifdef XAPIAN_HAS_CHERT_BACKEND
138 if (type
== "chert") {
139 resolve_relative_path(line
, file
);
140 db
.add_database(Database(new ChertDatabase(line
)));
145 #ifdef XAPIAN_HAS_GLASS_BACKEND
146 if (type
== "glass") {
147 resolve_relative_path(line
, file
);
148 db
.add_database(Database(new GlassDatabase(line
)));
153 #ifdef XAPIAN_HAS_REMOTE_BACKEND
154 if (type
== "remote") {
155 string::size_type colon
= line
.find(':');
159 // Is it a security risk?
160 space
= line
.find(' ');
162 if (space
!= string::npos
) {
163 args
.assign(line
, space
+ 1, string::npos
);
164 line
.assign(line
, 1, space
- 1);
168 db
.add_database(Remote::open(line
, args
));
169 } else if (colon
!= string::npos
) {
172 unsigned int port
= atoi(line
.c_str() + colon
+ 1);
174 db
.add_database(Remote::open(line
, port
));
180 #ifdef XAPIAN_HAS_INMEMORY_BACKEND
181 if (type
== "inmemory" && line
.empty()) {
182 db
.add_database(InMemory::open());
187 if (type
== "flint") {
188 throw FeatureUnavailableError("Flint backend no longer supported");
191 // Don't include the line itself - that might help an attacker
192 // by revealing part of a sensitive file's contents if they can
193 // arrange for it to be read as a stub database via infelicities in
194 // an application which uses Xapian. The line number is enough
195 // information to identify the problem line.
196 throw DatabaseOpeningError(file
+ ':' + str(line_no
) + ": Bad line");
199 // Allowing a stub database with no databases listed allows things like
200 // a "search all databases" feature to be implemented by generating a
201 // stub database file without having to special case there not being any
204 // 1.0.x throws DatabaseOpeningError here, but with a "Bad line" message
205 // with the line number just past the end of the file, which is a bit odd.
209 open_stub(WritableDatabase
&db
, const string
&file
, int flags
)
211 // A stub database is a text file with one or more lines of this format:
212 // <dbtype> <serialised db object>
214 // Lines which start with a "#" character, and lines which have no spaces
215 // in them, are ignored.
217 // Any paths specified in stub database files which are relative will be
218 // considered to be relative to the directory containing the stub database.
219 ifstream
stub(file
.c_str());
221 string msg
= "Couldn't open stub database file: ";
223 throw Xapian::DatabaseOpeningError(msg
, errno
);
226 unsigned int line_no
= 0;
228 if (!getline(stub
, line
)) break;
231 if (line
.empty() || line
[0] == '#')
233 string::size_type space
= line
.find(' ');
234 if (space
== string::npos
) space
= line
.size();
236 string
type(line
, 0, space
);
237 line
.erase(0, space
+ 1);
239 if (type
== "auto") {
240 resolve_relative_path(line
, file
);
241 db
.add_database(WritableDatabase(line
, flags
));
245 #ifdef XAPIAN_HAS_CHERT_BACKEND
246 if (type
== "chert") {
247 resolve_relative_path(line
, file
);
248 db
.add_database(WritableDatabase(line
, flags
|DB_BACKEND_CHERT
));
253 #ifdef XAPIAN_HAS_GLASS_BACKEND
254 if (type
== "glass") {
255 resolve_relative_path(line
, file
);
256 db
.add_database(WritableDatabase(line
, flags
|DB_BACKEND_GLASS
));
261 #ifdef XAPIAN_HAS_REMOTE_BACKEND
262 if (type
== "remote") {
263 string::size_type colon
= line
.find(':');
267 // Is it a security risk?
268 space
= line
.find(' ');
270 if (space
!= string::npos
) {
271 args
.assign(line
, space
+ 1, string::npos
);
272 line
.assign(line
, 1, space
- 1);
276 db
.add_database(Remote::open_writable(line
, args
, 0, flags
));
277 } else if (colon
!= string::npos
) {
280 unsigned int port
= atoi(line
.c_str() + colon
+ 1);
282 db
.add_database(Remote::open_writable(line
, port
, 0, 10000, flags
));
288 #ifdef XAPIAN_HAS_INMEMORY_BACKEND
289 if (type
== "inmemory" && line
.empty()) {
290 db
.add_database(InMemory::open());
295 if (type
== "flint") {
296 throw FeatureUnavailableError("Flint backend no longer supported");
299 // Don't include the line itself - that might help an attacker
300 // by revealing part of a sensitive file's contents if they can
301 // arrange for it to be read as a stub database via infelicities in
302 // an application which uses Xapian. The line number is enough
303 // information to identify the problem line.
304 throw DatabaseOpeningError(file
+ ':' + str(line_no
) + ": Bad line");
307 if (db
.internal
.empty()) {
308 throw DatabaseOpeningError(file
+ ": No databases listed");
312 Database::Database(const string
&path
, int flags
)
314 LOGCALL_CTOR(API
, "Database", path
|flags
);
316 int type
= flags
& DB_BACKEND_MASK_
;
318 case DB_BACKEND_CHERT
:
319 #ifdef XAPIAN_HAS_CHERT_BACKEND
320 internal
.push_back(new ChertDatabase(path
));
323 throw FeatureUnavailableError("Chert backend disabled");
325 case DB_BACKEND_GLASS
:
326 #ifdef XAPIAN_HAS_GLASS_BACKEND
327 internal
.push_back(new GlassDatabase(path
));
330 throw FeatureUnavailableError("Glass backend disabled");
332 case DB_BACKEND_STUB
:
333 open_stub(*this, path
);
338 if (stat(path
.c_str(), &statbuf
) == -1) {
339 throw DatabaseOpeningError("Couldn't stat '" + path
+ "'", errno
);
342 if (S_ISREG(statbuf
.st_mode
)) {
343 // Could be a stub database file, or a single file glass database.
345 if (check_if_single_file_db(statbuf
, path
, &fd
)) {
346 // Single file glass format.
347 internal
.push_back(new GlassDatabase(fd
));
351 open_stub(*this, path
);
355 if (rare(!S_ISDIR(statbuf
.st_mode
))) {
356 throw DatabaseOpeningError("Not a regular file or directory: '" + path
+ "'");
359 #ifdef XAPIAN_HAS_CHERT_BACKEND
360 if (file_exists(path
+ "/iamchert")) {
361 internal
.push_back(new ChertDatabase(path
));
366 #ifdef XAPIAN_HAS_GLASS_BACKEND
367 if (file_exists(path
+ "/iamglass")) {
368 internal
.push_back(new GlassDatabase(path
));
373 // Check for "stub directories".
374 string stub_file
= path
;
375 stub_file
+= "/XAPIANDB";
376 if (usual(file_exists(stub_file
))) {
377 open_stub(*this, stub_file
);
381 #ifndef XAPIAN_HAS_CHERT_BACKEND
382 if (file_exists(path
+ "/iamchert")) {
383 throw FeatureUnavailableError("Chert backend disabled");
386 #ifndef XAPIAN_HAS_GLASS_BACKEND
387 if (file_exists(path
+ "/iamglass")) {
388 throw FeatureUnavailableError("Glass backend disabled");
391 if (file_exists(path
+ "/iamflint")) {
392 throw FeatureUnavailableError("Flint backend no longer supported");
395 throw DatabaseOpeningError("Couldn't detect type of database");
398 Database::Database(int fd
, int flags
)
400 LOGCALL_CTOR(API
, "Database", fd
|flags
);
402 throw InvalidArgumentError("fd < 0");
404 #ifdef XAPIAN_HAS_GLASS_BACKEND
405 int type
= flags
& DB_BACKEND_MASK_
;
407 case 0: case DB_BACKEND_GLASS
:
408 internal
.push_back(new GlassDatabase(fd
));
416 throw DatabaseOpeningError("Couldn't detect type of database");
419 #if defined XAPIAN_HAS_CHERT_BACKEND || \
420 defined XAPIAN_HAS_GLASS_BACKEND
421 #define HAVE_DISK_BACKEND
424 WritableDatabase::WritableDatabase(const std::string
&path
, int flags
, int block_size
)
427 LOGCALL_CTOR(API
, "WritableDatabase", path
|flags
|block_size
);
428 // Avoid warning if both chert and glass are disabled.
430 int type
= flags
& DB_BACKEND_MASK_
;
431 // Clear the backend bits, so we just pass on other flags to open_stub, etc.
432 flags
&= ~DB_BACKEND_MASK_
;
435 if (stat(path
.c_str(), &statbuf
) == -1) {
436 // ENOENT probably just means that we need to create the directory.
438 throw DatabaseOpeningError("Couldn't stat '" + path
+ "'", errno
);
440 // File or directory already exists.
442 if (S_ISREG(statbuf
.st_mode
)) {
443 // The path is a file, so assume it is a stub database file.
444 open_stub(*this, path
, flags
);
448 if (rare(!S_ISDIR(statbuf
.st_mode
))) {
449 throw DatabaseOpeningError("Not a regular file or directory: '" + path
+ "'");
452 if (file_exists(path
+ "/iamchert")) {
453 // Existing chert DB.
454 #ifdef XAPIAN_HAS_CHERT_BACKEND
455 type
= DB_BACKEND_CHERT
;
457 throw FeatureUnavailableError("Chert backend disabled");
459 } else if (file_exists(path
+ "/iamglass")) {
460 // Existing glass DB.
461 #ifdef XAPIAN_HAS_GLASS_BACKEND
462 type
= DB_BACKEND_GLASS
;
464 throw FeatureUnavailableError("Glass backend disabled");
466 } else if (file_exists(path
+ "/iamflint")) {
467 // Existing flint DB.
468 throw FeatureUnavailableError("Flint backend no longer supported");
470 // Check for "stub directories".
471 string stub_file
= path
;
472 stub_file
+= "/XAPIANDB";
473 if (usual(file_exists(stub_file
))) {
474 open_stub(*this, stub_file
, flags
);
482 case DB_BACKEND_STUB
:
483 open_stub(*this, path
, flags
);
486 // Fall through to first enabled case, so order the remaining cases
488 #ifdef XAPIAN_HAS_GLASS_BACKEND
489 case DB_BACKEND_GLASS
:
490 internal
.push_back(new GlassWritableDatabase(path
, flags
, block_size
));
493 #ifdef XAPIAN_HAS_CHERT_BACKEND
494 case DB_BACKEND_CHERT
:
495 internal
.push_back(new ChertWritableDatabase(path
, flags
, block_size
));
499 #ifndef HAVE_DISK_BACKEND
500 throw FeatureUnavailableError("No disk-based writable backend is enabled");