File: chert_version.cc

package info (click to toggle)
xapian-core 1.4.3-2%2Bdeb9u3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 21,412 kB
  • sloc: cpp: 113,868; ansic: 8,723; sh: 4,433; perl: 836; makefile: 566; tcl: 317; python: 40
file content (143 lines) | stat: -rw-r--r-- 4,366 bytes parent folder | download | duplicates (2)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/** @file chert_version.cc
 * @brief ChertVersion class
 */
/* Copyright (C) 2006,2007,2008,2009,2013 Olly Betts
 * Copyright 2010 Richard Boulton
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <config.h>

#include "safeerrno.h"

#include <xapian/error.h>

#include "chert_version.h"
#include "io_utils.h"
#include "stringutils.h" // For STRINGIZE() and CONST_STRLEN().
#include "str.h"

#include <cstring> // For memcmp() and memcpy().
#include <string>

#include "common/safeuuid.h"

using namespace std;

// YYYYMMDDX where X allows multiple format revisions in a day
#define CHERT_VERSION 200912150
// 200804180       Chert debuts.
// 200903070 1.1.0 doclen bounds and wdf upper bound.
// 200912150 1.1.4 shorter position, postlist, record and termlist keys.

#define MAGIC_STRING "IAmChert"

#define MAGIC_LEN CONST_STRLEN(MAGIC_STRING)
// 4 for the version number; 16 for the UUID.
#define VERSIONFILE_SIZE (MAGIC_LEN + 4 + 16)

// Literal version of VERSIONFILE_SIZE, used for error message.  This needs
// to be updated by hand should VERSIONFILE_SIZE change, but that rarely
// happens so this isn't an onerous requirement.
#define VERSIONFILE_SIZE_LITERAL 28

void
ChertVersion::create()
{
    char buf[VERSIONFILE_SIZE] = MAGIC_STRING;
    unsigned char *v = reinterpret_cast<unsigned char *>(buf) + MAGIC_LEN;
    v[0] = static_cast<unsigned char>(CHERT_VERSION & 0xff);
    v[1] = static_cast<unsigned char>((CHERT_VERSION >> 8) & 0xff);
    v[2] = static_cast<unsigned char>((CHERT_VERSION >> 16) & 0xff);
    v[3] = static_cast<unsigned char>((CHERT_VERSION >> 24) & 0xff);

    uuid_generate(uuid);
    memcpy(buf + MAGIC_LEN + 4, uuid, 16);

    int fd = ::open(filename.c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_BINARY|O_CLOEXEC, 0666);

    if (fd < 0) {
	string msg("Failed to create chert version file: ");
	msg += filename;
	throw Xapian::DatabaseOpeningError(msg, errno);
    }

    try {
	io_write(fd, buf, VERSIONFILE_SIZE);
    } catch (...) {
	(void)close(fd);
	throw;
    }

    io_sync(fd);
    if (close(fd) != 0) {
	string msg("Failed to create chert version file: ");
	msg += filename;
	throw Xapian::DatabaseOpeningError(msg, errno);
    }
}

void
ChertVersion::read_and_check()
{
    int fd = ::open(filename.c_str(), O_RDONLY|O_BINARY|O_CLOEXEC);

    if (fd < 0) {
	string msg = filename;
	msg += ": Failed to open chert version file for reading";
	throw Xapian::DatabaseOpeningError(msg, errno);
    }

    // Try to read an extra byte so we know if the file is too long.
    char buf[VERSIONFILE_SIZE + 1];
    size_t size;
    try {
	size = io_read(fd, buf, VERSIONFILE_SIZE + 1);
    } catch (...) {
	(void)close(fd);
	throw;
    }
    (void)close(fd);

    if (size != VERSIONFILE_SIZE) {
	static_assert(VERSIONFILE_SIZE == VERSIONFILE_SIZE_LITERAL,
		      "VERSIONFILE_SIZE_LITERAL needs updating");
	string msg = filename;
	msg += ": Chert version file should be "
	       STRINGIZE(VERSIONFILE_SIZE_LITERAL)" bytes, actually ";
	msg += str(size);
	throw Xapian::DatabaseCorruptError(msg);
    }

    if (memcmp(buf, MAGIC_STRING, MAGIC_LEN) != 0) {
	string msg = filename;
	msg += ": Chert version file doesn't contain the right magic string";
	throw Xapian::DatabaseCorruptError(msg);
    }

    const unsigned char *v;
    v = reinterpret_cast<const unsigned char *>(buf) + MAGIC_LEN;
    unsigned int version = v[0] | (v[1] << 8) | (v[2] << 16) | (v[3] << 24);
    if (version != CHERT_VERSION) {
	string msg = filename;
	msg += ": Chert version file is version ";
	msg += str(version);
	msg += " but I only understand " STRINGIZE(CHERT_VERSION);
	throw Xapian::DatabaseVersionError(msg);
    }

    memcpy(uuid, buf + MAGIC_LEN + 4, 16);
}