[Accel-config] [PATCH v1 7/7] accel-config: Fix and refactor status return and printing

ramesh.thomas at intel.com
Newsgroups dev.linux.lists.accel-config
Message-ID <[email protected]>
From: Ramesh Thomas <ramesh.thomas(a)intel.com>

Return values should be 0 = success and < 0 = error. Some places were
not return correct values. Simplify the return and status messages.

Signed-off-by: Ramesh Thomas <ramesh.thomas(a)intel.com>
---
 accfg/enable.c | 88 ++++++++++++++------------------------------------
 1 file changed, 24 insertions(+), 64 deletions(-)

diff --git a/accfg/enable.c b/accfg/enable.c
index 642e30a..9cb6794 100644
--- a/accfg/enable.c
+++ b/accfg/enable.c
@@ -100,7 +100,7 @@ static int device_action(int argc, const char **argv, const char *usage,
 		usage,
 		NULL
 	};
-	int i, rc = 0, success = 0, fail = 0, fail_reason = 0;
+	int i, rc = -EINVAL, success = 0;
 	const char *all = "all";
 	enum accfg_device_state state;
 
@@ -139,13 +139,15 @@ static int device_action(int argc, const char **argv, const char *usage,
 				 * matches with the enable/disable
 				 */
 				state = accfg_device_get_state(device);
-				if (((state == 1) && (action == DEV_ACTION_ENABLE))
-				    ||((state == 0) && (action == DEV_ACTION_DISABLE)))
-					success++;
-				else
-					fail++;
-			} else if (!fail) {
-				fail_reason = rc;
+				if (((state != ACCFG_DEVICE_ENABLED) &&
+						(action == DEV_ACTION_ENABLE)) ||
+						((state != ACCFG_DEVICE_DISABLED) &&
+						 (action == DEV_ACTION_DISABLE)))
+					rc = ENXIO;
+			}
+			if (rc == 0) {
+				success++;
+			} else {
 				fprintf(stderr, "failed in %s\n", argv[i]);
 
 				print_device_cmd_status(device);
@@ -157,34 +159,14 @@ static int device_action(int argc, const char **argv, const char *usage,
 					argv[i]);
 	}
 
-	if (success) {
-		if (action == DEV_ACTION_ENABLE)
-			fprintf(stderr, "successfully enabled %d device%s\n",
-					success, success > 1 ? "s" : "");
-		else
-			fprintf(stderr, "successfully disabled %d device%s\n",
-					success, success > 1 ? "s" : "");
-		return success;
-	}
-
-	if (fail) {
-		if (action == DEV_ACTION_ENABLE)
-			fprintf(stderr, "failed to enable %d device%s\n", fail,
-				fail > 1 ? "s" : "");
-		else
-			fprintf(stderr, "failed to disable %d device%s\n", fail,
-				fail > 1 ? "s" : "");
+	fprintf(stderr, "%s %d device(s) out of %d\n",
+			action == DEV_ACTION_ENABLE ? "enabled" : "disabled",
+				success, argc);
 
-		return fail;
-	}
+	if (success)
+		return 0;
 
-	if (fail_reason) {
-		fprintf(stderr, "failed due to reason %s\n",
-				strerror(abs(fail_reason)));
-		return fail_reason;
-	}
-
-	return -ENXIO;
+	return rc;
 }
 
 static int action_disable_wq(struct accfg_wq *wq, const char *wq_name)
@@ -245,7 +227,7 @@ static int wq_action(int argc, const char **argv, const char *usage,
 		NULL
 	};
 	unsigned long dev_id, wq_id;
-	int i, rc = 0, success = 0, fail = 0, fail_reason = 0;
+	int i, rc = -EINVAL, success = 0;
 	const char *all = "all";
 
 	argc = parse_options(argc, argv, options, u, 0);
@@ -281,7 +263,7 @@ static int wq_action(int argc, const char **argv, const char *usage,
 
 		rc = sprintf(wq_name, "wq%lu.%lu", dev_id, wq_id);
 		if (rc < 0)
-			return errno;
+			return rc;
 
 		accfg_device_foreach(ctx, device) {
 			if (!util_device_filter(device, dev_name))
@@ -294,8 +276,6 @@ static int wq_action(int argc, const char **argv, const char *usage,
 				if (rc == 0) {
 					success++;
 				} else {
-					fail++;
-					fail_reason = rc;
 					fprintf(stderr, "failed in %s\n", wq_name);
 
 					print_device_cmd_status(device);
@@ -307,34 +287,14 @@ static int wq_action(int argc, const char **argv, const char *usage,
 			fprintf(stderr, "no wq matches id: %s\n", wq_name);
 	}
 
-	if (success) {
-		if (action == WQ_ACTION_ENABLE)
-			fprintf(stderr, "successfully enabled %d wq%s\n",
-					success,
-			success > 1 ? "s" : "");
-		else
-			fprintf(stderr, "successfully disabled %d wq%s\n",
-					success,
-			success > 1 ? "s" : "");
-		return success;
-	}
+	fprintf(stderr, "%s %d wq(s) out of %d\n",
+			action == WQ_ACTION_ENABLE ? "enabled" : "disabled",
+				success, argc);
 
-	if (fail) {
-		if (action == WQ_ACTION_ENABLE)
-			fprintf(stderr, "failed to enable %d wq%s\n",
-			fail, fail > 1 ? "s" : "");
-		else
-			fprintf(stderr, "failed to disable %d wq%s\n",
-			fail, fail > 1 ? "s" : "");
-		return fail;
-	}
+	if (success)
+		return 0;
 
-	if (fail_reason) {
-		fprintf(stderr, "failed due to reason %s\n",
-			strerror(abs(fail_reason)));
-		return fail_reason;
-	}
-	return -ENXIO;
+	return rc;
 }
 
 int cmd_disable_device(int argc, const char **argv, void *ctx)
-- 
2.26.2
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.