~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
#include "azio.h"
2
#include <string.h>
3
#include <assert.h>
4
#include <stdio.h>
5
#include <stdarg.h>
212.5.18 by Monty Taylor
Moved m_ctype, m_string and my_bitmap. Removed t_ctype.
6
#include <mystrings/m_ctype.h>
7
#include <mystrings/m_string.h>
212.5.21 by Monty Taylor
Moved my_getopt.h
8
#include <mysys/my_getopt.h>
1 by brian
clean slate
9
10
#define SHOW_VERSION "0.1"
11
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;
77.1.4 by Monty Taylor
Got archive compiling cleanly.
17
uint64_t new_auto_increment_value;
1 by brian
clean slate
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;
22
23
int main(int argc, char *argv[])
24
{
25
  unsigned int ret;
26
  azio_stream reader_handle;
27
28
  my_init();
29
  MY_INIT(argv[0]);
30
  get_options(&argc, &argv);
31
32
  if (argc < 1)
33
  {
34
    printf("No file specified. \n");
35
    return 0;
36
  }
37
492.1.14 by Monty Taylor
Removed O_BINARY and FILE_BINARY.
38
  if (!(ret= azopen(&reader_handle, argv[0], O_RDONLY, AZ_METHOD_BLOCK)))
1 by brian
clean slate
39
  {
40
    printf("Could not open Archive file\n");
41
    return 0;
42
  }
43
44
  if (opt_autoincrement)
45
  {
46
    azio_stream writer_handle;
47
48
    if (new_auto_increment_value)
49
    {
50
      if (reader_handle.auto_increment >= new_auto_increment_value)
51
      {
52
        printf("Value is lower then current value\n");
53
        goto end;
54
      }
55
    }
56
    else
57
    {
58
      new_auto_increment_value= reader_handle.auto_increment + 1;
59
    }
60
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
61
    if (!(ret= azopen(&writer_handle, argv[0], O_CREAT|O_RDWR,
1 by brian
clean slate
62
                      AZ_METHOD_BLOCK)))
63
    {
64
      printf("Could not open file for update: %s\n", argv[0]);
65
      goto end;
66
    }
67
68
    writer_handle.auto_increment= new_auto_increment_value;
69
70
    azclose(&writer_handle);
71
    azflush(&reader_handle, Z_SYNC_FLUSH);
72
  }
73
74
  printf("Version %u\n", reader_handle.version);
75
  if (reader_handle.version > 2)
76
  {
77
    printf("\tMinor version %u\n", reader_handle.minor_version);
77.1.103 by Monty Taylor
Fixed some format strings to use PRIu64 for the uint64_t types.
78
    printf("\tStart position %"PRIu64"\n", (uint64_t)reader_handle.start);
1 by brian
clean slate
79
    printf("\tBlock size %u\n", reader_handle.block_size);
77.1.103 by Monty Taylor
Fixed some format strings to use PRIu64 for the uint64_t types.
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);
1 by brian
clean slate
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)
93
    {
94
      char *comment =
95
        (char *) malloc(sizeof(char) * reader_handle.comment_length);
96
      azread_comment(&reader_handle, comment);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
97
      printf("\tComment length %u\n\t\t%.*s\n", reader_handle.comment_length,
1 by brian
clean slate
98
             reader_handle.comment_length, comment);
99
      free(comment);
100
    }
101
  }
102
  else
103
  {
104
    goto end;
105
  }
106
107
  printf("\n");
108
109
  if (opt_check)
110
  {
111
    int error;
112
    unsigned int read;
77.1.4 by Monty Taylor
Got archive compiling cleanly.
113
    uint64_t row_count= 0;
1 by brian
clean slate
114
115
    while ((read= azread_row(&reader_handle, &error)))
116
    {
53.2.33 by Monty Taylor
More warnings fixes.
117
      if (error == Z_STREAM_ERROR)
1 by brian
clean slate
118
      {
119
        printf("Table is damaged\n");
120
        goto end;
121
      }
122
123
      row_count++;
124
125
      if (read > reader_handle.longest_row)
126
      {
77.1.103 by Monty Taylor
Fixed some format strings to use PRIu64 for the uint64_t types.
127
        printf("Table is damaged, row %"PRIu64" is invalid\n", row_count);
1 by brian
clean slate
128
        goto end;
129
      }
130
    }
131
77.1.103 by Monty Taylor
Fixed some format strings to use PRIu64 for the uint64_t types.
132
    printf("Found %"PRIu64" rows\n", row_count);
1 by brian
clean slate
133
  }
