luat_fs_mem.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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"
  6. #if 1
  7. #define BLOCK_SIZE 4096
  8. #define MEMFS_MAX_FILE_NAME 63
  9. typedef struct ram_file_block
  10. {
  11. uint8_t data[BLOCK_SIZE];
  12. struct ram_file_block* next;
  13. } ram_file_block_t;
  14. typedef struct ram_file
  15. {
  16. size_t size; // 当前文件大小
  17. char name[MEMFS_MAX_FILE_NAME + 1]; // 文件名称
  18. ram_file_block_t* head; // 链表头指针
  19. } ram_file_t;
  20. typedef struct luat_ram_fd
  21. {
  22. int fid;
  23. uint32_t offset;
  24. uint8_t readonly;
  25. } luat_raw_fd_t;
  26. #define RAM_FILE_MAX (64)
  27. static ram_file_t* files[RAM_FILE_MAX];
  28. size_t luat_vfs_ram_fread(void* userdata, void *ptr, size_t size, size_t nmemb, FILE *stream);
  29. FILE* luat_vfs_ram_fopen(void* userdata, const char *filename, const char *mode) {
  30. (void)userdata;
  31. // LLOGD("ram fs open %s %s", filename, mode);
  32. if (filename == NULL || mode == NULL || strlen(filename) > MEMFS_MAX_FILE_NAME) {
  33. return NULL;
  34. }
  35. // 读文件
  36. if (!strcmp("r", mode) || !strcmp("rb", mode)) {
  37. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  38. {
  39. if (files[i] == NULL)
  40. continue;
  41. if (!strcmp(files[i]->name, filename)) {
  42. luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
  43. if (fd == NULL) {
  44. LLOGE("out of memory when malloc luat_raw_fd_t");
  45. return NULL;
  46. }
  47. fd->fid = i;
  48. fd->offset = 0;
  49. fd->readonly = 1;
  50. return (FILE*)fd;
  51. }
  52. }
  53. return NULL;
  54. }
  55. // 写文件
  56. else if (!strcmp("w", mode) || !strcmp("wb", mode) || !strcmp("w+", mode) || !strcmp("wb+", mode) || !strcmp("w+b", mode) || !strcmp("r+", mode) || !strcmp("rb+", mode) || !strcmp("r+b", mode)) {
  57. // 先看看是否存在, 如果存在就重用老的
  58. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  59. {
  60. if (files[i] == NULL)
  61. continue;
  62. if (!strcmp(files[i]->name, filename)) {
  63. luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
  64. if (fd == NULL) {
  65. LLOGE("out of memory when malloc luat_raw_fd_t");
  66. return NULL;
  67. }
  68. fd->fid = i;
  69. fd->readonly = 0;
  70. fd->offset = 0;
  71. if (!strcmp("w+", mode) || !strcmp("wb+", mode) || !strcmp("w+b", mode)) {
  72. // 截断模式
  73. files[i]->size = 0;
  74. ram_file_block_t* block = files[i]->head;
  75. while (block) {
  76. ram_file_block_t* next = block->next;
  77. luat_heap_free(block);
  78. block = next;
  79. }
  80. files[i]->head = NULL;
  81. }
  82. return (FILE*)fd;
  83. }
  84. }
  85. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  86. {
  87. if (files[i] != NULL)
  88. continue;
  89. ram_file_t *file = luat_heap_malloc(sizeof(ram_file_t));
  90. if (file == NULL) {
  91. LLOGE("out of memory when malloc ram_file_t");
  92. return NULL;
  93. }
  94. memset(file, 0, sizeof(ram_file_t));
  95. strcpy(file->name, filename);
  96. files[i] = file;
  97. luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
  98. if (fd == NULL) {
  99. LLOGE("out of memory when malloc luat_raw_fd_t");
  100. return NULL;
  101. }
  102. fd->fid = i;
  103. fd->offset = 0;
  104. fd->readonly = 0;
  105. return (FILE*)fd;
  106. }
  107. }
  108. // 追加模式
  109. else if (!strcmp("a", mode) || !strcmp("ab", mode) || !strcmp("a+", mode) || !strcmp("ab+", mode) || !strcmp("a+b", mode) ) {
  110. // 先看看是否存在, 如果存在就重用老的
  111. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  112. {
  113. if (files[i] == NULL)
  114. continue;
  115. if (!strcmp(files[i]->name, filename)) {
  116. luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
  117. if (fd == NULL) {
  118. LLOGE("out of memory when malloc luat_raw_fd_t");
  119. return NULL;
  120. }
  121. fd->fid = i;
  122. fd->offset = files[i]->size;
  123. fd->readonly = 0;
  124. return (FILE*)fd;
  125. }
  126. }
  127. LLOGW("file %s not found, can't open with mode %s", filename, mode);
  128. return NULL;
  129. }
  130. else {
  131. LLOGE("unkown open mode %s", mode);
  132. return NULL;
  133. }
  134. LLOGE("too many ram files >= %d", RAM_FILE_MAX);
  135. return NULL;
  136. }
  137. int luat_vfs_ram_getc(void* userdata, FILE* stream) {
  138. uint8_t c = 0;
  139. size_t len = luat_vfs_ram_fread(userdata, &c, 1, 1, stream);
  140. if (len == 1) {
  141. return c;
  142. }
  143. return -1;
  144. }
  145. int luat_vfs_ram_fseek(void* userdata, FILE* stream, long int offset, int origin) {
  146. (void)userdata;
  147. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  148. // LLOGE("luat_vfs_ram_fseek seek %p %p %d %d", userdata, stream, offset, origin);
  149. if (origin == SEEK_CUR) {
  150. fd->offset += offset;
  151. }
  152. else if (origin == SEEK_SET) {
  153. fd->offset = offset;
  154. }
  155. else {
  156. fd->offset = files[fd->fid]->size - offset;
  157. }
  158. if (fd->offset > files[fd->fid]->size) {
  159. // 如果偏移量超过了文件大小,设置为文件大小
  160. fd->offset = files[fd->fid]->size;
  161. }
  162. return 0;
  163. }
  164. int luat_vfs_ram_ftell(void* userdata, FILE* stream) {
  165. (void)userdata;
  166. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  167. // LLOGD("tell %p %p offset %d", userdata, stream, fd->offset);
  168. return fd->offset;
  169. }
  170. int luat_vfs_ram_fclose(void* userdata, FILE* stream) {
  171. (void)userdata;
  172. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  173. //LLOGD("fclose %p %p %d %d", userdata, stream, fd->size, fd->offset);
  174. luat_heap_free(fd);
  175. return 0;
  176. }
  177. int luat_vfs_ram_feof(void* userdata, FILE* stream) {
  178. (void)userdata;
  179. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  180. //LLOGD("feof %p %p %d %d", userdata, stream, fd->size, fd->offset);
  181. return fd->offset >= files[fd->fid]->size ? 1 : 0;
  182. }
  183. int luat_vfs_ram_ferror(void* userdata, FILE *stream) {
  184. (void)userdata;
  185. (void)stream;
  186. return 0;
  187. }
  188. size_t luat_vfs_ram_fread(void* userdata, void *ptr, size_t size, size_t nmemb, FILE *stream) {
  189. (void)userdata;
  190. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  191. size_t read_size = size * nmemb;
  192. // 如果偏移量已经超出文件大小
  193. if (fd->offset >= files[fd->fid]->size) {
  194. return 0;
  195. }
  196. // 如果读取的大小超出文件剩余大小,调整读取大小
  197. if (fd->offset + read_size > files[fd->fid]->size) {
  198. read_size = files[fd->fid]->size - fd->offset;
  199. }
  200. // 找到offset对应的起始block
  201. ram_file_block_t* block = files[fd->fid]->head;
  202. size_t offset = fd->offset;
  203. while (block != NULL && offset >= BLOCK_SIZE) {
  204. offset -= BLOCK_SIZE;
  205. block = block->next;
  206. }
  207. // 如果没有找到对应的block
  208. if (block == NULL) {
  209. LLOGW("no block for offset %d", fd->offset);
  210. return 0;
  211. }
  212. // 开始读取
  213. uint8_t* dst = (uint8_t*)ptr;
  214. size_t bytes_read = 0; // 用于记录实际读取的字节数
  215. while (block != NULL && read_size > 0) {
  216. size_t copy_size = BLOCK_SIZE - (offset % BLOCK_SIZE);
  217. if (copy_size > read_size) {
  218. copy_size = read_size;
  219. }
  220. memcpy(dst, block->data + (offset % BLOCK_SIZE), copy_size);
  221. dst += copy_size;
  222. read_size -= copy_size;
  223. offset += copy_size;
  224. bytes_read += copy_size; // 累加读取的字节数
  225. if (offset % BLOCK_SIZE == 0) {
  226. block = block->next;
  227. }
  228. }
  229. fd->offset += bytes_read; // 更新文件偏移量
  230. return bytes_read; // 返回实际读取的字节数
  231. }
  232. size_t luat_vfs_ram_fwrite(void* userdata, const void *ptr, size_t size, size_t nmemb, FILE *stream) {
  233. (void)userdata;
  234. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  235. size_t write_size = size * nmemb;
  236. if (fd->readonly) {
  237. LLOGW("readonly fd %d!! path %s", fd->fid, files[fd->fid]->name);
  238. return 0;
  239. }
  240. // 计算最终需要的总大小
  241. size_t total_size = fd->offset + write_size;
  242. // size_t current_size = files[fd->fid]->size; // 当前文件大小
  243. // 先补齐block
  244. ram_file_block_t* block = files[fd->fid]->head;
  245. size_t block_offset = 0; // 当前block的偏移量
  246. // 遍历现有的block,直到block_offset达到total_size
  247. while (block != NULL && block_offset < total_size) {
  248. block_offset += BLOCK_SIZE;
  249. block = block->next;
  250. }
  251. // 如果block_offset小于total_size,继续分配新的block
  252. while (block_offset < total_size) {
  253. block = luat_heap_malloc(sizeof(ram_file_block_t));
  254. if (block == NULL) {
  255. LLOGW("out of memory when malloc ram_file_block_t");
  256. return 0;
  257. }
  258. memset(block, 0, sizeof(ram_file_block_t));
  259. block->next = NULL;
  260. // 将新分配的block链接到链表末尾
  261. if (files[fd->fid]->head == NULL) {
  262. files[fd->fid]->head = block;
  263. } else {
  264. ram_file_block_t* last = files[fd->fid]->head;
  265. while (last->next) {
  266. last = last->next;
  267. }
  268. last->next = block;
  269. }
  270. block_offset += BLOCK_SIZE;
  271. }
  272. // 现在偏移到offset对应的block
  273. block = files[fd->fid]->head;
  274. size_t offset = fd->offset;
  275. while (block != NULL && offset >= BLOCK_SIZE) {
  276. offset -= BLOCK_SIZE;
  277. block = block->next;
  278. }
  279. // 开始写
  280. const uint8_t* src = (const uint8_t*)ptr;
  281. while (write_size > 0) {
  282. size_t copy_size = BLOCK_SIZE - (offset % BLOCK_SIZE);
  283. if (copy_size > write_size) {
  284. copy_size = write_size;
  285. }
  286. memcpy(block->data + (offset % BLOCK_SIZE), src, copy_size);
  287. src += copy_size;
  288. write_size -= copy_size;
  289. offset += copy_size;
  290. if (offset % BLOCK_SIZE == 0) {
  291. block = block->next;
  292. }
  293. }
  294. fd->offset += (size_t)(src - (const uint8_t*)ptr);
  295. // 更新文件大小
  296. files[fd->fid]->size = total_size;
  297. // 打印一下写入的数据
  298. // LLOGD("write data %s", (char*)ptr);
  299. return (size_t)(src - (const uint8_t*)ptr);
  300. }
  301. int luat_vfs_ram_remove(void* userdata, const char *filename) {
  302. (void)userdata;
  303. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  304. {
  305. if (files[i] == NULL)
  306. continue;
  307. if (!strcmp(filename, files[i]->name)) {
  308. ram_file_block_t* block = files[i]->head;
  309. while (block) {
  310. ram_file_block_t* next = block->next;
  311. luat_heap_free(block);
  312. block = next;
  313. }
  314. luat_heap_free(files[i]);
  315. files[i] = NULL;
  316. return 0;
  317. }
  318. }
  319. return 0;
  320. }
  321. int luat_vfs_ram_rename(void* userdata, const char *old_filename, const char *new_filename) {
  322. (void)userdata;
  323. if (old_filename == NULL || new_filename == NULL)
  324. return -1;
  325. if (strlen(old_filename) > 31 || strlen(new_filename) > 31)
  326. return -2;
  327. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  328. {
  329. if (files[i] == NULL)
  330. continue;
  331. if (!strcmp(old_filename, files[i]->name)) {
  332. strcpy(files[i]->name, new_filename);
  333. return 0;
  334. }
  335. }
  336. return 0;
  337. }
  338. int luat_vfs_ram_fexist(void* userdata, const char *filename) {
  339. (void)userdata;
  340. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  341. {
  342. if (files[i] == NULL)
  343. continue;
  344. if (!strcmp(filename, files[i]->name)) {
  345. return 1;
  346. }
  347. }
  348. return 0;
  349. }
  350. size_t luat_vfs_ram_fsize(void* userdata, const char *filename) {
  351. (void)userdata;
  352. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  353. {
  354. if (files[i] == NULL)
  355. continue;
  356. if (!strcmp(filename, files[i]->name)) {
  357. return files[i]->size;
  358. }
  359. }
  360. return 0;
  361. }
  362. void* luat_vfs_ram_mmap(void* userdata, FILE *stream) {
  363. (void)userdata;
  364. luat_raw_fd_t *fd = (luat_raw_fd_t*)(stream);
  365. //LLOGD("fsize %p %p %d %d", userdata, fd);
  366. return files[fd->fid]->head->data;
  367. }
  368. int luat_vfs_ram_mkfs(void* userdata, luat_fs_conf_t *conf) {
  369. (void)userdata;
  370. (void)conf;
  371. return -1;
  372. }
  373. int luat_vfs_ram_mount(void** userdata, luat_fs_conf_t *conf) {
  374. (void)userdata;
  375. (void)conf;
  376. return 0;
  377. }
  378. int luat_vfs_ram_umount(void* userdata, luat_fs_conf_t *conf) {
  379. (void)userdata;
  380. (void)conf;
  381. return 0;
  382. }
  383. int luat_vfs_ram_mkdir(void* userdata, char const* _DirName) {
  384. (void)userdata;
  385. (void)_DirName;
  386. return -1;
  387. }
  388. int luat_vfs_ram_rmdir(void* userdata, char const* _DirName) {
  389. (void)userdata;
  390. (void)_DirName;
  391. return -1;
  392. }
  393. int luat_vfs_ram_lsdir(void* userdata, char const* _DirName, luat_fs_dirent_t* ents, size_t offset, size_t len) {
  394. (void)userdata;
  395. (void)_DirName;
  396. size_t count = 0;
  397. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  398. {
  399. if (count >= len)
  400. break;
  401. if (files[i] == NULL)
  402. continue;
  403. if (offset > 0) {
  404. offset --;
  405. continue;
  406. }
  407. ents[count].d_type = 0;
  408. strcpy(ents[count].d_name, files[i]->name);
  409. count ++;
  410. }
  411. return count;
  412. }
  413. int luat_vfs_ram_info(void* userdata, const char* path, luat_fs_info_t *conf) {
  414. (void)userdata;
  415. (void)path;
  416. memcpy(conf->filesystem, "ram", strlen("ram")+1);
  417. size_t ftotal = 0;
  418. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  419. {
  420. if (files[i] == NULL)
  421. continue;
  422. ftotal += files[i]->size;
  423. }
  424. size_t total; size_t used; size_t max_used;
  425. luat_meminfo_sys(&total, &used, &max_used);
  426. conf->type = 0;
  427. conf->total_block = 64;
  428. conf->block_used = (ftotal + BLOCK_SIZE) / BLOCK_SIZE;
  429. conf->block_size = BLOCK_SIZE;
  430. return 0;
  431. }
  432. int luat_vfs_ram_truncate(void* fsdata, char const* path, size_t nsize) {
  433. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  434. {
  435. if (files[i] == NULL)
  436. continue;
  437. if (!strcmp(files[i]->name, path)) {
  438. ram_file_block_t* block = files[i]->head;
  439. size_t offset = 0;
  440. while (block) {
  441. if (offset + BLOCK_SIZE > nsize) {
  442. memset(block->data + (nsize - offset), 0, BLOCK_SIZE - (nsize - offset));
  443. }
  444. offset += BLOCK_SIZE;
  445. block = block->next;
  446. }
  447. files[i]->size = nsize;
  448. return 0;
  449. }
  450. }
  451. return 0;
  452. }
  453. #else
  454. typedef struct ram_file
  455. {
  456. size_t size; // 当前文件大小, 也是指针对应内存块的大小
  457. // size_t ptr_size; // 数值指针的大小
  458. char name[32]; // 文件名称
  459. char ptr[4];
  460. }ram_file_t;
  461. typedef struct luat_ram_fd
  462. {
  463. int fid;
  464. uint32_t offset;
  465. uint8_t readonly;
  466. }luat_raw_fd_t;
  467. #define RAM_FILE_MAX (64)
  468. static ram_file_t* files[RAM_FILE_MAX];
  469. FILE* luat_vfs_ram_fopen(void* userdata, const char *filename, const char *mode) {
  470. (void)userdata;
  471. // LLOGD("ram fs open %s %s", filename, mode);
  472. if (filename == NULL || mode == NULL || strlen(filename) > 31)
  473. return NULL;
  474. // 读文件
  475. if (!strcmp("r", mode) || !strcmp("rb", mode)) {
  476. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  477. {
  478. if (files[i]== NULL)
  479. continue;
  480. if (!strcmp(files[i]->name, filename)) {
  481. luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
  482. if (fd == NULL) {
  483. LLOGE("out of memory when malloc luat_raw_fd_t");
  484. return NULL;
  485. }
  486. fd->fid = i;
  487. fd->offset = 0;
  488. fd->readonly = 1;
  489. return (FILE*)fd;
  490. }
  491. }
  492. return NULL;
  493. }
  494. // 写文件
  495. else if (!strcmp("w", mode) || !strcmp("wb", mode) || !strcmp("w+", mode) || !strcmp("wb+", mode) || !strcmp("w+b", mode) || !strcmp("r+", mode) || !strcmp("rb+", mode) || !strcmp("r+b", mode)) {
  496. // 先看看是否存在, 如果存在就重用老的
  497. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  498. {
  499. if (files[i]== NULL)
  500. continue;
  501. if (!strcmp(files[i]->name, filename)) {
  502. luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
  503. if (fd == NULL) {
  504. LLOGE("out of memory when malloc luat_raw_fd_t");
  505. return NULL;
  506. }
  507. fd->fid = i;
  508. fd->readonly = 0;
  509. fd->offset = 0;
  510. if (!strcmp("w+", mode) || !strcmp("wb+", mode) || !strcmp("w+b", mode)) {
  511. // 截断模式
  512. char* tmp = luat_heap_realloc(files[i], sizeof(ram_file_t));
  513. if (tmp) {
  514. files[i] = (ram_file_t*)tmp;
  515. }
  516. else {
  517. LLOGE("realloc ram_file_t failed");
  518. luat_heap_free(fd);
  519. return NULL;
  520. }
  521. files[i]->size = 0;
  522. }
  523. return (FILE*)fd;
  524. }
  525. }
  526. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  527. {
  528. if (files[i] != NULL)
  529. continue;
  530. ram_file_t *file = luat_heap_malloc(sizeof(ram_file_t));
  531. if (file == NULL) {
  532. LLOGE("out of memory when malloc ram_file_t");
  533. return NULL;
  534. }
  535. memset(file, 0, sizeof(ram_file_t));
  536. strcpy(file->name, filename);
  537. files[i] = file;
  538. luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
  539. if (fd == NULL) {
  540. LLOGE("out of memory when malloc luat_raw_fd_t");
  541. return NULL;
  542. }
  543. fd->fid = i;
  544. fd->offset = 0;
  545. fd->readonly = 0;
  546. return (FILE*)fd;
  547. }
  548. }
  549. // 追加模式
  550. else if (!strcmp("a", mode) || !strcmp("ab", mode) || !strcmp("a+", mode) || !strcmp("ab+", mode) || !strcmp("a+b", mode) ) {
  551. // 先看看是否存在, 如果存在就重用老的
  552. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  553. {
  554. if (files[i] == NULL)
  555. continue;
  556. if (!strcmp(files[i]->name, filename)) {
  557. luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
  558. if (fd == NULL) {
  559. LLOGE("out of memory when malloc luat_raw_fd_t");
  560. return NULL;
  561. }
  562. fd->fid = i;
  563. fd->offset = files[i]->size;
  564. fd->readonly = 0;
  565. return (FILE*)fd;
  566. }
  567. }
  568. }
  569. else {
  570. LLOGE("unkown open mode %s", mode);
  571. return NULL;
  572. }
  573. LLOGE("too many ram files >= %d", RAM_FILE_MAX);
  574. return NULL;
  575. }
  576. int luat_vfs_ram_getc(void* userdata, FILE* stream) {
  577. (void)userdata;
  578. //LLOGD("getc %p %p", userdata, stream);
  579. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  580. //LLOGD("getc %p %p %d %d", userdata, stream, fd->offset, fd->size);
  581. if (fd->fid < 0 || fd->fid >= RAM_FILE_MAX) {
  582. return -1;
  583. }
  584. if (files[fd->fid] == NULL) {
  585. return -1;
  586. }
  587. if (fd->offset < files[fd->fid]->size) {
  588. uint8_t c = (uint8_t)files[fd->fid]->ptr[fd->offset];
  589. fd->offset ++;
  590. //LLOGD("getc %02X", c);
  591. return c;
  592. }
  593. return -1;
  594. }
  595. int luat_vfs_ram_fseek(void* userdata, FILE* stream, long int offset, int origin) {
  596. (void)userdata;
  597. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  598. if (origin == SEEK_CUR) {
  599. fd->offset += offset;
  600. return 0;
  601. }
  602. else if (origin == SEEK_SET) {
  603. fd->offset = offset;
  604. return 0;
  605. }
  606. else {
  607. fd->offset = files[fd->fid]->size - offset;
  608. return 0;
  609. }
  610. }
  611. int luat_vfs_ram_ftell(void* userdata, FILE* stream) {
  612. (void)userdata;
  613. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  614. // LLOGD("tell %p %p offset %d", userdata, stream, fd->offset);
  615. return fd->offset;
  616. }
  617. int luat_vfs_ram_fclose(void* userdata, FILE* stream) {
  618. (void)userdata;
  619. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  620. //LLOGD("fclose %p %p %d %d", userdata, stream, fd->size, fd->offset);
  621. luat_heap_free(fd);
  622. return 0;
  623. }
  624. int luat_vfs_ram_feof(void* userdata, FILE* stream) {
  625. (void)userdata;
  626. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  627. //LLOGD("feof %p %p %d %d", userdata, stream, fd->size, fd->offset);
  628. return fd->offset >= files[fd->fid]->size ? 1 : 0;
  629. }
  630. int luat_vfs_ram_ferror(void* userdata, FILE *stream) {
  631. (void)userdata;
  632. (void)stream;
  633. return 0;
  634. }
  635. size_t luat_vfs_ram_fread(void* userdata, void *ptr, size_t size, size_t nmemb, FILE *stream) {
  636. (void)userdata;
  637. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  638. //LLOGD("fread %p %p %d %d", userdata, stream, fd->size, fd->offset);
  639. //LLOGD("fread2 %p %p %d %d", userdata, stream, size * nmemb, fd->offset);
  640. size_t read_size = size*nmemb;
  641. if (fd->offset >= files[fd->fid]->size) {
  642. return 0;
  643. }
  644. if (fd->offset + read_size >= files[fd->fid]->size) {
  645. read_size = files[fd->fid]->size - fd->offset;
  646. }
  647. memcpy(ptr, files[fd->fid]->ptr + fd->offset, read_size);
  648. fd->offset += read_size;
  649. return read_size;
  650. }
  651. size_t luat_vfs_ram_fwrite(void* userdata, const void *ptr, size_t size, size_t nmemb, FILE *stream) {
  652. (void)userdata;
  653. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  654. size_t write_size = size*nmemb;
  655. if (write_size > 128 * 1024) {
  656. LLOGW("ramfs large write !! %ld", write_size);
  657. }
  658. if (fd->readonly) {
  659. LLOGW("readonly fd %d!! path %s", fd->fid, files[fd->fid]->name);
  660. return 0;
  661. }
  662. if (fd->offset + write_size > files[fd->fid]->size) {
  663. char* ptr = luat_heap_realloc(files[fd->fid], fd->offset + write_size + sizeof(ram_file_t));
  664. if (ptr == NULL) {
  665. LLOGW("/ram out of sys memory!! %ld", write_size);
  666. return 0;
  667. }
  668. files[fd->fid] = (ram_file_t*)ptr;
  669. files[fd->fid]->size = fd->offset + write_size;
  670. }
  671. memcpy(files[fd->fid]->ptr + fd->offset, ptr, write_size);
  672. fd->offset += write_size;
  673. return write_size;
  674. }
  675. int luat_vfs_ram_remove(void* userdata, const char *filename) {
  676. (void)userdata;
  677. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  678. {
  679. if (files[i] == NULL)
  680. continue;
  681. if (!strcmp(filename, files[i]->name)) {
  682. luat_heap_free(files[i]);
  683. files[i] = NULL;
  684. }
  685. }
  686. return 0;
  687. }
  688. int luat_vfs_ram_rename(void* userdata, const char *old_filename, const char *new_filename) {
  689. (void)userdata;
  690. if (old_filename == NULL || new_filename == NULL)
  691. return -1;
  692. if (strlen(old_filename) > 31 || strlen(new_filename) > 31)
  693. return -2;
  694. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  695. {
  696. if (files[i] == NULL)
  697. continue;
  698. if (!strcmp(old_filename, files[i]->name)) {
  699. strcpy(files[i]->name, new_filename);
  700. return 0;
  701. }
  702. }
  703. return 0;
  704. }
  705. int luat_vfs_ram_fexist(void* userdata, const char *filename) {
  706. (void)userdata;
  707. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  708. {
  709. if (files[i] == NULL)
  710. continue;
  711. if (!strcmp(filename, files[i]->name)) {
  712. return 1;
  713. }
  714. }
  715. return 0;
  716. }
  717. size_t luat_vfs_ram_fsize(void* userdata, const char *filename) {
  718. (void)userdata;
  719. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  720. {
  721. if (files[i] == NULL)
  722. continue;
  723. if (!strcmp(filename, files[i]->name)) {
  724. return files[i]->size;
  725. }
  726. }
  727. return 0;
  728. }
  729. void* luat_vfs_ram_mmap(void* userdata, FILE *stream) {
  730. (void)userdata;
  731. luat_raw_fd_t *fd = (luat_raw_fd_t*)(stream);
  732. //LLOGD("fsize %p %p %d %d", userdata, fd);
  733. return files[fd->fid]->ptr;
  734. }
  735. int luat_vfs_ram_mkfs(void* userdata, luat_fs_conf_t *conf) {
  736. (void)userdata;
  737. (void)conf;
  738. return -1;
  739. }
  740. int luat_vfs_ram_mount(void** userdata, luat_fs_conf_t *conf) {
  741. (void)userdata;
  742. (void)conf;
  743. return 0;
  744. }
  745. int luat_vfs_ram_umount(void* userdata, luat_fs_conf_t *conf) {
  746. (void)userdata;
  747. (void)conf;
  748. return 0;
  749. }
  750. int luat_vfs_ram_mkdir(void* userdata, char const* _DirName) {
  751. (void)userdata;
  752. (void)_DirName;
  753. return -1;
  754. }
  755. int luat_vfs_ram_rmdir(void* userdata, char const* _DirName) {
  756. (void)userdata;
  757. (void)_DirName;
  758. return -1;
  759. }
  760. int luat_vfs_ram_lsdir(void* userdata, char const* _DirName, luat_fs_dirent_t* ents, size_t offset, size_t len) {
  761. (void)userdata;
  762. (void)_DirName;
  763. size_t count = 0;
  764. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  765. {
  766. if (count >= len)
  767. break;
  768. if (files[i] == NULL)
  769. continue;
  770. if (offset > 0) {
  771. offset --;
  772. continue;
  773. }
  774. ents[count].d_type = 0;
  775. strcpy(ents[count].d_name, files[i]->name);
  776. count ++;
  777. }
  778. return count;
  779. }
  780. int luat_vfs_ram_info(void* userdata, const char* path, luat_fs_info_t *conf) {
  781. (void)userdata;
  782. (void)path;
  783. memcpy(conf->filesystem, "ram", strlen("ram")+1);
  784. size_t ftotal = 0;
  785. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  786. {
  787. if (files[i] == NULL)
  788. continue;
  789. ftotal += files[i]->size;
  790. }
  791. size_t total; size_t used; size_t max_used;
  792. luat_meminfo_sys(&total, &used, &max_used);
  793. conf->type = 0;
  794. conf->total_block = 64;
  795. conf->block_used = (ftotal + 1023) / 1024;
  796. conf->block_size = 1024;
  797. return 0;
  798. }
  799. int luat_vfs_ram_truncate(void* fsdata, char const* path, size_t nsize) {
  800. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  801. {
  802. if (files[i] == NULL)
  803. continue;
  804. if (!strcmp(files[i]->name, path)) {
  805. if (files[i]->size > nsize) {
  806. files[i]->size = nsize;
  807. char* ptr = luat_heap_realloc(files[i], nsize + sizeof(ram_file_t));
  808. if (ptr) {
  809. files[i] = (ram_file_t*)ptr;
  810. }
  811. }
  812. return 0;
  813. }
  814. }
  815. return 0;
  816. }
  817. #endif
  818. #define T(name) .name = luat_vfs_ram_##name
  819. const struct luat_vfs_filesystem vfs_fs_ram = {
  820. .name = "ram",
  821. .opts = {
  822. .mkfs = NULL,
  823. T(mount),
  824. T(umount),
  825. .mkdir = NULL,
  826. .rmdir = NULL,
  827. T(lsdir),
  828. T(remove),
  829. T(rename),
  830. T(fsize),
  831. T(fexist),
  832. T(info),
  833. T(truncate)
  834. },
  835. .fopts = {
  836. T(fopen),
  837. T(getc),
  838. T(fseek),
  839. T(ftell),
  840. T(fclose),
  841. T(feof),
  842. T(ferror),
  843. T(fread),
  844. T(fwrite)
  845. }
  846. };