2012-12-26 09:53:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012, Google Inc.
|
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2012-12-26 09:53:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <fs.h>
|
|
|
|
#include <os.h>
|
|
|
|
|
|
|
|
int sandbox_fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-17 22:39:37 +00:00
|
|
|
int sandbox_fs_read_at(const char *filename, loff_t pos, void *buffer,
|
|
|
|
loff_t maxsize, loff_t *actread)
|
2012-12-26 09:53:35 +00:00
|
|
|
{
|
2014-11-17 22:39:37 +00:00
|
|
|
loff_t size;
|
2012-12-26 09:53:35 +00:00
|
|
|
int fd, ret;
|
|
|
|
|
|
|
|
fd = os_open(filename, OS_O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
|
|
|
ret = os_lseek(fd, pos, OS_SEEK_SET);
|
|
|
|
if (ret == -1) {
|
|
|
|
os_close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-11-17 22:39:37 +00:00
|
|
|
if (!maxsize) {
|
|
|
|
ret = os_get_filesize(filename, &size);
|
|
|
|
if (ret) {
|
|
|
|
os_close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
maxsize = size;
|
|
|
|
}
|
|
|
|
|
2012-12-26 09:53:35 +00:00
|
|
|
size = os_read(fd, buffer, maxsize);
|
|
|
|
os_close(fd);
|
|
|
|
|
2014-11-17 22:39:37 +00:00
|
|
|
if (size < 0) {
|
|
|
|
ret = -1;
|
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
*actread = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2012-12-26 09:53:35 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 22:39:37 +00:00
|
|
|
int sandbox_fs_write_at(const char *filename, loff_t pos, void *buffer,
|
|
|
|
loff_t towrite, loff_t *actwrite)
|
2013-04-20 08:42:51 +00:00
|
|
|
{
|
|
|
|
ssize_t size;
|
|
|
|
int fd, ret;
|
|
|
|
|
|
|
|
fd = os_open(filename, OS_O_RDWR | OS_O_CREAT);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
|
|
|
ret = os_lseek(fd, pos, OS_SEEK_SET);
|
|
|
|
if (ret == -1) {
|
|
|
|
os_close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
size = os_write(fd, buffer, towrite);
|
|
|
|
os_close(fd);
|
|
|
|
|
2014-11-17 22:39:37 +00:00
|
|
|
if (size == -1) {
|
|
|
|
ret = -1;
|
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
*actwrite = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2013-04-20 08:42:51 +00:00
|
|
|
}
|
|
|
|
|
2012-12-26 09:53:35 +00:00
|
|
|
int sandbox_fs_ls(const char *dirname)
|
|
|
|
{
|
|
|
|
struct os_dirent_node *head, *node;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = os_dirent_ls(dirname, &head);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
for (node = head; node; node = node->next) {
|
|
|
|
printf("%s %10lu %s\n", os_dirent_get_typename(node->type),
|
|
|
|
node->size, node->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-03 20:21:07 +00:00
|
|
|
int sandbox_fs_exists(const char *filename)
|
|
|
|
{
|
2014-11-17 22:39:37 +00:00
|
|
|
loff_t size;
|
|
|
|
int ret;
|
2014-02-03 20:21:07 +00:00
|
|
|
|
2014-11-17 22:39:37 +00:00
|
|
|
ret = os_get_filesize(filename, &size);
|
|
|
|
return ret == 0;
|
2014-02-03 20:21:07 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 22:39:38 +00:00
|
|
|
int sandbox_fs_size(const char *filename, loff_t *size)
|
2014-06-11 18:47:26 +00:00
|
|
|
{
|
2014-11-17 22:39:38 +00:00
|
|
|
return os_get_filesize(filename, size);
|
2014-06-11 18:47:26 +00:00
|
|
|
}
|
|
|
|
|
2012-12-26 09:53:35 +00:00
|
|
|
void sandbox_fs_close(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-11-17 22:39:38 +00:00
|
|
|
int fs_read_sandbox(const char *filename, void *buf, loff_t offset, loff_t len,
|
|
|
|
loff_t *actread)
|
2012-12-26 09:53:35 +00:00
|
|
|
{
|
2014-11-17 22:39:37 +00:00
|
|
|
int ret;
|
2012-12-26 09:53:35 +00:00
|
|
|
|
2014-11-17 22:39:38 +00:00
|
|
|
ret = sandbox_fs_read_at(filename, offset, buf, len, actread);
|
|
|
|
if (ret)
|
2012-12-26 09:53:35 +00:00
|
|
|
printf("** Unable to read file %s **\n", filename);
|
|
|
|
|
2014-11-17 22:39:38 +00:00
|
|
|
return ret;
|
2012-12-26 09:53:35 +00:00
|
|
|
}
|
2013-04-20 08:42:51 +00:00
|
|
|
|
2014-11-17 22:39:38 +00:00
|
|
|
int fs_write_sandbox(const char *filename, void *buf, loff_t offset,
|
|
|
|
loff_t len, loff_t *actwrite)
|
2013-04-20 08:42:51 +00:00
|
|
|
{
|
2014-11-17 22:39:37 +00:00
|
|
|
int ret;
|
2013-04-20 08:42:51 +00:00
|
|
|
|
2014-11-17 22:39:38 +00:00
|
|
|
ret = sandbox_fs_write_at(filename, offset, buf, len, actwrite);
|
|
|
|
if (ret)
|
2013-04-20 08:42:51 +00:00
|
|
|
printf("** Unable to write file %s **\n", filename);
|
|
|
|
|
2014-11-17 22:39:38 +00:00
|
|
|
return ret;
|
2013-04-20 08:42:51 +00:00
|
|
|
}
|