Skip to contentSkip to conversation

env: allow to export only selected variables

PATCH New Wolfgang Denk 2 participants 4 messages Nov 8, 2011 – Nov 17, 2011 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 env: allow to export only selected variables

New syntax:

	env export [-t | -b | -c] [-s size] addr [var ...]

With this change it is possible to provide a list of variables names that shall be exported. Whenno arguments are given, the whole environment gets exported.

NOTE: The new handling of the "size" argument means a change to the user API.

---
 common/cmd_nvedit.c    |   33 +++++++++++++++++++++------------
 common/env_dataflash.c |    2 +-
 common/env_eeprom.c    |    2 +-
 common/env_flash.c     |    4 ++--
 common/env_mmc.c       |    2 +-
 common/env_nand.c      |    4 ++--
 common/env_nvram.c     |    2 +-
 common/env_onenand.c   |    2 +-
 common/env_sf.c        |    4 ++--
 include/search.h       |    3 ++-
 lib/hashtable.c        |   15 +++++++++++++--
 11 files changed, 47 insertions(+), 26 deletions(-)
common/cmd_nvedit.c +21 -12
index 396a171..7194ade 100644
@@ -125,7 +125,7 @@ static int env_print(char *name)
 	}
 
 	/* print whole list */
-	len = hexport_r(&env_htab, '\n', &res, 0);
+	len = hexport_r(&env_htab, '\n', &res, 0, 0, NULL);
 
 	if (len > 0) {
 		puts(res);
@@ -647,7 +647,7 @@ static int do_env_delete(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv
 
 #ifdef CONFIG_CMD_EXPORTENV
 /*
- * env export [-t | -b | -c] addr [size]
+ * env export [-t | -b | -c] [-s size] addr [var ...]
  *	-t:	export as text format; if size is given, data will be
  *		padded with '\0' bytes; if not, one terminating '\0'
  *		will be added (which is included in the "filesize"
@@ -657,8 +657,12 @@ static int do_env_delete(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv
  *		'\0', list end marked by double "\0\0")
  *	-c:	export as checksum protected environment format as
  *		used for example by "saveenv" command
+ *	-s size:
+ *		size of output buffer
  *	addr:	memory address where environment gets stored
- *	size:	size of output buffer
+ *	var...	List of variable names that get included into the
+ *		export. Without arguments, the whole environment gets
+ *		exported.
  *
  * With "-c" and size is NOT given, then the export command will
  * format the data as currently used for the persistent storage,
@@ -691,7 +695,7 @@ static int do_env_export(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv
 {
 	char	buf[32];
 	char	*addr, *cmd, *res;
-	size_t	size;
+	size_t	size = 0;
 	ssize_t	len;
 	env_t	*envp;
 	char	sep = '\n';
@@ -715,6 +719,11 @@ static int do_env_export(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv
 				sep = '\0';
 				chk = 1;
 				break;
+			case 's':		/* size given */
+				if (--argc <= 0)
+					return cmd_usage(cmdtp);
+				size = simple_strtoul(*++argv, NULL, 16);
+				goto NXTARG;
 			case 't':		/* text format */
 				if (fmt++)
 					goto sep_err;
@@ -724,6 +733,7 @@ static int do_env_export(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv
 				return cmd_usage(cmdtp);
 			}
 		}
+NXTARG:		;
 	}
 
 	if (argc < 1)
