forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndb_dump_frm_data.cpp
182 lines (166 loc) · 4.44 KB
/
ndb_dump_frm_data.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
/* Copyright (c) 2003, 2021, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
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, version 2.0, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
#include <ndb_global.h>
#include <ndb_opts.h>
#include <NdbOut.hpp>
#include <NdbApi.hpp>
#include <NDBT.hpp>
static struct my_option
my_long_options[] =
{
{ "help", '?',
"Display this help and exit.",
0, 0, 0,
GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ 0, 0,
0,
0, 0, 0,
GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0 }
};
const char*
load_default_groups[]= { 0 };
static void
short_usage_sub(void)
{
ndb_short_usage_sub("*.frm ...");
}
static void
usage()
{
printf("%s: pack and dump *.frm as C arrays\n", my_progname);
ndb_usage(short_usage_sub, load_default_groups, my_long_options);
}
static void
dodump(const char* name, const uchar* frm_data, uint frm_len)
{
printf("const uint g_%s_frm_len = %u;\n\n", name, frm_len);
printf("const uint8 g_%s_frm_data[%u] =\n{\n", name, frm_len);
uint n = 0;
while (n < frm_len) {
if (n % 8 == 0) {
if (n != 0) {
printf("\n");
}
printf(" ");
}
printf("0x%02x", frm_data[n]);
if (n + 1 < frm_len) {
printf(",");
}
n++;
}
if (n % 8 != 0) {
printf("\n");
}
printf("};\n");
}
static int
dofile(const char* file)
{
struct stat st;
size_t size = 0;
uchar* data = 0;
int fd = -1;
uchar* pack_data = 0;
size_t pack_len = 0;
char* namebuf = 0;
int ret = -1;
do
{
if (stat(file, &st) == -1)
{
fprintf(stderr, "%s: stat: %s\n", file, strerror(errno));
break;
}
size = st.st_size;
if ((data = (uchar*)malloc(size)) == 0)
{
fprintf(stderr, "%s: malloc %u: %s\n", file, (uint)size, strerror(errno));
break;
}
if ((fd = open(file, O_RDONLY)) == -1)
{
fprintf(stderr, "%s: open: %s\n", file, strerror(errno));
break;
}
ssize_t size2;
if ((size2 = read(fd, data, (unsigned)size)) == -1)
{
fprintf(stderr, "%s: read: %s\n", file, strerror(errno));
break;
}
if ((size_t)size2 != size)
{
fprintf(stderr, "%s: short read: %u != %u\n", file, (uint)size2, (uint)size);
break;
}
int error;
if ((error = packfrm(data, size, &pack_data, &pack_len)) != 0)
{
fprintf(stderr, "%s: packfrm: error %d\n", file, error);
break;
}
namebuf = strdup(file);
if (namebuf == 0)
{
fprintf(stderr, "%s: strdup: %s\n", file, strerror(errno));
break;
}
char* name = namebuf;
if (strchr(name, '/') != 0)
name = strrchr(name, '/') + 1;
char* dot;
if ((dot = strchr(name, '.')) != 0)
*dot = 0;
printf("\n/*\n");
printf(" name: %s\n", name);
printf(" orig: %u\n", (uint)size);
printf(" pack: %u\n", (uint)pack_len);
printf("*/\n\n");
dodump(name, pack_data, (uint)pack_len);
ret = 0;
}
while (0);
if (namebuf != 0)
free(namebuf);
if (pack_data != 0)
my_free(pack_data); // Free data returned from packfrm with my_free
if (fd != -1)
(void)close(fd);
if (data != 0)
free(data);
return ret;
}
int
main(int argc, char** argv)
{
my_progname = "ndb_pack_frm";
int ret;
ndb_init();
ndb_opt_set_usage_funcs(short_usage_sub, usage);
ret = handle_options(&argc, &argv, my_long_options, ndb_std_get_one_option);
if (ret != 0)
return NDBT_WRONGARGS;
for (int i = 0; i < argc; i++)
{
ret = dofile(argv[i]);
if (ret != 0)
return NDBT_FAILED;
}
return NDBT_OK;
}