|
|
4ee10e |
From 2e755c67064c0157e646acfa57fbcc738905f7d4 Mon Sep 17 00:00:00 2001
|
|
|
4ee10e |
From: Ming-Hung Tsai <mtsai@redhat.com>
|
|
|
4ee10e |
Date: Tue, 20 Oct 2020 15:18:06 +0800
|
|
|
4ee10e |
Subject: [PATCH 2/2] [thin_delta] Clean up duplicated code
|
|
|
4ee10e |
|
|
|
4ee10e |
---
|
|
|
4ee10e |
thin-provisioning/thin_delta.cc | 105 ++++++++++++----------------------------
|
|
|
4ee10e |
1 file changed, 31 insertions(+), 74 deletions(-)
|
|
|
4ee10e |
|
|
|
4ee10e |
diff --git a/thin-provisioning/thin_delta.cc b/thin-provisioning/thin_delta.cc
|
|
|
4ee10e |
index ee48dea..2ff3e69 100644
|
|
|
4ee10e |
--- a/thin-provisioning/thin_delta.cc
|
|
|
4ee10e |
+++ b/thin-provisioning/thin_delta.cc
|
|
|
4ee10e |
@@ -22,47 +22,7 @@ using namespace thin_provisioning;
|
|
|
4ee10e |
|
|
|
4ee10e |
//----------------------------------------------------------------
|
|
|
4ee10e |
|
|
|
4ee10e |
-namespace local {
|
|
|
4ee10e |
- class application {
|
|
|
4ee10e |
- public:
|
|
|
4ee10e |
- application(string const &cmd)
|
|
|
4ee10e |
- : cmd_(cmd) {
|
|
|
4ee10e |
- }
|
|
|
4ee10e |
-
|
|
|
4ee10e |
- void usage(ostream &out) {
|
|
|
4ee10e |
- out << "Usage: " << cmd_ << " [options] <device or file>\n"
|
|
|
4ee10e |
- << "Options:\n"
|
|
|
4ee10e |
- << " {--thin1, --snap1}\n"
|
|
|
4ee10e |
- << " {--thin2, --snap2}\n"
|
|
|
4ee10e |
- << " {-m, --metadata-snap} [block#]\n"
|
|
|
4ee10e |
- << " {--verbose}\n"
|
|
|
4ee10e |
- << " {-h|--help}\n"
|
|
|
4ee10e |
- << " {-V|--version}" << endl;
|
|
|
4ee10e |
- }
|
|
|
4ee10e |
-
|
|
|
4ee10e |
- void die(string const &msg) {
|
|
|
4ee10e |
- cerr << msg << endl;
|
|
|
4ee10e |
- usage(cerr);
|
|
|
4ee10e |
- exit(1);
|
|
|
4ee10e |
- }
|
|
|
4ee10e |
-
|
|
|
4ee10e |
- uint64_t parse_int(string const &str, string const &desc) {
|
|
|
4ee10e |
- try {
|
|
|
4ee10e |
- return boost::lexical_cast<uint64_t>(str);
|
|
|
4ee10e |
-
|
|
|
4ee10e |
- } catch (...) {
|
|
|
4ee10e |
- ostringstream out;
|
|
|
4ee10e |
- out << "Couldn't parse " << desc << ": '" << str << "'";
|
|
|
4ee10e |
- die(out.str());
|
|
|
4ee10e |
- }
|
|
|
4ee10e |
-
|
|
|
4ee10e |
- return 0; // never get here
|
|
|
4ee10e |
- }
|
|
|
4ee10e |
-
|
|
|
4ee10e |
- private:
|
|
|
4ee10e |
- string cmd_;
|
|
|
4ee10e |
- };
|
|
|
4ee10e |
-
|
|
|
4ee10e |
+namespace {
|
|
|
4ee10e |
struct flags {
|
|
|
4ee10e |
flags()
|
|
|
4ee10e |
: verbose(false),
|
|
|
4ee10e |
@@ -96,13 +56,6 @@ namespace local {
|
|
|
4ee10e |
uint64_t vbegin_, dbegin_, len_;
|
|
|
4ee10e |
};
|
|
|
4ee10e |
|
|
|
4ee10e |
- ostream &operator <<(ostream &out, mapping const &m) {
|
|
|
4ee10e |
- out << "mapping[vbegin = " << m.vbegin_
|
|
|
4ee10e |
- << ", dbegin = " << m.dbegin_
|
|
|
4ee10e |
- << ", len = " << m.len_ << "]";
|
|
|
4ee10e |
- return out;
|
|
|
4ee10e |
- }
|
|
|
4ee10e |
-
|
|
|
4ee10e |
//--------------------------------
|
|
|
4ee10e |
|
|
|
4ee10e |
template <typename Container>
|
|
|
4ee10e |
@@ -542,7 +495,7 @@ namespace local {
|
|
|
4ee10e |
out << "</diff>\n";
|
|
|
4ee10e |
}
|
|
|
4ee10e |
|
|
|
4ee10e |
- void delta_(application &app, flags const &fs) {
|
|
|
4ee10e |
+ void delta_(flags const &fs) {
|
|
|
4ee10e |
mapping_recorder mr1;
|
|
|
4ee10e |
mapping_recorder mr2;
|
|
|
4ee10e |
damage_visitor damage_v;
|
|
|
4ee10e |
@@ -560,7 +513,7 @@ namespace local {
|
|
|
4ee10e |
if (!snap1_root) {
|
|
|
4ee10e |
ostringstream out;
|
|
|
4ee10e |
out << "Unable to find mapping tree for snap1 (" << *fs.snap1 << ")";
|
|
|
4ee10e |
- app.die(out.str());
|
|
|
4ee10e |
+ throw std::runtime_error(out.str());
|
|
|
4ee10e |
}
|
|
|
4ee10e |
|
|
|
4ee10e |
single_mapping_tree snap1(*md->tm_, *snap1_root,
|
|
|
4ee10e |
@@ -572,7 +525,7 @@ namespace local {
|
|
|
4ee10e |
if (!snap2_root) {
|
|
|
4ee10e |
ostringstream out;
|
|
|
4ee10e |
out << "Unable to find mapping tree for snap2 (" << *fs.snap2 << ")";
|
|
|
4ee10e |
- app.die(out.str());
|
|
|
4ee10e |
+ throw std::runtime_error(out.str());
|
|
|
4ee10e |
}
|
|
|
4ee10e |
|
|
|
4ee10e |
single_mapping_tree snap2(*md->tm_, *snap2_root,
|
|
|
4ee10e |
@@ -609,12 +562,12 @@ namespace local {
|
|
|
4ee10e |
end_superblock(is);
|
|
|
4ee10e |
}
|
|
|
4ee10e |
|
|
|
4ee10e |
- int delta(application &app, flags const &fs) {
|
|
|
4ee10e |
+ int delta(flags const &fs) {
|
|
|
4ee10e |
try {
|
|
|
4ee10e |
- delta_(app, fs);
|
|
|
4ee10e |
+ delta_(fs);
|
|
|
4ee10e |
} catch (exception const &e) {
|
|
|
4ee10e |
- app.die(e.what());
|
|
|
4ee10e |
- return 1; // never get here
|
|
|
4ee10e |
+ cerr << e.what() << endl;
|
|
|
4ee10e |
+ return 1;
|
|
|
4ee10e |
}
|
|
|
4ee10e |
|
|
|
4ee10e |
return 0;
|
|
|
4ee10e |
@@ -633,27 +586,31 @@ thin_delta_cmd::thin_delta_cmd()
|
|
|
4ee10e |
void
|
|
|
4ee10e |
thin_delta_cmd::usage(std::ostream &out) const
|
|
|
4ee10e |
{
|
|
|
4ee10e |
- // FIXME: finish
|
|
|
4ee10e |
+ out << "Usage: " << get_name() << " [options] <device or file>\n"
|
|
|
4ee10e |
+ << "Options:\n"
|
|
|
4ee10e |
+ << " {--thin1, --snap1}\n"
|
|
|
4ee10e |
+ << " {--thin2, --snap2}\n"
|
|
|
4ee10e |
+ << " {-m, --metadata-snap} [block#]\n"
|
|
|
4ee10e |
+ << " {--verbose}\n"
|
|
|
4ee10e |
+ << " {-h|--help}\n"
|
|
|
4ee10e |
+ << " {-V|--version}" << endl;
|
|
|
4ee10e |
}
|
|
|
4ee10e |
|
|
|
4ee10e |
int
|
|
|
4ee10e |
thin_delta_cmd::run(int argc, char **argv)
|
|
|
4ee10e |
{
|
|
|
4ee10e |
- using namespace local;
|
|
|
4ee10e |
-
|
|
|
4ee10e |
int c;
|
|
|
4ee10e |
flags fs;
|
|
|
4ee10e |
- local::application app(basename(argv[0]));
|
|
|
4ee10e |
|
|
|
4ee10e |
char const shortopts[] = "hVm::";
|
|
|
4ee10e |
option const longopts[] = {
|
|
|
4ee10e |
{ "help", no_argument, NULL, 'h' },
|
|
|
4ee10e |
+ { "metadata-snap", optional_argument, NULL, 'm' },
|
|
|
4ee10e |
{ "version", no_argument, NULL, 'V' },
|
|
|
4ee10e |
{ "thin1", required_argument, NULL, 1 },
|
|
|
4ee10e |
{ "snap1", required_argument, NULL, 1 },
|
|
|
4ee10e |
{ "thin2", required_argument, NULL, 2 },
|
|
|
4ee10e |
{ "snap2", required_argument, NULL, 2 },
|
|
|
4ee10e |
- { "metadata-snap", optional_argument, NULL, 'm' },
|
|
|
4ee10e |
{ "verbose", no_argument, NULL, 4 },
|
|
|
4ee10e |
{ NULL, no_argument, NULL, 0 }
|
|
|
4ee10e |
};
|
|
|
4ee10e |
@@ -661,25 +618,25 @@ thin_delta_cmd::run(int argc, char **argv)
|
|
|
4ee10e |
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
|
|
|
4ee10e |
switch (c) {
|
|
|
4ee10e |
case 'h':
|
|
|
4ee10e |
- app.usage(cout);
|
|
|
4ee10e |
+ usage(cout);
|
|
|
4ee10e |
return 0;
|
|
|
4ee10e |
|
|
|
4ee10e |
+ case 'm':
|
|
|
4ee10e |
+ fs.use_metadata_snap = true;
|
|
|
4ee10e |
+ if (optarg)
|
|
|
4ee10e |
+ fs.metadata_snap = parse_uint64(optarg, "metadata snapshot block");
|
|
|
4ee10e |
+ break;
|
|
|
4ee10e |
+
|
|
|
4ee10e |
case 'V':
|
|
|
4ee10e |
cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
|
|
|
4ee10e |
return 0;
|
|
|
4ee10e |
|
|
|
4ee10e |
case 1:
|
|
|
4ee10e |
- fs.snap1 = app.parse_int(optarg, "thin id 1");
|
|
|
4ee10e |
+ fs.snap1 = parse_uint64(optarg, "thin id 1");
|
|
|
4ee10e |
break;
|
|
|
4ee10e |
|
|
|
4ee10e |
case 2:
|
|
|
4ee10e |
- fs.snap2 = app.parse_int(optarg, "thin id 2");
|
|
|
4ee10e |
- break;
|
|
|
4ee10e |
-
|
|
|
4ee10e |
- case 'm':
|
|
|
4ee10e |
- fs.use_metadata_snap = true;
|
|
|
4ee10e |
- if (optarg)
|
|
|
4ee10e |
- fs.metadata_snap = app.parse_int(optarg, "metadata snapshot block");
|
|
|
4ee10e |
+ fs.snap2 = parse_uint64(optarg, "thin id 2");
|
|
|
4ee10e |
break;
|
|
|
4ee10e |
|
|
|
4ee10e |
case 4:
|
|
|
4ee10e |
@@ -687,23 +644,23 @@ thin_delta_cmd::run(int argc, char **argv)
|
|
|
4ee10e |
break;
|
|
|
4ee10e |
|
|
|
4ee10e |
default:
|
|
|
4ee10e |
- app.usage(cerr);
|
|
|
4ee10e |
+ usage(cerr);
|
|
|
4ee10e |
return 1;
|
|
|
4ee10e |
}
|
|
|
4ee10e |
}
|
|
|
4ee10e |
|
|
|
4ee10e |
if (argc == optind)
|
|
|
4ee10e |
- app.die("No input device provided.");
|
|
|
4ee10e |
+ die("No input device provided.");
|
|
|
4ee10e |
else
|
|
|
4ee10e |
fs.dev = argv[optind];
|
|
|
4ee10e |
|
|
|
4ee10e |
if (!fs.snap1)
|
|
|
4ee10e |
- app.die("--snap1 not specified.");
|
|
|
4ee10e |
+ die("--snap1 not specified.");
|
|
|
4ee10e |
|
|
|
4ee10e |
if (!fs.snap2)
|
|
|
4ee10e |
- app.die("--snap2 not specified.");
|
|
|
4ee10e |
+ die("--snap2 not specified.");
|
|
|
4ee10e |
|
|
|
4ee10e |
- return delta(app, fs);
|
|
|
4ee10e |
+ return delta(fs);
|
|
|
4ee10e |
}
|
|
|
4ee10e |
|
|
|
4ee10e |
//----------------------------------------------------------------
|
|
|
4ee10e |
--
|
|
|
4ee10e |
1.8.3.1
|
|
|
4ee10e |
|