-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprint_journal.cpp
261 lines (220 loc) · 6.31 KB
/
print_journal.cpp
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
#include <stdio.h>
#include <string.h>
#include <string>
#include "mmap.h"
#include "util.h"
#include "journal_format.h"
#include "checksum.h"
#include "snappy/snappy.h"
struct JEntryInfo
{
int opCode;
const char* p;
};
class JSectionIterator
{
public:
JSectionIterator(const char* buf, int len) : buf_(buf), len_(len), offset_(0){}
bool atEof() const
{
return offset_ >= len_;
}
bool next(JEntryInfo& info)
{
if (atEof())
return false;
JEntry* entry = (JEntry*)(buf_ + offset_);
switch (entry->opcode)
{
case JEntry::OpCode_DbContext:
info.opCode = JEntry::OpCode_DbContext;
info.p = buf_ + offset_;
offset_ += sizeof(entry->opcode);
offset_ += strlen(buf_ + offset_) + 1;
break;
case JEntry::OpCode_FileCreated:
{
info.opCode = JEntry::OpCode_FileCreated;
info.p = buf_ + offset_;
FileCreatedOp* fileCreatedOp = (FileCreatedOp*)(buf_ + offset_);
offset_ += sizeof(FileCreatedOp) + strlen(fileCreatedOp->relativePath);
break;
}
case JEntry::OpCode_DropDb:
case JEntry::OpCode_Footer:
assert(false);
break;
default:
info.opCode = 0;
info.p = buf_ + offset_;
offset_ += sizeof(JEntry) + entry->len;
break;
}
return true;
}
private:
const char* buf_;
int len_;
int offset_;
};
class JSection
{
public:
JSection(const char* buf) : buf_(buf), valid_(false)
{
Checksum checksum;
checksum.gen(buf_, Header()->_sectionLen - sizeof(JSectFooter));
if (memcmp(checksum.bytes, Footer()->hash, sizeof(checksum.bytes)) != 0)
return;
if (!Footer()->magicOk())
return;
if (!snappy::Uncompress(buf_ + sizeof(JSectHeader), Header()->_sectionLen - sizeof(JSectFooter) - sizeof(JSectHeader), &body_))
{
printf("snappy::Uncompress fail.\n");
body_.clear();
return;
}
valid_ = true;
}
const bool Valid() const
{
return valid_;
}
const JSectHeader* Header() const
{
return (JSectHeader*)buf_;
}
const JSectFooter* Footer() const
{
return (JSectFooter*)(buf_ + Header()->_sectionLen - sizeof(JSectFooter));
}
const std::string& Body() const
{
return body_;
}
unsigned lenWithPadding() const
{
return Header()->sectionLenWithPadding();
}
JSectionIterator iterator() const
{
return JSectionIterator(body_.data(), body_.size());
}
private:
const char* buf_;
bool valid_;
std::string body_;
};
#ifdef WIN32
#define FORMAT_64U "%I64u"
#else
#define FORMAT_64U "%llu"
#endif
static void printHeader(FILE* fp, const JHeader* header)
{
fprintf(fp, "%c%c", header->magic[0], header->magic[1]);
fprintf(fp, "%x", header->_version);
fprintf(fp, "%c", header->n1);
fprintf(fp, "%s", header->ts);
fprintf(fp, "%c", header->n2);
fprintf(fp, "%s", header->dbpath);
fprintf(fp, "%c%c", header->n3, header->n4);
fprintf(fp, FORMAT_64U, header->fileId);
fprintf(fp, "%c%c", header->txt2[0], header->txt2[1]);
}
static void printSectionHeader(FILE* fp, const JSectHeader* sectHeader)
{
fprintf(fp, "--------Section--------\n");
fprintf(fp, "sectionLen = %d\n", sectHeader->_sectionLen);
fprintf(fp, "seqNumber = "FORMAT_64U"\n", sectHeader->seqNumber);
fprintf(fp, "fileId = "FORMAT_64U"\n", sectHeader->fileId);
}
//static void printSectionFooter(FILE* fp, const JSectFooter* sectFooter)
//{
// fprintf(fp, "sentinel = %x\n", sectFooter->sentinel);
//}
static void printSectionEntry(FILE* fp, JEntryInfo info)
{
switch (info.opCode)
{
case JEntry::OpCode_DbContext:
fprintf(fp, "OpCode_DbContext\n");
fprintf(fp, "%s\n", info.p + sizeof(JDbContext));
break;
case JEntry::OpCode_FileCreated:
{
fprintf(fp, "OpCode_FileCreated\n");
FileCreatedOp* fileCreatedOp = (FileCreatedOp*)info.p;
fprintf(fp, "len = "FORMAT_64U"\n", fileCreatedOp->len);
fprintf(fp, "relative path = %s\n", fileCreatedOp->relativePath);
break;
}
case JEntry::OpCode_DropDb:
case JEntry::OpCode_Footer:
assert(false);
break;
default:
{
fprintf(fp, "Op_Normal\n");
JEntry* entry = (JEntry*)info.p;
if (entry->isLocalDbContext())
fprintf(fp, "local db\n");
else if (entry->isNsSuffix())
fprintf(fp, ".ns file\n");
else
fprintf(fp, "file no = %d\n", entry->_fileNo);
fprintf(fp, "len = %d\n", entry->len);
fprintf(fp, "ofs = %d\n", entry->ofs);
break;
}
}
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
printf("Usage: ./print_journal journal_file\n");
return -1;
}
unsigned long long fileSize = getFileSize(argv[1]);
if (fileSize == 0)
{
printf("open journal file %s fail\n", argv[1]);
return -1;
}
MemoryMappedFile mmfile(argv[1]);
char* view = (char*)mmfile.map();
if (view == NULL)
{
printf("open journal file %s fail\n", argv[1]);
return -1;
}
FILE* fp = fopen("print_journal_out.txt", "w");
JHeader* header = (JHeader*)view;
if (!header->valid())
{
printf("invalid journal file %s\n", argv[1]);
return -1;
}
printHeader(fp, header);
unsigned long long offset = sizeof(JHeader);
while (offset < fileSize)
{
JSection section(view + offset);
if (!section.Valid())
{
printf("invalid journal file %s\n", argv[1]);
return -1;
}
printSectionHeader(fp, section.Header());
JSectionIterator it = section.iterator();
JEntryInfo info;
while (!it.atEof() && it.next(info))
{
printSectionEntry(fp, info);
}
offset += section.lenWithPadding();
}
fclose(fp);
return 0;
}