~drizzle-trunk/drizzle/development

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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* Copyright (C) 2009 PrimeBase Technologies GmbH, Germany
 *
 * PrimeBase Media Stream for MySQL
 *
 * 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
 *
 * Barry Leslie
 *
 * System table utility functions.
 *
 */
#include "cslib/CSConfig.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>
#include <inttypes.h>

#include "cslib/CSGlobal.h"
#include "cslib/CSStrUtil.h"
#include "cslib/CSPath.h"

#include "systab_util_ms.h"

//======================
void SysTabRec::logError(const char *text)
{
	char msg[80];
	enter_();
	
	snprintf(msg, 80, ", damaged at or near position %"PRIu32" \n", (uint32_t) (ptr - getBuffer(0)));
	CSL.log(self, CSLog::Warning, db_name);
	CSL.log(self, CSLog::Warning, ".");
	CSL.log(self, CSLog::Warning, table_name);
	CSL.log(self, CSLog::Warning, " table file ");
	CSL.log(self, CSLog::Warning, file_name);
	if (text) {
		CSL.log(self, CSLog::Warning, " ");
		CSL.log(self, CSLog::Warning, text);
	}
	CSL.log(self, CSLog::Warning, msg);
	exit_();
}

#define HEAD_MARKER ((uint32_t)0XABABABAB)
#define TAIL_MARKER ((uint32_t)0XCDCDCDCD)
//---------------------
bool SysTabRec::findRecord()
{
	uint32_t len, marker;
	CSDiskData d;

	badRecord = true;
	
	while (badRecord) {
		len = end_of_data - ptr;
		if (len < 12)
			return false;
		
		// Look for the record header.
		d.rec_chars = ptr;	ptr +=4;	
		marker = CS_GET_DISK_4(d.int_val->val_4);
		if (marker != HEAD_MARKER) 
			continue;
			
		// Get the record length.
		d.rec_chars = ptr;	
		recordLength = CS_GET_DISK_4(d.int_val->val_4);
		if (len < recordLength)
			continue;
		
		end_of_record = ptr + recordLength;
		
		// Look for the record trailer.
		d.rec_chars = end_of_record;	
		marker = CS_GET_DISK_4(d.int_val->val_4);
		if (marker != TAIL_MARKER) 
			continue;
			
		ptr +=4; // Skip the record length.
		badRecord = false;		
	}
	
	return true;
}

//---------------------
bool SysTabRec::firstRecord()
{
	ptr = getBuffer(0);
	end_of_data = ptr + length();
	
	if (!findRecord()) {
		logError("Missing record terminator, file being ignored");
		return false;
	}
	
	return true;
}

//---------------------
bool SysTabRec::nextRecord()
{
	if (!ptr)
		return firstRecord();
		
	if (ptr <= end_of_record) 
		ptr = end_of_record + 4;
	
	return findRecord();
}
	
//---------------------
void SysTabRec::resetRecord()
{
	ptr= 0;
}

//---------------------
uint8_t SysTabRec::getInt1Field()
{
	uint8_t val = 0;
	CSDiskData d;
	
	if (badRecord)
		return val;
		
	if (ptr > (end_of_record -1)) {
		logError("Missing 1 byte int field");
		ptr = end_of_record;
		badRecord = true;
		return val;
	}
	
	d.rec_chars = ptr;
	val = CS_GET_DISK_1(d.int_val->val_1);
	ptr += 1;		
	return val;
}

//---------------------
uint32_t SysTabRec::getInt4Field()
{
	uint32_t val = 0;
	CSDiskData d;
	
	if (badRecord)
		return val;
		
	if (ptr > (end_of_record -4)) {
		logError("Missing 4 byte int field");
		ptr = end_of_record;
		badRecord = true;
		return val;
	}
	
	d.rec_chars = ptr;
	val = CS_GET_DISK_4(d.int_val->val_4);
	ptr += 4;		
	return val;
}

