diff mbox series

[libubootenv,V2] Dont store to device if no value changes

Message ID 20200801181258.23323-1-liu.ming50@gmail.com
State Changes Requested
Headers show
Series [libubootenv,V2] Dont store to device if no value changes | expand

Commit Message

Ming Liu Aug. 1, 2020, 6:12 p.m. UTC
From: Ming Liu <liu.ming50@gmail.com>

Upstream-Status: Submitted

When fw_setenv is called, it could happen that the new value is same
with the old one, in which case, we should avoid storing data to
device.

Signed-off-by: Ming Liu <liu.ming50@gmail.com>
---
 src/fw_printenv.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/src/fw_printenv.c b/src/fw_printenv.c
index 18887f9..c77ae82 100644
--- a/src/fw_printenv.c
+++ b/src/fw_printenv.c
@@ -151,19 +151,31 @@  int main (int argc, char **argv) {
 			}
 		}
 	} else { /* setenv branch */
+		bool need_store = true;
 		if (scriptfile)
 			libuboot_load_file(ctx, scriptfile);
 		else {
 			for (i = 0; i < argc; i += 2) {
-				if (i + 1 == argc)
-					libuboot_set_env(ctx, argv[i], NULL);
-				else
-					libuboot_set_env(ctx, argv[i], argv[i+1]);
+				value = libuboot_get_env(ctx, argv[i]);
+				if (i + 1 == argc) {
+					if (value != NULL)
+						libuboot_set_env(ctx, argv[i], NULL);
+					else
+						need_store = false;
+				} else {
+					if (strcmp(value, argv[i+1]) != 0)
+						libuboot_set_env(ctx, argv[i], argv[i+1]);
+					else
+						need_store = false;
+				}
 			}
 		}
-		ret = libuboot_env_store(ctx);
-		if (ret)
-			fprintf(stderr, "Error storing the env\n");
+
+		if (need_store) {
+			ret = libuboot_env_store(ctx);
+			if (ret)
+				fprintf(stderr, "Error storing the env\n");
+		}
 	}
 
 	libuboot_close(ctx);