fix: 修正API生成逻辑中2024库的url
[LuatOS.git] / luat / vfs / luat_fs_inline.c
blobd9fbcaf93199c3c0784bee29504943501f59f043
1 #include "luat_base.h"
2 #include "luat_fs.h"
3 #include "luat_mem.h"
4 #define LUAT_LOG_TAG "fs"
5 #include "luat_log.h"
7 #include "luat_luadb.h"
9 typedef struct luat_fs_inline
11 const char* ptr;
12 uint32_t size;
13 uint32_t offset;
14 }luat_fs_inline_t;
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[];
19 #else
20 extern const luadb_file_t luat_inline2_libs[];
21 #endif
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;
31 #else
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;
35 #else
36 file = luat_inline2_libs_64bit_size32;
37 #endif
38 #else
39 file = luat_inline2_libs;
40 #endif
41 #endif
43 if (!strcmp("r", mode) || !strcmp("rb", mode)) {
44 luat_fs_inline_t* fd = luat_heap_malloc(sizeof(luat_fs_inline_t));
45 if (fd == NULL) {
46 LLOGE("out of memory when malloc luat_fs_inline_t");
47 return NULL;
49 while (file != NULL && file->ptr != NULL) {
50 if (!memcmp(file->name, filename, strlen(filename)+1)) {
51 break;
53 file ++;
55 if (file == NULL || file->ptr == NULL) {
56 //LLOGD("Not Found %s", filename);
57 return NULL;
59 fd->ptr = file->ptr;
60 fd->size = file->size;
61 fd->offset = 0;
62 return (FILE*)fd;
64 //LLOGD("Not Found %s", filename);
65 return NULL;
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];
74 fd->offset ++;
75 //LLOGD("getc %02X", c);
76 return c;
78 return -1;
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) {
85 fd->offset += offset;
86 return 0;
88 else if (origin == SEEK_SET) {
89 fd->offset = offset;
90 return 0;
92 else {
93 fd->offset = fd->size - offset;
94 return 0;
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);
101 return 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);
107 luat_heap_free(fd);
108 return 0;
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) {
116 return 0;
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;
128 return 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;
136 #else
137 file = luat_inline2_libs_64bit_size32;
138 #endif
139 #else
140 file = luat_inline2_libs;
141 #endif
143 while (file != NULL && file->ptr != NULL) {
144 if (!memcmp(file->name, filename, strlen(filename)+1)) {
145 return 1;
147 file ++;
149 //LLOGD("Not Found %s", filename);
150 return 0;
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);
156 return fd->size;
159 int luat_vfs_inline_mount(void** userdata, luat_fs_conf_t *conf) {
160 *userdata = (luat_fs_inline_t*)conf->busname;
161 return 0;
164 int luat_vfs_inline_info(void* userdata, const char* path, luat_fs_info_t *conf) {
165 memcpy(conf->filesystem, "inline", strlen("inline")+1);
166 conf->type = 0;
167 conf->total_block = 0;
168 conf->block_used = 0;
169 conf->block_size = 512;
170 return 0;
173 #define T(name) .name = luat_vfs_inline_##name
174 const struct luat_vfs_filesystem vfs_fs_inline = {
175 .name = "inline",
176 .opts = {
177 .mkfs = NULL,
178 T(mount),
179 .umount = NULL,
180 .mkdir = NULL,
181 .rmdir = NULL,
182 .lsdir = NULL,
183 .remove = NULL,
184 .rename = NULL,
185 T(fsize),
186 T(fexist),
187 T(info)
189 .fopts = {
190 T(fopen),
191 T(getc),
192 T(fseek),
193 T(ftell),
194 T(fclose),
195 T(feof),
196 T(ferror),
197 T(fread),
198 .fwrite = NULL
201 #endif