env: erase/protect in envfs_save
So that the envfs_save is more useful outside of the saveenv command Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
parent
54bea204e2
commit
ebb8a4b594
|
@ -29,7 +29,7 @@
|
||||||
|
|
||||||
static int do_saveenv(int argc, char *argv[])
|
static int do_saveenv(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int ret, fd;
|
int ret;
|
||||||
char *filename, *dirname;
|
char *filename, *dirname;
|
||||||
|
|
||||||
printf("saving environment\n");
|
printf("saving environment\n");
|
||||||
|
@ -42,52 +42,8 @@ static int do_saveenv(int argc, char *argv[])
|
||||||
else
|
else
|
||||||
filename = argv[1];
|
filename = argv[1];
|
||||||
|
|
||||||
fd = open(filename, O_WRONLY | O_CREAT);
|
|
||||||
if (fd < 0) {
|
|
||||||
printf("could not open %s: %s\n", filename, errno_str());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = protect(fd, ~0, 0, 0);
|
|
||||||
|
|
||||||
/* ENOSYS is no error here, many devices do not need it */
|
|
||||||
if (ret && errno != ENOSYS) {
|
|
||||||
printf("could not unprotect %s: %s\n", filename, errno_str());
|
|
||||||
close(fd);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = erase(fd, ~0, 0);
|
|
||||||
|
|
||||||
/* ENOSYS is no error here, many devices do not need it */
|
|
||||||
if (ret && errno != ENOSYS) {
|
|
||||||
printf("could not erase %s: %s\n", filename, errno_str());
|
|
||||||
close(fd);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
close(fd);
|
|
||||||
|
|
||||||
ret = envfs_save(filename, dirname);
|
ret = envfs_save(filename, dirname);
|
||||||
if (ret) {
|
|
||||||
printf("saveenv failed\n");
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
fd = open(filename, O_WRONLY | O_CREAT);
|
|
||||||
|
|
||||||
ret = protect(fd, ~0, 0, 1);
|
|
||||||
|
|
||||||
/* ENOSYS is no error here, many devices do not need it */
|
|
||||||
if (ret && errno != ENOSYS) {
|
|
||||||
printf("could not protect %s: %s\n", filename, errno_str());
|
|
||||||
close(fd);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = 0;
|
|
||||||
out:
|
|
||||||
close(fd);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -60,6 +60,16 @@ char *default_environment_path_get(void)
|
||||||
{
|
{
|
||||||
return default_environment_path;
|
return default_environment_path;
|
||||||
}
|
}
|
||||||
|
#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;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static int file_size_action(const char *filename, struct stat *statbuf,
|
static int file_size_action(const char *filename, struct stat *statbuf,
|
||||||
|
@ -196,11 +206,27 @@ int envfs_save(const char *filename, const char *dirname)
|
||||||
|
|
||||||
envfd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
|
envfd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
|
||||||
if (envfd < 0) {
|
if (envfd < 0) {
|
||||||
printf("Open %s %s\n", filename, errno_str());
|
printf("could not open %s: %s\n", filename, errno_str());
|
||||||
ret = envfd;
|
ret = -errno;
|
||||||
goto out1;
|
goto out1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ret = protect(envfd, ~0, 0, 0);
|
||||||
|
|
||||||
|
/* ENOSYS is no error here, many devices do not need it */
|
||||||
|
if (ret && errno != ENOSYS) {
|
||||||
|
printf("could not unprotect %s: %s\n", filename, errno_str());
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = erase(envfd, ~0, 0);
|
||||||
|
|
||||||
|
/* ENOSYS is no error here, many devices do not need it */
|
||||||
|
if (ret && errno != ENOSYS) {
|
||||||
|
printf("could not erase %s: %s\n", filename, errno_str());
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
size += sizeof(struct envfs_super);
|
size += sizeof(struct envfs_super);
|
||||||
|
|
||||||
wbuf = buf;
|
wbuf = buf;
|
||||||
|
@ -216,6 +242,14 @@ int envfs_save(const char *filename, const char *dirname)
|
||||||
size -= now;
|
size -= now;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ret = protect(envfd, ~0, 0, 1);
|
||||||
|
|
||||||
|
/* ENOSYS is no error here, many devices do not need it */
|
||||||
|
if (ret && errno != ENOSYS) {
|
||||||
|
printf("could not protect %s: %s\n", filename, errno_str());
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
||||||
out:
|
out:
|
||||||
|
|
Loading…
Reference in New Issue