xapian-core  1.4.26
databasehelpers.cc
Go to the documentation of this file.
1 
4 /* Copyright 2002-2019 Olly Betts
5  * Copyright 2008 Lemur Consulting Ltd
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 "databasehelpers.h"
26 
27 #include "backends.h"
28 
29 #include "glass/glass_defs.h"
30 
31 #include "io_utils.h"
32 #include "omassert.h"
33 #include "posixy_wrapper.h"
34 
35 #include <algorithm>
36 #include <cstring>
37 #include <string>
38 #include <sys/types.h>
39 #include "safesysstat.h"
40 #include "safeunistd.h"
41 
42 using namespace std;
43 
44 int
45 test_if_single_file_db(const struct stat& sb,
46  const string& path,
47  int* fd_ptr)
48 {
49  Assert(fd_ptr);
50 #if defined XAPIAN_HAS_GLASS_BACKEND
51  if (!S_ISREG(sb.st_mode)) return BACKEND_UNKNOWN;
52  // Look at the size as a clue - if it's less than GLASS_MIN_BLOCKSIZE then
53  // it's not a single-file glass database. For a larger file, we peek at
54  // the start of the file to determine what it is.
55  if (sb.st_size < GLASS_MIN_BLOCKSIZE)
56  return BACKEND_UNKNOWN;
57  int fd = posixy_open(path.c_str(), O_RDONLY|O_BINARY);
58  if (fd != -1) {
59  char magic_buf[14];
60  // FIXME: Don't duplicate magic check here...
61  if (io_read(fd, magic_buf, 14) == 14 &&
62  lseek(fd, 0, SEEK_SET) == 0 &&
63  memcmp(magic_buf, "\x0f\x0dXapian Glass", 14) == 0) {
64  *fd_ptr = fd;
65  return BACKEND_GLASS;
66  }
67  ::close(fd);
68  }
69 #else
70  (void)sb;
71  (void)path;
72  (void)fd_ptr;
73 #endif
74  return BACKEND_UNKNOWN;
75 }
int close(FD &fd)
Definition: fd.h:63
#define Assert(COND)
Definition: omassert.h:122
#define GLASS_MIN_BLOCKSIZE
Minimum B-tree block size.
Definition: glass_defs.h:33
Provides wrappers with POSIXy semantics.
Helper functions for database handling.
#define O_BINARY
Definition: safefcntl.h:81
STL namespace.
Definitions, types, etc for use inside glass.
include <sys/stat.h> with portability enhancements
int test_if_single_file_db(const struct stat &sb, const string &path, int *fd_ptr)
Probe if a path is a single-file database.
BACKEND_* constants.
#define S_ISREG(ST_MODE)
Definition: safesysstat.h:60
size_t io_read(int fd, char *p, size_t n, size_t min)
Read n bytes (or until EOF) into block pointed to by p from file descriptor fd.
Definition: io_utils.cc:123
Wrappers for low-level POSIX I/O routines.
#define posixy_open
<unistd.h>, but with compat.
Various assertion macros.