summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAhmad Fatoum <a.fatoum@pengutronix.de>2020-11-13 20:08:56 +0100
committerRoland Hieber <rhi@pengutronix.de>2021-03-16 15:23:44 +0100
commit0a75604bc61f74fb798001f308cf8b6406974db9 (patch)
tree217ffbaa432a975ec9a9b20e1ca725fe8a7839c8
parent4826dcbce7498b255e9840ad903fea8eca9e1bcf (diff)
downloaddt-utils-0a75604bc61f74fb798001f308cf8b6406974db9.tar.gz
dt-utils-0a75604bc61f74fb798001f308cf8b6406974db9.tar.xz
barebox-state: have the --set option to avoid writes if possible
barebox-state --set always dirties the state when successful. Users seeking to conserve write cycles thus have to --get the variable in question first to check whether to write it. Make life of such users easier by having barebox-state support this out-of-the-box. This allows users to fire and forget execute barebox-state. This arguably should have been the behavior from the beginning, the state implementation shared by barebox and dt-utils already marks the state dirty if buckets appear corrupted on probe, so there is no extra benefit in always executing the write. The comparison to determine whether the state should be dirtied does an extra allocation in interest of clarity. This overhead is deemed negligible compared to I/O and it makes the code easier to follow. Suggested-by: Jan Lübbe <jlu@pengutronix.de> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Message-Id: <20201113190856.3197-1-a.fatoum@pengutronix.de> Signed-off-by: Roland Hieber <rhi@pengutronix.de>
-rw-r--r--src/barebox-state.c9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/barebox-state.c b/src/barebox-state.c
index 7ac90c6..afd18c6 100644
--- a/src/barebox-state.c
+++ b/src/barebox-state.c
@@ -283,6 +283,7 @@ static int state_set_var(struct state *state, const char *var, const char *val)
{
struct state_variable *sv;
struct variable_str_type *vtype;
+ char *oldval;
int ret;
sv = state_find_var(state, var);
@@ -296,6 +297,14 @@ static int state_set_var(struct state *state, const char *var, const char *val)
if (!vtype->set)
return -EPERM;
+ oldval = vtype->get(sv);
+ if (!IS_ERR(oldval)) {
+ bool equal = strcmp(oldval, val) == 0;
+ free(oldval);
+ if (equal)
+ return 0;
+ }
+
ret = vtype->set(sv, val);
if (ret)
return ret;