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
#include <boost/scoped_ptr.hpp>
29
namespace po= boost::program_options;
37
#include <drizzled/charset_info.h>
38
#include <drizzled/internal/m_string.h>
9
#include <drizzle_version.h>
11
#define BUFFER_LEN 1024
12
#define ARCHIVE_ROW_HEADER_SIZE 4
40
14
#define SHOW_VERSION "0.1"
42
using namespace drizzled;
16
static void get_options(int *argc,char * * *argv);
17
static void print_version(void);
18
static void usage(void);
19
static const char *opt_tmpdir;
20
static const char *new_auto_increment;
45
21
uint64_t new_auto_increment_value;
49
opt_extract_table_message,
22
static const char *load_default_groups[]= { "archive_reader", 0 };
23
static char **default_argv;
24
int opt_check, opt_force, opt_quiet, opt_backup= 0, opt_extract_frm;
25
int opt_autoincrement;
55
27
int main(int argc, char *argv[])
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")
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.")
90
boost::scoped_ptr<azio_stream> reader_handle_ap(new azio_stream);
91
azio_stream &reader_handle= *reader_handle_ap.get();
93
std::string system_config_dir_archive_reader(SYSCONFDIR);
94
system_config_dir_archive_reader.append("/drizzle/archive_reader.cnf");
96
std::string user_config_dir((getenv("XDG_CONFIG_HOME")? getenv("XDG_CONFIG_HOME"):"~/.config"));
98
if (user_config_dir.compare(0, 2, "~/") == 0)
101
homedir= getenv("HOME");
103
user_config_dir.replace(0, 1, homedir);
106
po::options_description long_options("Allowed Options");
107
long_options.add(commandline_options).add(archive_reader_options);
109
po::variables_map vm;
110
po::store(po::parse_command_line(argc,argv,long_options),vm);
112
if (! vm["no-defaults"].as<bool>())
114
std::string user_config_dir_archive_reader(user_config_dir);
115
user_config_dir_archive_reader.append("/drizzle/archive_reader.cnf");
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);
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);
126
if (vm.count("force") || vm.count("quiet") || vm.count("tmpdir"))
127
cout << "Not implemented yet";
129
if (vm.count("version"))
131
printf("%s Ver %s, for %s-%s (%s)\n", internal::my_progname, SHOW_VERSION,
132
HOST_VENDOR, HOST_OS, HOST_CPU);
136
if (vm.count("set-auto-increment"))
138
opt_autoincrement= true;
141
if (vm.count("help") || argc == 0)
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 "
147
"and you are welcome to modify and redistribute it under the GPL "
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;
30
azio_stream reader_handle;
34
get_options(&argc, &argv);
157
38
printf("No file specified. \n");
161
if (!(ret= azopen(&reader_handle, argv[0], O_RDONLY, AZ_METHOD_BLOCK)))
42
if (!(ret= azopen(&reader_handle, argv[0], O_RDONLY|O_BINARY, AZ_METHOD_BLOCK)))
163
44
printf("Could not open Archive file\n");
167
48
if (opt_autoincrement)
169
boost::scoped_ptr<azio_stream> writer_handle_ap(new azio_stream);
170
azio_stream &writer_handle= *writer_handle_ap.get();
50
azio_stream writer_handle;
172
52
if (new_auto_increment_value)
331
204
azclose(&writer_handle);
334
if (opt_extract_table_message)
338
frm_file= internal::my_open(argv[1], O_CREAT|O_RDWR, MYF(0));
339
ptr= (char *)malloc(sizeof(char) * reader_handle.frm_length);
342
printf("Could not allocate enough memory\n");
211
frm_file= my_open(argv[1], O_CREAT|O_RDWR|O_BINARY, MYF(0));
212
ptr= (char *)my_malloc(sizeof(char) * reader_handle.frm_length, MYF(0));
345
213
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));
214
my_write(frm_file, (uchar*) ptr, reader_handle.frm_length, MYF(0));
215
my_close(frm_file, MYF(0));
216
my_free(ptr, MYF(0));
353
221
azclose(&reader_handle);
228
get_one_option(int optid,
229
const struct my_option *opt __attribute__((unused)),
244
printf("Not implemented yet\n");
248
printf("Not implemented yet\n");
254
printf("Not implemented yet\n");
257
opt_autoincrement= 1;
259
new_auto_increment_value= strtoull(argument, NULL, 0);
261
new_auto_increment_value= 0;
267
if (argument == disabled_my_option)
273
DBUG_PUSH(argument ? argument : "d:t:o,/tmp/archive_reader.trace");
280
static struct my_option my_long_options[] =
283
"Make a backup of an archive table.",
284
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
285
{"check", 'c', "Check table for errors.",
286
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
289
"Output debug log. Often this is 'd:t:o,filename'.",
290
0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
293
"Extract the frm file.",
294
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
296
"Restart with -r if there are any errors in the table.",
297
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
299
"Display this help and exit.",
300
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
301
{"quick", 'q', "Faster repair by not modifying the data file.",
302
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
303
{"repair", 'r', "Repair a damaged Archive version 3 or above file.",
304
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
305
{"set-auto-increment", 'A',
306
"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.",
307
(char**) &new_auto_increment,
308
(char**) &new_auto_increment,
309
0, GET_ULL, OPT_ARG, 0, 0, 0, 0, 0, 0},
311
"Only print errors. One can use two -s to make archive_reader very silent.",
312
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
314
"Path for temporary files.",
315
(char**) &opt_tmpdir,
316
0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
318
"Print version and exit.",
319
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
320
{ 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
323
static void usage(void)
326
puts("Copyright (C) 2007 MySQL AB");
327
puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\
328
\nand you are welcome to modify and redistribute it under the GPL \
330
puts("Read and modify Archive files directly\n");
331
printf("Usage: %s [OPTIONS] file_to_be_looked_at [file_for_backup]\n", my_progname);
332
print_defaults("my", load_default_groups);
333
my_print_help(my_long_options);
336
static void print_version(void)
338
printf("%s Ver %s Distrib %s, for %s (%s)\n", my_progname, SHOW_VERSION,
339
MYSQL_SERVER_VERSION, SYSTEM_TYPE, MACHINE_TYPE);
342
static void get_options(int *argc, char ***argv)
344
load_defaults("my", load_default_groups, argc, argv);
347
handle_options(argc, argv, my_long_options, get_one_option);
360
cerr<<"Error"<<e.what()<<endl;