~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/archive/archive_reader.c

  • Committer: Daniel Nichter
  • Date: 2011-10-23 16:01:37 UTC
  • mto: This revision was merged to the branch mainline in revision 2448.
  • Revision ID: daniel@percona.com-20111023160137-7ac3blgz8z4tf8za
Add Administration Getting Started and Logging.  Capitalize SQL clause keywords.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "azio.h"
2
 
#include <string.h>
3
 
#include <assert.h>
4
 
#include <stdio.h>
5
 
#include <stdarg.h>
6
 
#include <mystrings/m_ctype.h>
7
 
#include <mystrings/m_string.h>
8
 
#include <mysys/my_getopt.h>
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;
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;
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
 
 
38
 
  if (!(ret= azopen(&reader_handle, argv[0], O_RDONLY, AZ_METHOD_BLOCK)))
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
 
 
61
 
    if (!(ret= azopen(&writer_handle, argv[0], O_CREAT|O_RDWR, 
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);
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)
93
 
    {
94
 
      char *comment =
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);
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;
113
 
    uint64_t row_count= 0;
114
 
 
115
 
    while ((read= azread_row(&reader_handle, &error)))
116
 
    {
117
 
      if (error == Z_STREAM_ERROR)
118
 
      {
119
 
        printf("Table is damaged\n");
120
 
        goto end;
121
 
      }
122
 
 
123
 
      row_count++;
124
 
 
125
 
      if (read > reader_handle.longest_row)
126
 
      {
127
 
        printf("Table is damaged, row %"PRIu64" is invalid\n", row_count);
128
 
        goto end;
129
 
      }
130
 
    }
131
 
 
132
 
    printf("Found %"PRIu64" rows\n", row_count);
133
 
  }
134
 
 
135
 
  if (opt_backup)
136
 
  {
137
 
    int error;
138
 
    unsigned int read;
139
 
    uint64_t row_count= 0;
140
 
    char *buffer;
141
 
 
142
 
    azio_stream writer_handle;
143
 
 
144
 
    buffer= (char *)malloc(reader_handle.longest_row);
145
 
    if (buffer == NULL)
146
 
    {
147
 
      printf("Could not allocate memory for row %"PRIu64"\n", row_count);
148
 
      goto end;
149
 
    }
150
 
 
151
 
 
152
 
    if (!(ret= azopen(&writer_handle, argv[1], O_CREAT|O_RDWR,
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;
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);
166
 
      free(ptr);
167
 
    }
168
 
 
169
 
    if (reader_handle.comment_length)
170
 
    {
171
 
      char *ptr;
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);
175
 
      free(ptr);
176
 
    }
177
 
 
178
 
    while ((read= azread_row(&reader_handle, &error)))
179
 
    {
180
 
      if (error == Z_STREAM_ERROR || error)
181
 
      {
182
 
        printf("Table is damaged\n");
183
 
        goto end;
184
 
      }
185
 
 
186
 
      /* If we read nothing we are at the end of the file */
187
 
      if (read == 0)
188
 
        break;
189
 
 
190
 
      row_count++;
191
 
 
192
 
      azwrite_row(&writer_handle, reader_handle.row_ptr, read);
193
 
 
194
 
      if (reader_handle.rows == writer_handle.rows)
195
 
        break;
196
 
    }
197
 
 
198
 
    free(buffer);
199
 
 
200
 
    azclose(&writer_handle);
201
 
  }
202
 
 
203
 
  if (opt_extract_frm)
204
 
  {
205
 
    File frm_file;
206
 
    char *ptr;
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));
212
 
    free(ptr);
213
 
  }
214
 
 
215
 
end:
216
 
  printf("\n");
217
 
  azclose(&reader_handle);
218
 
 
219
 
  my_end(0);
220
 
  return 0;
221
 
}
222
 
 
223
 
static bool
224
 
get_one_option(int optid,
225
 
               const struct my_option *opt __attribute__((unused)),
226
 
               char *argument)
227
 
{
228
 
  switch (optid) {
229
 
  case 'b':
230
 
    opt_backup= 1;
231
 
    break;
232
 
  case 'c':
233
 
    opt_check= 1;
234
 
    break;
235
 
  case 'e':
236
 
    opt_extract_frm= 1;
237
 
    break;
238
 
  case 'f':
239
 
    opt_force= 1;
240
 
    printf("Not implemented yet\n");
241
 
    break;
242
 
  case 'q':
243
 
    opt_quiet= 1;
244
 
    printf("Not implemented yet\n");
245
 
    break;
246
 
  case 'V':
247
 
    print_version();
248
 
    exit(0);
249
 
  case 't':
250
 
    printf("Not implemented yet\n");
251
 
    break;
252
 
  case 'A':
253
 
    opt_autoincrement= 1;
254
 
    if (argument)
255
 
      new_auto_increment_value= strtoull(argument, NULL, 0);
256
 
    else
257
 
      new_auto_increment_value= 0;
258
 
    break;
259
 
  case '?':
260
 
    usage();
261
 
    exit(0);
262
 
  }
263
 
  return 0;
264
 
}
265
 
 
266
 
static struct my_option my_long_options[] =
267
 
{
268
 
  {"backup", 'b',
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},
273
 
  {"extract-frm", 'e',
274
 
   "Extract the frm file.",
275
 
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
276
 
  {"force", 'f',
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},
279
 
  {"help", '?',
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},
291
 
  {"silent", 's',
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},
294
 
  {"tmpdir", 't',
295
 
   "Path for temporary files.",
296
 
   (char**) &opt_tmpdir,
297
 
   0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
298
 
  {"version", 'V',
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}
302
 
};
303
 
 
304
 
static void usage(void)
305
 
{
306
 
  print_version();
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 \
310
 
       license\n");
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);
315
 
}
316
 
 
317
 
static void print_version(void)
318
 
{
319
 
  printf("%s  Ver %s, for %s (%s)\n", my_progname, SHOW_VERSION,
320
 
         SYSTEM_TYPE, MACHINE_TYPE);
321
 
}
322
 
 
323
 
static void get_options(int *argc, char ***argv)
324
 
{
325
 
  load_defaults("drizzle", load_default_groups, argc, argv);
326
 
  default_argv= *argv;
327
 
 
328
 
  handle_options(argc, argv, my_long_options, get_one_option);
329
 
 
330
 
  if (*argc == 0)
331
 
  {
332
 
    usage();
333
 
    exit(-1);
334
 
  }
335
 
 
336
 
  return;
337
 
} /* get options */