[base] introduce a command type that gets registered with the app

This commit is contained in:
Joe Thornber 2016-01-08 12:51:52 +00:00
parent a709b9718b
commit c93e728ef4
28 changed files with 776 additions and 411 deletions

View File

@ -38,6 +38,7 @@ SOURCE=\
caching/cache_metadata_size.cc \ caching/cache_metadata_size.cc \
caching/cache_repair.cc \ caching/cache_repair.cc \
caching/cache_restore.cc \ caching/cache_restore.cc \
caching/commands.cc \
caching/hint_array.cc \ caching/hint_array.cc \
caching/mapping_array.cc \ caching/mapping_array.cc \
caching/metadata.cc \ caching/metadata.cc \
@ -45,6 +46,7 @@ SOURCE=\
caching/restore_emitter.cc \ caching/restore_emitter.cc \
caching/superblock.cc \ caching/superblock.cc \
caching/xml_format.cc \ caching/xml_format.cc \
era/commands.cc \
era/era_array.cc \ era/era_array.cc \
era/era_check.cc \ era/era_check.cc \
era/era_detail.cc \ era/era_detail.cc \
@ -71,6 +73,7 @@ SOURCE=\
persistent-data/space_map.cc \ persistent-data/space_map.cc \
persistent-data/transaction_manager.cc \ persistent-data/transaction_manager.cc \
persistent-data/validators.cc \ persistent-data/validators.cc \
thin-provisioning/commands.cc \
thin-provisioning/device_tree.cc \ thin-provisioning/device_tree.cc \
thin-provisioning/human_readable_format.cc \ thin-provisioning/human_readable_format.cc \
thin-provisioning/mapping_tree.cc \ thin-provisioning/mapping_tree.cc \
@ -173,6 +176,7 @@ install: bin/pdata_tools
ln -s -f pdata_tools $(BINDIR)/thin_check ln -s -f pdata_tools $(BINDIR)/thin_check
ln -s -f pdata_tools $(BINDIR)/thin_delta ln -s -f pdata_tools $(BINDIR)/thin_delta
ln -s -f pdata_tools $(BINDIR)/thin_dump ln -s -f pdata_tools $(BINDIR)/thin_dump
ln -s -f pdata_tools $(BINDIR)/thin_ls
ln -s -f pdata_tools $(BINDIR)/thin_repair ln -s -f pdata_tools $(BINDIR)/thin_repair
ln -s -f pdata_tools $(BINDIR)/thin_restore ln -s -f pdata_tools $(BINDIR)/thin_restore
ln -s -f pdata_tools $(BINDIR)/thin_rmap ln -s -f pdata_tools $(BINDIR)/thin_rmap
@ -190,6 +194,7 @@ install: bin/pdata_tools
$(INSTALL_DATA) man8/thin_check.8 $(MANPATH)/man8 $(INSTALL_DATA) man8/thin_check.8 $(MANPATH)/man8
$(INSTALL_DATA) man8/thin_delta.8 $(MANPATH)/man8 $(INSTALL_DATA) man8/thin_delta.8 $(MANPATH)/man8
$(INSTALL_DATA) man8/thin_dump.8 $(MANPATH)/man8 $(INSTALL_DATA) man8/thin_dump.8 $(MANPATH)/man8
$(INSTALL_DATA) man8/thin_ls.8 $(MANPATH)/man8
$(INSTALL_DATA) man8/thin_repair.8 $(MANPATH)/man8 $(INSTALL_DATA) man8/thin_repair.8 $(MANPATH)/man8
$(INSTALL_DATA) man8/thin_restore.8 $(MANPATH)/man8 $(INSTALL_DATA) man8/thin_restore.8 $(MANPATH)/man8
$(INSTALL_DATA) man8/thin_rmap.8 $(MANPATH)/man8 $(INSTALL_DATA) man8/thin_rmap.8 $(MANPATH)/man8

View File

