thin-provisioning-tools/era/era_restore.cc

124 lines
2.5 KiB
C++
Raw Normal View History

2014-08-07 20:13:01 +05:30
#include "version.h"
#include "era/metadata.h"
#include "era/restore_emitter.h"
#include "era/xml_format.h"
#include "persistent-data/file_utils.h"
#include <boost/lexical_cast.hpp>
#include <fstream>
#include <getopt.h>
#include <iostream>
#include <libgen.h>
#include <string>
using namespace boost;
2014-08-12 14:50:29 +05:30
using namespace era;
2014-08-07 20:13:01 +05:30
using namespace persistent_data;
using namespace std;
//----------------------------------------------------------------
namespace {
struct flags {
flags()
: quiet(false) {
}
2014-08-07 20:13:01 +05:30
optional<string> input;
optional<string> output;
bool quiet;
2014-08-07 20:13:01 +05:30
};
int restore(flags const &fs, bool quiet) {
2014-08-07 20:13:01 +05:30
try {
block_manager<>::ptr bm = open_bm(*fs.output, block_manager<>::READ_WRITE);
metadata::ptr md(new metadata(bm, metadata::CREATE));
2014-08-12 14:50:29 +05:30
emitter::ptr restorer = create_restore_emitter(*md);
2014-08-07 20:13:01 +05:30
parse_xml(*fs.input, restorer, fs.quiet);
2014-08-07 20:13:01 +05:30
} catch (std::exception &e) {
cerr << e.what() << endl;
return 1;
}
return 0;
}
void usage(ostream &out, string const &cmd) {
out << "Usage: " << cmd << " [options]" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-i|--input} <input xml file>" << endl
<< " {-o|--output} <output device or file>" << endl
<< " {-q|--quiet}" << endl
2014-08-12 14:50:29 +05:30
<< " {-V|--version}" << endl;
2014-08-07 20:13:01 +05:30
}
}
int main(int argc, char **argv)
{
int c;
flags fs;
char const *prog_name = basename(argv[0]);
char const *short_opts = "hi:o:qV";
2014-08-07 20:13:01 +05:30
option const long_opts[] = {
{ "help", no_argument, NULL, 'h'},
{ "input", required_argument, NULL, 'i' },
{ "output", required_argument, NULL, 'o'},
{ "quiet", no_argument, NULL, 'q'},
2014-08-07 20:13:01 +05:30
{ "version", no_argument, NULL, 'V'},
{ NULL, no_argument, NULL, 0 }
};
while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
switch(c) {
case 'h':
usage(cout, prog_name);
return 0;
case 'i':
fs.input = optional<string>(string(optarg));
break;
case 'o':
fs.output = optional<string>(string(optarg));
break;
case 'q':
fs.quiet = true;
break;
2014-08-07 20:13:01 +05:30
case 'V':
cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
return 0;
default:
usage(cerr, prog_name);
return 1;
}
}
if (argc != optind) {
usage(cerr, prog_name);
return 1;
}
if (!fs.input) {
cerr << "No input file provided." << endl << endl;
usage(cerr, prog_name);
return 1;
}
if (!fs.output) {
cerr << "No output file provided." << endl << endl;
usage(cerr, prog_name);
return 1;
}
return restore(fs, fs.quiet);
2014-08-07 20:13:01 +05:30
}
//----------------------------------------------------------------