1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (c) 2006 MySQL AB
5
* Copyright (c) 2009 Sun Microsystems, Inc.
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.
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.
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
25
#include <drizzled/configmake.h>
27
#include <boost/program_options.hpp>
28
namespace po= boost::program_options;
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>
10
#define BUFFER_LEN 1024
11
#define ARCHIVE_ROW_HEADER_SIZE 4
38
13
#define SHOW_VERSION "0.1"
40
using namespace drizzled;
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;
47
opt_extract_table_message,
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;
53
26
int main(int argc, char *argv[])
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")
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.")
88
29
azio_stream reader_handle;
90
std::string system_config_dir_archive_reader(SYSCONFDIR);
91
system_config_dir_archive_reader.append("/drizzle/archive_reader.cnf");
93
std::string user_config_dir((getenv("XDG_CONFIG_HOME")? getenv("XDG_CONFIG_HOME"):"~/.config"));
95
po::options_description long_options("Allowed Options");
96
long_options.add(commandline_options).add(archive_reader_options);
99
po::store(po::parse_command_line(argc,argv,long_options),vm);
101
if (! vm["no-defaults"].as<bool>())
103
std::string user_config_dir_archive_reader(user_config_dir);
104
user_config_dir_archive_reader.append("/drizzle/archive_reader.cnf");
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);
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);
115
if (vm.count("force") || vm.count("quiet") || vm.count("tmpdir"))
116
cout << "Not implemented yet";
118
if (vm.count("version"))
120
printf("%s Ver %s, for %s-%s (%s)\n", internal::my_progname, SHOW_VERSION,
121
HOST_VENDOR, HOST_OS, HOST_CPU);
125
if (vm.count("set-auto-increment"))
127
opt_autoincrement= true;
130
if (vm.count("help") || argc == 0)
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 "
136
"and you are welcome to modify and redistribute it under the GPL "
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;
33
get_options(&argc, &argv);
146
37
printf("No file specified. \n");
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)))
152
43
printf("Could not open Archive file\n");
156
47
if (opt_autoincrement)
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)
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);
318
203
azclose(&writer_handle);
321
if (opt_extract_table_message)
325
frm_file= internal::my_open(argv[1], O_CREAT|O_RDWR, MYF(0));
326
ptr= (char *)malloc(sizeof(char) * reader_handle.frm_length);
329
printf("Could not allocate enough memory\n");
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));
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));
340
220
azclose(&reader_handle);
227
get_one_option(int optid,
228
const struct my_option *opt __attribute__((unused)),
243
printf("Not implemented yet\n");
247
printf("Not implemented yet\n");
253
printf("Not implemented yet\n");
256
opt_autoincrement= 1;
258
new_auto_increment_value= strtoull(argument, NULL, 0);
260
new_auto_increment_value= 0;
269
static struct my_option my_long_options[] =
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},
277
"Extract the frm file.",
278
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
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},
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},
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},
298
"Path for temporary files.",
299
(char**) &opt_tmpdir,
300
0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
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}
307
static void usage(void)
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 \
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);
320
static void print_version(void)
322
printf("%s Ver %s, for %s (%s)\n", my_progname, SHOW_VERSION,
323
SYSTEM_TYPE, MACHINE_TYPE);
326
static void get_options(int *argc, char ***argv)
328
load_defaults("my", load_default_groups, argc, argv);
331
handle_options(argc, argv, my_long_options, get_one_option);
347
cerr<<"Error"<<e.what()<<endl;