1
/* Copyright (c) 2009 PrimeBase Technologies GmbH, Germany
3
* PrimeBase Media Stream for MySQL
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
* System table utility functions.
26
#include <sys/types.h>
33
#include "CSStrUtil.h"
36
#include "SysTab_util.h"
38
//======================
39
void SysTabRec::logError(const char *text)
44
snprintf(msg, 80, ", damaged at or near position %"PRIdPTR" \n", ptr - getBuffer(0));
45
CSL.log(self, CSLog::Warning, db_name);
46
CSL.log(self, CSLog::Warning, ".");
47
CSL.log(self, CSLog::Warning, table_name);
48
CSL.log(self, CSLog::Warning, " table file ");
49
CSL.log(self, CSLog::Warning, file_name);
51
CSL.log(self, CSLog::Warning, " ");
52
CSL.log(self, CSLog::Warning, text);
54
CSL.log(self, CSLog::Warning, msg);
58
#define HEAD_MARKER ((uint32_t)0XABABABAB)
59
#define TAIL_MARKER ((uint32_t)0XCDCDCDCD)
60
//---------------------
61
bool SysTabRec::findRecord()
69
len = end_of_data - ptr;
73
// Look for the record header.
74
d.rec_chars = ptr; ptr +=4;
75
marker = CS_GET_DISK_4(d.int_val->val_4);
76
if (marker != HEAD_MARKER)
79
// Get the record length.
81
recordLength = CS_GET_DISK_4(d.int_val->val_4);
82
if (len < recordLength)
85
end_of_record = ptr + recordLength;
87
// Look for the record trailer.
88
d.rec_chars = end_of_record;
89
marker = CS_GET_DISK_4(d.int_val->val_4);
90
if (marker != TAIL_MARKER)
93
ptr +=4; // Skip the record length.
100
//---------------------
101
bool SysTabRec::firstRecord()
104
end_of_data = ptr + length();
107
logError("Missing record terminator, file being ignored");
114
//---------------------
115
bool SysTabRec::nextRecord()
118
return firstRecord();
120
if (ptr <= end_of_record)
121
ptr = end_of_record + 4;
126
//---------------------
127
uint8_t SysTabRec::getInt1Field()
135
if (ptr > (end_of_record -1)) {
136
logError("Missing 1 byte int field");
143
val = CS_GET_DISK_1(d.int_val->val_1);
148
//---------------------
149
uint32_t SysTabRec::getInt4Field()
157
if (ptr > (end_of_record -4)) {
158
logError("Missing 4 byte int field");
165
val = CS_GET_DISK_4(d.int_val->val_4);
170
//---------------------
171
const char *SysTabRec::getStringField()
173
const char *val = "";
178
if (ptr > (end_of_record -1)) {
179
logError("Missing string field");
184
while (*ptr && ptr < end_of_record) ptr++;
185
if (ptr == end_of_record) {
186
logError("Unterminated string field");
196
//---------------------
197
void SysTabRec::clear()
202
//---------------------
203
void SysTabRec::beginRecord()
206
u_int len = length();
208
setLength(len + 8); // Room for header marker and record length.
210
d.rec_chars = getBuffer(len);
211
CS_SET_DISK_4(d.int_val->val_4, HEAD_MARKER);
213
start_of_record = len + 4;
216
//---------------------
217
void SysTabRec::endRecord()
220
u_int len = length();
222
// Write the record length to the head of the record
223
d.rec_chars = getBuffer(start_of_record);
224
CS_SET_DISK_4(d.int_val->val_4, len - start_of_record);
226
// Write the record trailer
228
d.rec_chars = getBuffer(len);
229
CS_SET_DISK_4(d.int_val->val_4, TAIL_MARKER);
232
//---------------------
233
void SysTabRec::setInt1Field(uint8_t val)
237
u_int len = length();
239
setLength(len +1); // Important: set the length before getting the buffer pointer
240
d.rec_chars = getBuffer(len);
241
CS_SET_DISK_1(d.int_val->val_1, val);
244
//---------------------
245
void SysTabRec::setInt4Field(uint32_t val)
249
u_int len = length();
251
setLength(len +4); // Important: set the length before getting the buffer pointer
252
d.rec_chars = getBuffer(len);
253
CS_SET_DISK_4(d.int_val->val_4, val);
256
//---------------------
257
void SysTabRec::setStringField(const char *val)
260
append(val, strlen(val) +1);
263
//---------------------
264
void SysTabRec::setStringField(const char *val, uint32_t len)
272
//======================
273
CSString *getPBMSPath(CSString *db_path)
275
char pbms_path[PATH_MAX];
279
cs_strcpy(PATH_MAX, pbms_path, db_path->getCString());
282
cs_remove_last_name_of_path(pbms_path);
284
return_(CSString::newString(pbms_path));
288
//----------------------------
289
CSPath *getSysFile(CSString *db_path, const char *name_arg, size_t min_size)
292
CSStringBuffer *name;
298
new_(name, CSStringBuffer());
300
name->append(name_arg);
301
name->append(".dat");
303
ptr = name->getBuffer(strlen(name_arg));
306
path = CSPath::newPath(RETAIN(db_path), name->getCString());
308
if (!path->exists()) {
312
tmp_path = CSPath::newPath(RETAIN(db_path), name->getCString());
314
if (tmp_path->exists()) {
316
tmp_path->rename(name->getCString());
321
// If the file if too small assume it is garbage.
322
if (path->exists() && (path->getSize() < min_size)) {