~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/archive/archive_reader.c

  • Committer: Brian Aker
  • Date: 2009-02-21 00:18:15 UTC
  • Revision ID: brian@tangent.org-20090221001815-x20e8h71e984lvs1
Completion (?) of uint conversion.

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
 
#include <boost/scoped_ptr.hpp>
29
 
namespace po= boost::program_options;
30
1
#include "azio.h"
31
2
#include <string.h>
32
3
#include <assert.h>
33
4
#include <stdio.h>
34
5
#include <stdarg.h>
35
 
#include <fcntl.h>
36
 
#include <memory>
37
 
#include "drizzled/charset_info.h"
38
 
#include "drizzled/internal/m_string.h"
 
6
#include <mystrings/m_ctype.h>
 
7
#include <mystrings/m_string.h>
 
8
#include <mysys/my_getopt.h>
39
9
 
40
10
#define SHOW_VERSION "0.1"
41
11
 
42
 
using namespace drizzled;
43
 
 
44
 
string opt_tmpdir;
 
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;
45
17
uint64_t new_auto_increment_value;
46
 
bool opt_check, 
47
 
  opt_force,
48
 
  opt_backup, 
49
 
  opt_extract_table_message, 
50
 
  opt_silent,
51
 
  opt_quiet,
52
 
  opt_quick,
53
 
  opt_autoincrement;
 
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;
54
22
 