@ -1,14 +1,47 @@
#include "base/application.h" #include "base/application.h"
#include <boost/lexical_cast.hpp>
#include <libgen.h> #include <libgen.h>
#include <linux/limits.h> #include <linux/limits.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
using namespace base; using namespace base;
using namespace boost;
using namespace std; using namespace std;
//---------------------------------------------------------------- //----------------------------------------------------------------
command::command(string const &name)
: name_(name) {
}
void
command::die(string const &msg)
{
cerr << msg << endl;
usage(cerr);
exit(1);
}
uint64_t
command::parse_uint64(string const &str, string const &desc)
{
try {
// FIXME: check trailing garbage is handled
return lexical_cast<uint64_t>(str);
} catch (...) {
ostringstream out;
out << "Couldn't parse " << desc << ": '" << str << "'";
die(out.str());
}
return 0; // never get here
}
//----------------------------------------------------------------
int int
application::run(int argc, char **argv) application::run(int argc, char **argv)
{ {
@ -26,7 +59,7 @@ application::run(int argc, char **argv)
cmd = argv[0]; cmd = argv[0];
} }
std::list<command const *>::const_iterator it; std::list<command::ptr>::const_iterator it;
for (it = cmds_.begin(); it != cmds_.end(); ++it) { for (it = cmds_.begin(); it != cmds_.end(); ++it) {
if (cmd == (*it)->get_name()) if (cmd == (*it)->get_name())
return (*it)->run(argc, argv); return (*it)->run(argc, argv);
@ -43,7 +76,7 @@ application::usage()
std::cerr << "Usage: <command> <args>\n" std::cerr << "Usage: <command> <args>\n"
<< "commands:\n"; << "commands:\n";
std::list<command const *>::const_iterator it; std::list<command::ptr>::const_iterator it;
for (it = cmds_.begin(); it != cmds_.end(); ++it) { for (it = cmds_.begin(); it != cmds_.end(); ++it) {
std::cerr << " " << (*it)->get_name() << "\n"; std::cerr << " " << (*it)->get_name() << "\n";
} }

View File

@ -1,40 +1,60 @@
#ifndef BASE_APPLICATION_H #ifndef BASE_APPLICATION_H
#define BASE_APPLICATION_H #define BASE_APPLICATION_H
#include <boost/shared_ptr.hpp>
#include <iostream> #include <iostream>
#include <list> #include <list>
#include <string> #include <string>
#include <stdexcept> #include <stdexcept>
#include <stdint.h>
//---------------------------------------------------------------- //----------------------------------------------------------------
namespace base { namespace base {
class command { class command {
public: public:
typedef int (*cmd_fn)(int, char **); typedef boost::shared_ptr<command> ptr;
command(std::string const &name, cmd_fn fn) command(std::string const &name);
: name_(name), virtual ~command() {}
fn_(fn) {
} void die(std::string const &msg);
uint64_t parse_uint64(std::string const &str, std::string const &desc);
virtual void usage(std::ostream &out) const = 0;
virtual int run(int argc, char **argv) = 0;
std::string const &get_name() const { std::string const &get_name() const {
return name_; return name_;
} }
private:
std::string name_;
};
class command_old : public command {
public:
typedef int (*cmd_fn)(int, char **);
command_old(std::string const &name, cmd_fn fn)
: command(name),
fn_(fn) {
}
int run(int argc, char **argv) const { int run(int argc, char **argv) const {
return fn_(argc, argv); return fn_(argc, argv);
} }
private: private:
std::string name_;
cmd_fn fn_; cmd_fn fn_;
}; };
class application { class application {
public: public:
void add_cmd(command const &c) { void add_cmd(command::ptr c) {
cmds_.push_back(&c); cmds_.push_back(c);
} }
int run(int argc, char **argv); int run(int argc, char **argv);
@ -43,7 +63,7 @@ namespace base {
void usage(); void usage();
std::string get_basename(std::string const &path) const; std::string get_basename(std::string const &path) const;
std::list<command const *> cmds_; std::list<command::ptr> cmds_;
}; };
} }

View File

@ -31,7 +31,6 @@ using namespace std;
//---------------------------------------------------------------- //----------------------------------------------------------------
namespace { namespace {
class reporter_base { class reporter_base {
public: public:
reporter_base(nested_output &o) reporter_base(nested_output &o)
@ -326,24 +325,32 @@ namespace {
return r; return r;
} }
void usage(ostream &out, string const &cmd) {
out << "Usage: " << cmd << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-q|--quiet}" << endl
<< " {-h|--help}" << endl
<< " {-V|--version}" << endl
<< " {--clear-needs-check-flag}" << endl
<< " {--super-block-only}" << endl
<< " {--skip-mappings}" << endl
<< " {--skip-hints}" << endl
<< " {--skip-discards}" << endl;
}
} }
//---------------------------------------------------------------- //----------------------------------------------------------------
int cache_check_main(int argc, char **argv) cache_check_cmd::cache_check_cmd()
: command("cache_check")
{
}
void
cache_check_cmd::usage(std::ostream &out) const
{
out << "Usage: " << get_name() << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-q|--quiet}" << endl
<< " {-h|--help}" << endl
<< " {-V|--version}" << endl
<< " {--clear-needs-check-flag}" << endl
<< " {--super-block-only}" << endl
<< " {--skip-mappings}" << endl
<< " {--skip-hints}" << endl
<< " {--skip-discards}" << endl;
}
int
cache_check_cmd::run(int argc, char **argv)
{ {
int c; int c;
flags fs; flags fs;
@ -384,7 +391,7 @@ int cache_check_main(int argc, char **argv)
break; break;
case 'h': case 'h':
usage(cout, basename(argv[0])); usage(cout);
return 0; return 0;
case 'q': case 'q':
@ -396,20 +403,18 @@ int cache_check_main(int argc, char **argv)
return 0; return 0;
default: default:
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
} }
if (argc == optind) { if (argc == optind) {
cerr << "No input file provided." << endl; cerr << "No input file provided." << endl;
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
return check_with_exception_handling(argv[optind], fs); return check_with_exception_handling(argv[optind], fs);
} }
base::command caching::cache_check_cmd("cache_check", cache_check_main);
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -54,20 +54,28 @@ namespace {
return 0; return 0;
} }
void usage(ostream &out, string const &cmd) {
out << "Usage: " << cmd << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-o <xml file>}" << endl
<< " {-V|--version}" << endl
<< " {--repair}" << endl;
}
} }
//---------------------------------------------------------------- //----------------------------------------------------------------
int cache_dump_main(int argc, char **argv) cache_dump_cmd::cache_dump_cmd()
: command("cache_dump")
{
}
void
cache_dump_cmd::usage(std::ostream &out) const
{
out << "Usage: " << get_name() << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-o <xml file>}" << endl
<< " {-V|--version}" << endl
<< " {--repair}" << endl;
}
int
cache_dump_cmd::run(int argc, char **argv)
{ {
int c; int c;
flags fs; flags fs;
@ -89,7 +97,7 @@ int cache_dump_main(int argc, char **argv)
break; break;
case 'h': case 'h':
usage(cout, basename(argv[0])); usage(cout);
return 0; return 0;
case 'o': case 'o':
@ -101,20 +109,18 @@ int cache_dump_main(int argc, char **argv)
return 0; return 0;
default: default:
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
} }
if (argc == optind) { if (argc == optind) {
cerr << "No input file provided." << endl; cerr << "No input file provided." << endl;
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
return dump(argv[optind], output, fs); return dump(argv[optind], output, fs);
} }
base::command caching::cache_dump_cmd("cache_dump", cache_dump_main);
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -9,141 +9,145 @@
#include <stdint.h> #include <stdint.h>
#include <stdexcept> #include <stdexcept>
using namespace caching;
using namespace std; using namespace std;
//---------------------------------------------------------------- //----------------------------------------------------------------
namespace { cache_metadata_size_cmd::flags::flags()
struct flags { : max_hint_width(4) {
flags()
: max_hint_width(4) {
// Dance around some spurious compiler warnings // Dance around some spurious compiler warnings
device_size = 0; device_size = 0;
block_size = 0; block_size = 0;
nr_blocks = 0; nr_blocks = 0;
device_size.reset(); device_size.reset();
block_size.reset(); block_size.reset();
nr_blocks.reset(); nr_blocks.reset();
}
boost::optional<uint64_t> device_size;
boost::optional<uint32_t> block_size;
boost::optional<uint64_t> nr_blocks;
uint32_t max_hint_width;
};
void usage(ostream &out, string const &cmd) {
out << "Usage: " << cmd << " [options]" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-V|--version}" << endl
<< " {--block-size <sectors>}" << endl
<< " {--device-size <sectors>}" << endl
<< " {--nr-blocks <natural>}" << endl << endl
<< "These all relate to the size of the fast device (eg, SSD), rather" << endl
<< "than the whole cached device." << endl;
}
enum parse_result {
FINISH,
CONTINUE
};
parse_result parse_command_line(string const &prog_name, int argc, char **argv, flags &fs) {
int c;
char const short_opts[] = "hV";
option const long_opts[] = {
{ "block-size", required_argument, NULL, 0 },
{ "device-size", required_argument, NULL, 1 },
{ "nr-blocks", required_argument, NULL, 2 },
{ "max-hint-width", required_argument, NULL, 3 },
{ "help", no_argument, NULL, 'h' },
{ "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.block_size = boost::lexical_cast<uint32_t>(optarg);
break;
case 1:
fs.device_size = boost::lexical_cast<uint64_t>(optarg);
break;
case 2:
fs.nr_blocks = boost::lexical_cast<uint64_t>(optarg);
break;
case 3:
fs.max_hint_width = boost::lexical_cast<uint32_t>(optarg);
break;
case 'h':
usage(cout, prog_name);
return FINISH;
break;
case 'V':
cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
return FINISH;
break;
default:
usage(cerr, prog_name);
throw runtime_error("Invalid command line");
break;
}
}
return CONTINUE;
}
uint64_t get_nr_blocks(flags &fs) {
if (fs.device_size) {
if (!fs.block_size)
throw runtime_error("If you specify --device-size you must also give --block-size.");
uint64_t nr_blocks = *fs.device_size / *fs.block_size;
if (fs.nr_blocks) {
if (nr_blocks != *fs.nr_blocks)
throw runtime_error(
"Contradictory arguments given, --nr-blocks doesn't match the --device-size and --block-size.");
}
return nr_blocks;
}
if (fs.block_size && !fs.device_size)
throw runtime_error("If you specify --block-size you must also give --device-size.");
if (fs.nr_blocks)
return *fs.nr_blocks;
throw runtime_error("Please specify either --device-size and --block-size, or --nr-blocks.");
}
uint64_t meg(uint64_t n) {
return n * 2048;
}
uint64_t calc_size(uint64_t nr_blocks, uint32_t max_hint_width) {
uint64_t const SECTOR_SIZE = 512;
uint64_t const TRANSACTION_OVERHEAD = meg(4);
uint64_t const BYTES_PER_BLOCK = 16;
uint64_t const HINT_OVERHEAD_PER_BLOCK = 8;
uint64_t mapping_size = (nr_blocks * BYTES_PER_BLOCK) / SECTOR_SIZE;
uint64_t hint_size = (nr_blocks * (max_hint_width + HINT_OVERHEAD_PER_BLOCK)) / SECTOR_SIZE;
return TRANSACTION_OVERHEAD + mapping_size + hint_size;
}
} }
int cache_metadata_size_main(int argc, char **argv) cache_metadata_size_cmd::parse_result
cache_metadata_size_cmd::parse_command_line(string const &prog_name, int argc, char **argv, flags &fs)
{
int c;
char const short_opts[] = "hV";
option const long_opts[] = {
{ "block-size", required_argument, NULL, 0 },
{ "device-size", required_argument, NULL, 1 },
{ "nr-blocks", required_argument, NULL, 2 },
{ "max-hint-width", required_argument, NULL, 3 },
{ "help", no_argument, NULL, 'h' },
{ "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.block_size = boost::lexical_cast<uint32_t>(optarg);
break;
case 1:
fs.device_size = boost::lexical_cast<uint64_t>(optarg);
break;
case 2:
fs.nr_blocks = boost::lexical_cast<uint64_t>(optarg);
break;
case 3:
fs.max_hint_width = boost::lexical_cast<uint32_t>(optarg);
break;
case 'h':
usage(cout);
return FINISH;
break;
case 'V':
cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
return FINISH;
break;
default:
usage(cerr);
throw runtime_error("Invalid command line");
break;
}
}
return CONTINUE;
}
uint64_t
cache_metadata_size_cmd::get_nr_blocks(flags &fs)
{
if (fs.device_size) {
if (!fs.block_size)
throw runtime_error("If you specify --device-size you must also give --block-size.");
uint64_t nr_blocks = *fs.device_size / *fs.block_size;
if (fs.nr_blocks) {
if (nr_blocks != *fs.nr_blocks)
throw runtime_error(
"Contradictory arguments given, --nr-blocks doesn't match the --device-size and --block-size.");
}
return nr_blocks;
}
if (fs.block_size && !fs.device_size)
throw runtime_error("If you specify --block-size you must also give --device-size.");
if (fs.nr_blocks)
return *fs.nr_blocks;
throw runtime_error("Please specify either --device-size and --block-size, or --nr-blocks.");
}
uint64_t
cache_metadata_size_cmd::meg(uint64_t n)
{
return n * 2048;
}
uint64_t
cache_metadata_size_cmd::calc_size(uint64_t nr_blocks, uint32_t max_hint_width)
{
uint64_t const SECTOR_SIZE = 512;
uint64_t const TRANSACTION_OVERHEAD = meg(4);
uint64_t const BYTES_PER_BLOCK = 16;
uint64_t const HINT_OVERHEAD_PER_BLOCK = 8;
uint64_t mapping_size = (nr_blocks * BYTES_PER_BLOCK) / SECTOR_SIZE;
uint64_t hint_size = (nr_blocks * (max_hint_width + HINT_OVERHEAD_PER_BLOCK)) / SECTOR_SIZE;
return TRANSACTION_OVERHEAD + mapping_size + hint_size;
}
//----------------------------------------------------------------
cache_metadata_size_cmd::cache_metadata_size_cmd()
: command("cache_metadata_size")
{
}
void
cache_metadata_size_cmd::usage(ostream &out) const
{
out << "Usage: " << get_name() << " [options]" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-V|--version}" << endl
<< " {--block-size <sectors>}" << endl
<< " {--device-size <sectors>}" << endl
<< " {--nr-blocks <natural>}" << endl << endl
<< "These all relate to the size of the fast device (eg, SSD), rather" << endl
<< "than the whole cached device." << endl;
}
int
cache_metadata_size_cmd::run(int argc, char **argv)
{ {
flags fs; flags fs;
@ -167,6 +171,4 @@ int cache_metadata_size_main(int argc, char **argv)
return 0; return 0;
} }
base::command caching::cache_metadata_size_cmd("cache_metadata_size", cache_metadata_size_main);
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -40,20 +40,28 @@ namespace {
return 0; return 0;
} }
void usage(ostream &out, string const &cmd) {
out << "Usage: " << cmd << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-i|--input} <input metadata (binary format)>" << endl
<< " {-o|--output} <output metadata (binary format)>" << endl
<< " {-V|--version}" << endl;
}
} }
//---------------------------------------------------------------- //----------------------------------------------------------------
int cache_repair_main(int argc, char **argv) cache_repair_cmd::cache_repair_cmd()
: command("cache_repair")
{
}
void
cache_repair_cmd::usage(std::ostream &out) const
{
out << "Usage: " << get_name() << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-i|--input} <input metadata (binary format)>" << endl
<< " {-o|--output} <output metadata (binary format)>" << endl
<< " {-V|--version}" << endl;
}
int
cache_repair_cmd::run(int argc, char **argv)
{ {
int c; int c;
boost::optional<string> input_path, output_path; boost::optional<string> input_path, output_path;
@ -70,7 +78,7 @@ int cache_repair_main(int argc, char **argv)
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch(c) { switch(c) {
case 'h': case 'h':
usage(cout, basename(argv[0])); usage(cout);
return 0; return 0;
case 'i': case 'i':
@ -86,26 +94,24 @@ int cache_repair_main(int argc, char **argv)
return 0; return 0;
default: default:
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
} }
if (!input_path) { if (!input_path) {
cerr << "no input file provided" << endl; cerr << "no input file provided" << endl;
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
if (!output_path) { if (!output_path) {
cerr << "no output file provided" << endl; cerr << "no output file provided" << endl;
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
return repair(*input_path, *output_path); return repair(*input_path, *output_path);
} }
base::command caching::cache_repair_cmd("cache_repair", cache_repair_main);
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -80,27 +80,35 @@ namespace {
return 0; 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
<< " {-V|--version}" << endl
<< endl
<< " {--debug-override-metadata-version} <integer>" << endl
<< " {--omit-clean-shutdown}" << endl;
}
} }
int cache_restore_main(int argc, char **argv) //----------------------------------------------------------------
cache_restore_cmd::cache_restore_cmd()
: command("cache_restore")
{
}
void
cache_restore_cmd::usage(std::ostream &out) const
{
out << "Usage: " << get_name() << " [options]" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-i|--input} <input xml file>" << endl
<< " {-o|--output} <output device or file>" << endl
<< " {-q|--quiet}" << endl
<< " {-V|--version}" << endl
<< endl
<< " {--debug-override-metadata-version} <integer>" << endl
<< " {--omit-clean-shutdown}" << endl;
}
int
cache_restore_cmd::run(int argc, char **argv)
{ {
int c; int c;
flags fs; flags fs;
char const *prog_name = basename(argv[0]);
char const *short_opts = "hi:o:qV"; char const *short_opts = "hi:o:qV";
option const long_opts[] = { option const long_opts[] = {
{ "debug-override-metadata-version", required_argument, NULL, 0 }, { "debug-override-metadata-version", required_argument, NULL, 0 },
@ -125,7 +133,7 @@ int cache_restore_main(int argc, char **argv)
break; break;
case 'h': case 'h':
usage(cout, prog_name); usage(cout);
return 0; return 0;
case 'i': case 'i':
@ -145,31 +153,29 @@ int cache_restore_main(int argc, char **argv)
return 0; return 0;
default: default:
usage(cerr, prog_name); usage(cerr);
return 1; return 1;
} }
} }
if (argc != optind) { if (argc != optind) {
usage(cerr, prog_name); usage(cerr);
return 1; return 1;
} }
if (!fs.input) { if (!fs.input) {
cerr << "No input file provided." << endl << endl; cerr << "No input file provided." << endl << endl;
usage(cerr, prog_name); usage(cerr);
return 1; return 1;
} }
if (!fs.output) { if (!fs.output) {
cerr << "No output file provided." << endl << endl; cerr << "No output file provided." << endl << endl;
usage(cerr, prog_name); usage(cerr);
return 1; return 1;
} }
return restore(fs); return restore(fs);
} }
base::command caching::cache_restore_cmd("cache_restore", cache_restore_main);
//---------------------------------------------------------------- //----------------------------------------------------------------