//---------------------
const char *SysTabRec::getStringField()
{
	const char *val = "";
	
	if (badRecord)
		return val;
		
	if (ptr > (end_of_record -1)) {
		logError("Missing string field");
		badRecord = true;
		ptr = end_of_record;
	} else {
		val = ptr;
		while (*ptr && ptr < end_of_record) ptr++;
		if (ptr == end_of_record) {
			logError("Unterminated string field");
			badRecord = true;
			val = "";
		} else
			ptr++;
	}
	
	return val;
}

//---------------------
void SysTabRec::clear()
{
	setLength(0);
}

//---------------------
void SysTabRec::beginRecord()
{
	CSDiskData d;
	uint32_t len = length();
	
	setLength(len + 8); // Room for header marker and record length.

	d.rec_chars = getBuffer(len);
	CS_SET_DISK_4(d.int_val->val_4, HEAD_MARKER);	

	start_of_record = len + 4;
}

//---------------------
void SysTabRec::endRecord()
{
	CSDiskData d;
	uint32_t len = length();
	
	// Write the record length to the head of the record
	d.rec_chars = getBuffer(start_of_record);
	CS_SET_DISK_4(d.int_val->val_4, len - start_of_record);	

	// Write the record trailer
	setLength(len + 4); 
	d.rec_chars = getBuffer(len);
	CS_SET_DISK_4(d.int_val->val_4, TAIL_MARKER);		
}

//---------------------
void SysTabRec::setInt1Field(uint8_t val)
{
	CSDiskData d;

	uint32_t len = length();
	
	setLength(len +1); // Important: set the length before getting the buffer pointer
	d.rec_chars = getBuffer(len);
	CS_SET_DISK_1(d.int_val->val_1, val);	
}

//---------------------
void SysTabRec::setInt4Field(uint32_t val)
{
	CSDiskData d;

	uint32_t len = length();
	
	setLength(len +4); // Important: set the length before getting the buffer pointer
	d.rec_chars = getBuffer(len);
	CS_SET_DISK_4(d.int_val->val_4, val);	
}

//---------------------
void SysTabRec::setStringField(const char *val)
{
	if (!val) val = "";
	append(val, strlen(val) +1);
}

//---------------------
void SysTabRec::setStringField(const char *val, uint32_t len)
{
	if (val)
		append(val, len);
	append("", 1);
}


//======================
CSString *getPBMSPath(CSString *db_path)
{
	char pbms_path[PATH_MAX];
	enter_();
	
	push_(db_path);	
	cs_strcpy(PATH_MAX, pbms_path, db_path->getCString());
	release_(db_path);
	
	cs_remove_last_name_of_path(pbms_path);

	return_(CSString::newString(pbms_path));
}


//----------------------------
CSPath *getSysFile(CSString *db_path, const char *name_arg, size_t min_size)
{
	CSPath			*path;
	CSStringBuffer	*name;
	char			*ptr;

	enter_();
	
	push_(db_path);
	new_(name, CSStringBuffer());
	push_(name);
	name->append(name_arg);
	name->append(".dat");
	
	ptr = name->getBuffer(strlen(name_arg));

try_again:
	path = CSPath::newPath(RETAIN(db_path), name->getCString());
	push_(path);
	if (!path->exists()) {
		CSPath *tmp_path;

		strcpy(ptr, ".tmp");
		tmp_path = CSPath::newPath(RETAIN(db_path), name->getCString());
		push_(tmp_path);
		if (tmp_path->exists()) {
			strcpy(ptr, ".dat");
			tmp_path->rename(name->getCString());
		}
		release_(tmp_path);
	}
	
	// If the file if too small assume it is garbage.
	if (path->exists() && (path->getSize() < min_size)) {
		path->removeFile();
		release_(path);
		goto try_again;
	}
	
	pop_(path);
	release_(name);
	release_(db_path);
	return_(path);
}