2007-07-05 16:02:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-10-19 12:56:45 +00:00
|
|
|
/**
|
|
|
|
* @file
|
2007-11-09 22:19:54 +00:00
|
|
|
* @brief Environment handling support (host and target)
|
|
|
|
*
|
|
|
|
* Important: This file will also be used on the host to create
|
2009-12-15 08:11:09 +00:00
|
|
|
* the default environment when building the barebox binary. So
|
|
|
|
* do not add any new barebox related functions here!
|
2007-10-19 12:56:45 +00:00
|
|
|
*/
|
2009-12-15 08:11:09 +00:00
|
|
|
#ifdef __BAREBOX__
|
2007-07-05 16:01:44 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fs.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <envfs.h>
|
|
|
|
#include <xfuncs.h>
|
2007-09-25 10:58:52 +00:00
|
|
|
#include <libbb.h>
|
|
|
|
#include <libgen.h>
|
2007-11-09 22:19:54 +00:00
|
|
|
#include <environment.h>
|
2014-10-08 12:56:13 +00:00
|
|
|
#include <globalvar.h>
|
|
|
|
#include <libfile.h>
|
2007-10-18 16:32:02 +00:00
|
|
|
#else
|
|
|
|
# define errno_str(x) ("void")
|
2007-11-09 22:19:54 +00:00
|
|
|
#define EXPORT_SYMBOL(x)
|
2007-07-05 16:01:52 +00:00
|
|
|
#endif
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2014-10-23 14:58:11 +00:00
|
|
|
struct envfs_entry {
|
|
|
|
char *name;
|
|
|
|
void *buf;
|
|
|
|
int size;
|
|
|
|
struct envfs_entry *next;
|
|
|
|
mode_t mode;
|
|
|
|
};
|
|
|
|
|
2013-01-20 12:10:01 +00:00
|
|
|
struct action_data {
|
|
|
|
const char *base;
|
|
|
|
void *writep;
|
2014-10-23 14:58:11 +00:00
|
|
|
struct envfs_entry *env;
|
2013-01-20 12:10:01 +00:00
|
|
|
};
|
|
|
|
#define PAD4(x) ((x + 3) & ~3)
|
|
|
|
|
2014-01-23 12:58:49 +00:00
|
|
|
#ifdef __BAREBOX__
|
2014-10-08 12:56:13 +00:00
|
|
|
|
|
|
|
#define TMPDIR "/.defaultenv"
|
|
|
|
|
2014-01-23 12:58:49 +00:00
|
|
|
static char *default_environment_path = "/dev/env0";
|
|
|
|
|
|
|
|
void default_environment_path_set(char *path)
|
|
|
|
{
|
|
|
|
default_environment_path = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *default_environment_path_get(void)
|
|
|
|
{
|
|
|
|
return default_environment_path;
|
|
|
|
}
|
2014-10-08 12:56:13 +00:00
|
|
|
|
|
|
|
static int do_compare_file(const char *filename, const char *base)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char *cmp;
|
|
|
|
const char *relname = filename + strlen(base) + 1;
|
|
|
|
|
|
|
|
cmp = asprintf("%s/%s", TMPDIR, relname);
|
|
|
|
ret = compare_file(cmp, filename);
|
|
|
|
|
|
|
|
free(cmp);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-09 10:14:06 +00:00
|
|
|
#else
|
|
|
|
static inline int protect(int fd, size_t count, unsigned long offset, int prot)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int erase(int fd, size_t count, unsigned long offset)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2014-10-08 12:56:13 +00:00
|
|
|
|
|
|
|
static int do_compare_file(const char *filename, const char *base)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2014-01-23 12:58:49 +00:00
|
|
|
#endif
|
2011-03-31 10:43:59 +00:00
|
|
|
|
2014-10-23 14:58:11 +00:00
|
|
|
static int file_action(const char *filename, struct stat *statbuf,
|
2007-11-09 22:19:54 +00:00
|
|
|
void *userdata, int depth)
|
2007-09-27 10:04:17 +00:00
|
|
|
{
|
|
|
|
struct action_data *data = userdata;
|
2014-10-23 14:58:11 +00:00
|
|
|
struct envfs_entry *env;
|
|
|
|
int fd, ret;
|
2007-09-27 10:04:17 +00:00
|
|
|
|
2014-10-08 12:56:13 +00:00
|
|
|
if (!do_compare_file(filename, data->base))
|
|
|
|
return 1;
|
|
|
|
|
2014-10-23 14:58:11 +00:00
|
|
|
env = xzalloc(sizeof(*env));
|
|
|
|
env->name = strdup(filename + strlen(data->base));
|
|
|
|
env->size = statbuf->st_size;
|
|
|
|
|
|
|
|
env->buf = calloc(env->size + 1, 1);
|
|
|
|
if (!env->buf)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
env->mode = S_IRWXU | S_IRWXG | S_IRWXO;
|
2012-08-22 04:51:43 +00:00
|
|
|
|
2014-10-23 14:58:11 +00:00
|
|
|
if (S_ISLNK(statbuf->st_mode)) {
|
|
|
|
env->size++;
|
2012-08-22 04:51:43 +00:00
|
|
|
|
2014-10-23 14:58:11 +00:00
|
|
|
ret = readlink(filename, env->buf, env->size);
|
|
|
|
if (ret < 0) {
|
2012-08-22 04:51:43 +00:00
|
|
|
perror("read");
|
2014-10-23 14:58:11 +00:00
|
|
|
goto out;
|
2012-08-22 04:51:43 +00:00
|
|
|
}
|
2014-10-23 14:58:11 +00:00
|
|
|
|
|
|
|
env->mode |= S_IFLNK;
|
2012-08-22 04:51:43 +00:00
|
|
|
} else {
|
2014-10-23 14:58:11 +00:00
|
|
|
fd = open(filename, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
ret = read(fd, env->buf, env->size);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (ret < env->size) {
|
|
|
|
perror("read");
|
|
|
|
goto out;
|
|
|
|
}
|
2012-08-22 04:51:43 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 14:58:11 +00:00
|
|
|
env->next = data->env;
|
|
|
|
data->env = env;
|
|
|
|
out:
|
2007-09-27 10:04:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-10-23 14:58:11 +00:00
|
|
|
static void envfs_save_inode(struct action_data *data, struct envfs_entry *env)
|
2007-07-05 16:01:44 +00:00
|
|
|
{
|
2007-09-27 10:04:17 +00:00
|
|
|
struct envfs_inode *inode;
|
2012-08-21 13:47:53 +00:00
|
|
|
struct envfs_inode_end *inode_end;
|
2014-10-23 14:58:11 +00:00
|
|
|
int namelen = strlen(env->name) + 1;
|
2007-09-25 10:58:52 +00:00
|
|
|
|
2014-10-23 13:29:03 +00:00
|
|
|
inode = data->writep;
|
2008-02-15 12:40:30 +00:00
|
|
|
inode->magic = ENVFS_32(ENVFS_INODE_MAGIC);
|
2014-10-23 14:58:11 +00:00
|
|
|
inode->headerlen = ENVFS_32(PAD4(namelen) + sizeof(struct envfs_inode_end));
|
|
|
|
inode->size = ENVFS_32(env->size);
|
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
data->writep += sizeof(struct envfs_inode);
|
|
|
|
|
2014-10-23 14:58:11 +00:00
|
|
|
strcpy(data->writep, env->name);
|
2007-09-27 10:04:17 +00:00
|
|
|
data->writep += PAD4(namelen);
|
2014-10-23 14:58:11 +00:00
|
|
|
|
2014-10-23 13:29:03 +00:00
|
|
|
inode_end = data->writep;
|
2012-08-21 13:47:53 +00:00
|
|
|
inode_end->magic = ENVFS_32(ENVFS_INODE_END_MAGIC);
|
2014-10-23 14:58:11 +00:00
|
|
|
inode_end->mode = ENVFS_32(env->mode);
|
|
|
|
data->writep += sizeof(struct envfs_inode_end);
|
2007-09-25 10:58:52 +00:00
|
|
|
|
2014-10-23 14:58:11 +00:00
|
|
|
memcpy(data->writep, env->buf, env->size);
|
|
|
|
data->writep += PAD4(env->size);
|
2007-09-25 10:58:52 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 12:56:13 +00:00
|
|
|
#ifdef __BAREBOX__
|
|
|
|
static int file_remove_action(const char *filename, struct stat *statbuf,
|
|
|
|
void *userdata, int depth)
|
|
|
|
{
|
|
|
|
struct action_data *data = userdata;
|
|
|
|
char *envname;
|
|
|
|
struct stat s;
|
|
|
|
int ret;
|
|
|
|
struct envfs_entry *env;
|
|
|
|
|
|
|
|
filename += sizeof(TMPDIR) - 1;
|
|
|
|
|
|
|
|
envname = asprintf("%s/%s", data->base, filename);
|
|
|
|
|
|
|
|
ret = stat(envname, &s);
|
|
|
|
if (ret) {
|
|
|
|
char *base;
|
|
|
|
|
|
|
|
base = basename(envname);
|
|
|
|
|
|
|
|
env = xzalloc(sizeof(*env));
|
|
|
|
env->name = strdup(filename);
|
|
|
|
env->size = strlen(base) + 1;
|
|
|
|
|
|
|
|
env->buf = strdup(base);
|
|
|
|
if (!env->buf)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
env->mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFLNK;
|
|
|
|
|
|
|
|
env->next = data->env;
|
|
|
|
data->env = env;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
free(envname);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static int file_remove_action(const char *filename, struct stat *statbuf,
|
|
|
|
void *userdata, int depth)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int dir_remove_action(const char *filename, struct stat *statbuf,
|
|
|
|
void *userdata, int depth)
|
|
|
|
{
|
2015-02-19 21:15:15 +00:00
|
|
|
if (!depth)
|
|
|
|
return 1;
|
|
|
|
|
2014-10-08 12:56:13 +00:00
|
|
|
rmdir(filename);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-11-09 22:19:54 +00:00
|
|
|
/**
|
|
|
|
* Make the current environment persistent
|
|
|
|
* @param[in] filename where to store
|
|
|
|
* @param[in] dirname what to store (all files in this dir)
|
2014-07-31 10:39:02 +00:00
|
|
|
* @param[in] flags superblock flags (refer ENVFS_FLAGS_* macros)
|
2007-11-09 22:19:54 +00:00
|
|
|
* @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.
|
|
|
|
*/
|
2014-07-31 10:39:02 +00:00
|
|
|
int envfs_save(const char *filename, const char *dirname, unsigned flags)
|
2007-09-25 10:58:52 +00:00
|
|
|
{
|
2007-09-27 10:04:17 +00:00
|
|
|
struct envfs_super *super;
|
|
|
|
int envfd, size, ret;
|
2014-10-23 14:58:11 +00:00
|
|
|
struct action_data data = {};
|
2014-04-08 15:20:03 +00:00
|
|
|
void *buf = NULL, *wbuf;
|
2014-10-23 14:58:11 +00:00
|
|
|
struct envfs_entry *env;
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2015-03-05 14:37:14 +00:00
|
|
|
if (!filename)
|
|
|
|
filename = default_environment_path_get();
|
|
|
|
|
|
|
|
if (!dirname)
|
|
|
|
dirname = "/env";
|
|
|
|
|
2010-10-15 07:33:47 +00:00
|
|
|
data.writep = NULL;
|
2007-09-27 10:04:17 +00:00
|
|
|
data.base = dirname;
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2014-10-08 12:56:13 +00:00
|
|
|
#ifdef __BAREBOX__
|
|
|
|
defaultenv_load(TMPDIR, 0);
|
|
|
|
#endif
|
|
|
|
|
2014-07-31 10:39:02 +00:00
|
|
|
if (flags & ENVFS_FLAGS_FORCE_BUILT_IN) {
|
|
|
|
size = 0; /* force no content */
|
|
|
|
} else {
|
|
|
|
/* first pass: calculate size */
|
2014-10-23 14:58:11 +00:00
|
|
|
recursive_action(dirname, ACTION_RECURSE, file_action,
|
2014-07-31 10:39:02 +00:00
|
|
|
NULL, &data, 0);
|
2014-10-08 12:56:13 +00:00
|
|
|
recursive_action("/.defaultenv", ACTION_RECURSE,
|
|
|
|
file_remove_action, NULL, &data, 0);
|
2014-10-23 14:58:11 +00:00
|
|
|
size = 0;
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2014-10-23 14:58:11 +00:00
|
|
|
for (env = data.env; env; env = env->next) {
|
|
|
|
size += PAD4(env->size);
|
|
|
|
size += sizeof(struct envfs_inode);
|
|
|
|
size += PAD4(strlen(env->name) + 1);
|
|
|
|
size += sizeof(struct envfs_inode_end);
|
|
|
|
}
|
2014-07-31 10:39:02 +00:00
|
|
|
}
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
buf = xzalloc(size + sizeof(struct envfs_super));
|
|
|
|
data.writep = buf + sizeof(struct envfs_super);
|
|
|
|
|
2014-10-23 13:29:03 +00:00
|
|
|
super = buf;
|
2008-02-15 12:40:30 +00:00
|
|
|
super->magic = ENVFS_32(ENVFS_MAGIC);
|
2012-08-21 12:47:58 +00:00
|
|
|
super->major = ENVFS_MAJOR;
|
|
|
|
super->minor = ENVFS_MINOR;
|
2008-02-15 12:40:30 +00:00
|
|
|
super->size = ENVFS_32(size);
|
2014-07-31 10:39:02 +00:00
|
|
|
super->flags = ENVFS_32(flags);
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2014-07-31 10:39:02 +00:00
|
|
|
if (!(flags & ENVFS_FLAGS_FORCE_BUILT_IN)) {
|
|
|
|
/* second pass: copy files to buffer */
|
2014-10-23 14:58:11 +00:00
|
|
|
env = data.env;
|
|
|
|
while (env) {
|
|
|
|
struct envfs_entry *next = env->next;
|
|
|
|
|
|
|
|
envfs_save_inode(&data, env);
|
|
|
|
|
|
|
|
free(env->buf);
|
|
|
|
free(env->name);
|
|
|
|
free(env);
|
|
|
|
env = next;
|
|
|
|
}
|
2014-07-31 10:39:02 +00:00
|
|
|
}
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2008-02-15 12:40:30 +00:00
|
|
|
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));
|
2007-09-27 10:04:17 +00:00
|
|
|
|
2008-12-05 03:56:33 +00:00
|
|
|
envfd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
|
2007-09-27 10:04:17 +00:00
|
|
|
if (envfd < 0) {
|
2014-04-09 10:14:06 +00:00
|
|
|
printf("could not open %s: %s\n", filename, errno_str());
|
|
|
|
ret = -errno;
|
2007-09-27 10:04:17 +00:00
|
|
|
goto out1;
|
|
|
|
}
|
|
|
|
|
2014-04-09 10:14:06 +00:00
|
|
|
ret = protect(envfd, ~0, 0, 0);
|
|
|
|
|
2015-11-27 07:05:35 +00:00
|
|
|
/* ENOSYS and EOPNOTSUPP aren't errors here, many devices don't need it */
|
|
|
|
if (ret && errno != ENOSYS && errno != EOPNOTSUPP) {
|
2014-04-09 10:14:06 +00:00
|
|
|
printf("could not unprotect %s: %s\n", filename, errno_str());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = erase(envfd, ~0, 0);
|
|
|
|
|
2015-11-27 07:05:35 +00:00
|
|
|
/* ENOSYS and EOPNOTSUPP aren't errors here, many devices don't need it */
|
|
|
|
if (ret && errno != ENOSYS && errno != EOPNOTSUPP) {
|
2014-04-09 10:14:06 +00:00
|
|
|
printf("could not erase %s: %s\n", filename, errno_str());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-04-08 15:20:03 +00:00
|
|
|
size += sizeof(struct envfs_super);
|
|
|
|
|
|
|
|
wbuf = buf;
|
|
|
|
|
|
|
|
while (size) {
|
|
|
|
ssize_t now = write(envfd, wbuf, size);
|
|
|
|
if (now < 0) {
|
|
|
|
ret = -errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
wbuf += now;
|
|
|
|
size -= now;
|
2007-07-05 16:01:44 +00:00
|
|
|
}
|
2007-09-27 10:04:17 +00:00
|
|
|
|
2014-04-09 10:14:06 +00:00
|
|
|
ret = protect(envfd, ~0, 0, 1);
|
|
|
|
|
2015-11-27 07:05:35 +00:00
|
|
|
/* ENOSYS and EOPNOTSUPP aren't errors here, many devices don't need it */
|
|
|
|
if (ret && errno != ENOSYS && errno != EOPNOTSUPP) {
|
2014-04-09 10:14:06 +00:00
|
|
|
printf("could not protect %s: %s\n", filename, errno_str());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
ret = 0;
|
|
|
|
|
2007-07-05 16:01:44 +00:00
|
|
|
out:
|
|
|
|
close(envfd);
|
2007-09-27 10:04:17 +00:00
|
|
|
out1:
|
|
|
|
free(buf);
|
2014-10-08 12:56:13 +00:00
|
|
|
#ifdef __BAREBOX__
|
|
|
|
unlink_recursive(TMPDIR, NULL);
|
|
|
|
#endif
|
2007-09-27 10:04:17 +00:00
|
|
|
return ret;
|
2007-07-05 16:01:44 +00:00
|
|
|
}
|
2007-11-09 22:19:54 +00:00
|
|
|
EXPORT_SYMBOL(envfs_save);
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2014-02-20 07:16:01 +00:00
|
|
|
static int envfs_check_super(struct envfs_super *super, size_t *size)
|
2007-07-05 16:01:44 +00:00
|
|
|
{
|
2014-02-20 07:16:01 +00:00
|
|
|
if (ENVFS_32(super->magic) != ENVFS_MAGIC) {
|
|
|
|
printf("envfs: wrong magic\n");
|
|
|
|
return -EIO;
|
2007-07-05 16:01:44 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 07:16:01 +00:00
|
|
|
if (crc32(0, super, sizeof(*super) - 4) != ENVFS_32(super->sb_crc)) {
|
|
|
|
printf("wrong crc on env superblock\n");
|
|
|
|
return -EIO;
|
2007-07-05 16:01:44 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 07:16:01 +00:00
|
|
|
if (super->major < ENVFS_MAJOR)
|
|
|
|
printf("envfs version %d.%d loaded into %d.%d\n",
|
|
|
|
super->major, super->minor,
|
|
|
|
ENVFS_MAJOR, ENVFS_MINOR);
|
2007-09-13 13:24:32 +00:00
|
|
|
|
2014-02-20 07:16:01 +00:00
|
|
|
*size = ENVFS_32(super->size);
|
2007-09-27 10:04:17 +00:00
|
|
|
|
2014-02-20 07:16:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2007-09-27 10:04:17 +00:00
|
|
|
|
2014-02-18 09:25:13 +00:00
|
|
|
static int envfs_check_data(struct envfs_super *super, const void *buf, size_t size)
|
2014-02-20 07:16:01 +00:00
|
|
|
{
|
|
|
|
uint32_t crc;
|
|
|
|
|
|
|
|
crc = crc32(0, buf, size);
|
|
|
|
if (crc != ENVFS_32(super->crc)) {
|
2007-09-27 10:04:17 +00:00
|
|
|
printf("wrong crc on env\n");
|
2014-02-20 07:16:01 +00:00
|
|
|
return -EIO;
|
2007-09-27 10:04:17 +00:00
|
|
|
}
|
2008-02-15 12:40:30 +00:00
|
|
|
|
2014-02-20 07:16:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-29 05:44:57 +00:00
|
|
|
static int envfs_load_data(struct envfs_super *super, void *buf, size_t size,
|
|
|
|
const char *dir, unsigned flags)
|
2014-02-20 07:16:01 +00:00
|
|
|
{
|
|
|
|
int fd, ret = 0;
|
|
|
|
char *str, *tmp;
|
|
|
|
int headerlen_full;
|
|
|
|
/* for envfs < 1.0 */
|
|
|
|
struct envfs_inode_end inode_end_dummy;
|
2015-12-16 12:24:43 +00:00
|
|
|
struct stat s;
|
2014-02-20 07:16:01 +00:00
|
|
|
|
|
|
|
inode_end_dummy.mode = ENVFS_32(S_IRWXU | S_IRWXG | S_IRWXO);
|
|
|
|
inode_end_dummy.magic = ENVFS_32(ENVFS_INODE_END_MAGIC);
|
2012-08-21 13:47:53 +00:00
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
while (size) {
|
|
|
|
struct envfs_inode *inode;
|
2012-08-21 13:47:53 +00:00
|
|
|
struct envfs_inode_end *inode_end;
|
|
|
|
uint32_t inode_size, inode_headerlen, namelen;
|
2008-02-15 12:40:30 +00:00
|
|
|
|
2014-10-23 13:29:03 +00:00
|
|
|
inode = buf;
|
2012-08-21 13:47:53 +00:00
|
|
|
buf += sizeof(struct envfs_inode);
|
2008-02-15 12:40:30 +00:00
|
|
|
|
|
|
|
if (ENVFS_32(inode->magic) != ENVFS_INODE_MAGIC) {
|
2014-02-20 07:16:01 +00:00
|
|
|
printf("envfs: wrong magic\n");
|
2007-09-13 13:24:32 +00:00
|
|
|
ret = -EIO;
|
2007-07-05 16:01:44 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2008-02-15 12:40:30 +00:00
|
|
|
inode_size = ENVFS_32(inode->size);
|
2012-08-21 13:47:53 +00:00
|
|
|
inode_headerlen = ENVFS_32(inode->headerlen);
|
|
|
|
namelen = strlen(inode->data) + 1;
|
2014-03-29 05:44:57 +00:00
|
|
|
if (super->major < 1)
|
2012-08-21 13:47:53 +00:00
|
|
|
inode_end = &inode_end_dummy;
|
|
|
|
else
|
2014-10-23 13:29:03 +00:00
|
|
|
inode_end = buf + PAD4(namelen);
|
2008-02-15 12:40:30 +00:00
|
|
|
|
2012-08-21 13:47:53 +00:00
|
|
|
debug("loading %s size %d namelen %d headerlen %d\n", inode->data,
|
|
|
|
inode_size, namelen, inode_headerlen);
|
2008-02-15 12:40:30 +00:00
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
str = concat_path_file(dir, inode->data);
|
2008-02-15 12:40:30 +00:00
|
|
|
|
2012-08-21 13:47:53 +00:00
|
|
|
headerlen_full = PAD4(inode_headerlen);
|
|
|
|
buf += headerlen_full;
|
|
|
|
|
|
|
|
if (ENVFS_32(inode_end->magic) != ENVFS_INODE_END_MAGIC) {
|
2014-02-20 07:16:01 +00:00
|
|
|
printf("envfs: wrong inode_end_magic\n");
|
2012-08-21 13:47:53 +00:00
|
|
|
ret = -EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-10-08 12:56:13 +00:00
|
|
|
tmp = strdup(str);
|
|
|
|
make_directory(dirname(tmp));
|
|
|
|
free(tmp);
|
|
|
|
|
2015-12-16 12:24:43 +00:00
|
|
|
ret = stat(str, &s);
|
|
|
|
if (!ret && (flags & ENV_FLAG_NO_OVERWRITE)) {
|
|
|
|
printf("skip %s\n", str);
|
|
|
|
goto skip;
|
|
|
|
}
|
|
|
|
|
2012-08-22 04:51:43 +00:00
|
|
|
if (S_ISLNK(ENVFS_32(inode_end->mode))) {
|
|
|
|
debug("symlink: %s -> %s\n", str, (char*)buf);
|
2014-10-08 12:56:13 +00:00
|
|
|
if (!strcmp(buf, basename(str))) {
|
|
|
|
unlink(str);
|
|
|
|
} else {
|
2015-12-16 12:24:43 +00:00
|
|
|
if (!ret)
|
|
|
|
unlink(str);
|
|
|
|
|
2014-10-08 12:56:13 +00:00
|
|
|
ret = symlink(buf, str);
|
|
|
|
if (ret < 0)
|
|
|
|
printf("symlink: %s -> %s : %s\n",
|
|
|
|
str, (char*)buf, strerror(-errno));
|
2012-08-22 04:51:43 +00:00
|
|
|
}
|
|
|
|
free(str);
|
|
|
|
} else {
|
|
|
|
fd = open(str, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
|
|
|
free(str);
|
|
|
|
if (fd < 0) {
|
|
|
|
printf("Open %s\n", errno_str());
|
|
|
|
ret = fd;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = write(fd, buf, inode_size);
|
|
|
|
if (ret < inode_size) {
|
|
|
|
perror("write");
|
|
|
|
ret = -errno;
|
|
|
|
close(fd);
|
|
|
|
goto out;
|
|
|
|
}
|
2007-07-05 16:01:44 +00:00
|
|
|
close(fd);
|
|
|
|
}
|
2012-11-29 19:22:58 +00:00
|
|
|
skip:
|
2012-08-21 13:47:53 +00:00
|
|
|
buf += PAD4(inode_size);
|
|
|
|
size -= headerlen_full + PAD4(inode_size) +
|
2007-11-09 22:19:54 +00:00
|
|
|
sizeof(struct envfs_inode);
|
2007-07-05 16:01:44 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 12:56:13 +00:00
|
|
|
recursive_action(dir, ACTION_RECURSE | ACTION_DEPTHFIRST, NULL,
|
|
|
|
dir_remove_action, NULL, 0);
|
|
|
|
|
2014-02-20 07:16:01 +00:00
|
|
|
ret = 0;
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int envfs_load_from_buf(void *buf, int len, const char *dir, unsigned flags)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t size;
|
|
|
|
struct envfs_super *super = buf;
|
|
|
|
|
|
|
|
buf = super + 1;
|
|
|
|
|
|
|
|
ret = envfs_check_super(super, &size);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = envfs_check_data(super, buf, size);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2014-03-29 05:44:57 +00:00
|
|
|
ret = envfs_load_data(super, buf, size, dir, flags);
|
2014-02-20 07:16:01 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restore the last environment into the current one
|
|
|
|
* @param[in] filename from where to restore
|
|
|
|
* @param[in] dir where to store the last content
|
|
|
|
* @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_load(const char *filename, const char *dir, unsigned flags)
|
|
|
|
{
|
|
|
|
struct envfs_super super;
|
2014-04-08 15:32:05 +00:00
|
|
|
void *buf = NULL, *rbuf;
|
2014-02-20 07:16:01 +00:00
|
|
|
int envfd;
|
|
|
|
int ret = 0;
|
2014-04-08 15:32:05 +00:00
|
|
|
size_t size, rsize;
|
2014-02-20 07:16:01 +00:00
|
|
|
|
2015-03-05 14:37:14 +00:00
|
|
|
if (!filename)
|
|
|
|
filename = default_environment_path_get();
|
|
|
|
|
|
|
|
if (!dir)
|
|
|
|
dir = "/env";
|
|
|
|
|
2014-02-20 07:16:01 +00:00
|
|
|
envfd = open(filename, O_RDONLY);
|
|
|
|
if (envfd < 0) {
|
2014-04-09 09:51:18 +00:00
|
|
|
printf("environment load %s: %s\n", filename, errno_str());
|
|
|
|
if (errno == ENOENT)
|
|
|
|
printf("Maybe you have to create the partition.\n");
|
2014-02-20 07:16:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read superblock */
|
|
|
|
ret = read(envfd, &super, sizeof(struct envfs_super));
|
|
|
|
if ( ret < sizeof(struct envfs_super)) {
|
|
|
|
perror("read");
|
|
|
|
ret = -errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = envfs_check_super(&super, &size);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
2014-07-31 10:39:01 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-20 07:16:01 +00:00
|
|
|
buf = xmalloc(size);
|
2014-04-08 15:32:05 +00:00
|
|
|
|
|
|
|
rbuf = buf;
|
|
|
|
rsize = size;
|
|
|
|
|
|
|
|
while (rsize) {
|
|
|
|
ssize_t now;
|
|
|
|
|
|
|
|
now = read(envfd, rbuf, rsize);
|
|
|
|
if (now < 0) {
|
|
|
|
perror("read");
|
|
|
|
ret = -errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!now) {
|
|
|
|
printf("%s: premature end of file\n", filename);
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rbuf += now;
|
|
|
|
rsize -= now;
|
2014-02-20 07:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = envfs_check_data(&super, buf, size);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
2014-03-29 05:44:57 +00:00
|
|
|
ret = envfs_load_data(&super, buf, size, dir, flags);
|
2014-02-20 07:16:01 +00:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
ret = 0;
|
2014-06-18 13:12:05 +00:00
|
|
|
|
2007-07-05 16:01:44 +00:00
|
|
|
out:
|
|
|
|
close(envfd);
|
2014-02-20 07:16:01 +00:00
|
|
|
free(buf);
|
|
|
|
|
2007-09-13 13:24:32 +00:00
|
|
|
return ret;
|
2007-07-05 16:01:44 +00:00
|
|
|
}
|