mirror of
https://gitee.com/openLuat/LuatOS
synced 2025-08-17 22:18:03 +08:00
update: 支持获取/luadb/的已使用空间. 在尝试写入/luadb/下的文件时做出日志提醒,并改正demo里的错误
This commit is contained in:
parent
cc36a50524
commit
9895e7aa22
|
@ -33,8 +33,33 @@ local function fs_test()
|
|||
f:close()
|
||||
--end
|
||||
|
||||
log.info("io.writeFile", io.writeFile("/abc.txt", "ABCDEFG"))
|
||||
|
||||
log.info("io.readFile", io.readFile("/abc.txt"))
|
||||
local f = io.open("/abc.txt", "rb")
|
||||
local c = 0
|
||||
if f then
|
||||
local data = f:read("*a")
|
||||
log.info("fs", "data2", data, data:toHex())
|
||||
f:close()
|
||||
end
|
||||
|
||||
-- seek和tell测试
|
||||
local f = io.open("/abc.txt", "rb")
|
||||
local c = 0
|
||||
if f then
|
||||
f:seek("end", 0)
|
||||
f:seek("set", 0)
|
||||
local data = f:read("*a")
|
||||
log.info("fs", "data3", data, data:toHex())
|
||||
f:close()
|
||||
end
|
||||
|
||||
if fs then
|
||||
-- 根目录是可读写的
|
||||
log.info("fsstat", fs.fsstat("/"))
|
||||
-- /luadb/ 是只读的
|
||||
log.info("fsstat", fs.fsstat("/luadb/"))
|
||||
end
|
||||
|
||||
local ret, files = io.lsdir("/")
|
||||
|
@ -42,7 +67,7 @@ local function fs_test()
|
|||
|
||||
-- 读取刷机时加入的文件, 并演示按行读取
|
||||
-- 刷机时选取的非lua文件, 均存放在/luadb/目录下, 单层无子文件夹
|
||||
f = io.open("/luadb/abc.txt", "a")
|
||||
f = io.open("/luadb/abc.txt", "rb")
|
||||
if f then
|
||||
while true do
|
||||
local line = f:read("l")
|
||||
|
|
|
@ -41,6 +41,7 @@ int luat_luadb_umount(luadb_fs_t *fs) {
|
|||
}
|
||||
|
||||
int luat_luadb_remount(luadb_fs_t *fs, unsigned flags) {
|
||||
(void)flags;
|
||||
memset(fs->fds, 0, sizeof(luadb_fd_t)*LUAT_LUADB_MAX_OPENFILE);
|
||||
return 0;
|
||||
}
|
||||
|
@ -64,6 +65,8 @@ static luadb_file_t* find_by_name(luadb_fs_t *fs, const char *path) {
|
|||
}
|
||||
|
||||
int luat_luadb_open(luadb_fs_t *fs, const char *path, int flags, int /*mode_t*/ mode) {
|
||||
(void)flags;
|
||||
(void)mode;
|
||||
LLOGD("open luadb path = %s flags=%d", path, flags);
|
||||
int fd = -1;
|
||||
for (size_t j = 1; j < LUAT_LUADB_MAX_OPENFILE; j++)
|
||||
|
@ -139,7 +142,7 @@ luadb_fs_t* luat_luadb_mount(const char* _ptr) {
|
|||
int headok = 0;
|
||||
int dbver = 0;
|
||||
int headsize = 0;
|
||||
int filecount = 0;
|
||||
size_t filecount = 0;
|
||||
|
||||
const char * ptr = (const char *)_ptr;
|
||||
|
||||
|
@ -338,6 +341,12 @@ _after_head:
|
|||
#ifdef LUAT_USE_FS_VFS
|
||||
|
||||
FILE* luat_vfs_luadb_fopen(void* userdata, const char *filename, const char *mode) {
|
||||
if (!strcmp("r", mode) || !strcmp("rb", mode) || !strcmp("r+", mode) || !strcmp("rb+", mode)) {
|
||||
}
|
||||
else {
|
||||
// 暂时只警告
|
||||
LLOGW("/luadb is readonly %s %s", filename, mode);
|
||||
}
|
||||
return (FILE*)luat_luadb_open((luadb_fs_t*)userdata, filename, 0, 0);
|
||||
}
|
||||
|
||||
|
@ -363,6 +372,8 @@ int luat_vfs_luadb_feof(void* userdata, FILE* stream) {
|
|||
return cur >= end ? 1 : 0;
|
||||
}
|
||||
int luat_vfs_luadb_ferror(void* userdata, FILE *stream) {
|
||||
(void)userdata;
|
||||
(void)stream;
|
||||
return 0;
|
||||
}
|
||||
size_t luat_vfs_luadb_fread(void* userdata, void *ptr, size_t size, size_t nmemb, FILE *stream) {
|
||||
|
@ -378,12 +389,24 @@ int luat_vfs_luadb_getc(void* userdata, FILE* stream) {
|
|||
return -1;
|
||||
}
|
||||
size_t luat_vfs_luadb_fwrite(void* userdata, const void *ptr, size_t size, size_t nmemb, FILE *stream) {
|
||||
(void)userdata;
|
||||
(void)stream;
|
||||
(void)ptr;
|
||||
(void)size;
|
||||
(void)nmemb;
|
||||
return 0;
|
||||
}
|
||||
int luat_vfs_luadb_remove(void* userdata, const char *filename) {
|
||||
(void)userdata;
|
||||
(void)filename;
|
||||
LLOGW("/luadb is readonly %s", filename);
|
||||
return -1;
|
||||
}
|
||||
int luat_vfs_luadb_rename(void* userdata, const char *old_filename, const char *new_filename) {
|
||||
(void)userdata;
|
||||
(void)old_filename;
|
||||
(void)new_filename;
|
||||
LLOGW("/luadb is readonly %s", old_filename);
|
||||
return -1;
|
||||
}
|
||||
int luat_vfs_luadb_fexist(void* userdata, const char *filename) {
|
||||
|
@ -409,6 +432,8 @@ size_t luat_vfs_luadb_fsize(void* userdata, const char *filename) {
|
|||
|
||||
int luat_vfs_luadb_mkfs(void* userdata, luat_fs_conf_t *conf) {
|
||||
//LLOGE("not support yet : mkfs");
|
||||
(void)userdata;
|
||||
(void)conf;
|
||||
return -1;
|
||||
}
|
||||
int luat_vfs_luadb_mount(void** userdata, luat_fs_conf_t *conf) {
|
||||
|
@ -420,20 +445,27 @@ int luat_vfs_luadb_mount(void** userdata, luat_fs_conf_t *conf) {
|
|||
}
|
||||
int luat_vfs_luadb_umount(void* userdata, luat_fs_conf_t *conf) {
|
||||
//LLOGE("not support yet : umount");
|
||||
(void)userdata;
|
||||
(void)conf;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int luat_vfs_luadb_mkdir(void* userdata, char const* _DirName) {
|
||||
//LLOGE("not support yet : mkdir");
|
||||
(void)userdata;
|
||||
(void)_DirName;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int luat_vfs_luadb_rmdir(void* userdata, char const* _DirName) {
|
||||
//LLOGE("not support yet : rmdir");
|
||||
(void)userdata;
|
||||
(void)_DirName;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int luat_vfs_luadb_lsdir(void* userdata, char const* _DirName, luat_fs_dirent_t* ents, size_t offset, size_t len) {
|
||||
(void)_DirName;
|
||||
luadb_fs_t* fs = (luadb_fs_t*)userdata;
|
||||
if (fs->filecount > offset) {
|
||||
if (offset + len > fs->filecount)
|
||||
|
@ -449,21 +481,33 @@ int luat_vfs_luadb_lsdir(void* userdata, char const* _DirName, luat_fs_dirent_t*
|
|||
}
|
||||
|
||||
int luat_vfs_luadb_info(void* userdata, const char* path, luat_fs_info_t *conf) {
|
||||
(void)path;
|
||||
memcpy(conf->filesystem, "luadb", strlen("luadb")+1);
|
||||
// 把luadb的第一个文件的偏移量估算为起始位置
|
||||
// 最后一个文件的偏移量+文件大小, 作为结束位置
|
||||
// 从而估算出luadb的实际用量
|
||||
size_t used = 0;
|
||||
luadb_fs_t* fs = (luadb_fs_t*)userdata;
|
||||
if (fs != NULL && fs->filecount > 0) {
|
||||
size_t begin = (size_t)fs->files[0].ptr;
|
||||
size_t end = (size_t)(fs->files[fs->filecount - 1].ptr) + fs->files[fs->filecount - 1].size;
|
||||
used = end - begin + 512;
|
||||
}
|
||||
conf->type = 0;
|
||||
conf->total_block = 0;
|
||||
conf->block_used = 0;
|
||||
conf->block_used = (used / 512) + 1;
|
||||
conf->block_size = 512;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char* luat_vfs_luadb_mmap(void* userdata, int fd) {
|
||||
void* luat_vfs_luadb_mmap(void* userdata, FILE* f) {
|
||||
luadb_fs_t* fs = (luadb_fs_t*)userdata;
|
||||
if (fd < 0 || fd >= LUAT_LUADB_MAX_OPENFILE || fs->fds[fd].file == NULL)
|
||||
int fd = (int)f;
|
||||
if (fd < 0 || fd >= LUAT_LUADB_MAX_OPENFILE || fs->fds[(int)fd].file == NULL)
|
||||
return 0;
|
||||
luadb_fd_t *fdt = &fs->fds[fd];
|
||||
luadb_fd_t *fdt = &fs->fds[(int)fd];
|
||||
if (fdt != NULL) {
|
||||
return fdt->file->ptr;
|
||||
return (void*)fdt->file->ptr;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user