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

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