File: glass_changes.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 (262 lines) | stat: -rw-r--r-- 7,607 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/** @file glass_changes.cc
 * @brief Glass changesets
 */
/* Copyright 2014,2016 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 "glass_changes.h"

#include "glass_replicate_internal.h"
#include "fd.h"
#include "io_utils.h"
#include "pack.h"
#include "posixy_wrapper.h"
#include "str.h"
#include "stringutils.h"
#include "wordaccess.h"
#include "xapian/constants.h"
#include "xapian/error.h"

#include <cstdlib>
#include <string>
#include "safeerrno.h"

using namespace std;

GlassChanges::~GlassChanges()
{
    if (changes_fd >= 0) {
	::close(changes_fd);
	string changes_tmp = changes_stem;
	changes_tmp += "tmp";
	io_unlink(changes_tmp);
    }
}

GlassChanges *
GlassChanges::start(glass_revision_number_t old_rev,
		    glass_revision_number_t rev,
		    int flags)
{
    if (rev == 0) {
	// Don't generate a changeset for the first revision.
	return NULL;
    }

    // Always check max_changesets for modification since last revision.
    const char *p = getenv("XAPIAN_MAX_CHANGESETS");
    if (p) {
	max_changesets = atoi(p);
    } else {
	max_changesets = 0;
    }

    if (max_changesets == 0)
	return NULL;

    string changes_tmp = changes_stem;
    changes_tmp += "tmp";
    changes_fd = posixy_open(changes_tmp.c_str(),
			     O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0666);
    if (changes_fd < 0) {
	string message = "Couldn't open changeset ";
	message += changes_tmp;
	message += " to write";
	throw Xapian::DatabaseError(message, errno);
    }

    // Write header for changeset file.
    string header = CHANGES_MAGIC_STRING;
    header += char(CHANGES_VERSION);
    pack_uint(header, old_rev);
    pack_uint(header, rev);

    if (flags & Xapian::DB_DANGEROUS) {
	header += '\x01'; // Changes can't be applied to a live database.
    } else {
	header += '\x00'; // Changes can be applied to a live database.
    }

    io_write(changes_fd, header.data(), header.size());
    // FIXME: save the block stream as a single zlib stream...

    // bool compressed = CHANGES_VERSION != 1; FIXME: always true for glass, but make optional?
    return this;
}

void
GlassChanges::write_block(const char * p, size_t len)
{
    io_write(changes_fd, p, len);
}

void
GlassChanges::commit(glass_revision_number_t new_rev, int flags)
{
    if (changes_fd < 0)
	return;

    io_write(changes_fd, "\xff", 1);

    string changes_tmp = changes_stem;
    changes_tmp += "tmp";

    if (!(flags & Xapian::DB_NO_SYNC) && !io_sync(changes_fd)) {
	int saved_errno = errno;
	(void)::close(changes_fd);
	changes_fd = -1;
	(void)unlink(changes_tmp.c_str());
	string m = changes_tmp;
	m += ": Failed to sync";
	throw Xapian::DatabaseError(m, saved_errno);
    }

    (void)::close(changes_fd);
    changes_fd = -1;

    string changes_file = changes_stem;
    changes_file += str(new_rev - 1); // FIXME: ?

    if (!io_tmp_rename(changes_tmp, changes_file)) {
	string m = changes_tmp;
	m += ": Failed to rename to ";
	m += changes_file;
	throw Xapian::DatabaseError(m, errno);
    }

    if (new_rev <= max_changesets) {
	// We can't yet have max_changesets old changesets.
	return;
    }

    // Only remove old changesets if we successfully wrote a new changeset.
    // Start at the oldest changeset we know about, and stop at max_changesets
    // before new_rev.  If max_changesets is unchanged from the previous
    // commit and nothing went wrong, exactly one changeset file should be
    // deleted.
    glass_revision_number_t stop_changeset = new_rev - max_changesets;
    while (oldest_changeset < stop_changeset) {
	changes_file.resize(changes_stem.size());
	changes_file += str(oldest_changeset);
	(void)io_unlink(changes_file);
	++oldest_changeset;
    }
}

void
GlassChanges::check(const string & changes_file)
{
    FD fd(posixy_open(changes_file.c_str(), O_RDONLY | O_CLOEXEC, 0666));
    if (fd < 0) {
	string message = "Couldn't open changeset ";
	message += changes_file;
	throw Xapian::DatabaseError(message, errno);
    }

    char buf[10240];

    size_t n = io_read(fd, buf, sizeof(buf), CONST_STRLEN(CHANGES_MAGIC_STRING) + 4);
    if (memcmp(buf, CHANGES_MAGIC_STRING,
	       CONST_STRLEN(CHANGES_MAGIC_STRING)) != 0) {
	throw Xapian::DatabaseError("Changes file has wrong magic");
    }

    const char * p = buf + CONST_STRLEN(CHANGES_MAGIC_STRING);
    if (*p++ != CHANGES_VERSION) {
	throw Xapian::DatabaseError("Changes file has unknown version");
    }
    const char * end = buf + n;

    glass_revision_number_t old_rev, rev;
    if (!unpack_uint(&p, end, &old_rev))
	throw Xapian::DatabaseError("Changes file has bad old_rev");
    if (!unpack_uint(&p, end, &rev))
	throw Xapian::DatabaseError("Changes file has bad rev");
    if (rev <= old_rev)
	throw Xapian::DatabaseError("Changes file has rev <= old_rev");
    if (p == end || (*p != 0 && *p != 1))
	throw Xapian::DatabaseError("Changes file has bad dangerous flag");
    ++p;

    while (true) {
	n -= (p - buf);
	memmove(buf, p, n);
	n += io_read(fd, buf + n, sizeof(buf) - n);

	if (n == 0)
	    throw Xapian::DatabaseError("Changes file truncated");

	p = buf;
	end = buf + n;

	unsigned char v = *p++;
	if (v == 0xff) {
	    if (p != end)
		throw Xapian::DatabaseError("Changes file - junk at end");
	    break;
	}
	if (v == 0xfe) {
	    // Version file.
	    glass_revision_number_t version_rev;
	    if (!unpack_uint(&p, end, &version_rev))
		throw Xapian::DatabaseError("Changes file - bad version file revision");
	    if (rev != version_rev)
		throw Xapian::DatabaseError("Version file revision != changes file new revision");
	    size_t len;
	    if (!unpack_uint(&p, end, &len))
		throw Xapian::DatabaseError("Changes file - bad version file length");
	    if (len <= size_t(end - p)) {
		p += len;
	    } else {
		if (lseek(fd, len - (end - p), SEEK_CUR) == off_t(-1))
		    throw Xapian::DatabaseError("Changes file - version file data truncated");
		p = end = buf;
		n = 0;
	    }
	    continue;
	}
	unsigned table = (v & 0x7);
	v >>= 3;
	if (table > 5)
	    throw Xapian::DatabaseError("Changes file - bad table code");
	// Changed block.
	if (v > 5)
	    throw Xapian::DatabaseError("Changes file - bad block size");
	unsigned block_size = 2048 << v;
	uint4 block_number;
	if (!unpack_uint(&p, end, &block_number))
	    throw Xapian::DatabaseError("Changes file - bad block number");
	// Although the revision number is aligned within the block, the block
	// data may not be aligned to a word boundary here.
	uint4 block_rev = unaligned_read4(reinterpret_cast<const byte*>(p));
	(void)block_rev; // FIXME: Sanity check value.
	p += 4;
	unsigned level = static_cast<unsigned char>(*p++);
	(void)level; // FIXME: Sanity check value.
	if (block_size <= unsigned(end - p)) {
	    p += block_size;
	} else {
	    if (lseek(fd, block_size - (end - p), SEEK_CUR) == off_t(-1))
		throw Xapian::DatabaseError("Changes file - block data truncated");
	    p = end = buf;
	    n = 0;
	}
    }
}