-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.cc
More file actions
230 lines (223 loc) · 7.68 KB
/
main.cc
File metadata and controls
230 lines (223 loc) · 7.68 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
/*
* Copyright (c) [2025-2026] [Zhao Song]
*/
#include <getopt.h>
#include <algorithm>
#include "ibdNinja.h"
void Usage() {
fprintf(stdout, "Usage: ibdNinja [OPTIONS]\n");
fprintf(stdout, "Options:\n");
fprintf(stdout, " --help, -h Display this "
"help message\n");
fprintf(stdout, " --file, -f Specify the "
"path to the ibd file\n");
fprintf(stdout, " --list-tables, -l List all "
"*supported* tables and their supported indexes in the "
"specified ibd file\n");
fprintf(stdout, " --list-all-tables, -a List all "
"tables and their indexes in the specified ibd file\n");
fprintf(stdout, " --list-leftmost-pages, -e INDEX_ID Show the "
"leftmost page number at each level of the specified "
"index\n");
fprintf(stdout, " --analyze-table, -t TABLE_ID Analyze the "
"specified table\n");
fprintf(stdout, " --analyze-index, -i INDEX_ID Analyze the "
"specified index\n");
fprintf(stdout, " --parse-page, -p PAGE_ID Parse the "
"specified page\n");
fprintf(stdout, " --no-print-record, -n Skip printing "
"record details when parsing a page\n");
fprintf(stdout, " --version, -v Display version "
"information\n");
fprintf(stdout, " --blob-format, -b FORMAT LOB output format: "
"hex|summary (default: hex)\n");
fprintf(stdout, " --blob-truncate NUM Max bytes to show "
"for hex LOB output (default: 256)\n");
fprintf(stdout, " --lob-versions, -B Show LOB version "
"history for external fields\n");
fprintf(stdout, " --inspect-blob, -I PAGE_NO,REC_NO Interactive BLOB "
"inspection for a specific record\n");
}
int main(int argc, char* argv[]) {
if (argc < 2) {
Usage();
return 1;
}
struct option options[] = {
{"help", no_argument, 0, 'h'},
{"file", required_argument, 0, 'f'},
{"list-all-tables", no_argument, 0, 'a'},
{"list-tables", no_argument, 0, 'l'},
{"list-leftmost-pages", required_argument, 0, 'e'},
{"analyze-table", required_argument, 0, 't'},
{"analyze-index", required_argument, 0, 'i'},
{"parse-page", required_argument, 0, 'p'},
{"no-print-record", no_argument, 0, 'n'},
{"version", no_argument, 0, 'v'},
{"blob-format", required_argument, 0, 'b'},
{"blob-truncate", required_argument, 0, 0x100},
{"lob-versions", no_argument, 0, 'B'},
{"inspect-blob", required_argument, 0, 'I'},
{0, 0, 0, 0} // End of options
};
int opt;
int option_index = 0;
std::string ibd_file = "";
bool list_all_tables = false;
bool list_tables = false;
bool list_leftmost_pages = false;
uint32_t table_id = ibd_ninja::FIL_NULL;
uint32_t index_id = ibd_ninja::FIL_NULL;
uint32_t page_no = ibd_ninja::FIL_NULL;
bool print_record = true;
bool inspect_blob = false;
uint32_t inspect_page_no = 0;
uint32_t inspect_rec_no = 0;
while ((opt = getopt_long(argc,
argv, "halvf:e:t:i:p:nb:BI:", options, &option_index)) != -1) {
switch (opt) {
case 'h':
ibd_ninja::ibdNinja::PrintName();
Usage();
return 0;
case 'v':
ibd_ninja::ibdNinja::PrintName();
printf("Version: %s\n", ibd_ninja::ibdNinja::g_version_);
return 0;
case 'f':
ibd_file = optarg;
break;
case 'a':
list_all_tables = true;
break;
case 'l':
list_tables = true;
break;
case 'e': {
list_leftmost_pages = true;
std::string str(optarg);
if (std::all_of(str.begin(), str.end(), ::isdigit)) {
index_id = std::stoul(optarg);
} else {
Usage();
return 1;
}
}
break;
case 't': {
std::string str(optarg);
if (std::all_of(str.begin(), str.end(), ::isdigit)) {
table_id = std::stoul(optarg);
} else {
Usage();
return 1;
}
}
break;
case 'i': {
std::string str(optarg);
if (std::all_of(str.begin(), str.end(), ::isdigit)) {
index_id = std::stoul(optarg);
} else {
Usage();
return 1;
}
}
break;
case 'p': {
std::string str(optarg);
if (std::all_of(str.begin(), str.end(), ::isdigit)) {
page_no = std::stoul(optarg);
} else {
Usage();
return 1;
}
}
break;
case 'n':
print_record = false;
break;
case 'b': {
std::string fmt(optarg);
if (fmt == "hex") {
ibd_ninja::g_lob_output_format = ibd_ninja::LobOutputFormat::HEX;
} else if (fmt == "summary") {
ibd_ninja::g_lob_output_format = ibd_ninja::LobOutputFormat::SUMMARY_ONLY;
} else {
fprintf(stderr, "Unknown blob format: %s "
"(use hex or summary)\n", optarg);
return 1;
}
}
break;
case 0x100: {
std::string str(optarg);
if (std::all_of(str.begin(), str.end(), ::isdigit)) {
ibd_ninja::g_lob_text_truncate_len = std::stoul(optarg);
} else {
fprintf(stderr, "Invalid blob-truncate value: %s\n", optarg);
return 1;
}
}
break;
case 'B':
ibd_ninja::g_lob_show_version_history = true;
break;
case 'I': {
std::string arg(optarg);
size_t comma = arg.find(',');
if (comma == std::string::npos) {
fprintf(stderr, "Invalid format for --inspect-blob. "
"Use: -I PAGE_NO,REC_NO\n");
return 1;
}
std::string page_str = arg.substr(0, comma);
std::string rec_str = arg.substr(comma + 1);
if (!std::all_of(page_str.begin(), page_str.end(), ::isdigit) ||
!std::all_of(rec_str.begin(), rec_str.end(), ::isdigit) ||
page_str.empty() || rec_str.empty()) {
fprintf(stderr, "Invalid format for --inspect-blob. "
"Use: -I PAGE_NO,REC_NO\n");
return 1;
}
inspect_blob = true;
inspect_page_no = std::stoul(page_str);
inspect_rec_no = std::stoul(rec_str);
}
break;
case '?':
return 1;
default:
printf("Unknown option: %c\n", opt);
return 1;
}
}
if (ibd_file.empty()) {
fprintf(stderr, "You must specify the ibd file using the "
"--file (-f) option.\n");
return 1;
}
ibd_ninja::ibdNinja* ninja =
ibd_ninja::ibdNinja::CreateNinja(ibd_file.c_str());
if (ninja != nullptr) {
if (list_tables) {
ninja->ShowTables(true);
} else if (list_all_tables) {
ninja->ShowTables(false);
} else if (list_leftmost_pages) {
ninja->ShowLeftmostPages(index_id);
} else if (table_id != ibd_ninja::FIL_NULL) {
ninja->ParseTable(table_id);
} else if (index_id != ibd_ninja::FIL_NULL) {
ninja->ParseIndex(index_id);
} else if (inspect_blob) {
ninja->InspectBlob(inspect_page_no, inspect_rec_no);
} else if (page_no != ibd_ninja::FIL_NULL) {
ninja->ParsePage(page_no, nullptr, true, print_record);
} else {
ninja->ShowTables(true);
}
delete ninja;
}
return 0;
}