luat_fs_mem.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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 old_size = files[fd->fid]->size;
  242. size_t needed_size = fd->offset + write_size; // 仅覆盖时可能小于 old_size
  243. // 只在需要扩展时才分配新块, 避免覆盖写导致截断
  244. if (needed_size > old_size) {
  245. size_t old_blocks = (old_size + BLOCK_SIZE - 1) / BLOCK_SIZE;
  246. size_t needed_blocks = (needed_size + BLOCK_SIZE - 1) / BLOCK_SIZE;
  247. // 找到最后一个块
  248. ram_file_block_t* last = files[fd->fid]->head;
  249. if (last == NULL) {
  250. // 还没有任何块, 分配第一个
  251. last = luat_heap_malloc(sizeof(ram_file_block_t));
  252. if (last == NULL) {
  253. LLOGW("out of memory when malloc ram_file_block_t");
  254. return 0;
  255. }
  256. memset(last, 0, sizeof(ram_file_block_t));
  257. last->next = NULL;
  258. files[fd->fid]->head = last;
  259. old_blocks = 1; // 更新块数量
  260. } else {
  261. // 跳到最后一个现有块
  262. size_t idx = 1;
  263. while (last->next) {
  264. last = last->next;
  265. idx ++;
  266. }
  267. // idx 应等于 old_blocks, 不匹配也不影响后续逻辑
  268. }
  269. // 追加缺少的块
  270. for (size_t b = old_blocks; b < needed_blocks; b++) {
  271. ram_file_block_t* nb = luat_heap_malloc(sizeof(ram_file_block_t));
  272. if (nb == NULL) {
  273. LLOGW("out of memory when malloc ram_file_block_t");
  274. return 0;
  275. }
  276. memset(nb, 0, sizeof(ram_file_block_t));
  277. nb->next = NULL;
  278. last->next = nb;
  279. last = nb;
  280. }
  281. }
  282. // 现在偏移到offset对应的block
  283. ram_file_block_t* block = files[fd->fid]->head;
  284. size_t offset = fd->offset;
  285. while (block != NULL && offset >= BLOCK_SIZE) {
  286. offset -= BLOCK_SIZE;
  287. block = block->next;
  288. }
  289. // 开始写
  290. const uint8_t* src = (const uint8_t*)ptr;
  291. while (write_size > 0) {
  292. size_t copy_size = BLOCK_SIZE - (offset % BLOCK_SIZE);
  293. if (copy_size > write_size) {
  294. copy_size = write_size;
  295. }
  296. memcpy(block->data + (offset % BLOCK_SIZE), src, copy_size);
  297. src += copy_size;
  298. write_size -= copy_size;
  299. offset += copy_size;
  300. if (offset % BLOCK_SIZE == 0) {
  301. block = block->next;
  302. }
  303. }
  304. fd->offset += (size_t)(src - (const uint8_t*)ptr);
  305. // 仅当写入越过原末尾时更新大小, 避免覆盖写截断
  306. if (needed_size > old_size) {
  307. files[fd->fid]->size = needed_size;
  308. }
  309. // 打印一下写入的数据
  310. // LLOGD("write data %s", (char*)ptr);
  311. return (size_t)(src - (const uint8_t*)ptr);
  312. }
  313. int luat_vfs_ram_remove(void* userdata, const char *filename) {
  314. (void)userdata;
  315. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  316. {
  317. if (files[i] == NULL)
  318. continue;
  319. if (!strcmp(filename, files[i]->name)) {
  320. ram_file_block_t* block = files[i]->head;
  321. while (block) {
  322. ram_file_block_t* next = block->next;
  323. luat_heap_free(block);
  324. block = next;
  325. }
  326. luat_heap_free(files[i]);
  327. files[i] = NULL;
  328. return 0;
  329. }
  330. }
  331. return 0;
  332. }
  333. int luat_vfs_ram_rename(void* userdata, const char *old_filename, const char *new_filename) {
  334. (void)userdata;
  335. if (old_filename == NULL || new_filename == NULL)
  336. return -1;
  337. if (strlen(old_filename) > MEMFS_MAX_FILE_NAME || strlen(new_filename) > MEMFS_MAX_FILE_NAME)
  338. return -2;
  339. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  340. {
  341. if (files[i] == NULL)
  342. continue;
  343. if (!strcmp(old_filename, files[i]->name)) {
  344. strcpy(files[i]->name, new_filename);
  345. return 0;
  346. }
  347. }
  348. return 0;
  349. }
  350. int luat_vfs_ram_fexist(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 1;
  358. }
  359. }
  360. return 0;
  361. }
  362. size_t luat_vfs_ram_fsize(void* userdata, const char *filename) {
  363. (void)userdata;
  364. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  365. {
  366. if (files[i] == NULL)
  367. continue;
  368. if (!strcmp(filename, files[i]->name)) {
  369. return files[i]->size;
  370. }
  371. }
  372. return 0;
  373. }
  374. void* luat_vfs_ram_mmap(void* userdata, FILE *stream) {
  375. (void)userdata;
  376. luat_raw_fd_t *fd = (luat_raw_fd_t*)(stream);
  377. //LLOGD("fsize %p %p %d %d", userdata, fd);
  378. return files[fd->fid]->head->data;
  379. }
  380. int luat_vfs_ram_mkfs(void* userdata, luat_fs_conf_t *conf) {
  381. (void)userdata;
  382. (void)conf;
  383. return -1;
  384. }
  385. int luat_vfs_ram_mount(void** userdata, luat_fs_conf_t *conf) {
  386. (void)userdata;
  387. (void)conf;
  388. return 0;
  389. }
  390. int luat_vfs_ram_umount(void* userdata, luat_fs_conf_t *conf) {
  391. (void)userdata;
  392. (void)conf;
  393. return 0;
  394. }
  395. int luat_vfs_ram_mkdir(void* userdata, char const* _DirName) {
  396. (void)userdata;
  397. (void)_DirName;
  398. return -1;
  399. }
  400. int luat_vfs_ram_rmdir(void* userdata, char const* _DirName) {
  401. (void)userdata;
  402. (void)_DirName;
  403. return -1;
  404. }
  405. int luat_vfs_ram_lsdir(void* userdata, char const* _DirName, luat_fs_dirent_t* ents, size_t offset, size_t len) {
  406. (void)userdata;
  407. (void)_DirName;
  408. size_t count = 0;
  409. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  410. {
  411. if (count >= len)
  412. break;
  413. if (files[i] == NULL)
  414. continue;
  415. if (offset > 0) {
  416. offset --;
  417. continue;
  418. }
  419. ents[count].d_type = 0;
  420. strcpy(ents[count].d_name, files[i]->name);
  421. count ++;
  422. }
  423. return count;
  424. }
  425. int luat_vfs_ram_info(void* userdata, const char* path, luat_fs_info_t *conf) {
  426. (void)userdata;
  427. (void)path;
  428. memcpy(conf->filesystem, "ram", strlen("ram")+1);
  429. size_t ftotal = 0;
  430. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  431. {
  432. if (files[i] == NULL)
  433. continue;
  434. ftotal += files[i]->size;
  435. }
  436. size_t total; size_t used; size_t max_used;
  437. luat_meminfo_sys(&total, &used, &max_used);
  438. conf->type = 0;
  439. conf->total_block = 64;
  440. conf->block_used = (ftotal + BLOCK_SIZE - 1) / BLOCK_SIZE;
  441. conf->block_size = BLOCK_SIZE;
  442. return 0;
  443. }
  444. int luat_vfs_ram_truncate(void* fsdata, char const* path, size_t nsize) {
  445. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  446. {
  447. if (files[i] == NULL)
  448. continue;
  449. if (!strcmp(files[i]->name, path)) {
  450. // 计算需要保留的块数量
  451. size_t needed_blocks = (nsize + BLOCK_SIZE - 1) / BLOCK_SIZE;
  452. size_t idx = 0;
  453. ram_file_block_t* block = files[i]->head;
  454. ram_file_block_t* prev = NULL;
  455. while (block) {
  456. idx++;
  457. if (idx == needed_blocks) {
  458. // 清零最后一个块多余部分
  459. if (nsize % BLOCK_SIZE) {
  460. memset(block->data + (nsize % BLOCK_SIZE), 0, BLOCK_SIZE - (nsize % BLOCK_SIZE));
  461. }
  462. // 释放后续块
  463. ram_file_block_t* next = block->next;
  464. block->next = NULL;
  465. while (next) {
  466. ram_file_block_t* nn = next->next;
  467. luat_heap_free(next);
  468. next = nn;
  469. }
  470. break;
  471. }
  472. prev = block;
  473. block = block->next;
  474. }
  475. // 如果需要块为0, 释放所有
  476. if (needed_blocks == 0) {
  477. block = files[i]->head;
  478. while (block) {
  479. ram_file_block_t* nn = block->next;
  480. luat_heap_free(block);
  481. block = nn;
  482. }
  483. files[i]->head = NULL;
  484. }
  485. files[i]->size = nsize;
  486. return 0;
  487. }
  488. }
  489. return 0;
  490. }
  491. #else
  492. typedef struct ram_file
  493. {
  494. size_t size; // 当前文件大小, 也是指针对应内存块的大小
  495. // size_t ptr_size; // 数值指针的大小
  496. char name[32]; // 文件名称
  497. char ptr[4];
  498. }ram_file_t;
  499. typedef struct luat_ram_fd
  500. {
  501. int fid;
  502. uint32_t offset;
  503. uint8_t readonly;
  504. }luat_raw_fd_t;
  505. #define RAM_FILE_MAX (64)
  506. static ram_file_t* files[RAM_FILE_MAX];
  507. FILE* luat_vfs_ram_fopen(void* userdata, const char *filename, const char *mode) {
  508. (void)userdata;
  509. // LLOGD("ram fs open %s %s", filename, mode);
  510. if (filename == NULL || mode == NULL || strlen(filename) > 31)
  511. return NULL;
  512. // 读文件
  513. if (!strcmp("r", mode) || !strcmp("rb", mode)) {
  514. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  515. {
  516. if (files[i]== NULL)
  517. continue;
  518. if (!strcmp(files[i]->name, filename)) {
  519. luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
  520. if (fd == NULL) {
  521. LLOGE("out of memory when malloc luat_raw_fd_t");
  522. return NULL;
  523. }
  524. fd->fid = i;
  525. fd->offset = 0;
  526. fd->readonly = 1;
  527. return (FILE*)fd;
  528. }
  529. }
  530. return NULL;
  531. }
  532. // 写文件
  533. 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)) {
  534. // 先看看是否存在, 如果存在就重用老的
  535. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  536. {
  537. if (files[i]== NULL)
  538. continue;
  539. if (!strcmp(files[i]->name, filename)) {
  540. luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
  541. if (fd == NULL) {
  542. LLOGE("out of memory when malloc luat_raw_fd_t");
  543. return NULL;
  544. }
  545. fd->fid = i;
  546. fd->readonly = 0;
  547. fd->offset = 0;
  548. if (!strcmp("w+", mode) || !strcmp("wb+", mode) || !strcmp("w+b", mode)) {
  549. // 截断模式
  550. char* tmp = luat_heap_realloc(files[i], sizeof(ram_file_t));
  551. if (tmp) {
  552. files[i] = (ram_file_t*)tmp;
  553. }
  554. else {
  555. LLOGE("realloc ram_file_t failed");
  556. luat_heap_free(fd);
  557. return NULL;
  558. }
  559. files[i]->size = 0;
  560. }
  561. return (FILE*)fd;
  562. }
  563. }
  564. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  565. {
  566. if (files[i] != NULL)
  567. continue;
  568. ram_file_t *file = luat_heap_malloc(sizeof(ram_file_t));
  569. if (file == NULL) {
  570. LLOGE("out of memory when malloc ram_file_t");
  571. return NULL;
  572. }
  573. memset(file, 0, sizeof(ram_file_t));
  574. strcpy(file->name, filename);
  575. files[i] = file;
  576. luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
  577. if (fd == NULL) {
  578. LLOGE("out of memory when malloc luat_raw_fd_t");
  579. return NULL;
  580. }
  581. fd->fid = i;
  582. fd->offset = 0;
  583. fd->readonly = 0;
  584. return (FILE*)fd;
  585. }
  586. }
  587. // 追加模式
  588. else if (!strcmp("a", mode) || !strcmp("ab", mode) || !strcmp("a+", mode) || !strcmp("ab+", mode) || !strcmp("a+b", mode) ) {
  589. // 先看看是否存在, 如果存在就重用老的
  590. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  591. {
  592. if (files[i] == NULL)
  593. continue;
  594. if (!strcmp(files[i]->name, filename)) {
  595. luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
  596. if (fd == NULL) {
  597. LLOGE("out of memory when malloc luat_raw_fd_t");
  598. return NULL;
  599. }
  600. fd->fid = i;
  601. fd->offset = files[i]->size;
  602. fd->readonly = 0;
  603. return (FILE*)fd;
  604. }
  605. }
  606. }
  607. else {
  608. LLOGE("unkown open mode %s", mode);
  609. return NULL;
  610. }
  611. LLOGE("too many ram files >= %d", RAM_FILE_MAX);
  612. return NULL;
  613. }
  614. int luat_vfs_ram_getc(void* userdata, FILE* stream) {
  615. (void)userdata;
  616. //LLOGD("getc %p %p", userdata, stream);
  617. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  618. //LLOGD("getc %p %p %d %d", userdata, stream, fd->offset, fd->size);
  619. if (fd->fid < 0 || fd->fid >= RAM_FILE_MAX) {
  620. return -1;
  621. }
  622. if (files[fd->fid] == NULL) {
  623. return -1;
  624. }
  625. if (fd->offset < files[fd->fid]->size) {
  626. uint8_t c = (uint8_t)files[fd->fid]->ptr[fd->offset];
  627. fd->offset ++;
  628. //LLOGD("getc %02X", c);
  629. return c;
  630. }
  631. return -1;
  632. }
  633. int luat_vfs_ram_fseek(void* userdata, FILE* stream, long int offset, int origin) {
  634. (void)userdata;
  635. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  636. if (origin == SEEK_CUR) {
  637. fd->offset += offset;
  638. return 0;
  639. }
  640. else if (origin == SEEK_SET) {
  641. fd->offset = offset;
  642. return 0;
  643. }
  644. else {
  645. fd->offset = files[fd->fid]->size - offset;
  646. return 0;
  647. }
  648. }
  649. int luat_vfs_ram_ftell(void* userdata, FILE* stream) {
  650. (void)userdata;
  651. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  652. // LLOGD("tell %p %p offset %d", userdata, stream, fd->offset);
  653. return fd->offset;
  654. }
  655. int luat_vfs_ram_fclose(void* userdata, FILE* stream) {
  656. (void)userdata;
  657. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  658. //LLOGD("fclose %p %p %d %d", userdata, stream, fd->size, fd->offset);
  659. luat_heap_free(fd);
  660. return 0;
  661. }
  662. int luat_vfs_ram_feof(void* userdata, FILE* stream) {
  663. (void)userdata;
  664. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  665. //LLOGD("feof %p %p %d %d", userdata, stream, fd->size, fd->offset);
  666. return fd->offset >= files[fd->fid]->size ? 1 : 0;
  667. }
  668. int luat_vfs_ram_ferror(void* userdata, FILE *stream) {
  669. (void)userdata;
  670. (void)stream;
  671. return 0;
  672. }
  673. size_t luat_vfs_ram_fread(void* userdata, void *ptr, size_t size, size_t nmemb, FILE *stream) {
  674. (void)userdata;
  675. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  676. //LLOGD("fread %p %p %d %d", userdata, stream, fd->size, fd->offset);
  677. //LLOGD("fread2 %p %p %d %d", userdata, stream, size * nmemb, fd->offset);
  678. size_t read_size = size*nmemb;
  679. if (fd->offset >= files[fd->fid]->size) {
  680. return 0;
  681. }
  682. if (fd->offset + read_size >= files[fd->fid]->size) {
  683. read_size = files[fd->fid]->size - fd->offset;
  684. }
  685. memcpy(ptr, files[fd->fid]->ptr + fd->offset, read_size);
  686. fd->offset += read_size;
  687. return read_size;
  688. }
  689. size_t luat_vfs_ram_fwrite(void* userdata, const void *ptr, size_t size, size_t nmemb, FILE *stream) {
  690. (void)userdata;
  691. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  692. size_t write_size = size*nmemb;
  693. if (write_size > 128 * 1024) {
  694. LLOGW("ramfs large write !! %ld", write_size);
  695. }
  696. if (fd->readonly) {
  697. LLOGW("readonly fd %d!! path %s", fd->fid, files[fd->fid]->name);
  698. return 0;
  699. }
  700. if (fd->offset + write_size > files[fd->fid]->size) {
  701. char* ptr = luat_heap_realloc(files[fd->fid], fd->offset + write_size + sizeof(ram_file_t));
  702. if (ptr == NULL) {
  703. LLOGW("/ram out of sys memory!! %ld", write_size);
  704. return 0;
  705. }
  706. files[fd->fid] = (ram_file_t*)ptr;
  707. files[fd->fid]->size = fd->offset + write_size;
  708. }
  709. memcpy(files[fd->fid]->ptr + fd->offset, ptr, write_size);
  710. fd->offset += write_size;
  711. return write_size;
  712. }
  713. int luat_vfs_ram_remove(void* userdata, const char *filename) {
  714. (void)userdata;
  715. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  716. {
  717. if (files[i] == NULL)
  718. continue;
  719. if (!strcmp(filename, files[i]->name)) {
  720. luat_heap_free(files[i]);
  721. files[i] = NULL;
  722. }
  723. }
  724. return 0;
  725. }
  726. int luat_vfs_ram_rename(void* userdata, const char *old_filename, const char *new_filename) {
  727. (void)userdata;
  728. if (old_filename == NULL || new_filename == NULL)
  729. return -1;
  730. if (strlen(old_filename) > 31 || strlen(new_filename) > 31)
  731. return -2;
  732. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  733. {
  734. if (files[i] == NULL)
  735. continue;
  736. if (!strcmp(old_filename, files[i]->name)) {
  737. strcpy(files[i]->name, new_filename);
  738. return 0;
  739. }
  740. }
  741. return 0;
  742. }
  743. int luat_vfs_ram_fexist(void* userdata, const char *filename) {
  744. (void)userdata;
  745. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  746. {
  747. if (files[i] == NULL)
  748. continue;
  749. if (!strcmp(filename, files[i]->name)) {
  750. return 1;
  751. }
  752. }
  753. return 0;
  754. }
  755. size_t luat_vfs_ram_fsize(void* userdata, const char *filename) {
  756. (void)userdata;
  757. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  758. {
  759. if (files[i] == NULL)
  760. continue;
  761. if (!strcmp(filename, files[i]->name)) {
  762. return files[i]->size;
  763. }
  764. }
  765. return 0;
  766. }
  767. void* luat_vfs_ram_mmap(void* userdata, FILE *stream) {
  768. (void)userdata;
  769. luat_raw_fd_t *fd = (luat_raw_fd_t*)(stream);
  770. //LLOGD("fsize %p %p %d %d", userdata, fd);
  771. return files[fd->fid]->ptr;
  772. }
  773. int luat_vfs_ram_mkfs(void* userdata, luat_fs_conf_t *conf) {
  774. (void)userdata;
  775. (void)conf;
  776. return -1;
  777. }
  778. int luat_vfs_ram_mount(void** userdata, luat_fs_conf_t *conf) {
  779. (void)userdata;
  780. (void)conf;
  781. return 0;
  782. }
  783. int luat_vfs_ram_umount(void* userdata, luat_fs_conf_t *conf) {
  784. (void)userdata;
  785. (void)conf;
  786. return 0;
  787. }
  788. int luat_vfs_ram_mkdir(void* userdata, char const* _DirName) {
  789. (void)userdata;
  790. (void)_DirName;
  791. return -1;
  792. }
  793. int luat_vfs_ram_rmdir(void* userdata, char const* _DirName) {
  794. (void)userdata;
  795. (void)_DirName;
  796. return -1;
  797. }
  798. int luat_vfs_ram_lsdir(void* userdata, char const* _DirName, luat_fs_dirent_t* ents, size_t offset, size_t len) {
  799. (void)userdata;
  800. (void)_DirName;
  801. size_t count = 0;
  802. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  803. {
  804. if (count >= len)
  805. break;
  806. if (files[i] == NULL)
  807. continue;
  808. if (offset > 0) {
  809. offset --;
  810. continue;
  811. }
  812. ents[count].d_type = 0;
  813. strcpy(ents[count].d_name, files[i]->name);
  814. count ++;
  815. }
  816. return count;
  817. }
  818. int luat_vfs_ram_info(void* userdata, const char* path, luat_fs_info_t *conf) {
  819. (void)userdata;
  820. (void)path;
  821. memcpy(conf->filesystem, "ram", strlen("ram")+1);
  822. size_t ftotal = 0;
  823. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  824. {
  825. if (files[i] == NULL)
  826. continue;
  827. ftotal += files[i]->size;
  828. }
  829. size_t total; size_t used; size_t max_used;
  830. luat_meminfo_sys(&total, &used, &max_used);
  831. conf->type = 0;
  832. conf->total_block = 64;
  833. conf->block_used = (ftotal + 1023) / 1024;
  834. conf->block_size = 1024;
  835. return 0;
  836. }
  837. int luat_vfs_ram_truncate(void* fsdata, char const* path, size_t nsize) {
  838. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  839. {
  840. if (files[i] == NULL)
  841. continue;
  842. if (!strcmp(files[i]->name, path)) {
  843. if (files[i]->size > nsize) {
  844. files[i]->size = nsize;
  845. char* ptr = luat_heap_realloc(files[i], nsize + sizeof(ram_file_t));
  846. if (ptr) {
  847. files[i] = (ram_file_t*)ptr;
  848. }
  849. }
  850. return 0;
  851. }
  852. }
  853. return 0;
  854. }
  855. #endif
  856. #define T(name) .name = luat_vfs_ram_##name
  857. const struct luat_vfs_filesystem vfs_fs_ram = {
  858. .name = "ram",
  859. .opts = {
  860. .mkfs = NULL,
  861. T(mount),
  862. T(umount),
  863. .mkdir = NULL,
  864. .rmdir = NULL,
  865. T(lsdir),
  866. T(remove),
  867. T(rename),
  868. T(fsize),
  869. T(fexist),
  870. T(info),
  871. T(truncate)
  872. },
  873. .fopts = {
  874. T(fopen),
  875. T(getc),
  876. T(fseek),
  877. T(ftell),
  878. T(fclose),
  879. T(feof),
  880. T(ferror),
  881. T(fread),
  882. T(fwrite)
  883. }
  884. };