From: Sascha Hauer <s.hauer@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 09/11] Add defaultenv command
Date: Thu, 6 Nov 2014 13:59:36 +0100 [thread overview]
Message-ID: <1415278778-20826-10-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1415278778-20826-1-git-send-email-s.hauer@pengutronix.de>
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>
---
commands/Kconfig | 7 ++++
commands/Makefile | 1 +
commands/defaultenv.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 103 insertions(+)
create mode 100644 commands/defaultenv.c
diff --git a/commands/Kconfig b/commands/Kconfig
index cf32548..f4c7835 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -699,6 +699,13 @@ config CMD_EXPORT
Export an environment variable to subsequently executed scripts.
+config CMD_DEFAULTENV
+ tristate
+ select ENV_HANDLING
+ prompt "defaultenv"
+ help
+ restore environment from default environment
+
config CMD_GLOBAL
select GLOBALVAR
tristate
diff --git a/commands/Makefile b/commands/Makefile
index b4fc3d3..99a65d4 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -108,3 +108,4 @@ obj-$(CONFIG_CMD_USBGADGET) += usbgadget.o
obj-$(CONFIG_CMD_FIRMWARELOAD) += firmwareload.o
obj-$(CONFIG_CMD_CMP) += cmp.o
obj-$(CONFIG_CMD_NV) += nv.o
+obj-$(CONFIG_CMD_DEFAULTENV) += defaultenv.o
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
--
2.1.1
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2014-11-06 13:00 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-11-06 12:59 non volatile variables and incremental envrironment Sascha Hauer
2014-11-06 12:59 ` [PATCH 01/11] environment: drop unnecessary casts Sascha Hauer
2014-11-06 12:59 ` [PATCH 02/11] environment: remove unused variable Sascha Hauer
2014-11-06 12:59 ` [PATCH 03/11] environment: refactor saveenv Sascha Hauer
2014-11-06 12:59 ` [PATCH 04/11] environment: Only save changes to the defaultenv Sascha Hauer
2014-11-06 12:59 ` [PATCH 05/11] magicvar: Add support for dynamically added magicvars Sascha Hauer
2014-11-06 12:59 ` [PATCH 06/11] Add support for non volatile variables Sascha Hauer
2014-11-06 12:59 ` [PATCH 07/11] libfile: Add copy_recursive Sascha Hauer
2014-11-06 12:59 ` [PATCH 08/11] cp: Add recursive copy Sascha Hauer
2014-11-06 12:59 ` Sascha Hauer [this message]
2014-11-06 12:59 ` [PATCH 10/11] globalvar: Add support for printing all global variables Sascha Hauer
2014-11-06 12:59 ` [PATCH 11/11] defaultenv-2: Make use of nonvolatile variables Sascha Hauer
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=1415278778-20826-10-git-send-email-s.hauer@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/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