thin-provisioning-tools/caching/cache_dump.cc

121 lines
2.5 KiB
C++
Raw Normal View History

2013-09-11 16:10:46 +05:30
#include <fstream>
2013-08-19 17:10:03 +05:30
#include <getopt.h>
#include <libgen.h>
#include <iostream>
#include "version.h"
2014-08-27 18:31:31 +05:30
#include "caching/commands.h"
#include "caching/mapping_array.h"
#include "caching/metadata.h"
#include "caching/metadata_dump.h"
2013-09-11 16:10:46 +05:30
#include "caching/xml_format.h"
#include "persistent-data/file_utils.h"
2013-08-19 17:10:03 +05:30
using namespace std;
2013-09-11 16:10:46 +05:30
using namespace caching;
2013-08-19 17:10:03 +05:30
//----------------------------------------------------------------
namespace {
struct flags {
flags()
: repair_(false) {
}
bool repair_;
};
//--------------------------------
2013-09-11 16:10:46 +05:30
string const STDOUT_PATH("-");
bool want_stdout(string const &output) {
return output == STDOUT_PATH;
}
int dump(string const &dev, string const &output, flags const &fs) {
try {
block_manager<>::ptr bm = open_bm(dev, block_manager<>::READ_ONLY);
metadata::ptr md(new metadata(bm, metadata::OPEN));
if (want_stdout(output)) {
emitter::ptr e = create_xml_emitter(cout);
metadata_dump(md, e, fs.repair_);
} else {
ofstream out(output.c_str());
emitter::ptr e = create_xml_emitter(out);
metadata_dump(md, e, fs.repair_);
}
} catch (std::exception &e) {
cerr << e.what() << endl;
return 1;
}
return 0;
2013-09-11 16:10:46 +05:30
}
2013-08-19 17:10:03 +05:30
void usage(ostream &out, string const &cmd) {
out << "Usage: " << cmd << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
2013-09-11 16:10:46 +05:30
<< " {-o <xml file>}" << endl
<< " {-V|--version}" << endl
<< " {--repair}" << endl;
2013-08-19 17:10:03 +05:30
}
}
//----------------------------------------------------------------
2014-08-27 18:31:31 +05:30
int cache_dump_main(int argc, char **argv)
2013-08-19 17:10:03 +05:30
{
int c;
flags fs;
2013-09-11 16:10:46 +05:30
string output("-");
char const shortopts[] = "ho:V";
2013-08-19 17:10:03 +05:30
option const longopts[] = {
{ "help", no_argument, NULL, 'h' },
{ "output", required_argument, NULL, 'o' },
{ "version", no_argument, NULL, 'V' },
{ "repair", no_argument, NULL, 1 },
2013-08-19 17:10:03 +05:30
{ NULL, no_argument, NULL, 0 }
};
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch(c) {
case 1:
fs.repair_ = true;
break;
2013-08-19 17:10:03 +05:30
case 'h':
usage(cout, basename(argv[0]));
return 0;
2013-09-11 16:10:46 +05:30
case 'o':
output = optarg;
break;
2013-08-19 17:10:03 +05:30
case 'V':
cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
return 0;
default:
usage(cerr, basename(argv[0]));
return 1;
}
}
if (argc == optind) {
cerr << "No input file provided." << endl;
usage(cerr, basename(argv[0]));
return 1;
}
return dump(argv[optind], output, fs);
2013-08-19 17:10:03 +05:30
}
2014-08-27 18:31:31 +05:30
base::command caching::cache_dump_cmd("cache_dump", cache_dump_main);
2013-08-19 17:10:03 +05:30
//----------------------------------------------------------------