diff mbox series

[U-Boot,v2,3/3] env: Provide programmatic equivalent to 'setenv -f'

Message ID 0102016e8e614227-a8c99a21-6a99-4611-b510-a723bc021b52-000000@eu-west-1.amazonses.com
State Deferred
Headers show
Series [U-Boot,v2,1/3] tools: checkpatch: Restore 'debug' and 'printf' to logFunctions list | expand

Commit Message

James Byrne Nov. 21, 2019, 2:32 p.m. UTC
Add env_force_set() to provide an equivalent to 'setenv -f' that can be
used programmatically.

Signed-off-by: James Byrne <james.byrne@origamienergy.com>
---

Changes in v2: None

 cmd/nvedit.c  | 17 ++++++++++++++---
 include/env.h | 13 +++++++++++++
 2 files changed, 27 insertions(+), 3 deletions(-)

Comments

Simon Glass Dec. 28, 2019, 2:26 a.m. UTC | #1
On Thu, 21 Nov 2019 at 07:32, James Byrne <james.byrne@origamienergy.com> wrote:
>
> Add env_force_set() to provide an equivalent to 'setenv -f' that can be
> used programmatically.
>
> Signed-off-by: James Byrne <james.byrne@origamienergy.com>
> ---
>
> Changes in v2: None
>
>  cmd/nvedit.c  | 17 ++++++++++++++---
>  include/env.h | 13 +++++++++++++
>  2 files changed, 27 insertions(+), 3 deletions(-)

Reviewed-by: Simon Glass <sjg@chromium.org>
Wolfgang Denk Jan. 30, 2020, 8:40 p.m. UTC | #2
Dear James,

In message <0102016e8e614227-a8c99a21-6a99-4611-b510-a723bc021b52-000000@eu-west-1.amazonses.com> you wrote:
> Add env_force_set() to provide an equivalent to 'setenv -f' that can be
> used programmatically.

env_set_forced() ?

> -int env_set(const char *varname, const char *varvalue)
> +static int do_programmatic_env_set(const char *varname, const char *varvalue,
> +				   int env_flag)
>  {
>  	/* before import into hashtable */
>  	if (!(gd->flags & GD_FLG_ENV_READY))
>  		return 1;
>  
>  	if (!varvalue || varvalue[0] == '\0')
> -		return do_env_remove(varname, H_PROGRAMMATIC);
> +		return do_env_remove(varname, H_PROGRAMMATIC | env_flag);
> +
> +	return do_env_update(varname, varvalue, H_PROGRAMMATIC | env_flag);
> +}
> +
> +int env_set(const char *varname, const char *varvalue)
> +{
> +	return do_programmatic_env_set(varname, varvalue, 0);
> +}
>  
> -	return do_env_update(varname, varvalue, H_PROGRAMMATIC);
> +int env_force_set(const char *varname, const char *varvalue)
> +{
> +	return do_programmatic_env_set(varname, varvalue, H_FORCE);
>  }

You add another level of function nesting and add more lines of code
than if just coppying the 3 C statements.

If possible, please also try not to come up with so awfully long
names like do_programmatic_env_set() - hey, this is allprogrammatic
what we're coding here, isn;t it?


Best regards,

Wolfgang Denk
diff mbox series

Patch

diff --git a/cmd/nvedit.c b/cmd/nvedit.c
index b30669a45e..106c69147b 100644
--- a/cmd/nvedit.c
+++ b/cmd/nvedit.c
@@ -241,16 +241,27 @@  static int do_env_update(const char *name, const char *value, int env_flag)
 	return 0;
 }
 
-int env_set(const char *varname, const char *varvalue)
+static int do_programmatic_env_set(const char *varname, const char *varvalue,
+				   int env_flag)
 {
 	/* before import into hashtable */
 	if (!(gd->flags & GD_FLG_ENV_READY))
 		return 1;
 
 	if (!varvalue || varvalue[0] == '\0')
-		return do_env_remove(varname, H_PROGRAMMATIC);
+		return do_env_remove(varname, H_PROGRAMMATIC | env_flag);
+
+	return do_env_update(varname, varvalue, H_PROGRAMMATIC | env_flag);
+}
+
+int env_set(const char *varname, const char *varvalue)
+{
+	return do_programmatic_env_set(varname, varvalue, 0);
+}
 
-	return do_env_update(varname, varvalue, H_PROGRAMMATIC);
+int env_force_set(const char *varname, const char *varvalue)
+{
+	return do_programmatic_env_set(varname, varvalue, H_FORCE);
 }
 
 #ifndef CONFIG_SPL_BUILD
diff --git a/include/env.h b/include/env.h
index b72239f6a5..da54f51805 100644
--- a/include/env.h
+++ b/include/env.h
@@ -145,6 +145,19 @@  int env_get_yesno(const char *var);
  */
 int env_set(const char *varname, const char *value);
 
+/**
+ * env_force_set() - forcibly set an environment variable
+ *
+ * This sets or deletes the value of an environment variable. It is the same
+ * as env_set(), except that the variable will be forcibly updated/deleted,
+ * even if it has access protection flags set.
+ *
+ * @varname: Variable to adjust
+ * @value: Value to set for the variable, or NULL or "" to delete the variable
+ * @return 0 if OK, 1 on error
+ */
+int env_force_set(const char *varname, const char *varvalue);
+
 /**
  * env_get_ulong() - Return an environment variable as an integer value
  *