18
caching/commands.cc Normal file
View File

@ -0,0 +1,18 @@
#include "caching/commands.h"
using namespace base;
using namespace caching;
//----------------------------------------------------------------
void
caching::register_cache_commands(application &app)
{
app.add_cmd(command::ptr(new cache_check_cmd));
app.add_cmd(command::ptr(new cache_dump_cmd));
app.add_cmd(command::ptr(new cache_metadata_size_cmd));
app.add_cmd(command::ptr(new cache_restore_cmd));
app.add_cmd(command::ptr(new cache_repair_cmd));
}
//----------------------------------------------------------------

View File

@ -2,15 +2,68 @@
#define CACHING_COMMANDS_H #define CACHING_COMMANDS_H
#include "base/application.h" #include "base/application.h"
#include "boost/optional.hpp"
//---------------------------------------------------------------- //----------------------------------------------------------------
namespace caching { namespace caching {
extern base::command cache_check_cmd; class cache_check_cmd : public base::command {
extern base::command cache_dump_cmd; public:
extern base::command cache_metadata_size_cmd; cache_check_cmd();
extern base::command cache_restore_cmd; virtual void usage(std::ostream &out) const;
extern base::command cache_repair_cmd; virtual int run(int argc, char **argv);
};
class cache_dump_cmd : public base::command {
public:
cache_dump_cmd();
virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
};
class cache_metadata_size_cmd : public base::command {
public:
cache_metadata_size_cmd();
virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
private:
struct flags {
flags();
boost::optional<uint64_t> device_size;
boost::optional<uint32_t> block_size;
boost::optional<uint64_t> nr_blocks;
uint32_t max_hint_width;
};
enum parse_result {
FINISH,
CONTINUE
};
parse_result parse_command_line(std::string const &prog_name, int argc, char **argv, flags &fs);
uint64_t get_nr_blocks(flags &fs);
uint64_t meg(uint64_t n);
uint64_t calc_size(uint64_t nr_blocks, uint32_t max_hint_width);
};
class cache_repair_cmd : public base::command {
public:
cache_repair_cmd();
virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
};
class cache_restore_cmd : public base::command {
public:
cache_restore_cmd();
virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
};
void register_cache_commands(base::application &app);
} }
//---------------------------------------------------------------- //----------------------------------------------------------------

16
era/commands.cc Normal file
View File

@ -0,0 +1,16 @@
#include "era/commands.h"
using namespace base;
//----------------------------------------------------------------
void
era::register_era_commands(base::application &app)
{
app.add_cmd(command::ptr(new era_check_cmd()));
app.add_cmd(command::ptr(new era_dump_cmd()));
app.add_cmd(command::ptr(new era_invalidate_cmd()));
app.add_cmd(command::ptr(new era_restore_cmd()));
}
//----------------------------------------------------------------

View File

@ -6,10 +6,35 @@
//---------------------------------------------------------------- //----------------------------------------------------------------
namespace era { namespace era {
extern base::command era_check_cmd; class era_check_cmd : public base::command {
extern base::command era_dump_cmd; public:
extern base::command era_invalidate_cmd; era_check_cmd();
extern base::command era_restore_cmd; virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
};
class era_dump_cmd : public base::command {
public:
era_dump_cmd();
virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
};
class era_invalidate_cmd : public base::command {
public:
era_invalidate_cmd();
virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
};
class era_restore_cmd : public base::command {
public:
era_restore_cmd();
virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
};
void register_era_commands(base::application &app);
} }
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -262,20 +262,28 @@ namespace {
return r; return r;
} }
void usage(ostream &out, string const &cmd) {
out << "Usage: " << cmd << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-q|--quiet}" << endl
<< " {-h|--help}" << endl
<< " {-V|--version}" << endl
<< " {--super-block-only}" << endl;
}
} }
//---------------------------------------------------------------- //----------------------------------------------------------------
int era_check_main(int argc, char **argv) era_check_cmd::era_check_cmd()
: command("era_check")
{
}
void
era_check_cmd::usage(std::ostream &out) const
{
out << "Usage: " << get_name() << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-q|--quiet}" << endl
<< " {-h|--help}" << endl
<< " {-V|--version}" << endl
<< " {--super-block-only}" << endl;
}
int
era_check_cmd::run(int argc, char **argv)
{ {
int c; int c;
flags fs; flags fs;
@ -295,7 +303,7 @@ int era_check_main(int argc, char **argv)
break; break;
case 'h': case 'h':
usage(cout, basename(argv[0])); usage(cout);
return 0; return 0;
case 'q': case 'q':
@ -307,20 +315,18 @@ int era_check_main(int argc, char **argv)
return 0; return 0;
default: default:
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
} }
if (argc == optind) { if (argc == optind) {
cerr << "No input file provided." << endl; cerr << "No input file provided." << endl;
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
return check_with_exception_handling(argv[optind], fs); return check_with_exception_handling(argv[optind], fs);
} }
base::command era::era_check_cmd("era_check", era_check_main);
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -57,21 +57,29 @@ namespace {
return 0; return 0;
} }
void usage(ostream &out, string const &cmd) {
out << "Usage: " << cmd << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-o <xml file>}" << endl
<< " {-V|--version}" << endl
<< " {--repair}" << endl
<< " {--logical}" << endl;
}
} }
//---------------------------------------------------------------- //----------------------------------------------------------------
int era_dump_main(int argc, char **argv) era_dump_cmd::era_dump_cmd()
: command("era_dump")
{
}
void
era_dump_cmd::usage(std::ostream &out) const
{
out << "Usage: " << get_name() << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-o <xml file>}" << endl
<< " {-V|--version}" << endl
<< " {--repair}" << endl
<< " {--logical}" << endl;
}
int
era_dump_cmd::run(int argc, char **argv)
{ {
int c; int c;
flags fs; flags fs;
@ -98,7 +106,7 @@ int era_dump_main(int argc, char **argv)
break; break;
case 'h': case 'h':
usage(cout, basename(argv[0])); usage(cout);
return 0; return 0;
case 'o': case 'o':
@ -110,20 +118,18 @@ int era_dump_main(int argc, char **argv)
return 0; return 0;
default: default:
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
} }
if (argc == optind) { if (argc == optind) {
cerr << "No input file provided." << endl; cerr << "No input file provided." << endl;
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
return dump(argv[optind], output, fs); return dump(argv[optind], output, fs);
} }
base::command era::era_dump_cmd("era_dump", era_dump_main);
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -182,20 +182,28 @@ namespace {
return 0; return 0;
} }
void usage(ostream &out, string const &cmd) {
out << "Usage: " << cmd << " [options] --written-since <era> {device|file}\n"
<< "Options:\n"
<< " {-h|--help}\n"
<< " {-o <xml file>}\n"
<< " {--metadata-snapshot}\n"
<< " {-V|--version}" << endl;
}
} }
//---------------------------------------------------------------- //----------------------------------------------------------------
int era_invalidate_main(int argc, char **argv) era_invalidate_cmd::era_invalidate_cmd()
: command("era_invalidate")
{
}
void
era_invalidate_cmd::usage(std::ostream &out) const
{
out << "Usage: " << get_name() << " [options] --written-since <era> {device|file}\n"
<< "Options:\n"
<< " {-h|--help}\n"
<< " {-o <xml file>}\n"
<< " {--metadata-snapshot}\n"
<< " {-V|--version}" << endl;
}
int
era_invalidate_cmd::run(int argc, char **argv)
{ {
int c; int c;
flags fs; flags fs;
@ -222,7 +230,7 @@ int era_invalidate_main(int argc, char **argv)
break; break;
case 'h': case 'h':
usage(cout, basename(argv[0])); usage(cout);
return 0; return 0;
case 'o': case 'o':
@ -234,26 +242,24 @@ int era_invalidate_main(int argc, char **argv)
return 0; return 0;
default: default:
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
} }
if (argc == optind) { if (argc == optind) {
cerr << "No input file provided." << endl; cerr << "No input file provided." << endl;
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
if (!fs.era_threshold_) { if (!fs.era_threshold_) {
cerr << "Please specify --written-since" << endl; cerr << "Please specify --written-since" << endl;
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
return invalidate(argv[optind], output, fs); return invalidate(argv[optind], output, fs);
} }
base::command era::era_invalidate_cmd("era_invalidate", era_invalidate_main);
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -46,23 +46,32 @@ namespace {
return 0; 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
<< " {-V|--version}" << endl;
}
} }
int era_restore_main(int argc, char **argv) //----------------------------------------------------------------
era_restore_cmd::era_restore_cmd()
: command("era_restore")
{
}
void
era_restore_cmd::usage(std::ostream &out) const
{
out << "Usage: " << get_name() << " [options]" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-i|--input} <input xml file>" << endl
<< " {-o|--output} <output device or file>" << endl
<< " {-q|--quiet}" << endl
<< " {-V|--version}" << endl;
}
int
era_restore_cmd::run(int argc, char **argv)
{ {
int c; int c;
flags fs; flags fs;
char const *prog_name = basename(argv[0]);
char const *short_opts = "hi:o:qV"; char const *short_opts = "hi:o:qV";
option const long_opts[] = { option const long_opts[] = {
{ "help", no_argument, NULL, 'h'}, { "help", no_argument, NULL, 'h'},
@ -76,7 +85,7 @@ int era_restore_main(int argc, char **argv)
while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
switch(c) { switch(c) {
case 'h': case 'h':
usage(cout, prog_name); usage(cout);
return 0; return 0;
case 'i': case 'i':
@ -96,31 +105,29 @@ int era_restore_main(int argc, char **argv)
return 0; return 0;
default: default:
usage(cerr, prog_name); usage(cerr);
return 1; return 1;
} }
} }
if (argc != optind) { if (argc != optind) {
usage(cerr, prog_name); usage(cerr);
return 1; return 1;
} }
if (!fs.input) { if (!fs.input) {
cerr << "No input file provided." << endl << endl; cerr << "No input file provided." << endl << endl;
usage(cerr, prog_name); usage(cerr);
return 1; return 1;
} }
if (!fs.output) { if (!fs.output) {
cerr << "No output file provided." << endl << endl; cerr << "No output file provided." << endl << endl;
usage(cerr, prog_name); usage(cerr);
return 1; return 1;
} }
return restore(fs, fs.quiet); return restore(fs, fs.quiet);
} }
base::command era::era_restore_cmd("era_restore", era_restore_main);
//---------------------------------------------------------------- //----------------------------------------------------------------

