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