Blame SOURCES/0012-thin_delta-Clean-up-duplicated-code.patch

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