thin-provisioning-tools/caching/cache_restore.cc

145 lines
3.2 KiB
C++
Raw Normal View History

#include "version.h"
2013-09-11 16:10:46 +05:30
#include "caching/metadata.h"
2013-09-12 17:03:32 +05:30
#include "caching/restore_emitter.h"
2013-09-11 16:10:46 +05:30
#include "caching/xml_format.h"
#include "persistent-data/file_utils.h"
#include <boost/lexical_cast.hpp>
2013-09-11 16:10:46 +05:30
#include <fstream>
#include <getopt.h>
#include <iostream>
#include <libgen.h>
#include <string>
using namespace boost;
2013-09-11 16:10:46 +05:30
using namespace caching;
using namespace persistent_data;
using namespace std;
//----------------------------------------------------------------
namespace {
struct flags {
flags()
: metadata_version(1),
2013-10-29 18:16:23 +05:30
override_metadata_version(false),
clean_shutdown(true) {
}
optional<string> input;
optional<string> output;
2013-10-29 18:16:23 +05:30
uint32_t metadata_version;
bool override_metadata_version;
2013-10-29 18:16:23 +05:30
bool clean_shutdown;
};
int restore(flags const &fs) {
2013-09-11 16:10:46 +05:30
try {
block_manager<>::ptr bm = open_bm(*fs.output, block_manager<>::READ_WRITE);
2013-09-11 16:10:46 +05:30
metadata::ptr md(new metadata(bm, metadata::CREATE));
2013-10-29 18:16:23 +05:30
emitter::ptr restorer = create_restore_emitter(md, fs.clean_shutdown);
2013-09-11 16:10:46 +05:30
if (fs.override_metadata_version) {
cerr << "overriding" << endl;
md->sb_.version = fs.metadata_version;
}
check_file_exists(*fs.input);
ifstream in(fs.input->c_str(), ifstream::in);
2013-09-11 16:10:46 +05:30
parse_xml(in, restorer);
} 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
<< " {-V|--version}" << endl
<< endl
2013-10-29 18:16:23 +05:30
<< " {--debug-override-metadata-version} <integer>" << endl
<< " {--omit-clean-shutdown}" << endl;
}
}
int main(int argc, char **argv)
{
int c;
flags fs;
char const *prog_name = basename(argv[0]);
char const *short_opts = "hi:o:V";
option const long_opts[] = {
{ "debug-override-metadata-version", required_argument, NULL, 0 },
2013-10-29 18:16:23 +05:30
{ "omit-clean-shutdown", no_argument, NULL, 1 },
{ "help", no_argument, NULL, 'h'},
{ "input", required_argument, NULL, 'i' },
{ "output", required_argument, NULL, 'o'},
{ "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 0:
fs.metadata_version = lexical_cast<uint32_t>(optarg);
fs.override_metadata_version = true;
break;
2013-10-29 18:16:23 +05:30
case 1:
fs.clean_shutdown = false;
break;
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 '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);
}
//----------------------------------------------------------------