24
main.cc
View File

@ -14,27 +14,9 @@ int main(int argc, char **argv)
application app; application app;
app.add_cmd(caching::cache_check_cmd); caching::register_cache_commands(app);
app.add_cmd(caching::cache_dump_cmd); era::register_era_commands(app);
app.add_cmd(caching::cache_metadata_size_cmd); thin_provisioning::register_thin_commands(app);
app.add_cmd(caching::cache_restore_cmd);
app.add_cmd(caching::cache_repair_cmd);
app.add_cmd(era::era_check_cmd);
app.add_cmd(era::era_dump_cmd);
app.add_cmd(era::era_invalidate_cmd);
app.add_cmd(era::era_restore_cmd);
app.add_cmd(thin_provisioning::thin_check_cmd);
app.add_cmd(thin_provisioning::thin_delta_cmd);
app.add_cmd(thin_provisioning::thin_dump_cmd);
app.add_cmd(thin_provisioning::thin_metadata_size_cmd);
app.add_cmd(thin_provisioning::thin_restore_cmd);
app.add_cmd(thin_provisioning::thin_repair_cmd);
app.add_cmd(thin_provisioning::thin_rmap_cmd);
// FIXME: convert thin_metadata_size to c++
//app.add_cmd(thin_provisioning::thin_metadata_size_cmd);
return app.run(argc, argv); return app.run(argc, argv);
} }

