File: databasehelpers.cc

package info (click to toggle)
xapian-core 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 25,008 kB
  • sloc: cpp: 136,717; ansic: 11,798; sh: 5,416; perl: 1,024; javascript: 551; makefile: 460; tcl: 299; python: 40
file content (119 lines) | stat: -rw-r--r-- 2,999 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/** @file
 * @brief Helper functions for database handling
 */
/* Copyright 2002-2020 Olly Betts
 * Copyright 2008 Lemur Consulting Ltd
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see
 * <https://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "databasehelpers.h"

#include "backends.h"

#include "glass/glass_defs.h"
#include "honey/honey_defs.h"

#include "io_utils.h"
#include "omassert.h"
#include "posixy_wrapper.h"

#include <algorithm>
#include <cstring>
#include <string>
#include <sys/types.h>
#include "safesysstat.h"
#include "safeunistd.h"

using namespace std;

static int
test_if_single_file_db_(int fd, off_t pos)
{
#if defined XAPIAN_HAS_GLASS_BACKEND || \
    defined XAPIAN_HAS_HONEY_BACKEND
    char magic_buf[14];
    // FIXME: Don't duplicate magic check here...
    if (io_read(fd, magic_buf, 14) == 14 &&
	lseek(fd, pos, SEEK_SET) == pos &&
	memcmp(magic_buf, "\x0f\x0dXapian ", 9) == 0) {
	switch (magic_buf[9]) {
#ifdef XAPIAN_HAS_GLASS_BACKEND
	    case 'G':
		if (memcmp(magic_buf + 10, "lass", 4) == 0) {
		    return BACKEND_GLASS;
		}
		break;
#endif
#ifdef XAPIAN_HAS_HONEY_BACKEND
	    case 'H':
		if (memcmp(magic_buf + 10, "oney", 4) == 0) {
		    return BACKEND_HONEY;
		}
		break;
#endif
	}
	return BACKEND_UNKNOWN;
    }
#else
    (void)fd;
#endif
    return BACKEND_UNKNOWN;
}

int
test_if_single_file_db(int fd)
{
    off_t pos = lseek(fd, 0, SEEK_CUR);
    if (pos < 0) {
	return BACKEND_UNKNOWN;
    }
    return test_if_single_file_db_(fd, pos);
}

int
test_if_single_file_db(const struct stat& sb,
		       const string& path,
		       int* fd_ptr)
{
    Assert(fd_ptr);
#if defined XAPIAN_HAS_GLASS_BACKEND || \
    defined XAPIAN_HAS_HONEY_BACKEND
    if (!S_ISREG(sb.st_mode)) return BACKEND_UNKNOWN;
    // Look at the size as a clue - if it's less than both GLASS_MIN_BLOCKSIZE
    // and HONEY_MIN_DB_SIZE then it's not a single-file glass or honey
    // database.  For a larger file, we peek at the start of the file to
    // determine what it is.
    if (sb.st_size < min(GLASS_MIN_BLOCKSIZE, HONEY_MIN_DB_SIZE))
	return BACKEND_UNKNOWN;
    int fd = posixy_open(path.c_str(), O_RDONLY|O_BINARY);
    if (fd != -1) {
	int result = test_if_single_file_db_(fd, off_t{0});
	if (result != BACKEND_UNKNOWN) {
	    *fd_ptr = fd;
	} else {
	    ::close(fd);
	}
	return result;
    }
#else
    (void)sb;
    (void)path;
    (void)fd_ptr;
#endif
    return BACKEND_UNKNOWN;
}