summaryrefslogtreecommitdiffstats
path: root/commands/defaultenv.c
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2014-10-23 22:52:14 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2014-11-06 12:23:12 +0100
commitb25d333493f4d58b8b99ef20e9ca4cdf32e869c4 (patch)
tree226951cc0057be33fe5ca70ea914082c341b56c6 /commands/defaultenv.c
parent837e517669aee65173f185ed912d95f26b0ff5fd (diff)
downloadbarebox-b25d333493f4d58b8b99ef20e9ca4cdf32e869c4.tar.gz
Add defaultenv command
This adds a command to explicitly restore the environment (or parts thereof) from the default environment. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'commands/defaultenv.c')
-rw-r--r--commands/defaultenv.c95
1 files changed, 95 insertions, 0 deletions
diff --git a/commands/defaultenv.c b/commands/defaultenv.c
new file mode 100644
index 0000000..bae2d78
--- /dev/null
+++ b/commands/defaultenv.c
@@ -0,0 +1,95 @@
+/*
+ * Copyright (c) 2014 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include <common.h>
+#include <getopt.h>
+#include <command.h>
+#include <envfs.h>
+#include <errno.h>
+#include <fs.h>
+#include <libfile.h>
+#include <malloc.h>
+#include <globalvar.h>
+
+static int do_defaultenv(int argc, char *argv[])
+{
+ char *dirname;
+ int opt, ret;
+ char *restorepath = "/";
+ char *from, *to;
+ int restore = 0, scrub = 0;
+
+ while ((opt = getopt(argc, argv, "p:rs")) > 0) {
+ switch (opt) {
+ case 'r':
+ restore = 1;
+ break;
+ case 'p':
+ restorepath = optarg;
+ break;
+ case 's':
+ scrub = 1;
+ break;
+ default:
+ return COMMAND_ERROR_USAGE;
+ }
+ }
+
+ if (!restore || argc != optind)
+ return COMMAND_ERROR_USAGE;
+
+ dirname = "/env";
+
+ make_directory(dirname);
+
+ ret = defaultenv_load("/.defaultenv", 0);
+ if (ret)
+ return ret;
+
+ from = asprintf("/.defaultenv/%s", restorepath);
+ to = asprintf("%s/%s", dirname, restorepath);
+
+ printf("Restoring %s from default environment\n", restorepath);
+
+ if (scrub)
+ unlink_recursive(to, NULL);
+
+ ret = copy_recursive(from, to);
+ free(from);
+ free(to);
+
+ nvvar_load();
+
+ unlink_recursive("/.defaultenv", NULL);
+
+ return ret;
+}
+
+BAREBOX_CMD_HELP_START(defaultenv)
+BAREBOX_CMD_HELP_TEXT("Options:")
+BAREBOX_CMD_HELP_OPT("-r", "restore default environment")
+BAREBOX_CMD_HELP_OPT("-s", "scrub, remove files not in default environment")
+BAREBOX_CMD_HELP_OPT("-p <PATH>", "limit to <PATH>")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(defaultenv)
+ .cmd = do_defaultenv,
+ BAREBOX_CMD_DESC("Restore environment from default environment")
+ BAREBOX_CMD_OPTS("[-rs] [-p <PATH>]")
+ BAREBOX_CMD_GROUP(CMD_GRP_ENV)
+ BAREBOX_CMD_HELP(cmd_defaultenv_help)
+BAREBOX_CMD_END