55
23
int main(int argc, char *argv[])
56
24
{
57
 
try
58
 
{
59
 
  po::options_description commandline_options("Options used only in command line");
60
 
  commandline_options.add_options()
61
 
  ("force,f",po::value<bool>(&opt_force)->default_value(false)->zero_tokens(),
62
 
  "Restart with -r if there are any errors in the table")
63
 
  ("help,?","Display this help and exit")
64
 
  ("version,V","Print version and exit")
65
 
  ("no-defaults", po::value<bool>()->default_value(false)->zero_tokens(),
66
 
  "Configuration file defaults are not used if no-defaults is set")
67
 
  ;
68
 
 
69
 
  po::options_description archive_reader_options("Options specific to the archive reader");
70
 
  archive_reader_options.add_options()
71
 
  ("tmpdir,t",po::value<string>(&opt_tmpdir)->default_value(""),
72
 
  "Path for temporary files.") 
73
 
  ("set-auto-increment,A",po::value<uint64_t>(&new_auto_increment_value)->default_value(0),
74
 
  "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.")
75
 
  ("silent,s",po::value<bool>(&opt_silent)->default_value(false)->zero_tokens(),
76
 
  "Only print errors. One can use two -s to make archive_reader very silent.")
77
 
  ("quick,q",po::value<bool>(&opt_quick)->default_value(false)->zero_tokens(),
78
 
  "Faster repair but not modifying the data")
79
 
  ("repair,r",po::value<bool>(&opt_quick)->default_value(false)->zero_tokens(),
80
 
  "Repair a damaged Archive version 3 or above file.")
81
 
  ("back-up,b",po::value<bool>(&opt_backup)->default_value(false)->zero_tokens(),
82
 
  "Make a backup of an archive table.")
83
 
  ("check,c",po::value<bool>(&opt_check)->default_value(false)->zero_tokens(),
84
 
  "Check table for errors")
85
 
  ("extract-table-message,e",po::value<bool>(&opt_extract_table_message)->default_value(false)->zero_tokens(),
86
 
  "Extract the table protobuf message.")
87
 
  ;
88
 
 
89
25
  unsigned int ret;
90
 
  boost::scoped_ptr<azio_stream> reader_handle_ap(new azio_stream);
91
 
  azio_stream &reader_handle= *reader_handle_ap.get();
92
 
 
93
 
  std::string system_config_dir_archive_reader(SYSCONFDIR); 
94
 
  system_config_dir_archive_reader.append("/drizzle/archive_reader.cnf");
95
 
 
96
 
  std::string user_config_dir((getenv("XDG_CONFIG_HOME")? getenv("XDG_CONFIG_HOME"):"~/.config"));
97
 
 
98
 
  if (user_config_dir.compare(0, 2, "~/") == 0)
99
 
  {
100
 
    char *homedir;
101
 
    homedir= getenv("HOME");
102
 
    if (homedir != NULL)
103
 
      user_config_dir.replace(0, 1, homedir);
104
 
  }
105
 
  
106
 
  po::options_description long_options("Allowed Options");
107
 
  long_options.add(commandline_options).add(archive_reader_options);
108
 
 
109
 
  po::variables_map vm;
110
 
  po::store(po::parse_command_line(argc,argv,long_options),vm);
111
 
 
112
 
  if (! vm["no-defaults"].as<bool>())
113
 
  {
114
 
    std::string user_config_dir_archive_reader(user_config_dir);
115
 
    user_config_dir_archive_reader.append("/drizzle/archive_reader.cnf");
116
 
  
117
 
    ifstream user_archive_reader_ifs(user_config_dir_archive_reader.c_str());
118
 
    po::store(parse_config_file(user_archive_reader_ifs, archive_reader_options), vm);
119
 
 
120
 
    ifstream system_archive_reader_ifs(system_config_dir_archive_reader.c_str());
121
 
    store(parse_config_file(system_archive_reader_ifs, archive_reader_options), vm);
122
 
 
123
 
  }
124
 
  po::notify(vm);
125
 
 
126
 
  if (vm.count("force") || vm.count("quiet") || vm.count("tmpdir"))
127
 
    cout << "Not implemented yet";
128
 
 
129
 
  if (vm.count("version"))
130
 
  {
131
 
    printf("%s  Ver %s, for %s-%s (%s)\n", internal::my_progname, SHOW_VERSION,
132
 
        HOST_VENDOR, HOST_OS, HOST_CPU);
133
 
    exit(0);
134
 
  }
135
 
  
136
 
  if (vm.count("set-auto-increment"))
137
 
  {
138
 
    opt_autoincrement= true;
139
 
  }
140
 
 
141
 
  if (vm.count("help") || argc == 0)
142
 
  {
143
 
    printf("%s  Ver %s, for %s-%s (%s)\n", internal::my_progname, SHOW_VERSION,
144
 
        HOST_VENDOR, HOST_OS, HOST_CPU);
145
 
    puts("This software comes with ABSOLUTELY NO WARRANTY. This is free "
146
 
         "software,\n"
147
 
         "and you are welcome to modify and redistribute it under the GPL "
148
 
         "license\n");
149
 
    puts("Read and modify Archive files directly\n");
150
 
    printf("Usage: %s [OPTIONS] file_to_be_looked_at [file_for_backup]\n", internal::my_progname);
151
 
    cout << long_options << endl;    
152
 
    exit(0); 
153
 
  }
154
 
  
 
26
  azio_stream reader_handle;
 
27
 
 
28
  my_init();
 
29
  MY_INIT(argv[0]);
 
30
  get_options(&argc, &argv);
 
31
 
155
32
  if (argc < 1)
156
33
  {
157
34
    printf("No file specified. \n");
158
 
    return -1;
 
35
    return 0;
159
36
  }
160
37
 
161
38
  if (!(ret= azopen(&reader_handle, argv[0], O_RDONLY, AZ_METHOD_BLOCK)))
162
39
  {
163
40
    printf("Could not open Archive file\n");
164
 
    return -1;
 
41
    return 0;
165
42
  }
166
43
 
167
44
  if (opt_autoincrement)
168
45
  {
169
 
    boost::scoped_ptr<azio_stream> writer_handle_ap(new azio_stream);
170
 
    azio_stream &writer_handle= *writer_handle_ap.get();
 
46
    azio_stream writer_handle;
171
47
 
172
48
    if (new_auto_increment_value)
173
49
    {
208
84
    printf("\tLongest Row %u\n", reader_handle.longest_row);
209
85
    printf("\tShortest Row %u\n", reader_handle.shortest_row);
210
86
    printf("\tState %s\n", ( reader_handle.dirty ? "dirty" : "clean"));
211
 
    printf("\tTable protobuf message stored at %u\n",
212
 
           reader_handle.frm_start_pos);
 
87
    printf("\tFRM stored at %u\n", reader_handle.frm_start_pos);
213
88
    printf("\tComment stored at %u\n", reader_handle.comment_start_pos);
214
89
    printf("\tData starts at %u\n", (unsigned int)reader_handle.start);
215
90
    if (reader_handle.frm_start_pos)
216
 
      printf("\tTable proto message length %u\n", reader_handle.frm_length);
 
91
      printf("\tFRM length %u\n", reader_handle.frm_length);
217
92
    if (reader_handle.comment_start_pos)
218
93
    {
219
94
      char *comment =
264
139
    uint64_t row_count= 0;
265
140
    char *buffer;
266
141
 
267
 
    boost::scoped_ptr<azio_stream> writer_handle_ap(new azio_stream);
268
 
    azio_stream &writer_handle= *writer_handle_ap.get();
 
142
    azio_stream writer_handle;
269
143
 
270
144
    buffer= (char *)malloc(reader_handle.longest_row);
271
145
    if (buffer == NULL)
331
205
    azclose(&writer_handle);
332
206
  }
333
207
 
334
 
  if (opt_extract_table_message)
 
208
  if (opt_extract_frm)
335
209
  {
336
 
    int frm_file;
 
210
    File frm_file;
337
211
    char *ptr;
338
 
    frm_file= internal::my_open(argv[1], O_CREAT|O_RDWR, MYF(0));
 
212
    frm_file= my_open(argv[1], O_CREAT|O_RDWR, MYF(0));
339
213
    ptr= (char *)malloc(sizeof(char) * reader_handle.frm_length);
340
214
    if (ptr == NULL)
341
215
    {
343
217
      goto end;
344
218
    }
345
219
    azread_frm(&reader_handle, ptr);
346
 
    internal::my_write(frm_file, (unsigned char*) ptr, reader_handle.frm_length, MYF(0));
347
 
    internal::my_close(frm_file, MYF(0));
 
220
    my_write(frm_file, (unsigned char*) ptr, reader_handle.frm_length, MYF(0));
 
221
    my_close(frm_file, MYF(0));
348
222
    free(ptr);
349
223
  }
350
224
 
352
226
  printf("\n");
353
227
  azclose(&reader_handle);
354
228
 
355
 
  internal::my_end();
356
 
 
357
 
}
358
 
  catch(exception &e)
 
229
  my_end(0);
 
230
  return 0;
 
231
}
 
232
 
 
233
static bool
 
234
get_one_option(int optid, const struct my_option *opt, char *argument)
 
235
{
 
236
  (void)opt;
 
237
  switch (optid) {
 
238
  case 'b':
 
239
    opt_backup= 1;
 
240
    break;
 
241
  case 'c':
 
242
    opt_check= 1;
 
243
    break;
 
244
  case 'e':
 
245
    opt_extract_frm= 1;
 
246
    break;
 
247
  case 'f':
 
248
    opt_force= 1;
 
249
    printf("Not implemented yet\n");
 
250
    break;
 
251
  case 'q':
 
252
    opt_quiet= 1;
 
253
    printf("Not implemented yet\n");
 
254
    break;
 
255
  case 'V':
 
256
    print_version();
 
257
    exit(0);
 
258
  case 't':
 
259
    printf("Not implemented yet\n");
 
260
    break;
 
261
  case 'A':
 
262
    opt_autoincrement= 1;
 
263
    if (argument)
 
264
      new_auto_increment_value= strtoull(argument, NULL, 0);
 
265
    else
 
266
      new_auto_increment_value= 0;
 
267
    break;
 
268
  case '?':
 
269
    usage();
 
270
    exit(0);
 
271
  }
 
272
  return 0;
 
273
}
 
274
 
 
275
static struct my_option my_long_options[] =
 
276
{
 
277
  {"backup", 'b',
 
278
   "Make a backup of an archive table.",
 
279
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
280
  {"check", 'c', "Check table for errors.",
 
281
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
282
  {"extract-frm", 'e',
 
283
   "Extract the frm file.",
 
284
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
285
  {"force", 'f',
 
286
   "Restart with -r if there are any errors in the table.",
 
287
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
288
  {"help", '?',
 
289
   "Display this help and exit.",
 
290
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
291
  {"quick", 'q', "Faster repair by not modifying the data file.",
 
292
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
293
  {"repair", 'r', "Repair a damaged Archive version 3 or above file.",
 
294
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
295
  {"set-auto-increment", 'A',
 
296
   "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.",
 
297
   (char**) &new_auto_increment,
 
298
   (char**) &new_auto_increment,
 
299
   0, GET_ULL, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
300
  {"silent", 's',
 
301
   "Only print errors. One can use two -s to make archive_reader very silent.",
 
302
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
303
  {"tmpdir", 't',
 
304
   "Path for temporary files.",
 
305
   (char**) &opt_tmpdir,
 
306
   0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
307
  {"version", 'V',
 
308
   "Print version and exit.",
 
309
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
310
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
 
311
};
 
312
 
 
313
static void usage(void)
 
314
{
 
315
  print_version();
 
316
  puts("Copyright (C) 2007 MySQL AB");
 
317
  puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\
 
318
       \nand you are welcome to modify and redistribute it under the GPL \
 
319
       license\n");
 
320
  puts("Read and modify Archive files directly\n");
 
321
  printf("Usage: %s [OPTIONS] file_to_be_looked_at [file_for_backup]\n", my_progname);
 
322
  print_defaults("drizzle", load_default_groups);
 
323
  my_print_help(my_long_options);
 
324
}
 
325
 
 
326
static void print_version(void)
 
327
{
 
328
  printf("%s  Ver %s, for %s (%s)\n", my_progname, SHOW_VERSION,
 
329
         SYSTEM_TYPE, MACHINE_TYPE);
 
330
}
 
331
 
 
332
static void get_options(int *argc, char ***argv)
 
333
{
 
334
  load_defaults("drizzle", load_default_groups, argc, argv);
 
335
  default_argv= *argv;
 
336
 
 
337
  handle_options(argc, argv, my_long_options, get_one_option);
 
338
 
 
339
  if (*argc == 0)
359
340
  {
360
 
    cerr<<"Error"<<e.what()<<endl;
 
341
    usage();
 
342
    exit(-1);
361
343
  }
362
 
  return 0;
363
 
}
364
344
 
 
345
  return;
 
346
} /* get options */