~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/archive/archive_reader.c

  • Committer: Monty Taylor
  • Date: 2008-08-02 00:06:32 UTC
  • mto: (236.1.42 codestyle)
  • mto: This revision was merged to the branch mainline in revision 261.
  • Revision ID: monty@inaugust.com-20080802000632-jsse0zdd9r6ic5ku
Actually turn gettext on...

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (c) 2006 MySQL AB
5
 
 *  Copyright (c) 2009 Sun Microsystems, Inc.
6
 
 *
7
 
 *  This program is free software; you can redistribute it and/or modify
8
 
 *  it under the terms of the GNU General Public License as published by
9
 
 *  the Free Software Foundation; version 2 of the License.
10
 
 *
11
 
 *  This program is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with this program; if not, write to the Free Software
18
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 
 */
20
 
 
21
 
#include "config.h"
22
 
#include <iostream>
23
 
#include <string>
24
 
#include <fstream>
25
 
#include <drizzled/configmake.h>
26
 
using namespace std;
27
 
#include <boost/program_options.hpp>
28
 
namespace po= boost::program_options;
29
1
#include "azio.h"
30
2
#include <string.h>
31
3
#include <assert.h>
32
4
#include <stdio.h>
33
5
#include <stdarg.h>
34
 
#include <fcntl.h>
35
 
#include "drizzled/charset_info.h"
36
 
#include "drizzled/internal/m_string.h"
 
6
#include <mystrings/m_ctype.h>
 
7
#include <mystrings/m_string.h>
 
8
#include <mysys/my_getopt.h>
 
9
 
 
10
#define BUFFER_LEN 1024
 
11
#define ARCHIVE_ROW_HEADER_SIZE 4
37
12
 
38
13
#define SHOW_VERSION "0.1"
39
14
 
40
 
using namespace drizzled;
41
 
 
42
 
string opt_tmpdir;
 
15
static void get_options(int *argc,char * * *argv);
 
16
static void print_version(void);
 
17
static void usage(void);
 
18
static const char *opt_tmpdir;
 
19
static const char *new_auto_increment;
43
20
uint64_t new_auto_increment_value;
44
 
bool opt_check, 
45
 
  opt_force,
46
 
  opt_backup, 
47
 
  opt_extract_table_message, 
48
 
  opt_silent,
49
 
  opt_quiet,
50
 
  opt_quick,
51
 
  opt_autoincrement;
 
21
static const char *load_default_groups[]= { "archive_reader", 0 };
 
22
static char **default_argv;
 
23
int opt_check, opt_force, opt_quiet, opt_backup= 0, opt_extract_frm;
 
24
int opt_autoincrement;
52
25
 
