Skip to contentSkip to conversation

Fixes for nv command handling

PATCH Superseded Enrico Jorns 2 participants 9 messages Sep 13, 2017 – Sep 14, 2017

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Fixes for nv command handling

This series adds a couple of fixes and enhancement in option parsing and error handling for the 'nv' command.

Enrico Jorns (5):
commands: nv: argc cannot be < 1
commands: nv: call nvvar_print() only if no argument is given
commands: nv: assure error code will be returned when an error
occurred
commands: nv: fail if required var arguments are missing
common: globvar: let nvvar_remove() report non-existing variable

 commands/nv.c      | 31 ++++++++++++++++++++++---------
 common/globalvar.c |  4 +++-
 2 files changed, 25 insertions(+), 10 deletions(-)
Signature · 7 lines
2.11.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

PATCH 1/5 commands: nv: argc cannot be < 1

argc == 1 will only contain the name of the program itself which must be present when invoking nv command code.

---
 commands/nv.c | 3 ---
 1 file changed, 3 deletions(-)
commands/nv.c +0 -3
index 37cdb96647..eb396bfd9b 100644
@@ -55,9 +55,6 @@ static int do_nv(int argc, char *argv[])
 	argc -= optind;
 	argv += optind;
 
-	if (argc < 1)
-		return COMMAND_ERROR_USAGE;
-
 	for (i = 0; i < argc; i++) {
 		value = strchr(argv[0], '=');
 		if (value) {
Signature · 7 lines
2.11.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw

Re: [PATCH 1/5] commands: nv: argc cannot be < 1

Quoted 2 lines · Enrico Jorns

argc == 1 will only contain the name of the program itself which must be present when invoking nv command code.

This reasoning is not quite right. You miss the argc -= optind above the argc < 1 test. In fact in patch 4/5 you re-add the same test again (written as argc == 0 this time).

Shouldn't this patch simply be merged with 4/5?

Sascha

Quoted 29 lines
---
 commands/nv.c | 3 ---
 1 file changed, 3 deletions(-)
commands/nv.c +0 -3
index 37cdb96647..eb396bfd9b 100644
@@ -55,9 +55,6 @@ static int do_nv(int argc, char *argv[])
 	argc -= optind;
 	argv += optind;
 
-	if (argc < 1)
-		return COMMAND_ERROR_USAGE;
-
 	for (i = 0; i < argc; i++) {
 		value = strchr(argv[0], '=');
 		if (value) {
Signature · 7 lines
2.11.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw

Re: [PATCH 1/5] commands: nv: argc cannot be < 1

Quoted 9 lines · Sascha Hauer
Quoted 2 lines · Enrico Jorns

argc == 1 will only contain the name of the program itself which must be present when invoking nv command code.

This reasoning is not quite right. You miss the argc -= optind above the argc < 1 test. In fact in patch 4/5 you re-add the same test again (written as argc == 0 this time).

Shouldn't this patch simply be merged with 4/5?

In fact, you're right. Seems as if I splitted up changes a bit too much and confused myself. Merging it with 4/5 sound more reasonable (or simply drop both of them).

Enrico

Signature · 10 lines
Pengutronix e.K.                           | Enrico Jörns                |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-5080 |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#4 lore raw

PATCH 2/5 commands: nv: call nvvar_print() only if no argument is given

In reply to Enrico Jorns

Printing the nv variables (before performing any modification!) is useless and irritating.

---
 commands/nv.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
commands/nv.c +5 -4
index eb396bfd9b..b141198a93 100644
@@ -44,14 +44,15 @@ static int do_nv(int argc, char *argv[])
 		}
 	}
 
-	if (do_save)
-		return nvvar_save();
-
-	if (argc == optind) {
+	if (argc == 1) {
 		nvvar_print();
 		return 0;
 	}
 
+	if (do_save) {
+		return nvvar_save();
+	}
+
 	argc -= optind;
 	argv += optind;
 
Signature · 7 lines
2.11.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#5 lore raw

PATCH 3/5 commands: nv: assure error code will be returned when an error occurred

In reply to Enrico Jorns

Due to iteration over possibly multiple variables, the return value of an individual call to nvvar_remove() / nvvar_add() gets lost.

This will result in do_nv() returning success (0) if any variable processing failed as long as processing of the last variable succeeded.

Processing will not be aborted on individual errors, such as the 'cp' handles it for invalid files.

---
 commands/nv.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
commands/nv.c +14 -4
index b141198a93..d391f9e6b4 100644
@@ -28,7 +28,7 @@ static int do_nv(int argc, char *argv[])
 {
 	int opt;
 	int do_remove = 0, do_save = 0;
-	int ret, i;
+	int failed = 0, i;
 	char *value;
 
 	while ((opt = getopt(argc, argv, "rs")) > 0) {
@@ -57,19 +57,29 @@ static int do_nv(int argc, char *argv[])
 	argv += optind;
 
 	for (i = 0; i < argc; i++) {
+		int ret;
 		value = strchr(argv[0], '=');
 		if (value) {
 			*value = 0;
 			value++;
 		}
 
-		if (do_remove)
+		if (do_remove) {
 			ret = nvvar_remove(argv[i]);
-		else
+			if (ret) {
+				printf("Failed removing %s: %s\n", argv[i], strerror(-ret));
+				failed = 1;
+			}
+		} else {
 			ret = nvvar_add(argv[i], value);
+			if (ret) {
+				printf("Failed adding %s: %s\n", argv[i], strerror(-ret));
+				failed = 1;
+			}
+		}
 	}
 
-	return ret;
+	return failed;
 }
 
 BAREBOX_CMD_HELP_START(nv)
Signature · 7 lines
2.11.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#6 lore raw

PATCH 4/5 commands: nv: fail if required var arguments are missing

In reply to Enrico Jorns
---
 commands/nv.c | 5 +++++
 1 file changed, 5 insertions(+)
commands/nv.c +5 -0
index d391f9e6b4..bb13ed1db0 100644
@@ -56,6 +56,11 @@ static int do_nv(int argc, char *argv[])
 	argc -= optind;
 	argv += optind;
 
+	if (argc == 0) {
+		printf("Invalid usage: Missing argument");
+		return COMMAND_ERROR_USAGE;
+	}
+
 	for (i = 0; i < argc; i++) {
 		int ret;
 		value = strchr(argv[0], '=');
Signature · 7 lines
2.11.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#7 lore raw

PATCH 5/5 common: globvar: let nvvar_remove() report non-existing variable

In reply to Enrico Jorns

The former implementation did not allow to detect whether the call to nvvar_remove() succeeded or failed and always returned 0.

This changes the implementation to return 0 only if a variable with the given name was found and return ENOENT otherwise.

---
 common/globalvar.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
common/globalvar.c +3 -1
index fdfaf76fae..ee756e5140 100644
@@ -261,9 +261,11 @@ int nvvar_remove(const char *name)
 
 		unlink(fname);
 		free(fname);
+
+		return 0;
 	}
 
-	return 0;
+	return -ENOENT;
 }
 
 int nvvar_load(void)
Signature · 7 lines
2.11.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#8 lore raw

PATCH 1/1 commands: nv: adjust command help

In reply to Enrico Jorns

Help did not point out that -r option requires n arguments and did not mention support for removing variables in help text.

---

Noticed this change would also be required to round out the prior changes for option parsing.

 commands/nv.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
commands/nv.c +4 -4
index bb13ed1db0..e3ca9879bd 100644
@@ -89,18 +89,18 @@ static int do_nv(int argc, char *argv[])
 
 BAREBOX_CMD_HELP_START(nv)
 BAREBOX_CMD_HELP_TEXT("Add a new non volatile variable named VAR, optionally set to VALUE.")
-BAREBOX_CMD_HELP_TEXT("non volatile variables are persistent variables that overwrite the")
+BAREBOX_CMD_HELP_TEXT("Non volatile variables are persistent variables that overwrite the")
 BAREBOX_CMD_HELP_TEXT("global variables of the same name. Their value is saved implicitly with")
 BAREBOX_CMD_HELP_TEXT("'saveenv' or explicitly with 'nv -s'")
 BAREBOX_CMD_HELP_TEXT("")
 BAREBOX_CMD_HELP_TEXT("Options:")
-BAREBOX_CMD_HELP_OPT("-r", "remove non volatile variables")
-BAREBOX_CMD_HELP_OPT("-s", "Save NV variables")
+BAREBOX_CMD_HELP_OPT("-r VAR1 ...", "remove non volatile variable(s)")
+BAREBOX_CMD_HELP_OPT("-s\t", "save NV variables")
 BAREBOX_CMD_HELP_END
 
 BAREBOX_CMD_START(nv)
 	.cmd		= do_nv,
-	BAREBOX_CMD_DESC("create or set non volatile variables")
+	BAREBOX_CMD_DESC("create, set or remove non volatile variables")
 	BAREBOX_CMD_OPTS("[-r] VAR[=VALUE] ...")
 	BAREBOX_CMD_GROUP(CMD_GRP_ENV)
 	BAREBOX_CMD_HELP(cmd_nv_help)
Signature · 7 lines
2.11.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox