# btardump.cc

    00001 ///
    00002 /// \file       tardump.cc
    00003 ///             Utility to dump tarball backup records to stdout.
    00004 ///
    00005
    00006 /*
    00007     Copyright (C) 2010-2011, Net Direct Inc. (http://www.netdirect.ca/)
    00008
    00009     This program is free software; you can redistribute it and/or modify
    00010     it under the terms of the GNU General Public License as published by
    00011     the Free Software Foundation; either version 2 of the License, or
    00012     (at your option) any later version.
    00013
    00014     This program is distributed in the hope that it will be useful,
    00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00017
    00018     See the GNU General Public License in the COPYING file at the
    00019     root directory of this project for more details.
    00020 */
    00021
    00022 #include <barry/barry.h>
    00023 #ifdef __BARRY_SYNC_MODE__
    00024 #include <barry/barrysync.h>
    00025 #include "mimedump.h"
    00026 #endif
    00027 #include <barry/barrybackup.h>
    00028 #include <iostream>
    00029 #include <iomanip>
    00030 #include <getopt.h>
    00031
    00032 using namespace std;
    00033 using namespace Barry;
    00034
    00035 void Usage()
    00036 {
    00037    int major, minor;
    00038    const char *Version = Barry::Version(major, minor);
    00039
    00040    cerr
    00041    << "btardump - Command line parser for Barry backup files\n"
    00042    << "           Copyright 2010-2011, Net Direct Inc. (http://www.netdirect.ca/)\n"
    00043    << "           Using: " << Version << "\n"
    00044    << "\n"
    00045    << "   -d db     Name of database to dump.  Can be used multiple times\n"
    00046    << "             to parse multiple databases at once.  If not specified\n"
    00047    << "             at all, all available databases from the backup are\n"
    00048    << "             dumped.\n"
    00049    << "   -h        This help\n"
    00050    << "   -i cs     International charset for string conversions\n"
    00051    << "             Valid values here are available with 'iconv --list'\n"
    00052 #ifdef __BARRY_SYNC_MODE__
    00053    << "   -V        Dump records using MIME vformats where possible\n"
    00054 #endif
    00055    << "\n"
    00056    << "   [files...] Backup file(s), created by btool or the backup GUI.\n"
    00057    << endl;
    00058 }
    00059
    00060 class MyAllRecordDumpStore : public AllRecordStore
    00061 {
    00062         bool m_vformat_mode;
    00063         std::ostream &m_os;
    00064
    00065 public:
    00066         explicit MyAllRecordDumpStore(std::ostream &os, bool vformat_mode=false)
    00067                 : m_vformat_mode(vformat_mode)
    00068                 , m_os(os)
    00069         {
    00070         }
    00071
    00072 #undef HANDLE_PARSER
    00073
    00074 #ifdef __BARRY_SYNC_MODE__
    00075
    00076 #define HANDLE_PARSER(tname) \
    00077         void operator() (const Barry::tname &r) \
    00078         { \
    00079                 if( m_vformat_mode ) \
    00080                         MimeDump<tname>::Dump(m_os, r); \
    00081                 else \
    00082                         m_os << r << std::endl; \
    00083         }
    00084
    00085 #else
    00086
    00087 #define HANDLE_PARSER(tname) \
    00088         void operator() (const Barry::tname &r) \
    00089         { \
    00090                 m_os << r << std::endl; \
    00091         }
    00092
    00093 #endif
    00094
    00095         ALL_KNOWN_PARSER_TYPES
    00096 };
    00097
    00098 int main(int argc, char *argv[])
    00099 {
    00100         try {
    00101                 bool vformat_mode = false;
    00102
    00103                 vector<string> db_names;
    00104                 vector<string> backup_files;
    00105                 string iconvCharset;
    00106
    00107                 // process command line options
    00108                 for(;;) {
    00109                         int cmd = getopt(argc, argv, "d:hi:V");
    00110                         if( cmd == -1 )
    00111                                 break;
    00112
    00113                         switch( cmd )
    00114                         {
    00115                         case 'd':       // show dbname
    00116                                 db_names.push_back(string(optarg));
    00117                                 break;
    00118
    00119                         case 'V':       // vformat MIME mode
    00120 #ifdef __BARRY_SYNC_MODE__
    00121                                 vformat_mode = true;
    00122 #else
    00123                                 cerr << "-V option not supported - no Sync "
    00124                                         "library support available\n";
    00125                                 return 1;
    00126 #endif
    00127                                 break;
    00128
    00129                         case 'i':       // international charset (iconv)
    00130                                 iconvCharset = optarg;
    00131                                 break;
    00132
    00133                         case 'h':       // help
    00134                         default:
    00135                                 Usage();
    00136                                 return 0;
    00137                         }
    00138                 }
    00139
    00140                 // grab all backup filenames
    00141                 while( optind < argc ) {
    00142                         backup_files.push_back(string(argv[optind++]));
    00143                 }
    00144
    00145                 if( backup_files.size() == 0 ) {
    00146                         Usage();
    00147                         return 0;
    00148                 }
    00149
    00150
    00151
    00152                 Barry::Init();
    00153
    00154                 // Create an IConverter object if needed
    00155                 auto_ptr<IConverter> ic;
    00156                 if( iconvCharset.size() ) {
    00157                         ic.reset( new IConverter(iconvCharset.c_str(), true) );
    00158                 }
    00159
    00160                 // create the parser, and use stdout dump objects for output
    00161                 AllRecordParser parser(cout,
    00162                         new HexDumpParser(cout),
    00163                         new MyAllRecordDumpStore(cout, vformat_mode));
    00164
    00165                 for( size_t i = 0; i < backup_files.size(); i++ ) {
    00166
    00167                         cout << "Reading file: " << backup_files[i] << endl;
    00168
    00169                         Restore builder(backup_files[i]);
    00170
    00171                         // add desired database names
    00172                         for( size_t j = 0; j < db_names.size(); j++ ) {
    00173                                 builder.AddDB(db_names[i]);
    00174                         }
    00175
    00176                         // create the pipe to connect builder to parser and
    00177                         // move the data
    00178                         Pipe pipe(builder);
    00179                         pipe.PumpFile(parser, ic.get());
    00180                 }
    00181
    00182         }
    00183         catch( exception &e ) {
    00184                 cerr << e.what() << endl;
    00185                 return 1;
    00186         }
    00187
    00188         return 0;
    00189 }
    00190