9
0
Fork 0

Merge branch 'for-next/env'

This commit is contained in:
Sascha Hauer 2014-08-07 06:14:58 +02:00
commit 50d10b224e
4 changed files with 61 additions and 24 deletions

View File

@ -18,26 +18,39 @@
#include <common.h>
#include <command.h>
#include <errno.h>
#include <getopt.h>
#include <fs.h>
#include <fcntl.h>
#include <envfs.h>
static int do_saveenv(int argc, char *argv[])
{
int ret;
int ret, opt;
unsigned envfs_flags = 0;
char *filename, *dirname;
printf("saving environment\n");
if (argc < 3)
dirname = "/env";
else
dirname = argv[2];
if (argc < 2)
filename = default_environment_path_get();
else
filename = argv[1];
while ((opt = getopt(argc, argv, "z")) > 0) {
switch (opt) {
case 'z':
envfs_flags |= ENVFS_FLAGS_FORCE_BUILT_IN;
break;
}
}
ret = envfs_save(filename, dirname);
/* destination and source are given? */
if (argc == optind + 2)
dirname = argv[optind + 1];
else
dirname = "/env";
/* destination only given? */
if (argc == optind + 1)
filename = argv[optind];
else
filename = default_environment_path_get();
ret = envfs_save(filename, dirname, envfs_flags);
return ret;
}
@ -47,15 +60,15 @@ BAREBOX_CMD_HELP_TEXT("Save the files in DIRECTORY to the persistent storage dev
BAREBOX_CMD_HELP_TEXT("")
BAREBOX_CMD_HELP_TEXT("ENVFS is usually a block in flash but can be any other file. If")
BAREBOX_CMD_HELP_TEXT("omitted, DIRECTORY defaults to /env and ENVFS defaults to")
BAREBOX_CMD_HELP_TEXT("/dev/env0. Note that envfs can only handle files, directories are being")
BAREBOX_CMD_HELP_TEXT("skipped silently.")
BAREBOX_CMD_HELP_TEXT("/dev/env0.")
BAREBOX_CMD_HELP_OPT ("-z", "force the built-in default environment at startup")
BAREBOX_CMD_HELP_END
BAREBOX_CMD_START(saveenv)
.cmd = do_saveenv,
BAREBOX_CMD_DESC("save environment to persistent storage")
BAREBOX_CMD_OPTS("[ENVFS] [DIRECTORY]")
BAREBOX_CMD_OPTS("[-z] [ENVFS [DIRECTORY]]")
BAREBOX_CMD_GROUP(CMD_GRP_ENV)
BAREBOX_CMD_HELP(cmd_saveenv_help)
BAREBOX_CMD_END

View File

@ -167,12 +167,13 @@ out:
* Make the current environment persistent
* @param[in] filename where to store
* @param[in] dirname what to store (all files in this dir)
* @param[in] flags superblock flags (refer ENVFS_FLAGS_* macros)
* @return 0 on success, anything else in case of failure
*
* Note: This function will also be used on the host! See note in the header
* of this file.
*/
int envfs_save(const char *filename, const char *dirname)
int envfs_save(const char *filename, const char *dirname, unsigned flags)
{
struct envfs_super *super;
int envfd, size, ret;
@ -182,11 +183,15 @@ int envfs_save(const char *filename, const char *dirname)
data.writep = NULL;
data.base = dirname;
/* first pass: calculate size */
recursive_action(dirname, ACTION_RECURSE, file_size_action,
NULL, &data, 0);
if (flags & ENVFS_FLAGS_FORCE_BUILT_IN) {
size = 0; /* force no content */
} else {
/* first pass: calculate size */
recursive_action(dirname, ACTION_RECURSE, file_size_action,
NULL, &data, 0);
size = (unsigned long)data.writep;
size = (unsigned long)data.writep;
}
buf = xzalloc(size + sizeof(struct envfs_super));
data.writep = buf + sizeof(struct envfs_super);
@ -196,10 +201,13 @@ int envfs_save(const char *filename, const char *dirname)
super->major = ENVFS_MAJOR;
super->minor = ENVFS_MINOR;
super->size = ENVFS_32(size);
super->flags = ENVFS_32(flags);
/* second pass: copy files to buffer */
recursive_action(dirname, ACTION_RECURSE, file_save_action,
NULL, &data, 0);
if (!(flags & ENVFS_FLAGS_FORCE_BUILT_IN)) {
/* second pass: copy files to buffer */
recursive_action(dirname, ACTION_RECURSE, file_save_action,
NULL, &data, 0);
}
super->crc = ENVFS_32(crc32(0, buf + sizeof(struct envfs_super), size));
super->sb_crc = ENVFS_32(crc32(0, buf, sizeof(struct envfs_super) - 4));
@ -447,6 +455,15 @@ int envfs_load(const char *filename, const char *dir, unsigned flags)
if (ret)
goto out;
if (super.flags & ENVFS_FLAGS_FORCE_BUILT_IN) {
printf("found force-builtin environment, using defaultenv\n");
ret = defaultenv_load(dir, 0);
if (ret)
printf("failed to load default environment: %s\n",
strerror(-ret));
goto out;
}
buf = xmalloc(size);
rbuf = buf;

View File

@ -43,6 +43,7 @@ struct envfs_super {
uint8_t minor; /* minor */
uint16_t future; /* reserved for future use */
uint32_t flags; /* feature flags */
#define ENVFS_FLAGS_FORCE_BUILT_IN (1 << 0)
uint32_t sb_crc; /* crc for the superblock */
};
@ -92,7 +93,7 @@ struct envfs_super {
#define ENV_FLAG_NO_OVERWRITE (1 << 0)
int envfs_load(const char *filename, const char *dirname, unsigned flags);
int envfs_save(const char *filename, const char *dirname);
int envfs_save(const char *filename, const char *dirname, unsigned flags);
int envfs_load_from_buf(void *buf, int len, const char *dir, unsigned flags);
/* defaults to /dev/env0 */

View File

@ -109,6 +109,7 @@ static void usage(char *prgname)
"\n"
"options:\n"
" -s save (directory -> environment sector)\n"
" -z force the built-in default environment at startup\n"
" -l load (environment sector -> directory)\n"
" -p <size> pad output file to given size\n"
" -v verbose\n",
@ -120,9 +121,10 @@ int main(int argc, char *argv[])
int opt;
int save = 0, load = 0, pad = 0, err = 0, fd;
char *filename = NULL, *dirname = NULL;
unsigned envfs_flags = 0;
int verbose = 0;
while((opt = getopt(argc, argv, "slp:v")) != -1) {
while((opt = getopt(argc, argv, "slp:vz")) != -1) {
switch (opt) {
case 's':
save = 1;
@ -133,6 +135,10 @@ int main(int argc, char *argv[])
case 'p':
pad = strtoul(optarg, NULL, 0);
break;
case 'z':
envfs_flags |= ENVFS_FLAGS_FORCE_BUILT_IN;
save = 1;
break;
case 'v':
verbose = 1;
break;
@ -181,7 +187,7 @@ int main(int argc, char *argv[])
if (verbose)
printf("saving contents of %s to file %s\n", dirname, filename);
err = envfs_save(filename, dirname);
err = envfs_save(filename, dirname, envfs_flags);
if (verbose && err)
printf("saving env failed: %d\n", err);