2 * @brief Database factories for non-remote databases.
4 /* Copyright 2002,2003,2004,2005,2006,2007,2008,2009,2011,2012,2013,2014,2015,2016,2017 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.
34 #include "filetests.h"
35 #include "fileutils.h"
36 #include "posixy_wrapper.h"
39 #include "safeerrno.h"
40 #include <cstdlib> // For atoi().
42 #ifdef XAPIAN_HAS_GLASS_BACKEND
43 # include "glass/glass_database.h"
44 # include "glass/glass_defs.h"
46 #ifdef XAPIAN_HAS_HONEY_BACKEND
47 # include "honey/honey_database.h"
48 # include "honey/honey_defs.h"
50 #ifdef XAPIAN_HAS_INMEMORY_BACKEND
51 # include "inmemory/inmemory_database.h"
53 // Even if none of the above get included, we still need a definition of
54 // Database::Internal.
55 #include "backends/databaseinternal.h"
62 /** Return a BACKEND_* constant from backends.h.
64 * BACKEND_UNKNOWN : stub file
65 * BACKEND_GLASS : glass single file
66 * BACKEND_HONEY : honey single file
69 check_if_single_file_db(const struct stat
& sb
, const string
& path
,
72 #if defined XAPIAN_HAS_GLASS_BACKEND || \
73 defined XAPIAN_HAS_HONEY_BACKEND
74 if (!S_ISREG(sb
.st_mode
)) return BACKEND_UNKNOWN
;
75 // Look at the size as a clue - if it's 0 or not a multiple of
76 // GLASS_MIN_BLOCKSIZE, then it's not a single-file glass database, and
77 // not a honey single-file database either, as we pad those to a multiple
78 // of GLASS_MIN_BLOCKSIZE too. Otherwise peek at the start of the file to
79 // determine which it is.
80 if (sb
.st_size
== 0 || sb
.st_size
% GLASS_MIN_BLOCKSIZE
!= 0)
81 // Look at the size as a clue - if it's less than GLASS_MIN_BLOCKSIZE, then
82 // it's not a single-file glass database and too small to be a honey one
83 // If it is, peek at the start of the file to determine what it is.
84 if (sb
.st_size
< GLASS_MIN_BLOCKSIZE
)
86 int fd
= posixy_open(path
.c_str(), O_RDONLY
|O_BINARY
);
89 // FIXME: Don't duplicate magic check here...
90 if (io_read(fd
, magic_buf
, 14) == 14 &&
91 lseek(fd
, 0, SEEK_SET
) == 0 &&
92 memcmp(magic_buf
, "\x0f\x0dXapian ", 9) == 0) {
95 switch (magic_buf
[9]) {
97 if (memcmp(magic_buf
+ 10, "lass", 4) == 0) {
100 return BACKEND_GLASS
;
104 if (memcmp(magic_buf
+ 10, "oney", 4) == 0) {
107 return BACKEND_HONEY
;
113 return BACKEND_UNKNOWN
;
122 return BACKEND_UNKNOWN
;
128 open_stub(Database
&db
, const string
&file
)
130 // A stub database is a text file with one or more lines of this format:
131 // <dbtype> <serialised db object>
133 // Lines which start with a "#" character are ignored.
135 // Any paths specified in stub database files which are relative will be
136 // considered to be relative to the directory containing the stub database.
137 ifstream
stub(file
.c_str());
139 string msg
= "Couldn't open stub database file: ";
141 throw Xapian::DatabaseOpeningError(msg
, errno
);
144 unsigned int line_no
= 0;
145 while (getline(stub
, line
)) {
147 if (line
.empty() || line
[0] == '#')
149 string::size_type space
= line
.find(' ');
150 if (space
== string::npos
) space
= line
.size();
152 string
type(line
, 0, space
);
153 line
.erase(0, space
+ 1);
155 if (type
== "auto") {
156 resolve_relative_path(line
, file
);
157 db
.add_database(Database(line
));
161 if (type
== "glass") {
162 #ifdef XAPIAN_HAS_GLASS_BACKEND
163 resolve_relative_path(line
, file
);
164 db
.add_database(Database(new GlassDatabase(line
)));
167 throw FeatureUnavailableError("Glass backend disabled");
171 if (type
== "honey") {
172 #ifdef XAPIAN_HAS_HONEY_BACKEND
173 resolve_relative_path(line
, file
);
174 db
.add_database(Database(new HoneyDatabase(line
)));
177 throw FeatureUnavailableError("Honey backend disabled");
181 if (type
== "remote" && !line
.empty()) {
182 #ifdef XAPIAN_HAS_REMOTE_BACKEND
183 if (line
[0] == ':') {
186 // Is it a security risk?
187 space
= line
.find(' ');
189 if (space
!= string::npos
) {
190 args
.assign(line
, space
+ 1, string::npos
);
191 line
.assign(line
, 1, space
- 1);
195 db
.add_database(Remote::open(line
, args
));
198 string::size_type colon
= line
.rfind(':');
199 if (colon
!= string::npos
) {
202 // Avoid misparsing an IPv6 address without a port number. The
203 // port number is required, so just leave that case to the
204 // error handling further below.
205 if (!(line
[0] == '[' && line
.back() == ']')) {
206 unsigned int port
= atoi(line
.c_str() + colon
+ 1);
208 if (line
[0] == '[' && line
.back() == ']') {
209 line
.erase(line
.size() - 1, 1);
212 db
.add_database(Remote::open(line
, port
));
217 throw FeatureUnavailableError("Remote backend disabled");
221 if (type
== "inmemory" && line
.empty()) {
222 #ifdef XAPIAN_HAS_INMEMORY_BACKEND
223 db
.add_database(Database(string(), DB_BACKEND_INMEMORY
));
226 throw FeatureUnavailableError("Inmemory backend disabled");
230 if (type
== "chert") {
231 throw FeatureUnavailableError("Chert backend no longer supported");
234 if (type
== "flint") {
235 throw FeatureUnavailableError("Flint backend no longer supported");
238 // Don't include the line itself - that might help an attacker
239 // by revealing part of a sensitive file's contents if they can
240 // arrange for it to be read as a stub database via infelicities in
241 // an application which uses Xapian. The line number is enough
242 // information to identify the problem line.
243 throw DatabaseOpeningError(file
+ ':' + str(line_no
) + ": Bad line");
246 // Allowing a stub database with no databases listed allows things like
247 // a "search all databases" feature to be implemented by generating a
248 // stub database file without having to special case there not being any
251 // 1.0.x throws DatabaseOpeningError here, but with a "Bad line" message
252 // with the line number just past the end of the file, which is a bit odd.
256 open_stub(WritableDatabase
&db
, const string
&file
, int flags
)
258 // A stub database is a text file with one or more lines of this format:
259 // <dbtype> <serialised db object>
261 // Lines which start with a "#" character, and lines which have no spaces
262 // in them, are ignored.
264 // Any paths specified in stub database files which are relative will be
265 // considered to be relative to the directory containing the stub database.
266 ifstream
stub(file
.c_str());
268 string msg
= "Couldn't open stub database file: ";
270 throw Xapian::DatabaseOpeningError(msg
, errno
);
273 unsigned int line_no
= 0;
275 if (!getline(stub
, line
)) break;
278 if (line
.empty() || line
[0] == '#')
280 string::size_type space
= line
.find(' ');
281 if (space
== string::npos
) space
= line
.size();
283 string
type(line
, 0, space
);
284 line
.erase(0, space
+ 1);
286 if (type
== "auto") {
287 resolve_relative_path(line
, file
);
288 db
.add_database(WritableDatabase(line
, flags
));
292 if (type
== "glass") {
293 #ifdef XAPIAN_HAS_GLASS_BACKEND
294 resolve_relative_path(line
, file
);
295 db
.add_database(WritableDatabase(line
, flags
|DB_BACKEND_GLASS
));
298 throw FeatureUnavailableError("Glass backend disabled");
302 if (type
== "remote" && !line
.empty()) {
303 #ifdef XAPIAN_HAS_REMOTE_BACKEND
304 if (line
[0] == ':') {
307 // Is it a security risk?
308 space
= line
.find(' ');
310 if (space
!= string::npos
) {
311 args
.assign(line
, space
+ 1, string::npos
);
312 line
.assign(line
, 1, space
- 1);
316 db
.add_database(Remote::open_writable(line
, args
, 0, flags
));
319 string::size_type colon
= line
.rfind(':');
320 if (colon
!= string::npos
) {
323 // Avoid misparsing an IPv6 address without a port number. The
324 // port number is required, so just leave that case to the
325 // error handling further below.
326 if (!(line
[0] == '[' && line
.back() == ']')) {
327 unsigned int port
= atoi(line
.c_str() + colon
+ 1);
329 if (line
[0] == '[' && line
.back() == ']') {
330 line
.erase(line
.size() - 1, 1);
333 db
.add_database(Remote::open_writable(line
, port
, 0, 10000, flags
));
338 throw FeatureUnavailableError("Remote backend disabled");
342 if (type
== "inmemory" && line
.empty()) {
343 #ifdef XAPIAN_HAS_INMEMORY_BACKEND
344 db
.add_database(WritableDatabase(string(), DB_BACKEND_INMEMORY
));
347 throw FeatureUnavailableError("Inmemory backend disabled");
351 if (type
== "chert") {
352 throw FeatureUnavailableError("Chert backend no longer supported");
355 if (type
== "flint") {
356 throw FeatureUnavailableError("Flint backend no longer supported");
359 // Don't include the line itself - that might help an attacker
360 // by revealing part of a sensitive file's contents if they can
361 // arrange for it to be read as a stub database via infelicities in
362 // an application which uses Xapian. The line number is enough
363 // information to identify the problem line.
364 throw DatabaseOpeningError(file
+ ':' + str(line_no
) + ": Bad line");
367 if (db
.internal
->size() == 0) {
368 throw DatabaseOpeningError(file
+ ": No databases listed");
372 Database::Database(const string
& path
, int flags
)
375 LOGCALL_CTOR(API
, "Database", path
|flags
);
377 int type
= flags
& DB_BACKEND_MASK_
;
379 case DB_BACKEND_CHERT
:
380 throw FeatureUnavailableError("Chert backend no longer supported");
381 case DB_BACKEND_GLASS
:
382 #ifdef XAPIAN_HAS_GLASS_BACKEND
383 internal
= new GlassDatabase(path
);
386 throw FeatureUnavailableError("Glass backend disabled");
388 case DB_BACKEND_HONEY
:
389 #ifdef XAPIAN_HAS_HONEY_BACKEND
390 internal
= new HoneyDatabase(path
);
393 throw FeatureUnavailableError("Honey backend disabled");
395 case DB_BACKEND_STUB
:
396 open_stub(*this, path
);
398 case DB_BACKEND_INMEMORY
:
399 #ifdef XAPIAN_HAS_INMEMORY_BACKEND
400 internal
= new InMemoryDatabase();
403 throw FeatureUnavailableError("Inmemory backend disabled");
408 if (stat(path
.c_str(), &statbuf
) == -1) {
409 throw DatabaseOpeningError("Couldn't stat '" + path
+ "'", errno
);
412 if (S_ISREG(statbuf
.st_mode
)) {
413 // Could be a stub database file, or a single file glass database.
415 switch (check_if_single_file_db(statbuf
, path
, &fd
)) {
417 #ifdef XAPIAN_HAS_GLASS_BACKEND
418 // Single file glass format.
419 internal
= new GlassDatabase(fd
);
422 throw FeatureUnavailableError("Glass backend disabled");
425 #ifdef XAPIAN_HAS_HONEY_BACKEND
426 // Single file honey format.
427 internal
= new HoneyDatabase(fd
);
430 throw FeatureUnavailableError("Honey backend disabled");
434 open_stub(*this, path
);
438 if (rare(!S_ISDIR(statbuf
.st_mode
))) {
439 throw DatabaseOpeningError("Not a regular file or directory: '" + path
+ "'");
442 #ifdef XAPIAN_HAS_GLASS_BACKEND
443 if (file_exists(path
+ "/iamglass")) {
444 internal
= new GlassDatabase(path
);
449 #ifdef XAPIAN_HAS_HONEY_BACKEND
450 if (file_exists(path
+ "/iamhoney")) {
451 internal
= new HoneyDatabase(path
);
456 // Check for "stub directories".
457 string stub_file
= path
;
458 stub_file
+= "/XAPIANDB";
459 if (usual(file_exists(stub_file
))) {
460 open_stub(*this, stub_file
);
464 #ifndef XAPIAN_HAS_GLASS_BACKEND
465 if (file_exists(path
+ "/iamglass")) {
466 throw FeatureUnavailableError("Glass backend disabled");
469 #ifndef XAPIAN_HAS_HONEY_BACKEND
470 if (file_exists(path
+ "/iamhoney")) {
471 throw FeatureUnavailableError("Honey backend disabled");
474 if (file_exists(path
+ "/iamchert")) {
475 throw FeatureUnavailableError("Chert backend no longer supported");
477 if (file_exists(path
+ "/iamflint")) {
478 throw FeatureUnavailableError("Flint backend no longer supported");
481 throw DatabaseOpeningError("Couldn't detect type of database");
484 /** Helper factory function.
486 * This allows us to initialise Database::internal via the constructor's
487 * initialiser list, which we want to be able to do as Database::internal
488 * is an intrusive_ptr_nonnull, so we can't set it to NULL in the initialiser
489 * list and then fill it in later in the constructor body.
491 static Database::Internal
*
492 database_factory(int fd
, int flags
)
495 throw InvalidArgumentError("fd < 0");
497 #ifdef XAPIAN_HAS_GLASS_BACKEND
498 int type
= flags
& DB_BACKEND_MASK_
;
501 case DB_BACKEND_GLASS
:
502 return new GlassDatabase(fd
);
509 throw DatabaseOpeningError("Couldn't detect type of database");
512 Database::Database(int fd
, int flags
)
513 : internal(database_factory(fd
, flags
))
515 LOGCALL_CTOR(API
, "Database", fd
|flags
);
518 #if defined XAPIAN_HAS_GLASS_BACKEND
519 #define HAVE_DISK_BACKEND
522 WritableDatabase::WritableDatabase(const std::string
&path
, int flags
, int block_size
)
525 LOGCALL_CTOR(API
, "WritableDatabase", path
|flags
|block_size
);
526 // Avoid warning if all disk-based backends are disabled.
528 int type
= flags
& DB_BACKEND_MASK_
;
529 // Clear the backend bits, so we just pass on other flags to open_stub, etc.
530 flags
&= ~DB_BACKEND_MASK_
;
533 if (stat(path
.c_str(), &statbuf
) == -1) {
534 // ENOENT probably just means that we need to create the directory.
536 throw DatabaseOpeningError("Couldn't stat '" + path
+ "'", errno
);
538 // File or directory already exists.
540 if (S_ISREG(statbuf
.st_mode
)) {
541 // The path is a file, so assume it is a stub database file.
542 open_stub(*this, path
, flags
);
546 if (rare(!S_ISDIR(statbuf
.st_mode
))) {
547 throw DatabaseOpeningError("Not a regular file or directory: '" + path
+ "'");
550 if (file_exists(path
+ "/iamglass")) {
551 // Existing glass DB.
552 #ifdef XAPIAN_HAS_GLASS_BACKEND
553 type
= DB_BACKEND_GLASS
;
555 throw FeatureUnavailableError("Glass backend disabled");
557 } else if (file_exists(path
+ "/iamhoney")) {
558 // Existing honey DB.
559 throw InvalidOperationError("Honey backend doesn't support "
560 "updating existing databases");
561 } else if (file_exists(path
+ "/iamchert")) {
562 // Existing chert DB.
563 throw FeatureUnavailableError("Chert backend no longer supported");
564 } else if (file_exists(path
+ "/iamflint")) {
565 // Existing flint DB.
566 throw FeatureUnavailableError("Flint backend no longer supported");
568 // Check for "stub directories".
569 string stub_file
= path
;
570 stub_file
+= "/XAPIANDB";
571 if (usual(file_exists(stub_file
))) {
572 open_stub(*this, stub_file
, flags
);
580 case DB_BACKEND_STUB
:
581 open_stub(*this, path
, flags
);
584 // Fall through to first enabled case, so order the remaining cases
586 #ifdef XAPIAN_HAS_GLASS_BACKEND
587 case DB_BACKEND_GLASS
:
588 internal
= new GlassWritableDatabase(path
, flags
, block_size
);
591 case DB_BACKEND_HONEY
:
592 throw InvalidArgumentError("Honey backend doesn't support "
593 "updating existing databases");
594 case DB_BACKEND_CHERT
:
595 throw FeatureUnavailableError("Chert backend no longer supported");
596 case DB_BACKEND_INMEMORY
:
597 #ifdef XAPIAN_HAS_INMEMORY_BACKEND
598 internal
= new InMemoryDatabase();
601 throw FeatureUnavailableError("Inmemory backend disabled");
604 #ifndef HAVE_DISK_BACKEND
605 throw FeatureUnavailableError("No disk-based writable backend is enabled");