mailarchive of the pengutronix oss-tools mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: oss-tools@pengutronix.de
Subject: [OSS-Tools] [PATCH v2] barebox-state: have the --set option to avoid writes if possible
Date: Fri, 13 Nov 2020 20:08:56 +0100	[thread overview]
Message-ID: <20201113190856.3197-1-a.fatoum@pengutronix.de> (raw)

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>
---
v1 -> v2:
  - incorporate Jan's (off-list) suggestion to just change --set
    behavior. state implementation already dirties state if a
    bucket is corrupt, so there is really no valid use case for
    not conserving writes by default.
---
 src/barebox-state.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/barebox-state.c b/src/barebox-state.c
index cd56ce7192c3..7b5c0dae00dd 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;
-- 
2.28.0


_______________________________________________
OSS-Tools mailing list
OSS-Tools@pengutronix.de

             reply	other threads:[~2020-11-13 19:08 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-13 19:08 Ahmad Fatoum [this message]
2021-03-16 14:24 ` Roland Hieber

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201113190856.3197-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=oss-tools@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox