6
#include <mystrings/m_ctype.h>
7
#include <mystrings/m_string.h>
8
#include <mysys/my_getopt.h>
10
#define SHOW_VERSION "0.1"
12
static void get_options(int *argc,char * * *argv);
13
static void print_version(void);
14
static void usage(void);
15
static const char *opt_tmpdir;
16
static const char *new_auto_increment;
17
uint64_t new_auto_increment_value;
18
static const char *load_default_groups[]= { "archive_reader", 0 };
19
static char **default_argv;
20
int opt_check, opt_force, opt_quiet, opt_backup= 0, opt_extract_frm;
21
int opt_autoincrement;
23
int main(int argc, char *argv[])
26
azio_stream reader_handle;
30
get_options(&argc, &argv);
34
printf("No file specified. \n");
38
if (!(ret= azopen(&reader_handle, argv[0], O_RDONLY, AZ_METHOD_BLOCK)))
40
printf("Could not open Archive file\n");
44
if (opt_autoincrement)
46
azio_stream writer_handle;
48
if (new_auto_increment_value)
50
if (reader_handle.auto_increment >= new_auto_increment_value)
52
printf("Value is lower then current value\n");
58
new_auto_increment_value= reader_handle.auto_increment + 1;
61
if (!(ret= azopen(&writer_handle, argv[0], O_CREAT|O_RDWR,
64
printf("Could not open file for update: %s\n", argv[0]);
68
writer_handle.auto_increment= new_auto_increment_value;
70
azclose(&writer_handle);
71
azflush(&reader_handle, Z_SYNC_FLUSH);
74
printf("Version %u\n", reader_handle.version);
75
if (reader_handle.version > 2)
77
printf("\tMinor version %u\n", reader_handle.minor_version);
78
printf("\tStart position %"PRIu64"\n", (uint64_t)reader_handle.start);
79
printf("\tBlock size %u\n", reader_handle.block_size);
80
printf("\tRows %"PRIu64"\n", reader_handle.rows);
81
printf("\tAutoincrement %"PRIu64"\n", reader_handle.auto_increment);
82
printf("\tCheck Point %"PRIu64"\n", reader_handle.check_point);
83
printf("\tForced Flushes %"PRIu64"\n", reader_handle.forced_flushes);
84
printf("\tLongest Row %u\n", reader_handle.longest_row);
85
printf("\tShortest Row %u\n", reader_handle.shortest_row);
86
printf("\tState %s\n", ( reader_handle.dirty ? "dirty" : "clean"));
87
printf("\tFRM stored at %u\n", reader_handle.frm_start_pos);
88
printf("\tComment stored at %u\n", reader_handle.comment_start_pos);
89
printf("\tData starts at %u\n", (unsigned int)reader_handle.start);
90
if (reader_handle.frm_start_pos)
91
printf("\tFRM length %u\n", reader_handle.frm_length);
92
if (reader_handle.comment_start_pos)
95
(char *) malloc(sizeof(char) * reader_handle.comment_length);
96
azread_comment(&reader_handle, comment);
97
printf("\tComment length %u\n\t\t%.*s\n", reader_handle.comment_length,
98
reader_handle.comment_length, comment);
113
uint64_t row_count= 0;
115
while ((read= azread_row(&reader_handle, &error)))
117
if (error == Z_STREAM_ERROR)
119
printf("Table is damaged\n");
125
if (read > reader_handle.longest_row)
127
printf("Table is damaged, row %"PRIu64" is invalid\n", row_count);
132
printf("Found %"PRIu64" rows\n", row_count);
139
uint64_t row_count= 0;
142
azio_stream writer_handle;
144
buffer= (char *)malloc(reader_handle.longest_row);
147
printf("Could not allocate memory for row %"PRIu64"\n", row_count);
152
if (!(ret= azopen(&writer_handle, argv[1], O_CREAT|O_RDWR,
155
printf("Could not open file for backup: %s\n", argv[1]);
159
writer_handle.auto_increment= reader_handle.auto_increment;
160
if (reader_handle.frm_length)
163
ptr= (char *)my_malloc(sizeof(char) * reader_handle.frm_length, MYF(0));
164
azread_frm(&reader_handle, ptr);
165
azwrite_frm(&writer_handle, ptr, reader_handle.frm_length);
169
if (reader_handle.comment_length)
172
ptr= (char *)my_malloc(sizeof(char) * reader_handle.comment_length, MYF(0));
173
azread_comment(&reader_handle, ptr);
174
azwrite_comment(&writer_handle, ptr, reader_handle.comment_length);
178
while ((read= azread_row(&reader_handle, &error)))
180
if (error == Z_STREAM_ERROR || error)
182
printf("Table is damaged\n");
186
/* If we read nothing we are at the end of the file */
192
azwrite_row(&writer_handle, reader_handle.row_ptr, read);
194
if (reader_handle.rows == writer_handle.rows)
200
azclose(&writer_handle);
207
frm_file= my_open(argv[1], O_CREAT|O_RDWR, MYF(0));
208
ptr= (char *)my_malloc(sizeof(char) * reader_handle.frm_length, MYF(0));
209
azread_frm(&reader_handle, ptr);
210
my_write(frm_file, (unsigned char*) ptr, reader_handle.frm_length, MYF(0));
211
my_close(frm_file, MYF(0));
217
azclose(&reader_handle);
224
get_one_option(int optid,
225
const struct my_option *opt __attribute__((unused)),
240
printf("Not implemented yet\n");
244
printf("Not implemented yet\n");
250
printf("Not implemented yet\n");
253
opt_autoincrement= 1;
255
new_auto_increment_value= strtoull(argument, NULL, 0);
257
new_auto_increment_value= 0;
266
static struct my_option my_long_options[] =
269
"Make a backup of an archive table.",
270
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
271
{"check", 'c', "Check table for errors.",
272
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
274
"Extract the frm file.",
275
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
277
"Restart with -r if there are any errors in the table.",
278
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
280
"Display this help and exit.",
281
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
282
{"quick", 'q', "Faster repair by not modifying the data file.",
283
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
284
{"repair", 'r', "Repair a damaged Archive version 3 or above file.",
285
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
286
{"set-auto-increment", 'A',
287
"Force auto_increment to start at this or higher value. If no value is given, then sets the next auto_increment value to the highest used value for the auto key + 1.",
288
(char**) &new_auto_increment,
289
(char**) &new_auto_increment,
290
0, GET_ULL, OPT_ARG, 0, 0, 0, 0, 0, 0},
292
"Only print errors. One can use two -s to make archive_reader very silent.",
293
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
295
"Path for temporary files.",
296
(char**) &opt_tmpdir,
297
0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
299
"Print version and exit.",
300
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
301
{ 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
304
static void usage(void)
307
puts("Copyright (C) 2007 MySQL AB");
308
puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\
309
\nand you are welcome to modify and redistribute it under the GPL \
311
puts("Read and modify Archive files directly\n");
312
printf("Usage: %s [OPTIONS] file_to_be_looked_at [file_for_backup]\n", my_progname);
313
print_defaults("drizzle", load_default_groups);
314
my_print_help(my_long_options);
317
static void print_version(void)
319
printf("%s Ver %s, for %s (%s)\n", my_progname, SHOW_VERSION,
320
SYSTEM_TYPE, MACHINE_TYPE);
323
static void get_options(int *argc, char ***argv)
325
load_defaults("drizzle", load_default_groups, argc, argv);
328
handle_options(argc, argv, my_long_options, get_one_option);