Repository navigation
Expand file tree
/
Copy pathdatabase.cpp
More file actions
253 lines (231 loc) · 6 KB
/
Copy pathdatabase.cpp
File metadata and controls
253 lines (231 loc) · 6 KB
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
#define __USE_MINGW_ANSI_STDIO 1
#define __STDC_FORMAT_MACROS
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <memory>
#include <map>
#include <stdexcept>
#include "database.h"
#include "static_exception.h"
namespace Database
{
static const char* decodePageType(uint8_t pageType)
{
static constexpr const char* const typeNames[] =
{
"unused",
"Header Page",
"PIP",
"TIP",
"Pointer Page",
"Data Page",
"Index Root",
"Index Page",
"BLOB Page",
"Generator Page",
"SCN Page"
};
static_assert(std::size(typeNames) == Ods::pag_max + 1);
if (pageType <= Ods::pag_max)
{
return typeNames[pageType];
}
static char buffer[20];
sprintf(buffer, "Unknown (%u)", pageType);
return buffer;
}
static const std::string decodeFlags(uint8_t pageType, uint8_t flags)
{
std::string result;
bool first = true;
auto addName = [&](const char* name)
{
if (!first)
{
result += ',';
}
else
{
first = false;
}
result += name;
};
for (int i = 0; i < 8; ++i)
{
if (flags & (1 << i))
{
switch (pageType)
{
case Ods::pag_blob:
{
static constexpr const char* const flagNames[8] =
{
"pointers",
"unknown",
"unknown",
"unknown",
"unknown",
"unknown",
"unknown",
"encrypted"
};
addName(flagNames[i]);
break;
}
case Ods::pag_index:
{
static constexpr const char* const flagNames[8] =
{
"don't gc",
"descending",
"unknown",
"unknown",
"jump info",
"released",
"unknown",
"encrypted"
};
addName(flagNames[i]);
break;
}
case Ods::pag_data:
{
static constexpr const char* const flagNames[8] =
{
"orphan",
"full",
"large",
"swept",
"secondary",
"unknown",
"unknown",
"encrypted"
};
addName(flagNames[i]);
break;
}
case Ods::pag_pointer:
{
static constexpr const char* const flagNames[8] =
{
"eof",
"unknown",
"unknown",
"unknown",
"unknown",
"unknown",
"unknown",
"encrypted"
};
addName(flagNames[i]);
break;
}
default:
{
static constexpr const char* const flagNames[8] =
{
"unknown",
"unknown",
"unknown",
"unknown",
"unknown",
"unknown",
"unknown",
"encrypted"
};
addName(flagNames[i]);
break;
}
}
}
}
return result;
}
// For common pieces among versions
struct OdsDumper
{
// Just in case if some further ODS have different offset of page size
unsigned getPageSize(Reader& file)
{
// file is supposed to have first kilobyte already read and endianness set to a right value
return file.gatherInt16(file.buffer + offsetof(Ods::header_page13, hdr_page_size));
}
};
// Specific to Firebird 5 ODS 13
struct Ods13: public OdsDumper
{
};
void dumpIt(Reader& file)
{
struct stat64 st{};
if (fstat64(file.f, &st) == -1)
{
fprintf(stderr, "fstat error %u\n", errno);
}
printf("Firebird database %s, length %llu\n", to_string(file.fileName).c_str(), (unsigned long long)st.st_size);
// Sanity checks
if (file.buffer[offsetof(Ods::pag, pag_type)] != Ods::pag_header)
{
throw static_exception("First page is not a header page");
}
// These calls to gatherInt are safe even if endianness is not detected yet because zero value has a symmetric layout
if (file.gatherInt32(file.buffer + offsetof(Ods::pag, pag_pageno)) != 0)
{
throw static_exception("Wrong page number of a header page");
}
if (file.gatherInt32(file.buffer + offsetof(Ods::pag, pag_generation)) == 0)
{
throw static_exception("First page was not ever written");
}
// Get endianness from compatibility flag. It must be more reliable than to derive it from major ODS number value
file.endianness = (file.buffer[offsetof(Ods::header_page13, hdr_compatibility_flags)] & 1) ? __ORDER_BIG_ENDIAN__ : __ORDER_LITTLE_ENDIAN__;
uint16_t ods = file.gatherInt16(file.buffer + offsetof(Ods::header_page13, hdr_ods_version));
if ((ods & 0xFF00) != 0x8000) // Check whole high byte, just in case
{
throw static_exception("Not a Firebird database");
}
ods &= ~0x8000;
std::unique_ptr<OdsDumper> dumper;
switch (ods)
{
case 13:
dumper.reset(new Ods13);
break;
default:
throw std::runtime_error(std::string("Unsupported ODS ") + std::to_string(ods) + " found");
}
unsigned pageSize = dumper->getPageSize(file);
// Another sanity check
if ((pageSize & (pageSize - 1)) != 0)
{
throw std::runtime_error(std::string("Page size ") + std::to_string(pageSize) + " is invalid (must be a power of two)");
}
// Now we have enough info to go through whole database dumping every single page
std::unique_ptr<unsigned char[]> buffer(new unsigned char[pageSize]);
unsigned realPageNumber = 0;
// This map is ordered to produce ordered stats without further sort.
// Though perhaps unordered_map + sort could be faster.
std::map<uint8_t, unsigned> stats;
while (!file.eof())
{
file.readBuffer(buffer.get(), pageSize);
// Dump header
const unsigned char* p = buffer.get(); // Just for convenience
const uint8_t pageType = p[offsetof(Ods::pag, pag_type)];
printf("Page %u (%u), type %s, flags %x (%s), written %u times, SCN %u\n",
realPageNumber, file.gatherInt32(p + offsetof(Ods::pag, pag_pageno)),
decodePageType(pageType),
p[offsetof(Ods::pag, pag_flags)], decodeFlags(pageType, p[offsetof(Ods::pag, pag_flags)]).c_str(),
file.gatherInt32(p + offsetof(Ods::pag, pag_generation)),
file.gatherInt32(p + offsetof(Ods::pag, pag_scn)));
stats[pageType]++;
++realPageNumber;
}
printf("\nSummary:\n");
for (const auto& itr : stats)
{
printf("\t%s: %u (%0.2f%%, %0.2fmb)\n", decodePageType(itr.first), itr.second, itr.second * 100.0 / realPageNumber, itr.second / (1024.0 * 1024.0) * pageSize);
}
}
}