4 #define LUAT_LOG_TAG "fs"
7 #include "luat_luadb.h"
9 typedef struct luat_fs_inline
16 #ifdef LUAT_CONF_VM_64bit
17 extern const luadb_file_t luat_inline2_libs_64bit_size64
[];
18 extern const luadb_file_t luat_inline2_libs_64bit_size32
[];
20 extern const luadb_file_t luat_inline2_libs
[];
22 extern const luadb_file_t luat_inline2_libs_source
[];
24 #ifdef LUAT_USE_FS_VFS
26 FILE* luat_vfs_inline_fopen(void* userdata
, const char *filename
, const char *mode
) {
27 //LLOGD("open inline %s", filename);
28 const luadb_file_t
* file
= NULL
;
29 #ifdef LUAT_CONF_USE_LIBSYS_SOURCE
30 file
= luat_inline2_libs_source
;
32 #ifdef LUAT_CONF_VM_64bit
33 #if defined(LUA_USE_LINUX) || (defined(LUA_USE_WINDOWS) && defined(__XMAKE_BUILD__))
34 file
= luat_inline2_libs_64bit_size64
;
36 file
= luat_inline2_libs_64bit_size32
;
39 file
= luat_inline2_libs
;
43 if (!strcmp("r", mode
) || !strcmp("rb", mode
)) {
44 luat_fs_inline_t
* fd
= luat_heap_malloc(sizeof(luat_fs_inline_t
));
46 LLOGE("out of memory when malloc luat_fs_inline_t");
49 while (file
!= NULL
&& file
->ptr
!= NULL
) {
50 if (!memcmp(file
->name
, filename
, strlen(filename
)+1)) {
55 if (file
== NULL
|| file
->ptr
== NULL
) {
56 //LLOGD("Not Found %s", filename);
60 fd
->size
= file
->size
;
64 //LLOGD("Not Found %s", filename);
68 int luat_vfs_inline_getc(void* userdata
, FILE* stream
) {
69 //LLOGD("getc %p %p", userdata, stream);
70 luat_fs_inline_t
* fd
= (luat_fs_inline_t
*)stream
;
71 //LLOGD("getc %p %p %d %d", userdata, stream, fd->offset, fd->size);
72 if (fd
->offset
< fd
->size
) {
73 uint8_t c
= (uint8_t)fd
->ptr
[fd
->offset
];
75 //LLOGD("getc %02X", c);
81 int luat_vfs_inline_fseek(void* userdata
, FILE* stream
, long int offset
, int origin
) {
82 //LLOGD("fseek %p %p %d %d", userdata, stream, offset, origin);
83 luat_fs_inline_t
* fd
= (luat_fs_inline_t
*)stream
;
84 if (origin
== SEEK_CUR
) {
88 else if (origin
== SEEK_SET
) {
93 fd
->offset
= fd
->size
- offset
;
98 int luat_vfs_inline_ftell(void* userdata
, FILE* stream
) {
99 luat_fs_inline_t
* fd
= (luat_fs_inline_t
*)stream
;
100 //LLOGD("tell %p %p %d %d", userdata, stream, fd->size, fd->offset);
104 int luat_vfs_inline_fclose(void* userdata
, FILE* stream
) {
105 luat_fs_inline_t
* fd
= (luat_fs_inline_t
*)stream
;
106 //LLOGD("fclose %p %p %d %d", userdata, stream, fd->size, fd->offset);
110 int luat_vfs_inline_feof(void* userdata
, FILE* stream
) {
111 luat_fs_inline_t
* fd
= (luat_fs_inline_t
*)stream
;
112 //LLOGD("feof %p %p %d %d", userdata, stream, fd->size, fd->offset);
113 return fd
->offset
>= fd
->size
? 1 : 0;
115 int luat_vfs_inline_ferror(void* userdata
, FILE *stream
) {
118 size_t luat_vfs_inline_fread(void* userdata
, void *ptr
, size_t size
, size_t nmemb
, FILE *stream
) {
119 luat_fs_inline_t
* fd
= (luat_fs_inline_t
*)stream
;
120 //LLOGD("fread %p %p %d %d", userdata, stream, fd->size, fd->offset);
121 //LLOGD("fread2 %p %p %d %d", userdata, stream, size * nmemb, fd->offset);
122 size_t read_size
= size
*nmemb
;
123 if (fd
->offset
+ read_size
> fd
->size
) {
124 read_size
= fd
->size
- fd
->offset
;
126 memcpy(ptr
, fd
->ptr
+ fd
->offset
, read_size
);
127 fd
->offset
+= read_size
;
131 int luat_vfs_inline_fexist(void* userdata
, const char *filename
) {
132 const luadb_file_t
* file
= NULL
;
133 #ifdef LUAT_CONF_VM_64bit
134 #if defined(LUA_USE_LINUX) || (defined(LUA_USE_WINDOWS) && defined(__XMAKE_BUILD__))
135 file
= luat_inline2_libs_64bit_size64
;
137 file
= luat_inline2_libs_64bit_size32
;
140 file
= luat_inline2_libs
;
143 while (file
!= NULL
&& file
->ptr
!= NULL
) {
144 if (!memcmp(file
->name
, filename
, strlen(filename
)+1)) {
149 //LLOGD("Not Found %s", filename);
153 size_t luat_vfs_inline_fsize(void* userdata
, const char *filename
) {
154 luat_fs_inline_t
*fd
= (luat_fs_inline_t
*)(userdata
);
155 //LLOGD("fsize %p %p %d %d", userdata, fd);
159 int luat_vfs_inline_mount(void** userdata
, luat_fs_conf_t
*conf
) {
160 *userdata
= (luat_fs_inline_t
*)conf
->busname
;
164 int luat_vfs_inline_info(void* userdata
, const char* path
, luat_fs_info_t
*conf
) {
165 memcpy(conf
->filesystem
, "inline", strlen("inline")+1);
167 conf
->total_block
= 0;
168 conf
->block_used
= 0;
169 conf
->block_size
= 512;
173 #define T(name) .name = luat_vfs_inline_##name
174 const struct luat_vfs_filesystem vfs_fs_inline
= {