summaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2012-11-29 20:22:58 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2012-11-29 20:23:04 +0100
commit1533521bff4121dcd8dc37ef34fd09fc6a5018fb (patch)
treecaeca762cedb4772b651b23d4e92853008633a72 /commands
parent5a9d53c47e3c6ce5bc7d38c60501a37ce2c2b276 (diff)
downloadbarebox-1533521bff4121dcd8dc37ef34fd09fc6a5018fb.tar.gz
loadenv: allow more fine grained environment loading
This implements two new options for the loadenv command: -s: removes (scrubs) old directory contents to be able to create a fresh environment from for example /dev/defaultenv -n: no overwrite. Do not overwrite existing files. This allows to keep parts of the old environment. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'commands')
-rw-r--r--commands/loadenv.c53
1 files changed, 47 insertions, 6 deletions
diff --git a/commands/loadenv.c b/commands/loadenv.c
index 5bf1740..48284d7 100644
--- a/commands/loadenv.c
+++ b/commands/loadenv.c
@@ -21,27 +21,68 @@
*/
#include <common.h>
+#include <getopt.h>
#include <command.h>
#include <environment.h>
+#include <fs.h>
static int do_loadenv(int argc, char *argv[])
{
char *filename, *dirname;
+ unsigned flags = 0;
+ int opt;
+ int scrub = 0;
- if (argc < 3)
+ while ((opt = getopt(argc, argv, "ns")) > 0) {
+ switch (opt) {
+ case 'n':
+ flags |= ENV_FLAG_NO_OVERWRITE;
+ break;
+ case 's':
+ scrub = 1;
+ break;
+ default:
+ return COMMAND_ERROR_USAGE;
+ }
+ }
+
+ if (argc - optind < 2)
dirname = "/env";
else
- dirname = argv[2];
- if (argc < 2)
+ dirname = argv[optind + 1];
+
+ if (argc - optind < 1)
filename = default_environment_path;
else
- filename = argv[1];
+ filename = argv[optind];
+
+ if (scrub) {
+ int ret;
+
+ ret = unlink_recursive(dirname, NULL);
+ if (ret) {
+ eprintf("cannot remove %s: %s\n", dirname,
+ strerror(-ret));
+ return 1;
+ }
+
+ ret = mkdir(dirname, 0);
+ if (ret) {
+ eprintf("cannot create %s: %s\n", dirname,
+ strerror(-ret));
+ return ret;
+ }
+ }
+
printf("loading environment from %s\n", filename);
- return envfs_load(filename, dirname);
+
+ return envfs_load(filename, dirname, flags);
}
BAREBOX_CMD_HELP_START(loadenv)
-BAREBOX_CMD_HELP_USAGE("loadenv [ENVFS] [DIRECTORY]\n")
+BAREBOX_CMD_HELP_USAGE("loadenv OPTIONS [ENVFS] [DIRECTORY]\n")
+BAREBOX_CMD_HELP_OPT("-n", "do not overwrite existing files\n")
+BAREBOX_CMD_HELP_OPT("-s", "scrub old environment\n")
BAREBOX_CMD_HELP_SHORT("Load environment from ENVFS into DIRECTORY (default: /dev/env0 -> /env).\n")
BAREBOX_CMD_HELP_END