134
135
  if (opt_backup)
136
  {
137
    int error;
138
    unsigned int read;
77.1.4 by Monty Taylor
Got archive compiling cleanly.
139
    uint64_t row_count= 0;
1 by brian
clean slate
140
    char *buffer;
141
142
    azio_stream writer_handle;
143
144
    buffer= (char *)malloc(reader_handle.longest_row);
145
    if (buffer == NULL)
146
    {
77.1.103 by Monty Taylor
Fixed some format strings to use PRIu64 for the uint64_t types.
147
      printf("Could not allocate memory for row %"PRIu64"\n", row_count);
1 by brian
clean slate
148
      goto end;
149
    }
150
151
492.1.14 by Monty Taylor
Removed O_BINARY and FILE_BINARY.
152
    if (!(ret= azopen(&writer_handle, argv[1], O_CREAT|O_RDWR,
1 by brian
clean slate
153
                      AZ_METHOD_BLOCK)))
154
    {
155
      printf("Could not open file for backup: %s\n", argv[1]);
156
      goto end;
157
    }
158
159
    writer_handle.auto_increment= reader_handle.auto_increment;
160
    if (reader_handle.frm_length)
161
    {
162
      char *ptr;
656.1.25 by Monty Taylor
Removed my_malloc stuff from storage/
163
      ptr= (char *)malloc(sizeof(char) * reader_handle.frm_length);
656.1.47 by Monty Taylor
More malloc return check fixes.
164
      if (ptr == NULL)
165
      {
166
        printf("Could not allocate enough memory\n");
167
        goto end;
168
      }
1 by brian
clean slate
169
      azread_frm(&reader_handle, ptr);
170
      azwrite_frm(&writer_handle, ptr, reader_handle.frm_length);
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
171
      free(ptr);
1 by brian
clean slate
172
    }
173
174
    if (reader_handle.comment_length)
175
    {
176
      char *ptr;
656.1.25 by Monty Taylor
Removed my_malloc stuff from storage/
177
      ptr= (char *)malloc(sizeof(char) * reader_handle.comment_length);
1 by brian
clean slate
178
      azread_comment(&reader_handle, ptr);
179
      azwrite_comment(&writer_handle, ptr, reader_handle.comment_length);
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
180
      free(ptr);
1 by brian
clean slate
181
    }
182
183
    while ((read= azread_row(&reader_handle, &error)))
184
    {
185
      if (error == Z_STREAM_ERROR || error)
186
      {
187
        printf("Table is damaged\n");
188
        goto end;
189
      }
190
191
      /* If we read nothing we are at the end of the file */
192
      if (read == 0)
193
        break;
194
195
      row_count++;
196
197
      azwrite_row(&writer_handle, reader_handle.row_ptr, read);
198
199
      if (reader_handle.rows == writer_handle.rows)
200
        break;
201
    }
202
203
    free(buffer);
204
205
    azclose(&writer_handle);
206
  }
207
208
  if (opt_extract_frm)
209
  {
210
    File frm_file;
211
    char *ptr;
492.1.14 by Monty Taylor
Removed O_BINARY and FILE_BINARY.
212
    frm_file= my_open(argv[1], O_CREAT|O_RDWR, MYF(0));
656.1.25 by Monty Taylor
Removed my_malloc stuff from storage/
213
    ptr= (char *)malloc(sizeof(char) * reader_handle.frm_length);
656.1.47 by Monty Taylor
More malloc return check fixes.
214
    if (ptr == NULL)
215
    {
216
      printf("Could not allocate enough memory\n");
217
      goto end;
218
    }
1 by brian
clean slate
219
    azread_frm(&reader_handle, ptr);
481 by Brian Aker
Remove all of uchar.
220
    my_write(frm_file, (unsigned char*) ptr, reader_handle.frm_length, MYF(0));
1 by brian
clean slate
221
    my_close(frm_file, MYF(0));
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
222
    free(ptr);
1 by brian
clean slate
223
  }
224
225
end:
226
  printf("\n");