@@ -731,15 +741,14 @@ static int do_env_export(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv
 
 	addr = (char *)simple_strtoul(argv[0], NULL, 16);
 
-	if (argc == 2) {
-		size = simple_strtoul(argv[1], NULL, 16);
+	if (size)
 		memset(addr, '\0', size);
-	} else {
-		size = 0;
-	}
+
+	argc--;
+	argv++;
 
 	if (sep) {		/* export as text file */
-		len = hexport_r(&env_htab, sep, &addr, size);
+		len = hexport_r(&env_htab, sep, &addr, size, argc, argv);
 		if (len < 0) {
 			error("Cannot export environment: errno = %d\n",
 				errno);
@@ -758,7 +767,7 @@ static int do_env_export(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv
 	else			/* export as raw binary data */
 		res = addr;
 
-	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
+	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, argc, argv);
 	if (len < 0) {
 		error("Cannot export environment: errno = %d\n",
 			errno);
@@ -965,7 +974,7 @@ U_BOOT_CMD(
 #if defined(CONFIG_CMD_EDITENV)
 	"env edit name - edit environment variable\n"
 #endif
-	"env export [-t | -b | -c] addr [size] - export environment\n"
+	"env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
 #if defined(CONFIG_CMD_GREPENV)
 	"env grep string [...] - search environment\n"
 #endif
common/env_dataflash.c +1 -1
index 1d57079..0c4c676 100644
@@ -68,7 +68,7 @@ int saveenv(void)
 	char	*res;
 
 	res = (char *)&env_new.data;
-	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
+	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
 	if (len < 0) {
 		error("Cannot export environment: errno = %d\n", errno);
 		return 1;
common/env_eeprom.c +1 -1
index 0a179ad..1233b1a 100644
@@ -143,7 +143,7 @@ int saveenv(void)
 	BUG_ON(env_ptr != NULL);
 
 	res = (char *)&env_new.data;
-	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
+	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
 	if (len < 0) {
 		error("Cannot export environment: errno = %d\n", errno);
 		return 1;
common/env_flash.c +2 -2
index 50ca4ffa..8e415d7 100644
@@ -155,7 +155,7 @@ int saveenv(void)
 	}
 
 	res = (char *)&env_new.data;
-	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
+	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
 	if (len < 0) {
 		error("Cannot export environment: errno = %d\n", errno);
 		goto done;
@@ -289,7 +289,7 @@ int saveenv(void)
 		goto done;
 
 	res = (char *)&env_new.data;
-	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
+	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
 	if (len < 0) {
 		error("Cannot export environment: errno = %d\n", errno);
 		goto done;
common/env_mmc.c +1 -1
index 83f40f4..12d647a 100644
@@ -124,7 +124,7 @@ int saveenv(void)
 		return 1;
 
 	res = (char *)&env_new.data;
-	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
+	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
 	if (len < 0) {
 		error("Cannot export environment: errno = %d\n", errno);
 		return 1;
common/env_nand.c +2 -2
index 14446a6..da4d3b1 100644
@@ -200,7 +200,7 @@ int saveenv(void)
 		return 1;
 
 	res = (char *)&env_new.data;
-	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
+	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
 	if (len < 0) {
 		error("Cannot export environment: errno = %d\n", errno);
 		return 1;
@@ -255,7 +255,7 @@ int saveenv(void)
 		return 1;
 
 	res = (char *)&env_new.data;
-	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
+	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
 	if (len < 0) {
 		error("Cannot export environment: errno = %d\n", errno);
 		return 1;
common/env_nvram.c +1 -1
index 544ce47..b108e6e 100644
@@ -94,7 +94,7 @@ int saveenv(void)
 	int	rcode = 0;
 
 	res = (char *)&env_new.data;
-	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
+	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
 	if (len < 0) {
 		error("Cannot export environment: errno = %d\n", errno);
 		return 1;
common/env_onenand.c +1 -1
index 5e04a06..4144475 100644
@@ -109,7 +109,7 @@ int saveenv(void)
 	};
 
 	res = (char *)&env_new.data;
-	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
+	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
 	if (len < 0) {
 		error("Cannot export environment: errno = %d\n", errno);
 		return 1;
common/env_sf.c +2 -2
index d3b36d0..a1ff297 100644
@@ -91,7 +91,7 @@ int saveenv(void)
 	}
 
 	res = (char *)&env_new.data;
-	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
+	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
 	if (len < 0) {
 		error("Cannot export environment: errno = %d\n", errno);
 		return 1;
@@ -293,7 +293,7 @@ int saveenv(void)
 	}
 
 	res = (char *)&env_new.data;
-	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
+	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
 	if (len < 0) {
 		error("Cannot export environment: errno = %d\n", errno);
 		goto done;
include/search.h +2 -1
index b4edd43..ef53edb 100644
@@ -91,7 +91,8 @@ extern int hstrstr_r(const char *__match, int __last_idx, ENTRY ** __retval,
 extern int hdelete_r(const char *__key, struct hsearch_data *__htab);
 
 extern ssize_t hexport_r(struct hsearch_data *__htab,
-		     const char __sep, char **__resp, size_t __size);
+		     const char __sep, char **__resp, size_t __size,
+		     int argc, char * const argv[]);
 
 extern int himport_r(struct hsearch_data *__htab,
 		     const char *__env, size_t __size, const char __sep,
lib/hashtable.c +13 -2
index 6895550..b7ba341 100644
@@ -478,7 +478,8 @@ static int cmpkey(const void *p1, const void *p2)
 }
 
 ssize_t hexport_r(struct hsearch_data *htab, const char sep,
-		 char **resp, size_t size)
+		 char **resp, size_t size,
+		 int argc, char * const argv[])
 {
 	ENTRY *list[htab->size];
 	char *res, *p;
@@ -502,6 +503,16 @@ ssize_t hexport_r(struct hsearch_data *htab, const char sep,
 
 		if (htab->table[i].used > 0) {
 			ENTRY *ep = &htab->table[i].entry;
+			int arg, found = 0;
+
+			for (arg = 0; arg < argc; ++arg) {
+				if (strcmp(argv[arg], ep->key) == 0) {
+					found = 1;
+					break;
+				}
+			}
+			if ((argc > 0) && (found == 0))
+				continue;
 
 			list[n++] = ep;
 
@@ -539,7 +550,7 @@ ssize_t hexport_r(struct hsearch_data *htab, const char sep,
 	/* Check if the user supplied buffer size is sufficient */
 	if (size) {
 		if (size < totlen + 1) {	/* provided buffer too small */
-			debug("### buffer too small: %d, but need %d\n",
+			printf("Env export buffer too small: %d, but need %d\n",
 				size, totlen + 1);
 			__set_errno(ENOMEM);
 			return (-1);
Signature · 1 line
1.7.6.4
#1 lore raw

PATCH 1/1 env: allow to export only selected variables

Dear Wolfgang Denk,

common/cmd_nvedit.c static int env_print(char *name) · line 131

Quoted 11 lines
common/cmd_nvedit.c +1 -1
@@ -125,7 +125,7 @@ static int env_print(char *name)
  	}
 
  	/* print whole list */
-	len = hexport_r(&env_htab, '\n',&res, 0);
+	len = hexport_r(&env_htab, '\n',&res, 0, 0, NULL);
 
  	if (len>  0) {
  		puts(res);

Here you extended the function signature by adding 2 new arguments and therefore you had to touch 12 existing function calls so to add the default values for the new args.

In my previous patches, I had renamed the sibling himport_r() function to himport_ex() with the 2 extra args and reimplemented himport_r() as a wrapper, so to maintain compatibility with the existing code. Just to realize now that, after some reworking, there is only one function call to the original himport_r() left.
I'm going to get rid of this renaming.

I mean: completely opposite approaches... funny, huh?

Best,
Gerlando Falauto

#2 lore raw

PATCH 1/1 env: allow to export only selected variables

In reply to Wolfgang Denk

Dear Wolfgang Denk,

Quoted 17 lines
include/search.h +2 -1
@@ -91,7 +91,8 @@ extern int hstrstr_r(const char *__match, int __last_idx, ENTRY ** __retval,
  extern int hdelete_r(const char *__key, struct hsearch_data *__htab);
 
  extern ssize_t hexport_r(struct hsearch_data *__htab,
-		     const char __sep, char **__resp, size_t __size);
+		     const char __sep, char **__resp, size_t __size,
+		     int argc, char * const argv[]);
 
  extern int himport_r(struct hsearch_data *__htab,
  		     const char *__env, size_t __size, const char __sep,
lib/hashtable.c +0 -0
index 6895550..b7ba341 100644

lib/hashtable.c static int cmpkey(const void *p1, const void *p2) · line 485

Quoted 9 lines
lib/hashtable.c +2 -1
@@ -478,7 +478,8 @@ static int cmpkey(const void *p1, const void *p2)
  }
 
  ssize_t hexport_r(struct hsearch_data *htab, const char sep,
-		 char **resp, size_t size)
+		 char **resp, size_t size,
+		 int argc, char * const argv[])
  {
  	ENTRY *list[htab->size];
  	char *res, *p;

What happened to "please indent with TABs only"?
Have I missed something, perhaps?

Thank you,
Gerlando Falauto

#3 lore raw

PATCH 1/1 env: allow to export only selected variables

Dear Gerlando Falauto,

Quoted 11 lines · In message <[email protected]> you wrote:

lib/hashtable.c static int cmpkey(const void *p1, const void *p2) · line 485

Quoted 8 lines

ssize_t hexport_r(struct hsearch_data *htab, const char sep,

lib/hashtable.c +2 -1
-		 char **resp, size_t size)
+		 char **resp, size_t size,
+		 int argc, char * const argv[])
  {
  	ENTRY *list[htab->size];
  	char *res, *p;

What happened to "please indent with TABs only"?
Have I missed something, perhaps?

Yes, you missed this error in the initial code review :-)

[Unfortunaltely I haven't found yet a way to convince Lindent or similar to use only TABs in places like that. Hints welcome.]

Please feel free to submit cleanup-patches.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Wenn das dann in die Hose geht, nehme ich es auf meine Kappe.
                                         -- Rudi V?ller, 15. Nov 2003