File 0003-bcache-tools-Add-parameter-check.patch of Package bcache-tools.14286

From fbcfdfc409f37274d00bd730102bc5c20719ea12 Mon Sep 17 00:00:00 2001
From: Shaoxiong Li <dahefanteng@gmail.com>
Date: Thu, 13 Sep 2018 14:23:08 +0800
Subject: [PATCH 08/16] bcache-tools: Add parameter check
Git-commit: fbcfdfc409f37274d00bd730102bc5c20719ea12
Patch-mainline: bcache-tools-1.1
References: bsc#1139948

Check parameter when using 'bcache set-label' and 'bcache set-cachemode'

Signed-off-by: Shaoxiong Li <dahefanteng@gmail.com>
Signed-off-by: Coly Li <colyli@suse.de>
---
 bcache-main.c | 37 ++++++++++++++++++++++++++++++++++++-
 lib.c         |  3 +--
 2 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/bcache-main.c b/bcache-main.c
index e424535..dd90d50 100644
--- a/bcache-main.c
+++ b/bcache-main.c
@@ -452,7 +452,7 @@ int attach_both(char *cdev, char *backdev)
 	char buf[100];
 
 	ret = detail_dev(backdev, &bd, &cd, &type);
-	if (ret < 0)
+	if (ret != 0)
 		return ret;
 	if (type != BCACHE_SB_VERSION_BDEV
 	    && type != BCACHE_SB_VERSION_BDEV_WITH_OFFSET) {
@@ -613,6 +613,24 @@ int main(int argc, char **argv)
 			fprintf(stderr, "Error:Wrong device name found\n");
 			return 1;
 		}
+		struct bdev bd;
+		struct cdev cd;
+		int type = 1;
+		int ret;
+
+		ret = detail_dev(argv[1], &bd, &cd, &type);
+		if (ret != 0) {
+			fprintf(stderr,
+		"This device doesn't exist or failed to receive info from this device\n");
+			return ret;
+		}
+		if (type != BCACHE_SB_VERSION_BDEV
+		    && type != BCACHE_SB_VERSION_BDEV_WITH_OFFSET) {
+			fprintf(stderr,
+				"Only backend device is suppported\n");
+			return 1;
+		}
+		return set_backdev_cachemode(argv[1], argv[2]);
 	} else if (strcmp(subcmd, "set-label") == 0) {
 		if (argc != 3)
 			return setlabel_usage();
@@ -620,6 +638,23 @@ int main(int argc, char **argv)
 			fprintf(stderr, "Error:Wrong device name found\n");
 			return 1;
 		}
+		struct bdev bd;
+		struct cdev cd;
+		int type = 5;
+		int ret;
+
+		ret = detail_dev(argv[1], &bd, &cd, &type);
+		if (ret != 0) {
+			fprintf(stderr,
+		"This device doesn't exist or failed to receive info from this device\n");
+			return ret;
+		}
+		if (type != BCACHE_SB_VERSION_BDEV
+		    && type != BCACHE_SB_VERSION_BDEV_WITH_OFFSET) {
+			fprintf(stderr,
+				"Only backend device is suppported\n");
+			return 1;
+		}
 		if (strlen(argv[2]) >= SB_LABEL_SIZE) {
 			fprintf(stderr, "Label is too long\n");
 			return 1;
diff --git a/lib.c b/lib.c
index 6592110..9e69419 100644
--- a/lib.c
+++ b/lib.c
@@ -669,8 +669,7 @@ int set_label(char *devname, char *label)
 	fd = open(path, O_WRONLY);
 	if (fd < 0) {
 		fprintf(stderr,
-			"Can't open %s,Make sure the device name is correct\n",
-			path);
+			"Please register this device first\n");
 		return 1;
 	}
 	if (dprintf(fd, "%s\n", label) < 0) {
-- 
2.25.0
openSUSE Build Service is sponsored by