1 /* Copyright (c) 2007, 2010, Oracle and/or its affiliates
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
15
16 #include "azlib.h"
17 #include <string.h>
18 #include <assert.h>
19 #include <stdio.h>
20 #include <stdarg.h>
21 #include <m_ctype.h>
22 #include <m_string.h>
23 #include <my_getopt.h>
24 #include <mysql_version.h>
25
26 #define BUFFER_LEN 1024
27 #define ARCHIVE_ROW_HEADER_SIZE 4
28
29 #define SHOW_VERSION "0.1"
30
31 static void get_options(int *argc,char * * *argv);
32 static void print_version(void);
33 static void usage(void);
34 static const char *opt_tmpdir;
35 static const char *new_auto_increment;
36 unsigned long long new_auto_increment_value;
37 static const char *load_default_groups[]= { "archive_reader", 0 };
38 static char **default_argv;
39 int opt_check, opt_force, opt_quiet, opt_backup= 0, opt_extract_frm;
40 int opt_autoincrement;
41
main(int argc,char * argv[])42 int main(int argc, char *argv[])
43 {
44 unsigned int ret;
45 azio_stream reader_handle;
46
47 MY_INIT(argv[0]);
48 get_options(&argc, &argv);
49
50 if (argc < 1)
51 {
52 printf("No file specified. \n");
53 return 0;
54 }
55
56 if (!(ret= azopen(&reader_handle, argv[0], O_RDONLY|O_BINARY)))
57 {
58 printf("Could not open Archive file\n");
59 return 0;
60 }
61
62 if (opt_autoincrement)
63 {
64 azio_stream writer_handle;
65
66 if (new_auto_increment_value)
67 {
68 if (reader_handle.auto_increment >= new_auto_increment_value)
69 {
70 printf("Value is lower then current value\n");
71 goto end;
72 }
73 }
74 else
75 {
76 new_auto_increment_value= reader_handle.auto_increment + 1;
77 }
78
79 if (!(ret= azopen(&writer_handle, argv[0], O_CREAT|O_RDWR|O_BINARY)))
80 {
81 printf("Could not open file for update: %s\n", argv[0]);
82 goto end;
83 }
84
85 writer_handle.auto_increment= new_auto_increment_value;
86
87 azclose(&writer_handle);
88 azflush(&reader_handle, Z_SYNC_FLUSH);
89 }
90
91 printf("Version %u\n", reader_handle.version);
92 if (reader_handle.version > 2)
93 {
94 printf("\tMinor version %u\n", reader_handle.minor_version);
95 printf("\tStart position %llu\n", (unsigned long long)reader_handle.start);
96 printf("\tBlock size %u\n", reader_handle.block_size);
97 printf("\tRows %llu\n", reader_handle.rows);
98 printf("\tAutoincrement %llu\n", reader_handle.auto_increment);
99 printf("\tCheck Point %llu\n", reader_handle.check_point);
100 printf("\tForced Flushes %llu\n", reader_handle.forced_flushes);
101 printf("\tLongest Row %u\n", reader_handle.longest_row);
102 printf("\tShortest Row %u\n", reader_handle.shortest_row);
103 printf("\tState %s\n", ( reader_handle.dirty ? "dirty" : "clean"));
104 printf("\tFRM stored at %u\n", reader_handle.frm_start_pos);
105 printf("\tComment stored at %u\n", reader_handle.comment_start_pos);
106 printf("\tData starts at %u\n", (unsigned int)reader_handle.start);
107 if (reader_handle.frm_start_pos)
108 printf("\tFRM length %u\n", reader_handle.frm_length);
109 if (reader_handle.comment_start_pos)
110 {
111 char *comment = (char *) my_malloc(reader_handle.comment_length,
112 MYF(MY_WME));
113 if (comment)
114 {
115 azread_comment(&reader_handle, comment);
116 printf("\tComment length %u\n\t\t%.*s\n",
117 reader_handle.comment_length,
118 reader_handle.comment_length, comment);
119 my_free(comment,MYF(0));
120 }
121 }
122 }
123 else
124 {
125 goto end;
126 }
127
128 printf("\n");
129
130 if (opt_check)
131 {
132 uchar size_buffer[ARCHIVE_ROW_HEADER_SIZE];
133 int error;
134 unsigned int x;
135 unsigned int read;
136 unsigned int row_len;
137 unsigned long long row_count= 0;
138 char buffer;
139
140 while ((read= azread(&reader_handle, (uchar *)size_buffer,
141 ARCHIVE_ROW_HEADER_SIZE, &error)))
142 {
143 if (error == Z_STREAM_ERROR || (read && read < ARCHIVE_ROW_HEADER_SIZE))
144 {
145 printf("Table is damaged\n");
146 goto end;
147 }
148
149 /* If we read nothing we are at the end of the file */
150 if (read == 0 || read != ARCHIVE_ROW_HEADER_SIZE)
151 break;
152
153 row_len= uint4korr(size_buffer);
154 row_count++;
155
156 if (row_len > reader_handle.longest_row)
157 {
158 printf("Table is damaged, row %llu is invalid\n",
159 row_count);
160 goto end;
161 }
162
163
164 for (read= x= 0; x < row_len ; x++)
165 {
166 read+= (unsigned int)azread(&reader_handle, &buffer, sizeof(char), &error);
167 if (!read)
168 break;
169 }
170
171
172 if (row_len != read)
173 {
174 printf("Row length did not match row (at %llu). %u != %u \n",
175 row_count, row_len, read);
176 goto end;
177 }
178 }
179
180 if (0)
181 {
182 printf("Table is damaged\n");
183 goto end;
184 }
185 else
186 {
187 printf("Found %llu rows\n", row_count);
188 }
189 }
190
191 if (opt_backup)
192 {
193 uchar size_buffer[ARCHIVE_ROW_HEADER_SIZE];
194 int error;
195 unsigned int read;
196 unsigned int row_len;
197 unsigned long long row_count= 0;
198 char *buffer;
199
200 azio_stream writer_handle;
201
202 buffer= (char *) my_malloc(reader_handle.longest_row, MYF(0));
203 if (buffer == NULL)
204 {
205 printf("Could not allocate memory for row %llu\n", row_count);
206 goto end;
207 }
208
209
210 if (!(ret= azopen(&writer_handle, argv[1], O_CREAT|O_RDWR|O_BINARY)))
211 {
212 printf("Could not open file for backup: %s\n", argv[1]);
213 goto end;
214 }
215
216 writer_handle.auto_increment= reader_handle.auto_increment;
217 if (reader_handle.frm_length)
218 {
219 char *ptr;
220 ptr= (char *)my_malloc(sizeof(char) * reader_handle.frm_length, MYF(0));
221 azread_frm(&reader_handle, ptr);
222 azwrite_frm(&writer_handle, ptr, reader_handle.frm_length);
223 my_free(ptr);
224 }
225
226 if (reader_handle.comment_length)
227 {
228 char *ptr;
229 ptr= (char *)my_malloc(sizeof(char) * reader_handle.comment_length, MYF(0));
230 azread_comment(&reader_handle, ptr);
231 azwrite_comment(&writer_handle, ptr, reader_handle.comment_length);
232 my_free(ptr);
233 }
234
235 while ((read= azread(&reader_handle, (uchar *)size_buffer,
236 ARCHIVE_ROW_HEADER_SIZE, &error)))
237 {
238 if (error == Z_STREAM_ERROR || (read && read < ARCHIVE_ROW_HEADER_SIZE))
239 {
240 printf("Table is damaged\n");
241 goto end;
242 }
243
244 /* If we read nothing we are at the end of the file */
245 if (read == 0 || read != ARCHIVE_ROW_HEADER_SIZE)
246 break;
247
248 row_len= uint4korr(size_buffer);
249
250 row_count++;
251
252 memcpy(buffer, size_buffer, ARCHIVE_ROW_HEADER_SIZE);
253
254 read= (unsigned int)azread(&reader_handle, buffer + ARCHIVE_ROW_HEADER_SIZE,
255 row_len, &error);
256
257 DBUG_ASSERT(read == row_len);
258
259 azwrite(&writer_handle, buffer, row_len + ARCHIVE_ROW_HEADER_SIZE);
260
261
262 if (row_len != read)
263 {
264 printf("Row length did not match row (at %llu). %u != %u \n",
265 row_count, row_len, read);
266 goto end;
267 }
268
269 if (reader_handle.rows == writer_handle.rows)
270 break;
271 }
272
273 my_free(buffer, MYF(0));
274
275 azclose(&writer_handle);
276 }
277
278 if (opt_extract_frm)
279 {
280 File frm_file;
281 char *ptr;
282 frm_file= my_open(argv[1], O_CREAT|O_RDWR|O_BINARY, MYF(0));
283 ptr= (char *)my_malloc(sizeof(char) * reader_handle.frm_length, MYF(0));
284 azread_frm(&reader_handle, ptr);
285 my_write(frm_file, (uchar*) ptr, reader_handle.frm_length, MYF(0));
286 my_close(frm_file, MYF(0));
287 my_free(ptr);
288 }
289
290 end:
291 printf("\n");
292 azclose(&reader_handle);
293
294 return 0;
295 }
296
297 static my_bool
get_one_option(int optid,const struct my_option * opt,char * argument)298 get_one_option(int optid,
299 const struct my_option *opt __attribute__((unused)),
300 char *argument)
301 {
302 switch (optid) {
303 case 'b':
304 opt_backup= 1;
305 break;
306 case 'c':
307 opt_check= 1;
308 break;
309 case 'e':
310 opt_extract_frm= 1;
311 break;
312 case 'f':
313 opt_force= 1;
314 printf("Not implemented yet\n");
315 break;
316 case 'q':
317 opt_quiet= 1;
318 printf("Not implemented yet\n");
319 break;
320 case 'V':
321 print_version();
322 exit(0);
323 case 't':
324 printf("Not implemented yet\n");
325 break;
326 case 'A':
327 opt_autoincrement= 1;
328 if (argument)
329 new_auto_increment_value= strtoull(argument, NULL, 0);
330 else
331 new_auto_increment_value= 0;
332 break;
333 case '?':
334 usage();
335 exit(0);
336 case '#':
337 if (argument == disabled_my_option)
338 {
339 DBUG_POP();
340 }
341 else
342 {
343 DBUG_PUSH(argument ? argument : "d:t:o,/tmp/archive_reader.trace");
344 }
345 break;
346 }
347 return 0;
348 }
349
350 static struct my_option my_long_options[] =
351 {
352 {"backup", 'b',
353 "Make a backup of an archive table.",
354 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
355 {"check", 'c', "Check table for errors.",
356 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
357 #ifndef DBUG_OFF
358 {"debug", '#',
359 "Output debug log. Often this is 'd:t:o,filename'.",
360 0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
361 #endif
362 {"extract-frm", 'e',
363 "Extract the frm file.",
364 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
365 {"force", 'f',
366 "Restart with -r if there are any errors in the table.",
367 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
368 {"help", '?',
369 "Display this help and exit.",
370 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
371 {"quick", 'q', "Faster repair by not modifying the data file.",
372 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
373 {"repair", 'r', "Repair a damaged Archive version 3 or above file.",
374 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
375 {"set-auto-increment", 'A',
376 "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.",
377 &new_auto_increment, &new_auto_increment,
378 0, GET_ULL, OPT_ARG, 0, 0, 0, 0, 0, 0},
379 {"silent", 's',
380 "Only print errors. One can use two -s to make archive_reader very silent.",
381 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
382 {"tmpdir", 't',
383 "Path for temporary files.",
384 &opt_tmpdir,
385 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
386 {"version", 'V',
387 "Print version and exit.",
388 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
389 { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
390 };
391
usage(void)392 static void usage(void)
393 {
394 print_version();
395 puts("Copyright 2007-2008 MySQL AB, 2008 Sun Microsystems, Inc.");
396 puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\nand you are welcome to modify and redistribute it under the GPL license\n");
397 puts("Read and modify Archive files directly\n");
398 printf("Usage: %s [OPTIONS] file_to_be_looked_at [file_for_backup]\n", my_progname);
399 print_defaults("my", load_default_groups);
400 my_print_help(my_long_options);
401 }
402
print_version(void)403 static void print_version(void)
404 {
405 printf("%s Ver %s Distrib %s, for %s (%s)\n", my_progname, SHOW_VERSION,
406 MYSQL_SERVER_VERSION, SYSTEM_TYPE, MACHINE_TYPE);
407 }
408
get_options(int * argc,char *** argv)409 static void get_options(int *argc, char ***argv)
410 {
411 load_defaults_or_exit("my", load_default_groups, argc, argv);
412 default_argv= *argv;
413
414 handle_options(argc, argv, my_long_options, get_one_option);
415
416 if (*argc == 0)
417 {
418 usage();
419 exit(-1);
420 }
421
422 return;
423 } /* get options */
424