53
26
int main(int argc, char *argv[])
54
27
{
55
 
try
56
 
{
57
 
  po::options_description commandline_options("Options used only in command line");
58
 
  commandline_options.add_options()
59
 
  ("force,f",po::value<bool>(&opt_force)->default_value(false)->zero_tokens(),
60
 
  "Restart with -r if there are any errors in the table")
61
 
  ("help,?","Display this help and exit")
62
 
  ("version,V","Print version and exit")
63
 
  ("no-defaults", po::value<bool>()->default_value(false)->zero_tokens(),
64
 
  "Configuration file defaults are not used if no-defaults is set")
65
 
  ;
66
 
 
67
 
  po::options_description archive_reader_options("Options specific to the archive reader");
68
 
  archive_reader_options.add_options()
69
 
  ("tmpdir,t",po::value<string>(&opt_tmpdir)->default_value(""),
70
 
  "Path for temporary files.") 
71
 
  ("set-auto-increment,A",po::value<uint64_t>(&new_auto_increment_value)->default_value(0),
72
 
  "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.")
73
 
  ("silent,s",po::value<bool>(&opt_silent)->default_value(false)->zero_tokens(),
74
 
  "Only print errors. One can use two -s to make archive_reader very silent.")
75
 
  ("quick,q",po::value<bool>(&opt_quick)->default_value(false)->zero_tokens(),
76
 
  "Faster repair but not modifying the data")
77
 
  ("repair,r",po::value<bool>(&opt_quick)->default_value(false)->zero_tokens(),
78
 
  "Repair a damaged Archive version 3 or above file.")
79
 
  ("back-up,b",po::value<bool>(&opt_backup)->default_value(false)->zero_tokens(),
80
 
  "Make a backup of an archive table.")
81
 
  ("check,c",po::value<bool>(&opt_check)->default_value(false)->zero_tokens(),
82
 
  "Check table for errors")
83
 
  ("extract-table-message,e",po::value<bool>(&opt_extract_table_message)->default_value(false)->zero_tokens(),
84
 
  "Extract the table protobuf message.")
85
 
  ;
86
 
 
87
28
  unsigned int ret;
88
29
  azio_stream reader_handle;
89
30
 
90
 
  std::string system_config_dir_archive_reader(SYSCONFDIR); 
91
 
  system_config_dir_archive_reader.append("/drizzle/archive_reader.cnf");
92
 
 
93
 
  std::string user_config_dir((getenv("XDG_CONFIG_HOME")? getenv("XDG_CONFIG_HOME"):"~/.config"));
94
 
  
95
 
  po::options_description long_options("Allowed Options");
96
 
  long_options.add(commandline_options).add(archive_reader_options);
97
 
 
98
 
  po::variables_map vm;
99
 
  po::store(po::parse_command_line(argc,argv,long_options),vm);
100
 
 
101
 
  if (! vm["no-defaults"].as<bool>())
102
 
  {
103
 
    std::string user_config_dir_archive_reader(user_config_dir);
104
 
    user_config_dir_archive_reader.append("/drizzle/archive_reader.cnf");
105
 
  
106
 
    ifstream user_archive_reader_ifs(user_config_dir_archive_reader.c_str());
107
 
    po::store(parse_config_file(user_archive_reader_ifs, archive_reader_options), vm);
108
 
 
109
 
    ifstream system_archive_reader_ifs(system_config_dir_archive_reader.c_str());
110
 
    store(parse_config_file(system_archive_reader_ifs, archive_reader_options), vm);
111
 
 
112
 
  }
113
 
  po::notify(vm);
114
 
 
115
 
  if (vm.count("force") || vm.count("quiet") || vm.count("tmpdir"))
116
 
    cout << "Not implemented yet";
117
 
 
118
 
  if (vm.count("version"))
119
 
  {
120
 
    printf("%s  Ver %s, for %s-%s (%s)\n", internal::my_progname, SHOW_VERSION,
121
 
        HOST_VENDOR, HOST_OS, HOST_CPU);
122
 
    exit(0);
123
 
  }
124
 
  
125
 
  if (vm.count("set-auto-increment"))
126
 
  {
127
 
    opt_autoincrement= true;
128
 
  }
129
 
 
130
 
  if (vm.count("help") || argc == 0)
131
 
  {
132
 
    printf("%s  Ver %s, for %s-%s (%s)\n", internal::my_progname, SHOW_VERSION,
133
 
        HOST_VENDOR, HOST_OS, HOST_CPU);
134
 
    puts("This software comes with ABSOLUTELY NO WARRANTY. This is free "
135
 
         "software,\n"
136
 
         "and you are welcome to modify and redistribute it under the GPL "
137
 
         "license\n");
138
 
    puts("Read and modify Archive files directly\n");
139
 
    printf("Usage: %s [OPTIONS] file_to_be_looked_at [file_for_backup]\n", internal::my_progname);
140
 
    cout << long_options << endl;    
141
 
    exit(0); 
142
 
  }
143
 
  
 
31
  my_init();
 
32
  MY_INIT(argv[0]);
 
33
  get_options(&argc, &argv);
 
34
 
144
35
  if (argc < 1)
145
36
  {
146
37
    printf("No file specified. \n");
147
 
    return -1;
 
38
    return 0;
148
39
  }
149
40
 
150
 
  if (!(ret= azopen(&reader_handle, argv[0], O_RDONLY, AZ_METHOD_BLOCK)))
 
41
  if (!(ret= azopen(&reader_handle, argv[0], O_RDONLY|O_BINARY, AZ_METHOD_BLOCK)))
151
42
  {
152
43
    printf("Could not open Archive file\n");
153
 
    return -1;
 
44
    return 0;
154
45
  }
155
46
 
156
47
  if (opt_autoincrement)
170
61
      new_auto_increment_value= reader_handle.auto_increment + 1;
171
62
    }
172
63
 
173
 
    if (!(ret= azopen(&writer_handle, argv[0], O_CREAT|O_RDWR,
 
64
    if (!(ret= azopen(&writer_handle, argv[0], O_CREAT|O_RDWR|O_BINARY, 
174
65
                      AZ_METHOD_BLOCK)))
175
66
    {
176
67
      printf("Could not open file for update: %s\n", argv[0]);
196
87
    printf("\tLongest Row %u\n", reader_handle.longest_row);
197
88
    printf("\tShortest Row %u\n", reader_handle.shortest_row);
198
89
    printf("\tState %s\n", ( reader_handle.dirty ? "dirty" : "clean"));
199
 
    printf("\tTable protobuf message stored at %u\n",
200
 
           reader_handle.frm_start_pos);
 
90
    printf("\tFRM stored at %u\n", reader_handle.frm_start_pos);
201
91
    printf("\tComment stored at %u\n", reader_handle.comment_start_pos);
202
92
    printf("\tData starts at %u\n", (unsigned int)reader_handle.start);
203
93
    if (reader_handle.frm_start_pos)
204
 
      printf("\tTable proto message length %u\n", reader_handle.frm_length);
 
94
      printf("\tFRM length %u\n", reader_handle.frm_length);
205
95
    if (reader_handle.comment_start_pos)
206
96
    {
207
97
      char *comment =
208
98
        (char *) malloc(sizeof(char) * reader_handle.comment_length);
209
99
      azread_comment(&reader_handle, comment);
210
 
      printf("\tComment length %u\n\t\t%.*s\n", reader_handle.comment_length,
 
100
      printf("\tComment length %u\n\t\t%.*s\n", reader_handle.comment_length, 
211
101
             reader_handle.comment_length, comment);
212
102
      free(comment);
213
103
    }
222
112
  if (opt_check)
223
113
  {
224
114
    int error;
225
 
    unsigned int row_read;
 
115
    unsigned int read;
226
116
    uint64_t row_count= 0;
227
117
 
228
 
    while ((row_read= azread_row(&reader_handle, &error)))
 
118
    while ((read= azread_row(&reader_handle, &error)))
229
119
    {
230
120
      if (error == Z_STREAM_ERROR)
231
121
      {
235
125
 
236
126
      row_count++;
237
127
 
238
 
      if (row_read > reader_handle.longest_row)
 
128
      if (read > reader_handle.longest_row)
239
129
      {
240
130
        printf("Table is damaged, row %"PRIu64" is invalid\n", row_count);
241
131
        goto end;
248
138
  if (opt_backup)
249
139
  {
250
140
    int error;
251
 
    unsigned int row_read;
 
141
    unsigned int read;
252
142
    uint64_t row_count= 0;
253
143
    char *buffer;
254
144
 
262
152
    }
263
153
 
264
154
 
265
 
    if (!(ret= azopen(&writer_handle, argv[1], O_CREAT|O_RDWR,
 
155
    if (!(ret= azopen(&writer_handle, argv[1], O_CREAT|O_RDWR|O_BINARY,
266
156
                      AZ_METHOD_BLOCK)))
267
157
    {
268
158
      printf("Could not open file for backup: %s\n", argv[1]);
273
163
    if (reader_handle.frm_length)
274
164
    {
275
165
      char *ptr;
276
 
      ptr= (char *)malloc(sizeof(char) * reader_handle.frm_length);
277
 
      if (ptr == NULL)
278
 
      {
279
 
        printf("Could not allocate enough memory\n");
280
 
        goto end;
281
 
      }
 
166
      ptr= (char *)my_malloc(sizeof(char) * reader_handle.frm_length, MYF(0));
282
167
      azread_frm(&reader_handle, ptr);
283
168
      azwrite_frm(&writer_handle, ptr, reader_handle.frm_length);
284
 
      free(ptr);
 
169
      my_free(ptr, MYF(0));
285
170
    }
286
171
 
287
172
    if (reader_handle.comment_length)
288
173
    {
289
174
      char *ptr;
290
 
      ptr= (char *)malloc(sizeof(char) * reader_handle.comment_length);
 
175
      ptr= (char *)my_malloc(sizeof(char) * reader_handle.comment_length, MYF(0));
291
176
      azread_comment(&reader_handle, ptr);
292
177
      azwrite_comment(&writer_handle, ptr, reader_handle.comment_length);
293
 
      free(ptr);
 
178
      my_free(ptr, MYF(0));
294
179
    }
295
180
 
296
 
    while ((row_read= azread_row(&reader_handle, &error)))
 
181
    while ((read= azread_row(&reader_handle, &error)))
297
182
    {
298
183
      if (error == Z_STREAM_ERROR || error)
299
184
      {
302
187
      }
303
188
 
304
189
      /* If we read nothing we are at the end of the file */
305
 
      if (row_read == 0)
 
190
      if (read == 0)
306
191
        break;
307
192
 
308
193
      row_count++;
309
194
 
310
 
      azwrite_row(&writer_handle, reader_handle.row_ptr, row_read);
 
195
      azwrite_row(&writer_handle, reader_handle.row_ptr, read);
311
196
 
312
197
      if (reader_handle.rows == writer_handle.rows)
313
198
        break;
318
203
    azclose(&writer_handle);
319
204
  }
320
205
 
321
 
  if (opt_extract_table_message)
 
206
  if (opt_extract_frm)
322
207
  {
323
 
    int frm_file;
 
208
    File frm_file;
324
209
    char *ptr;
325
 
    frm_file= internal::my_open(argv[1], O_CREAT|O_RDWR, MYF(0));
326
 
    ptr= (char *)malloc(sizeof(char) * reader_handle.frm_length);
327
 
    if (ptr == NULL)
328
 
    {
329
 
      printf("Could not allocate enough memory\n");
330
 
      goto end;
331
 
    }
 
210
    frm_file= my_open(argv[1], O_CREAT|O_RDWR|O_BINARY, MYF(0));
 
211
    ptr= (char *)my_malloc(sizeof(char) * reader_handle.frm_length, MYF(0));
332
212
    azread_frm(&reader_handle, ptr);
333
 
    internal::my_write(frm_file, (unsigned char*) ptr, reader_handle.frm_length, MYF(0));
334
 
    internal::my_close(frm_file, MYF(0));
335
 
    free(ptr);
 
213
    my_write(frm_file, (uchar*) ptr, reader_handle.frm_length, MYF(0));
 
214
    my_close(frm_file, MYF(0));
 
215
    my_free(ptr, MYF(0));
336
216
  }
337
217
 
338
218
end:
339
219
  printf("\n");
340
220
  azclose(&reader_handle);
341
221
 
342
 
  internal::my_end();
343
 
 
344
 
}
345
 
  catch(exception &e)
 
222
  my_end(0);
 
223
  return 0;
 
224
}
 
225
 
 
226
static bool
 
227
get_one_option(int optid,
 
228
               const struct my_option *opt __attribute__((unused)),
 
229
               char *argument)
 
230
{
 
231
  switch (optid) {
 
232
  case 'b':
 
233
    opt_backup= 1;
 
234
    break;
 
235
  case 'c':
 
236
    opt_check= 1;
 
237
    break;
 
238
  case 'e':
 
239
    opt_extract_frm= 1;
 
240
    break;
 
241
  case 'f':
 
242
    opt_force= 1;
 
243
    printf("Not implemented yet\n");
 
244
    break;
 
245
  case 'q':
 
246
    opt_quiet= 1;
 
247
    printf("Not implemented yet\n");
 
248
    break;
 
249
  case 'V':
 
250
    print_version();
 
251
    exit(0);
 
252
  case 't':
 
253
    printf("Not implemented yet\n");
 
254
    break;
 
255
  case 'A':
 
256
    opt_autoincrement= 1;
 
257
    if (argument)
 
258
      new_auto_increment_value= strtoull(argument, NULL, 0);
 
259
    else
 
260
      new_auto_increment_value= 0;
 
261
    break;
 
262
  case '?':
 
263
    usage();
 
264
    exit(0);
 
265
  }
 
266
  return 0;
 
267
}
 
268
 
 
269
static struct my_option my_long_options[] =
 
270
{
 
271
  {"backup", 'b',
 
272
   "Make a backup of an archive table.",
 
273
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
274
  {"check", 'c', "Check table for errors.",
 
275
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
276
  {"extract-frm", 'e',
 
277
   "Extract the frm file.",
 
278
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
279
  {"force", 'f',
 
280
   "Restart with -r if there are any errors in the table.",
 
281
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
282
  {"help", '?',
 
283
   "Display this help and exit.",
 
284
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
285
  {"quick", 'q', "Faster repair by not modifying the data file.",
 
286
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
287
  {"repair", 'r', "Repair a damaged Archive version 3 or above file.",
 
288
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
289
  {"set-auto-increment", 'A',
 
290
   "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.",
 
291
   (char**) &new_auto_increment,
 
292
   (char**) &new_auto_increment,
 
293
   0, GET_ULL, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
294
  {"silent", 's',
 
295
   "Only print errors. One can use two -s to make archive_reader very silent.",
 
296
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
297
  {"tmpdir", 't',
 
298
   "Path for temporary files.",
 
299
   (char**) &opt_tmpdir,
 
300
   0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
301
  {"version", 'V',
 
302
   "Print version and exit.",
 
303
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
304
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
 
305
};
 
306
 
 
307
static void usage(void)
 
308
{
 
309
  print_version();
 
310
  puts("Copyright (C) 2007 MySQL AB");
 
311
  puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\
 
312
       \nand you are welcome to modify and redistribute it under the GPL \
 
313
       license\n");
 
314
  puts("Read and modify Archive files directly\n");
 
315
  printf("Usage: %s [OPTIONS] file_to_be_looked_at [file_for_backup]\n", my_progname);
 
316
  print_defaults("my", load_default_groups);
 
317
  my_print_help(my_long_options);
 
318
}
 
319
 
 
320
static void print_version(void)
 
321
{
 
322
  printf("%s  Ver %s, for %s (%s)\n", my_progname, SHOW_VERSION,
 
323
         SYSTEM_TYPE, MACHINE_TYPE);
 
324
}
 
325
 
 
326
static void get_options(int *argc, char ***argv)
 
327
{
 
328
  load_defaults("my", load_default_groups, argc, argv);
 
329
  default_argv= *argv;
 
330
 
 
331
  handle_options(argc, argv, my_long_options, get_one_option);
 
332
 
 
333
  if (*argc == 0)
346
334
  {
347
 
    cerr<<"Error"<<e.what()<<endl;
 
335
    usage();
 
336
    exit(-1);
348
337
  }
349
 
  return 0;
350
 
}
351
338
 
 
339
  return;
 
340
} /* get options */