227
  azclose(&reader_handle);
228
229
  my_end(0);
230
  return 0;
231
}
232
146 by Brian Aker
my_bool cleanup.
233
static bool
1 by brian
clean slate
234
get_one_option(int optid,
235
	       const struct my_option *opt __attribute__((unused)),
236
	       char *argument)
237
{
238
  switch (optid) {
239
  case 'b':
240
    opt_backup= 1;
241
    break;
242
  case 'c':
243
    opt_check= 1;
244
    break;
245
  case 'e':
246
    opt_extract_frm= 1;
247
    break;
248
  case 'f':
249
    opt_force= 1;
250
    printf("Not implemented yet\n");
251
    break;
252
  case 'q':
253
    opt_quiet= 1;
254
    printf("Not implemented yet\n");
255
    break;
256
  case 'V':
257
    print_version();
258
    exit(0);
259
  case 't':
260
    printf("Not implemented yet\n");
261
    break;
262
  case 'A':
263
    opt_autoincrement= 1;
264
    if (argument)
265
      new_auto_increment_value= strtoull(argument, NULL, 0);
266
    else
267
      new_auto_increment_value= 0;
268
    break;
269
  case '?':
270
    usage();
271
    exit(0);
272
  }
273
  return 0;
274
}
275
276
static struct my_option my_long_options[] =
277
{
278
  {"backup", 'b',
279
   "Make a backup of an archive table.",
280
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
281
  {"check", 'c', "Check table for errors.",
282
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
283
  {"extract-frm", 'e',
284
   "Extract the frm file.",
285
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
286
  {"force", 'f',
287
   "Restart with -r if there are any errors in the table.",
288
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
289
  {"help", '?',
290
   "Display this help and exit.",
291
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
292
  {"quick", 'q', "Faster repair by not modifying the data file.",
293
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
294
  {"repair", 'r', "Repair a damaged Archive version 3 or above file.",
295
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
296
  {"set-auto-increment", 'A',
297
   "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.",
77.1.77 by Monty Taylor
A crapton more warning cleanups (I turned on more warnings)
298
   (char**) &new_auto_increment,
299
   (char**) &new_auto_increment,
1 by brian
clean slate
300
   0, GET_ULL, OPT_ARG, 0, 0, 0, 0, 0, 0},
301
  {"silent", 's',
302
   "Only print errors. One can use two -s to make archive_reader very silent.",
303
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
304
  {"tmpdir", 't',
305
   "Path for temporary files.",
77.1.77 by Monty Taylor
A crapton more warning cleanups (I turned on more warnings)
306
   (char**) &opt_tmpdir,
1 by brian
clean slate
307
   0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
308
  {"version", 'V',
309
   "Print version and exit.",
310
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
311
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
312
};
313
314
static void usage(void)
315
{
316
  print_version();
317
  puts("Copyright (C) 2007 MySQL AB");
318
  puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\
319
       \nand you are welcome to modify and redistribute it under the GPL \
320
       license\n");
321
  puts("Read and modify Archive files directly\n");
322
  printf("Usage: %s [OPTIONS] file_to_be_looked_at [file_for_backup]\n", my_progname);
520.4.28 by Monty Taylor
Changed my.cnf to drizzle.cnf and /etc/mysql to /etc/drizzle.
323
  print_defaults("drizzle", load_default_groups);
1 by brian
clean slate
324
  my_print_help(my_long_options);
325
}
326
327
static void print_version(void)
328
{
212.5.36 by Monty Taylor
Moved drizzle_version.
329
  printf("%s  Ver %s, for %s (%s)\n", my_progname, SHOW_VERSION,
330
         SYSTEM_TYPE, MACHINE_TYPE);
1 by brian
clean slate
331
}
332
333
static void get_options(int *argc, char ***argv)
334
{
520.4.28 by Monty Taylor
Changed my.cnf to drizzle.cnf and /etc/mysql to /etc/drizzle.
335
  load_defaults("drizzle", load_default_groups, argc, argv);
1 by brian
clean slate
336
  default_argv= *argv;
337
338
  handle_options(argc, argv, my_long_options, get_one_option);
339
340
  if (*argc == 0)
341
  {
342
    usage();
343
    exit(-1);
344
  }
345
346
  return;
347
} /* get options */