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"
|
2013-09-19 18:15:56 +05:30
|
|
|
#include "caching/metadata.h"
|
2013-09-11 16:10:46 +05:30
|
|
|
#include "caching/xml_format.h"
|
2013-09-19 18:15:56 +05:30
|
|
|
#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-09-19 18:15:56 +05:30
|
|
|
using namespace caching::superblock_detail;
|
2013-08-19 17:10:03 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
2013-09-19 18:15:56 +05:30
|
|
|
string to_string(unsigned char const *data) {
|
|
|
|
// FIXME: we're assuming the data is zero terminated here
|
|
|
|
return std::string(reinterpret_cast<char const *>(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------
|
|
|
|
|
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, ostream &out) {
|
2013-09-19 18:15:56 +05:30
|
|
|
block_manager<>::ptr bm = open_bm(dev, block_io<>::READ_ONLY);
|
|
|
|
metadata::ptr md(new metadata(bm, metadata::OPEN));
|
2013-09-11 16:10:46 +05:30
|
|
|
emitter::ptr e = create_xml_emitter(out);
|
2013-09-19 18:15:56 +05:30
|
|
|
|
|
|
|
superblock const &sb = md->sb_;
|
|
|
|
e->begin_superblock(to_string(sb.uuid), sb.data_block_size, sb.cache_blocks, to_string(sb.policy_name));
|
|
|
|
e->end_superblock();
|
|
|
|
|
2013-09-11 16:10:46 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dump(string const &dev, string const &output) {
|
2013-09-19 18:15:56 +05:30
|
|
|
try {
|
|
|
|
if (want_stdout(output))
|
|
|
|
return dump_(dev, cout);
|
|
|
|
else {
|
|
|
|
ofstream out(output.c_str());
|
|
|
|
return dump_(dev, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
cerr << e.what() << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
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
|
2013-08-19 17:10:03 +05:30
|
|
|
<< " {-V|--version}" << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int c;
|
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'},
|
2013-09-11 16:10:46 +05:30
|
|
|
{ "output", required_argument, NULL, 'o'},
|
2013-08-19 17:10:03 +05:30
|
|
|
{ "version", no_argument, NULL, 'V'},
|
|
|
|
{ NULL, no_argument, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
|
|
|
|
switch(c) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:10:46 +05:30
|
|
|
return dump(argv[optind], output);
|
2013-08-19 17:10:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|