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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
|
/* Copyright (C) 2003 MySQL AB
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; version 2 of the License.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include <ndb_global.h>
#include <NdbTCP.h>
#include <NdbOut.hpp>
#include "BackupFormat.hpp"
#include <AttributeHeader.hpp>
#include <SimpleProperties.hpp>
#include <ndb_version.h>
bool readHeader(FILE*, BackupFormat::FileHeader *);
bool readFragHeader(FILE*, BackupFormat::DataFile::FragmentHeader *);
bool readFragFooter(FILE*, BackupFormat::DataFile::FragmentFooter *);
Int32 readRecord(FILE*, Uint32 **);
NdbOut & operator<<(NdbOut&, const BackupFormat::FileHeader &);
NdbOut & operator<<(NdbOut&, const BackupFormat::DataFile::FragmentHeader &);
NdbOut & operator<<(NdbOut&, const BackupFormat::DataFile::FragmentFooter &);
bool readTableList(FILE*, BackupFormat::CtlFile::TableList **);
bool readTableDesc(FILE*, BackupFormat::CtlFile::TableDescription **);
bool readGCPEntry(FILE*, BackupFormat::CtlFile::GCPEntry **);
NdbOut & operator<<(NdbOut&, const BackupFormat::CtlFile::TableList &);
NdbOut & operator<<(NdbOut&, const BackupFormat::CtlFile::TableDescription &);
NdbOut & operator<<(NdbOut&, const BackupFormat::CtlFile::GCPEntry &);
Int32 readLogEntry(FILE*, Uint32**);
static Uint32 recNo;
static Uint32 logEntryNo;
int
main(int argc, const char * argv[]){
ndb_init();
if(argc <= 1){
printf("Usage: %s <filename>\n", argv[0]);
exit(1);
}
FILE * f = fopen(argv[1], "rb");
if(!f){
ndbout << "No such file!" << endl;
exit(1);
}
BackupFormat::FileHeader fileHeader;
if(!readHeader(f, &fileHeader)){
ndbout << "Invalid file!" << endl;
exit(1);
}
ndbout << fileHeader << endl;
switch(fileHeader.FileType){
case BackupFormat::DATA_FILE:
while(!feof(f)){
BackupFormat::DataFile::FragmentHeader fragHeader;
if(!readFragHeader(f, &fragHeader))
break;
ndbout << fragHeader << endl;
Uint32 len, * data;
while((len = readRecord(f, &data)) > 0){
#if 0
ndbout << "-> " << hex;
for(Uint32 i = 0; i<len; i++){
ndbout << data[i] << " ";
}
ndbout << endl;
#endif
}
BackupFormat::DataFile::FragmentFooter fragFooter;
if(!readFragFooter(f, &fragFooter))
break;
ndbout << fragFooter << endl;
}
break;
case BackupFormat::CTL_FILE:{
BackupFormat::CtlFile::TableList * tabList;
if(!readTableList(f, &tabList)){
ndbout << "Invalid file! No table list" << endl;
break;
}
ndbout << (* tabList) << endl;
const Uint32 noOfTables = tabList->SectionLength - 2;
for(Uint32 i = 0; i<noOfTables; i++){
BackupFormat::CtlFile::TableDescription * tabDesc;
if(!readTableDesc(f, &tabDesc)){
ndbout << "Invalid file missing table description" << endl;
break;
}
ndbout << (* tabDesc) << endl;
}
BackupFormat::CtlFile::GCPEntry * gcpE;
if(!readGCPEntry(f, &gcpE)){
ndbout << "Invalid file! GCP ENtry" << endl;
break;
}
ndbout << (* gcpE) << endl;
break;
}
case BackupFormat::LOG_FILE:{
logEntryNo = 0;
typedef BackupFormat::LogFile::LogEntry LogEntry;
Uint32 len, * data;
while((len = readLogEntry(f, &data)) > 0){
LogEntry * logEntry = (LogEntry *) data;
/**
* Log Entry
*/
Uint32 event = ntohl(logEntry->TriggerEvent);
bool gcp = (event & 0x10000) != 0;
event &= 0xFFFF;
if(gcp)
len --;
ndbout << "LogEntry Table: " << (Uint32)ntohl(logEntry->TableId)
<< " Event: " << event
<< " Length: " << (len - 2);
const Uint32 dataLen = len - 2;
#if 0
Uint32 pos = 0;
while(pos < dataLen){
AttributeHeader * ah = (AttributeHeader*)&logEntry->Data[pos];
ndbout_c(" Attribut: %d Size: %d",
ah->getAttributeId(),
ah->getDataSize());
pos += ah->getDataSize() + 1;
}
#endif
if(gcp)
ndbout << " GCP: " << (Uint32)ntohl(logEntry->Data[dataLen]);
ndbout << endl;
}
break;
}
case BackupFormat::LCP_FILE:
{
BackupFormat::CtlFile::TableList * tabList;
if(!readTableList(f, &tabList)){
ndbout << "Invalid file! No table list" << endl;
break;
}
ndbout << (* tabList) << endl;
const Uint32 noOfTables = tabList->SectionLength - 2;
for(Uint32 i = 0; i<noOfTables; i++){
BackupFormat::CtlFile::TableDescription * tabDesc;
if(!readTableDesc(f, &tabDesc)){
ndbout << "Invalid file missing table description" << endl;
break;
}
ndbout << (* tabDesc) << endl;
}
while(!feof(f)){
BackupFormat::DataFile::FragmentHeader fragHeader;
if(!readFragHeader(f, &fragHeader))
break;
ndbout << fragHeader << endl;
Uint32 len, * data;
while((len = readRecord(f, &data)) > 0){
#if 0
ndbout << "-> " << hex;
for(Uint32 i = 0; i<len; i++){
ndbout << data[i] << " ";
}
ndbout << endl;
#endif
}
BackupFormat::DataFile::FragmentFooter fragFooter;
if(!readFragFooter(f, &fragFooter))
break;
ndbout << fragFooter << endl;
}
break;
}
default:
ndbout << "Unsupported file type for printer: "
<< fileHeader.FileType << endl;
break;
}
fclose(f);
return 0;
}
#define RETURN_FALSE() { ndbout_c("false: %d", __LINE__); abort(); return false; }
static bool endian = false;
bool
readHeader(FILE* f, BackupFormat::FileHeader * dst){
if(fread(dst, 4, 3, f) != 3)
RETURN_FALSE();
if(memcmp(dst->Magic, BACKUP_MAGIC, sizeof(BACKUP_MAGIC)) != 0)
RETURN_FALSE();
dst->NdbVersion = ntohl(dst->NdbVersion);
if(dst->NdbVersion != NDB_VERSION)
RETURN_FALSE();
if(fread(&dst->SectionType, 4, 2, f) != 2)
RETURN_FALSE();
dst->SectionType = ntohl(dst->SectionType);
dst->SectionLength = ntohl(dst->SectionLength);
if(dst->SectionType != BackupFormat::FILE_HEADER)
RETURN_FALSE();
if(dst->SectionLength != ((sizeof(BackupFormat::FileHeader) - 12) >> 2))
RETURN_FALSE();
if(fread(&dst->FileType, 4, dst->SectionLength - 2, f) !=
(dst->SectionLength - 2))
RETURN_FALSE();
dst->FileType = ntohl(dst->FileType);
dst->BackupId = ntohl(dst->BackupId);
dst->BackupKey_0 = ntohl(dst->BackupKey_0);
dst->BackupKey_1 = ntohl(dst->BackupKey_1);
if(dst->ByteOrder != 0x12345678)
endian = true;
return true;
}
bool
readFragHeader(FILE* f, BackupFormat::DataFile::FragmentHeader * dst){
if(fread(dst, 1, sizeof(* dst), f) != sizeof(* dst))
return false;
dst->SectionType = ntohl(dst->SectionType);
dst->SectionLength = ntohl(dst->SectionLength);
dst->TableId = ntohl(dst->TableId);
dst->FragmentNo = ntohl(dst->FragmentNo);
dst->ChecksumType = ntohl(dst->ChecksumType);
if(dst->SectionLength != (sizeof(* dst) >> 2))
RETURN_FALSE();
if(dst->SectionType != BackupFormat::FRAGMENT_HEADER)
RETURN_FALSE();
recNo = 0;
return true;
}
bool
readFragFooter(FILE* f, BackupFormat::DataFile::FragmentFooter * dst){
if(fread(dst, 1, sizeof(* dst), f) != sizeof(* dst))
RETURN_FALSE();
dst->SectionType = ntohl(dst->SectionType);
dst->SectionLength = ntohl(dst->SectionLength);
dst->TableId = ntohl(dst->TableId);
dst->FragmentNo = ntohl(dst->FragmentNo);
dst->NoOfRecords = ntohl(dst->NoOfRecords);
dst->Checksum = ntohl(dst->Checksum);
if(dst->SectionLength != (sizeof(* dst) >> 2))
RETURN_FALSE();
if(dst->SectionType != BackupFormat::FRAGMENT_FOOTER)
RETURN_FALSE();
return true;
}
static Uint32 buf[8192];
Int32
readRecord(FILE* f, Uint32 **dst){
Uint32 len;
if(fread(&len, 1, 4, f) != 4)
RETURN_FALSE();
len = ntohl(len);
if(fread(buf, 4, len, f) != len)
{
return -1;
}
if(len > 0)
recNo++;
else
ndbout_c("Found %d records", recNo);
* dst = &buf[0];
return len;
}
Int32
readLogEntry(FILE* f, Uint32 **dst){
Uint32 len;
if(fread(&len, 1, 4, f) != 4)
RETURN_FALSE();
len = ntohl(len);
if(fread(&buf[1], 4, len, f) != len)
return -1;
buf[0] = len;
if(len > 0)
logEntryNo++;
* dst = &buf[0];
return len;
}
NdbOut &
operator<<(NdbOut& ndbout, const BackupFormat::FileHeader & hf){
char buf[9];
memcpy(buf, hf.Magic, sizeof(hf.Magic));
buf[8] = 0;
ndbout << "-- FileHeader:" << endl;
ndbout << "Magic: " << buf << endl;
ndbout << "NdbVersion: " << hf.NdbVersion << endl;
ndbout << "SectionType: " << hf.SectionType << endl;
ndbout << "SectionLength: " << hf.SectionLength << endl;
ndbout << "FileType: " << hf.FileType << endl;
ndbout << "BackupId: " << hf.BackupId << endl;
ndbout << "BackupKey: [ " << hex << hf.BackupKey_0
<< " "<< hf.BackupKey_1 << " ]" << endl;
ndbout << "ByteOrder: " << hex << hf.ByteOrder << endl;
return ndbout;
}
NdbOut & operator<<(NdbOut& ndbout,
const BackupFormat::DataFile::FragmentHeader & hf){
ndbout << "-- Fragment header:" << endl;
ndbout << "SectionType: " << hf.SectionType << endl;
ndbout << "SectionLength: " << hf.SectionLength << endl;
ndbout << "TableId: " << hf.TableId << endl;
ndbout << "FragmentNo: " << hf.FragmentNo << endl;
ndbout << "ChecksumType: " << hf.ChecksumType << endl;
return ndbout;
}
NdbOut & operator<<(NdbOut& ndbout,
const BackupFormat::DataFile::FragmentFooter & hf){
ndbout << "-- Fragment footer:" << endl;
ndbout << "SectionType: " << hf.SectionType << endl;
ndbout << "SectionLength: " << hf.SectionLength << endl;
ndbout << "TableId: " << hf.TableId << endl;
ndbout << "FragmentNo: " << hf.FragmentNo << endl;
ndbout << "NoOfRecords: " << hf.NoOfRecords << endl;
ndbout << "Checksum: " << hf.Checksum << endl;
return ndbout;
}
bool
readTableList(FILE* f, BackupFormat::CtlFile::TableList **ret){
BackupFormat::CtlFile::TableList * dst =
(BackupFormat::CtlFile::TableList *)&buf[0];
if(fread(dst, 4, 2, f) != 2)
RETURN_FALSE();
dst->SectionType = ntohl(dst->SectionType);
dst->SectionLength = ntohl(dst->SectionLength);
if(dst->SectionType != BackupFormat::TABLE_LIST)
RETURN_FALSE();
const Uint32 len = dst->SectionLength - 2;
if(fread(&dst->TableIds[0], 4, len, f) != len)
RETURN_FALSE();
for(Uint32 i = 0; i<len; i++){
dst->TableIds[i] = ntohl(dst->TableIds[i]);
}
* ret = dst;
return true;
}
bool
readTableDesc(FILE* f, BackupFormat::CtlFile::TableDescription **ret){
BackupFormat::CtlFile::TableDescription * dst =
(BackupFormat::CtlFile::TableDescription *)&buf[0];
if(fread(dst, 4, 2, f) != 2)
RETURN_FALSE();
dst->SectionType = ntohl(dst->SectionType);
dst->SectionLength = ntohl(dst->SectionLength);
if(dst->SectionType != BackupFormat::TABLE_DESCRIPTION)
RETURN_FALSE();
const Uint32 len = dst->SectionLength - 2;
if(fread(&dst->DictTabInfo[0], 4, len, f) != len)
RETURN_FALSE();
* ret = dst;
return true;
}
bool
readGCPEntry(FILE* f, BackupFormat::CtlFile::GCPEntry **ret){
BackupFormat::CtlFile::GCPEntry * dst =
(BackupFormat::CtlFile::GCPEntry *)&buf[0];
if(fread(dst, 4, 4, f) != 4)
RETURN_FALSE();
dst->SectionType = ntohl(dst->SectionType);
dst->SectionLength = ntohl(dst->SectionLength);
if(dst->SectionType != BackupFormat::GCP_ENTRY)
RETURN_FALSE();
dst->StartGCP = ntohl(dst->StartGCP);
dst->StopGCP = ntohl(dst->StopGCP);
* ret = dst;
return true;
}
NdbOut &
operator<<(NdbOut& ndbout, const BackupFormat::CtlFile::TableList & hf) {
ndbout << "-- Table List:" << endl;
ndbout << "SectionType: " << hf.SectionType << endl;
ndbout << "SectionLength: " << hf.SectionLength << endl;
for(Uint32 i = 0; i < hf.SectionLength - 2; i++){
ndbout << hf.TableIds[i] << " ";
if((i + 1) % 16 == 0)
ndbout << endl;
}
return ndbout;
}
NdbOut &
operator<<(NdbOut& ndbout, const BackupFormat::CtlFile::TableDescription & hf){
ndbout << "-- Table Description:" << endl;
ndbout << "SectionType: " << hf.SectionType << endl;
ndbout << "SectionLength: " << hf.SectionLength << endl;
SimplePropertiesLinearReader it(&hf.DictTabInfo[0], hf.SectionLength - 2);
char buf[1024];
for(it.first(); it.valid(); it.next()){
switch(it.getValueType()){
case SimpleProperties::Uint32Value:
ndbout << "Key: " << it.getKey()
<< " value(" << it.getValueLen() << ") : "
<< it.getUint32() << endl;
break;
case SimpleProperties::StringValue:
if(it.getValueLen() < sizeof(buf)){
it.getString(buf);
ndbout << "Key: " << it.getKey()
<< " value(" << it.getValueLen() << ") : "
<< "\"" << buf << "\"" << endl;
} else {
ndbout << "Key: " << it.getKey()
<< " value(" << it.getValueLen() << ") : "
<< "\"" << "<TOO LONG>" << "\"" << endl;
}
break;
default:
ndbout << "Unknown type for key: " << it.getKey()
<< " type: " << it.getValueType() << endl;
}
}
return ndbout;
}
NdbOut &
operator<<(NdbOut& ndbout, const BackupFormat::CtlFile::GCPEntry & hf) {
ndbout << "-- GCP Entry:" << endl;
ndbout << "SectionType: " << hf.SectionType << endl;
ndbout << "SectionLength: " << hf.SectionLength << endl;
ndbout << "Start GCP: " << hf.StartGCP << endl;
ndbout << "Stop GCP: " << hf.StopGCP << endl;
return ndbout;
}
|