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

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