View File

@ -0,0 +1,22 @@
#include "thin-provisioning/commands.h"
using namespace base;
using namespace thin_provisioning;
//----------------------------------------------------------------
void
thin_provisioning::register_thin_commands(base::application &app)
{
app.add_cmd(command::ptr(new thin_check_cmd()));
app.add_cmd(command::ptr(new thin_delta_cmd()));
app.add_cmd(command::ptr(new thin_dump_cmd()));
//app.add_cmd(command::ptr(new thin_ls_cmd()));
app.add_cmd(command::ptr(new thin_metadata_size_cmd()));
app.add_cmd(command::ptr(new thin_restore_cmd()));
app.add_cmd(command::ptr(new thin_repair_cmd()));
app.add_cmd(command::ptr(new thin_rmap_cmd()));
app.add_cmd(command::ptr(new thin_trim_cmd()));
}
//----------------------------------------------------------------

View File

@ -2,19 +2,75 @@
#define THIN_PROVISIONING_COMMANDS_H #define THIN_PROVISIONING_COMMANDS_H
#include "base/application.h" #include "base/application.h"
#include "boost/optional.hpp"
//---------------------------------------------------------------- //----------------------------------------------------------------
namespace thin_provisioning { namespace thin_provisioning {
extern base::command thin_check_cmd; class thin_check_cmd : public base::command {
extern base::command thin_delta_cmd; public:
extern base::command thin_dump_cmd; thin_check_cmd();
extern base::command thin_metadata_size_cmd; virtual void usage(std::ostream &out) const;
extern base::command thin_restore_cmd; virtual int run(int argc, char **argv);
extern base::command thin_repair_cmd; };
extern base::command thin_rmap_cmd;
extern base::command thin_trim_cmd; class thin_delta_cmd : public base::command {
extern base::command thin_metadata_size_cmd; public:
thin_delta_cmd();
virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
};
class thin_dump_cmd : public base::command {
public:
thin_dump_cmd();
virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
};
class thin_ls_cmd : public base::command {
public:
thin_ls_cmd();
virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
};
class thin_metadata_size_cmd : public base::command {
public:
thin_metadata_size_cmd();
virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
};
class thin_restore_cmd : public base::command {
public:
thin_restore_cmd();
virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
};
class thin_repair_cmd : public base::command {
public:
thin_repair_cmd();
virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
};
class thin_rmap_cmd : public base::command {
public:
thin_rmap_cmd();
virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
};
class thin_trim_cmd : public base::command {
public:
thin_trim_cmd();
virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
};
void register_thin_commands(base::application &app);
} }
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -84,7 +84,7 @@ namespace {
//-------------------------------- //--------------------------------
typedef map<block_address, device_tree_detail::device_details> dd_map; typedef map<block_address, device_tree_detail::device_details> dd_map;
class details_extractor : public device_tree_detail::device_visitor { class details_extractor : public device_tree_detail::device_visitor {
public: public:

View File

@ -344,21 +344,31 @@ namespace {
return !success; return !success;
} }
void usage(ostream &out, string const &cmd) {
out << "Usage: " << cmd << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-q|--quiet}" << endl
<< " {-h|--help}" << endl
<< " {-V|--version}" << endl
<< " {--clear-needs-check-flag}" << endl
<< " {--ignore-non-fatal-errors}" << endl
<< " {--skip-mappings}" << endl
<< " {--super-block-only}" << endl;
}
} }
int thin_check_main(int argc, char **argv) //----------------------------------------------------------------
thin_check_cmd::thin_check_cmd()
: command("thin_check")
{
}
void
thin_check_cmd::usage(std::ostream &out) const
{
out << "Usage: " << get_name() << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-q|--quiet}" << endl
<< " {-h|--help}" << endl
<< " {-V|--version}" << endl
<< " {--clear-needs-check-flag}" << endl
<< " {--ignore-non-fatal-errors}" << endl
<< " {--skip-mappings}" << endl
<< " {--super-block-only}" << endl;
}
int
thin_check_cmd::run(int argc, char **argv)
{ {
int c; int c;
flags fs; flags fs;
@ -378,7 +388,7 @@ int thin_check_main(int argc, char **argv)
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch(c) { switch(c) {
case 'h': case 'h':
usage(cout, basename(argv[0])); usage(cout);
return 0; return 0;
case 'q': case 'q':
@ -412,7 +422,7 @@ int thin_check_main(int argc, char **argv)
break; break;
default: default:
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
} }
@ -420,7 +430,7 @@ int thin_check_main(int argc, char **argv)
if (argc == optind) { if (argc == optind) {
if (!fs.quiet) { if (!fs.quiet) {
cerr << "No input file provided." << endl; cerr << "No input file provided." << endl;
usage(cerr, basename(argv[0])); usage(cerr);
} }
exit(1); exit(1);
@ -429,6 +439,4 @@ int thin_check_main(int argc, char **argv)
return check(argv[optind], fs); return check(argv[optind], fs);
} }
base::command thin_provisioning::thin_check_cmd("thin_check", thin_check_main);
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -602,7 +602,19 @@ namespace local {
// FIXME: add metadata snap switch // FIXME: add metadata snap switch
int thin_delta_main(int argc, char **argv) thin_delta_cmd::thin_delta_cmd()
: command("thin_delta")
{
}
void
thin_delta_cmd::usage(std::ostream &out) const
{
// FIXME: finish
}
int
thin_delta_cmd::run(int argc, char **argv)
{ {
using namespace local; using namespace local;
@ -670,6 +682,4 @@ int thin_delta_main(int argc, char **argv)
return delta(app, fs); return delta(app, fs);
} }
base::command thin_provisioning::thin_delta_cmd("thin_delta", thin_delta_main);
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -100,7 +100,28 @@ namespace {
} }
} }
int thin_dump_main(int argc, char **argv) //----------------------------------------------------------------
thin_dump_cmd::thin_dump_cmd()
: command("thin_dump")
{
}
void
thin_dump_cmd::usage(std::ostream &out) const
{
out << "Usage: " << get_name() << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-f|--format} {xml|human_readable}" << endl
<< " {-r|--repair}" << endl
<< " {-m|--metadata-snap} [block#]" << endl
<< " {-o <xml file>}" << endl
<< " {-V|--version}" << endl;
}
int
thin_dump_cmd::run(int argc, char **argv)
{ {
int c; int c;
char const *output = NULL; char const *output = NULL;
@ -123,7 +144,7 @@ int thin_dump_main(int argc, char **argv)
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch(c) { switch(c) {
case 'h': case 'h':
usage(cout, basename(argv[0])); usage(cout);
return 0; return 0;
case 'f': case 'f':
@ -141,7 +162,7 @@ int thin_dump_main(int argc, char **argv)
metadata_snap = strtoull(optarg, &end_ptr, 10); metadata_snap = strtoull(optarg, &end_ptr, 10);
if (end_ptr == optarg) { if (end_ptr == optarg) {
cerr << "couldn't parse <metadata_snap>" << endl; cerr << "couldn't parse <metadata_snap>" << endl;
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
@ -158,20 +179,18 @@ int thin_dump_main(int argc, char **argv)
return 0; return 0;
default: default:
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
} }
if (argc == optind) { if (argc == optind) {
cerr << "No input file provided." << endl; cerr << "No input file provided." << endl;
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
return dump(argv[optind], output, format, flags); return dump(argv[optind], output, format, flags);
} }
base::command thin_provisioning::thin_dump_cmd("thin_dump", thin_dump_main);
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -35,6 +35,8 @@
#include <search.h> #include <search.h>
using namespace thin_provisioning;
/*----------------------------------------------------------------*/ /*----------------------------------------------------------------*/
enum numeric_options { BLOCKSIZE, POOLSIZE, MAXTHINS, NUMERIC, OPT_END}; enum numeric_options { BLOCKSIZE, POOLSIZE, MAXTHINS, NUMERIC, OPT_END};
@ -362,7 +364,21 @@ static void print_estimated_result(struct global *g)
print_precision(g, r, g->options.unit_idx); print_precision(g, r, g->options.unit_idx);
} }
int thin_metadata_size_main(int argc, char **argv) //----------------------------------------------------------------
thin_metadata_size_cmd::thin_metadata_size_cmd()
: command("thin_metadata_size")
{
}
void
thin_metadata_size_cmd::usage(std::ostream &out) const
{
// FIXME: finish
}
int
thin_metadata_size_cmd::run(int argc, char **argv)
{ {
struct global *g = init_prg(*argv); struct global *g = init_prg(*argv);
@ -372,4 +388,4 @@ int thin_metadata_size_main(int argc, char **argv)
return 0; /* Doesn't get here... */ return 0; /* Doesn't get here... */
} }
base::command thin_provisioning::thin_metadata_size_cmd("thin_metadata_size", thin_metadata_size_main); //----------------------------------------------------------------

View File

@ -33,18 +33,28 @@ namespace {
return 0; return 0;
} }
void usage(ostream &out, string const &cmd) {
out << "Usage: " << cmd << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-i|--input} <input metadata (binary format)>" << endl
<< " {-o|--output} <output metadata (binary format)>" << endl
<< " {-V|--version}" << endl;
}
} }
int thin_repair_main(int argc, char **argv) //----------------------------------------------------------------
thin_repair_cmd::thin_repair_cmd()
: command("thin_repair")
{
}
void
thin_repair_cmd::usage(std::ostream &out) const
{
out << "Usage: " << get_name() << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-i|--input} <input metadata (binary format)>" << endl
<< " {-o|--output} <output metadata (binary format)>" << endl
<< " {-V|--version}" << endl;
}
int
thin_repair_cmd::run(int argc, char **argv)
{ {
int c; int c;
boost::optional<string> input_path, output_path; boost::optional<string> input_path, output_path;
@ -61,7 +71,7 @@ int thin_repair_main(int argc, char **argv)
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch(c) { switch(c) {
case 'h': case 'h':
usage(cout, basename(argv[0])); usage(cout);
return 0; return 0;
case 'i': case 'i':
@ -77,26 +87,24 @@ int thin_repair_main(int argc, char **argv)
return 0; return 0;
default: default:
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
} }
if (!input_path) { if (!input_path) {
cerr << "no input file provided" << endl; cerr << "no input file provided" << endl;
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
if (!output_path) { if (!output_path) {
cerr << "no output file provided" << endl; cerr << "no output file provided" << endl;
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
return repair(*input_path, *output_path); return repair(*input_path, *output_path);
} }
base::command thin_provisioning::thin_repair_cmd("thin_repair", thin_repair_main);
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -60,20 +60,32 @@ namespace {
} }
void usage(ostream &out, string const &cmd) { 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
<< " {-V|--version}" << endl;
} }
} }
int thin_restore_main(int argc, char **argv) //----------------------------------------------------------------
thin_restore_cmd::thin_restore_cmd()
: command("thin_restore")
{
}
void
thin_restore_cmd::usage(std::ostream &out) const
{
out << "Usage: " << get_name() << " [options]" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-i|--input} <input xml file>" << endl
<< " {-o|--output} <output device or file>" << endl
<< " {-q|--quiet}" << endl
<< " {-V|--version}" << endl;
}
int
thin_restore_cmd::run(int argc, char **argv)
{ {
int c; int c;
char const *prog_name = basename(argv[0]);
const char *shortopts = "hi:o:qV"; const char *shortopts = "hi:o:qV";
string input, output; string input, output;
bool quiet = false; bool quiet = false;
@ -89,7 +101,7 @@ int thin_restore_main(int argc, char **argv)
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch(c) { switch(c) {
case 'h': case 'h':
usage(cout, prog_name); usage(cout);
return 0; return 0;
case 'i': case 'i':
@ -109,31 +121,29 @@ int thin_restore_main(int argc, char **argv)
return 0; return 0;
default: default:
usage(cerr, prog_name); usage(cerr);
return 1; return 1;
} }
} }
if (argc != optind) { if (argc != optind) {
usage(cerr, prog_name); usage(cerr);
return 1; return 1;
} }
if (input.empty()) { if (input.empty()) {
cerr << "No input file provided." << endl << endl; cerr << "No input file provided." << endl << endl;
usage(cerr, prog_name); usage(cerr);
return 1; return 1;
} }
if (output.empty()) { if (output.empty()) {
cerr << "No output file provided." << endl << endl; cerr << "No output file provided." << endl << endl;
usage(cerr, prog_name); usage(cerr);
return 1; return 1;
} }
return restore(input, output, quiet); return restore(input, output, quiet);
} }
base::command thin_provisioning::thin_restore_cmd("thin_restore", thin_restore_main);
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -111,22 +111,30 @@ namespace {
return region(begin, end); return region(begin, end);
}; };
void usage(ostream &out, string const &cmd) {
out << "Usage: " << cmd << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-V|--version}" << endl
<< " {--region <block range>}*" << endl
<< "Where:" << endl
<< " <block range> is of the form <begin>..<one-past-the-end>" << endl
<< " for example 5..45 denotes blocks 5 to 44 inclusive, but not block 45" << endl;
}
} }
//---------------------------------------------------------------- //----------------------------------------------------------------
int thin_rmap_main(int argc, char **argv) thin_rmap_cmd::thin_rmap_cmd()
: command("thin_rmap")
{
}
void
thin_rmap_cmd::usage(std::ostream &out) const
{
out << "Usage: " << get_name() << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-V|--version}" << endl
<< " {--region <block range>}*" << endl
<< "Where:" << endl
<< " <block range> is of the form <begin>..<one-past-the-end>" << endl
<< " for example 5..45 denotes blocks 5 to 44 inclusive, but not block 45" << endl;
}
int
thin_rmap_cmd::run(int argc, char **argv)
{ {
int c; int c;
vector<region> regions; vector<region> regions;
@ -141,7 +149,7 @@ int thin_rmap_main(int argc, char **argv)
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch (c) { switch (c) {
case 'h': case 'h':
usage(cout, basename(argv[0])); usage(cout);
return 0; return 0;
case 'V': case 'V':
@ -161,20 +169,18 @@ int thin_rmap_main(int argc, char **argv)
break; break;
default: default:
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
} }
if (argc == optind) { if (argc == optind) {
cerr << "No input file provided." << endl; cerr << "No input file provided." << endl;
usage(cerr, basename(argv[0])); usage(cerr);
exit(1); exit(1);
} }
return rmap(argv[optind], regions); return rmap(argv[optind], regions);
} }
base::command thin_provisioning::thin_rmap_cmd("thin_rmap", thin_rmap_main);
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -135,14 +135,6 @@ namespace {
return 0; return 0;
} }
void usage(ostream &out, string const &cmd) {
out << "Usage: " << cmd << " [options] {device|file}\n"
<< "Options:\n"
<< " {--pool-inactive}\n"
<< " {-h|--help}\n"
<< " {-V|--version}" << endl;
}
struct flags { struct flags {
boost::optional<string> metadata_dev; boost::optional<string> metadata_dev;
boost::optional<string> data_dev; boost::optional<string> data_dev;
@ -151,7 +143,23 @@ namespace {
//---------------------------------------------------------------- //----------------------------------------------------------------
int thin_trim_main(int argc, char **argv) thin_trim_cmd::thin_trim_cmd()
: command("thin_trim")
{
}
void
thin_trim_cmd::usage(std::ostream &out) const
{
out << "Usage: " << get_name() << " [options] {device|file}\n"
<< "Options:\n"
<< " {--pool-inactive}\n"
<< " {-h|--help}\n"
<< " {-V|--version}" << endl;
}
int
thin_trim_cmd::run(int argc, char **argv)
{ {
int c; int c;
flags fs; flags fs;
@ -177,7 +185,7 @@ int thin_trim_main(int argc, char **argv)
break; break;
case 'h': case 'h':
usage(cout, basename(argv[0])); usage(cout);
return 0; return 0;
case 'V': case 'V':
@ -185,13 +193,13 @@ int thin_trim_main(int argc, char **argv)
return 0; return 0;
default: default:
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }
} }
if (!fs.metadata_dev || !fs.data_dev) { if (!fs.metadata_dev || !fs.data_dev) {
usage(cerr, basename(argv[0])); usage(cerr);
return 1; return 1;
} }