File: flint_version.cc

package info (click to toggle)
xapian-core 1.0.7-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 13,940 kB
  • ctags: 13,203
  • sloc: cpp: 69,340; sh: 9,355; ansic: 7,251; perl: 782; makefile: 471
file content (158 lines) | stat: -rw-r--r-- 4,964 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/** @file flint_version.cc
 * @brief FlintVersion class
 */
/* Copyright (C) 2006,2007 Olly Betts
 *
 * 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 "flint_io.h"
#include "flint_version.h"
#include "stringutils.h" // For STRINGIZE() and CONST_STRLEN().
#include "utils.h"

#ifdef __WIN32__
# include "msvc_posix_wrapper.h"
#endif

#include <string>

#include <string.h> // for memcmp

using std::string;

// YYYYMMDDX where X allows multiple format revisions in a day
#define FLINT_VERSION 200709120
// 200709120 1.0.3 Database::get_metadata(), WritableDatabase::set_metadata().
//                 Kill the unused "has_termfreqs" flag in the termlist table.
// 200706140 1.0.2 Optional value and position tables.
// 200704230 1.0.0 Use zlib compression of tags for record and termlist tables.
// 200611200  N/A  Fixed occasional, architecture-dependent surplus bits in
//		   interpolative coding; "flicklock" -> "flintlock".
// 200506110 0.9.2 Fixed interpolative coding to work(!)
// 200505310 0.9.1 Interpolative coding for position lists.
// 200505280  N/A  Total doclen and last docid entry moved to postlist table.
// 200505270  N/A  First dated version.

#define MAGIC_STRING "IAmFlint"

#define MAGIC_LEN CONST_STRLEN(MAGIC_STRING)
#define VERSIONFILE_SIZE (MAGIC_LEN + 4)

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

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

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

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

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

void FlintVersion::read_and_check(bool readonly)
{
    int fd = ::open(filename.c_str(), O_RDONLY|O_BINARY);

    if (fd < 0) {
	string msg("Failed to open flint version file for reading: ");
	msg += filename;
	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 = flint_io_read(fd, buf, VERSIONFILE_SIZE + 1, 0);
    } catch (...) {
	(void)close(fd);
	throw;
    }
    (void)close(fd);

    if (size != VERSIONFILE_SIZE) {
	string msg("Flint version file ");
	msg += filename;
	msg += " should be "STRINGIZE(VERSIONFILE_SIZE)" bytes, actually ";
	msg += om_tostring(size);
	throw Xapian::DatabaseCorruptError(msg);
    }

    if (memcmp(buf, MAGIC_STRING, MAGIC_LEN) != 0) {
	string msg("Flint version file doesn't contain the right magic string: ");
	msg += filename;
	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 >= 200704230 && version < 200709120) {
	if (readonly) return;
	// Upgrade the database to the current version since any changes we
	// make won't be compatible with older versions of Xapian.
	string filename_save = filename;
	filename += ".tmp";
	create();
	int result;
#ifdef __WIN32__
	result = msvc_posix_rename(filename.c_str(), filename_save.c_str());
#else
	result = rename(filename.c_str(), filename_save.c_str());
#endif
	filename = filename_save;
	if (result == -1) {
	    string msg("Failed to update flint version file: ");
	    msg += filename;
	    throw Xapian::DatabaseOpeningError(msg);
	}
	return;
    }
    if (version != FLINT_VERSION) {
	string msg("Flint version file ");
	msg += filename;
	msg += " is version ";
	msg += om_tostring(version);
	msg += " but I only understand "STRINGIZE(FLINT_VERSION);
	throw Xapian::DatabaseVersionError(msg);
    }
}