luat_fs_mem.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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. while (block) {
  455. idx++;
  456. if (idx == needed_blocks) {
  457. // 清零最后一个块多余部分
  458. if (nsize % BLOCK_SIZE) {
  459. memset(block->data + (nsize % BLOCK_SIZE), 0, BLOCK_SIZE - (nsize % BLOCK_SIZE));
  460. }
  461. // 释放后续块
  462. ram_file_block_t* next = block->next;
  463. block->next = NULL;
  464. while (next) {
  465. ram_file_block_t* nn = next->next;
  466. luat_heap_free(next);
  467. next = nn;
  468. }
  469. break;
  470. }
  471. block = block->next;
  472. }
  473. // 如果需要块为0, 释放所有
  474. if (needed_blocks == 0) {
  475. block = files[i]->head;
  476. while (block) {
  477. ram_file_block_t* nn = block->next;
  478. luat_heap_free(block);
  479. block = nn;
  480. }
  481. files[i]->head = NULL;
  482. }
  483. files[i]->size = nsize;
  484. return 0;
  485. }
  486. }
  487. return 0;
  488. }
  489. #else
  490. typedef struct ram_file
  491. {
  492. size_t size; // 当前文件大小, 也是指针对应内存块的大小
  493. // size_t ptr_size; // 数值指针的大小
  494. char name[32]; // 文件名称
  495. char ptr[4];
  496. }ram_file_t;
  497. typedef struct luat_ram_fd
  498. {
  499. int fid;
  500. uint32_t offset;
  501. uint8_t readonly;
  502. }luat_raw_fd_t;
  503. #define RAM_FILE_MAX (64)
  504. static ram_file_t* files[RAM_FILE_MAX];
  505. FILE* luat_vfs_ram_fopen(void* userdata, const char *filename, const char *mode) {
  506. (void)userdata;
  507. // LLOGD("ram fs open %s %s", filename, mode);
  508. if (filename == NULL || mode == NULL || strlen(filename) > 31)
  509. return NULL;
  510. // 读文件
  511. if (!strcmp("r", mode) || !strcmp("rb", mode)) {
  512. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  513. {
  514. if (files[i]== NULL)
  515. continue;
  516. if (!strcmp(files[i]->name, filename)) {
  517. luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
  518. if (fd == NULL) {
  519. LLOGE("out of memory when malloc luat_raw_fd_t");
  520. return NULL;
  521. }
  522. fd->fid = i;
  523. fd->offset = 0;
  524. fd->readonly = 1;
  525. return (FILE*)fd;
  526. }
  527. }
  528. return NULL;
  529. }
  530. // 写文件
  531. 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)) {
  532. // 先看看是否存在, 如果存在就重用老的
  533. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  534. {
  535. if (files[i]== NULL)
  536. continue;
  537. if (!strcmp(files[i]->name, filename)) {
  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->readonly = 0;
  545. fd->offset = 0;
  546. if (!strcmp("w+", mode) || !strcmp("wb+", mode) || !strcmp("w+b", mode)) {
  547. // 截断模式
  548. char* tmp = luat_heap_realloc(files[i], sizeof(ram_file_t));
  549. if (tmp) {
  550. files[i] = (ram_file_t*)tmp;
  551. }
  552. else {
  553. LLOGE("realloc ram_file_t failed");
  554. luat_heap_free(fd);
  555. return NULL;
  556. }
  557. files[i]->size = 0;
  558. }
  559. return (FILE*)fd;
  560. }
  561. }
  562. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  563. {
  564. if (files[i] != NULL)
  565. continue;
  566. ram_file_t *file = luat_heap_malloc(sizeof(ram_file_t));
  567. if (file == NULL) {
  568. LLOGE("out of memory when malloc ram_file_t");
  569. return NULL;
  570. }
  571. memset(file, 0, sizeof(ram_file_t));
  572. strcpy(file->name, filename);
  573. files[i] = file;
  574. luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
  575. if (fd == NULL) {
  576. LLOGE("out of memory when malloc luat_raw_fd_t");
  577. return NULL;
  578. }
  579. fd->fid = i;
  580. fd->offset = 0;
  581. fd->readonly = 0;
  582. return (FILE*)fd;
  583. }
  584. }
  585. // 追加模式
  586. else if (!strcmp("a", mode) || !strcmp("ab", mode) || !strcmp("a+", mode) || !strcmp("ab+", mode) || !strcmp("a+b", mode) ) {
  587. // 先看看是否存在, 如果存在就重用老的
  588. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  589. {
  590. if (files[i] == NULL)
  591. continue;
  592. if (!strcmp(files[i]->name, filename)) {
  593. luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
  594. if (fd == NULL) {
  595. LLOGE("out of memory when malloc luat_raw_fd_t");
  596. return NULL;
  597. }
  598. fd->fid = i;
  599. fd->offset = files[i]->size;
  600. fd->readonly = 0;
  601. return (FILE*)fd;
  602. }
  603. }
  604. }
  605. else {
  606. LLOGE("unkown open mode %s", mode);
  607. return NULL;
  608. }
  609. LLOGE("too many ram files >= %d", RAM_FILE_MAX);
  610. return NULL;
  611. }
  612. int luat_vfs_ram_getc(void* userdata, FILE* stream) {
  613. (void)userdata;
  614. //LLOGD("getc %p %p", userdata, stream);
  615. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  616. //LLOGD("getc %p %p %d %d", userdata, stream, fd->offset, fd->size);
  617. if (fd->fid < 0 || fd->fid >= RAM_FILE_MAX) {
  618. return -1;
  619. }
  620. if (files[fd->fid] == NULL) {
  621. return -1;
  622. }
  623. if (fd->offset < files[fd->fid]->size) {
  624. uint8_t c = (uint8_t)files[fd->fid]->ptr[fd->offset];
  625. fd->offset ++;
  626. //LLOGD("getc %02X", c);
  627. return c;
  628. }
  629. return -1;
  630. }
  631. int luat_vfs_ram_fseek(void* userdata, FILE* stream, long int offset, int origin) {
  632. (void)userdata;
  633. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  634. if (origin == SEEK_CUR) {
  635. fd->offset += offset;
  636. return 0;
  637. }
  638. else if (origin == SEEK_SET) {
  639. fd->offset = offset;
  640. return 0;
  641. }
  642. else {
  643. fd->offset = files[fd->fid]->size - offset;
  644. return 0;
  645. }
  646. }
  647. int luat_vfs_ram_ftell(void* userdata, FILE* stream) {
  648. (void)userdata;
  649. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  650. // LLOGD("tell %p %p offset %d", userdata, stream, fd->offset);
  651. return fd->offset;
  652. }
  653. int luat_vfs_ram_fclose(void* userdata, FILE* stream) {
  654. (void)userdata;
  655. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  656. //LLOGD("fclose %p %p %d %d", userdata, stream, fd->size, fd->offset);
  657. luat_heap_free(fd);
  658. return 0;
  659. }
  660. int luat_vfs_ram_feof(void* userdata, FILE* stream) {
  661. (void)userdata;
  662. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  663. //LLOGD("feof %p %p %d %d", userdata, stream, fd->size, fd->offset);
  664. return fd->offset >= files[fd->fid]->size ? 1 : 0;
  665. }
  666. int luat_vfs_ram_ferror(void* userdata, FILE *stream) {
  667. (void)userdata;
  668. (void)stream;
  669. return 0;
  670. }
  671. size_t luat_vfs_ram_fread(void* userdata, void *ptr, size_t size, size_t nmemb, FILE *stream) {
  672. (void)userdata;
  673. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  674. //LLOGD("fread %p %p %d %d", userdata, stream, fd->size, fd->offset);
  675. //LLOGD("fread2 %p %p %d %d", userdata, stream, size * nmemb, fd->offset);
  676. size_t read_size = size*nmemb;
  677. if (fd->offset >= files[fd->fid]->size) {
  678. return 0;
  679. }
  680. if (fd->offset + read_size >= files[fd->fid]->size) {
  681. read_size = files[fd->fid]->size - fd->offset;
  682. }
  683. memcpy(ptr, files[fd->fid]->ptr + fd->offset, read_size);
  684. fd->offset += read_size;
  685. return read_size;
  686. }
  687. size_t luat_vfs_ram_fwrite(void* userdata, const void *ptr, size_t size, size_t nmemb, FILE *stream) {
  688. (void)userdata;
  689. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  690. size_t write_size = size*nmemb;
  691. if (write_size > 128 * 1024) {
  692. LLOGW("ramfs large write !! %ld", write_size);
  693. }
  694. if (fd->readonly) {
  695. LLOGW("readonly fd %d!! path %s", fd->fid, files[fd->fid]->name);
  696. return 0;
  697. }
  698. if (fd->offset + write_size > files[fd->fid]->size) {
  699. char* ptr = luat_heap_realloc(files[fd->fid], fd->offset + write_size + sizeof(ram_file_t));
  700. if (ptr == NULL) {
  701. LLOGW("/ram out of sys memory!! %ld", write_size);
  702. return 0;
  703. }
  704. files[fd->fid] = (ram_file_t*)ptr;
  705. files[fd->fid]->size = fd->offset + write_size;
  706. }
  707. memcpy(files[fd->fid]->ptr + fd->offset, ptr, write_size);
  708. fd->offset += write_size;
  709. return write_size;
  710. }
  711. int luat_vfs_ram_remove(void* userdata, const char *filename) {
  712. (void)userdata;
  713. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  714. {
  715. if (files[i] == NULL)
  716. continue;
  717. if (!strcmp(filename, files[i]->name)) {
  718. luat_heap_free(files[i]);
  719. files[i] = NULL;
  720. }
  721. }
  722. return 0;
  723. }
  724. int luat_vfs_ram_rename(void* userdata, const char *old_filename, const char *new_filename) {
  725. (void)userdata;
  726. if (old_filename == NULL || new_filename == NULL)
  727. return -1;
  728. if (strlen(old_filename) > 31 || strlen(new_filename) > 31)
  729. return -2;
  730. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  731. {
  732. if (files[i] == NULL)
  733. continue;
  734. if (!strcmp(old_filename, files[i]->name)) {
  735. strcpy(files[i]->name, new_filename);
  736. return 0;
  737. }
  738. }
  739. return 0;
  740. }
  741. int luat_vfs_ram_fexist(void* userdata, const char *filename) {
  742. (void)userdata;
  743. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  744. {
  745. if (files[i] == NULL)
  746. continue;
  747. if (!strcmp(filename, files[i]->name)) {
  748. return 1;
  749. }
  750. }
  751. return 0;
  752. }
  753. size_t luat_vfs_ram_fsize(void* userdata, const char *filename) {
  754. (void)userdata;
  755. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  756. {
  757. if (files[i] == NULL)
  758. continue;
  759. if (!strcmp(filename, files[i]->name)) {
  760. return files[i]->size;
  761. }
  762. }
  763. return 0;
  764. }
  765. void* luat_vfs_ram_mmap(void* userdata, FILE *stream) {
  766. (void)userdata;
  767. luat_raw_fd_t *fd = (luat_raw_fd_t*)(stream);
  768. //LLOGD("fsize %p %p %d %d", userdata, fd);
  769. return files[fd->fid]->ptr;
  770. }
  771. int luat_vfs_ram_mkfs(void* userdata, luat_fs_conf_t *conf) {
  772. (void)userdata;
  773. (void)conf;
  774. return -1;
  775. }
  776. int luat_vfs_ram_mount(void** userdata, luat_fs_conf_t *conf) {
  777. (void)userdata;
  778. (void)conf;
  779. return 0;
  780. }
  781. int luat_vfs_ram_umount(void* userdata, luat_fs_conf_t *conf) {
  782. (void)userdata;
  783. (void)conf;
  784. return 0;
  785. }
  786. int luat_vfs_ram_mkdir(void* userdata, char const* _DirName) {
  787. (void)userdata;
  788. (void)_DirName;
  789. return -1;
  790. }
  791. int luat_vfs_ram_rmdir(void* userdata, char const* _DirName) {
  792. (void)userdata;
  793. (void)_DirName;
  794. return -1;
  795. }
  796. int luat_vfs_ram_lsdir(void* userdata, char const* _DirName, luat_fs_dirent_t* ents, size_t offset, size_t len) {
  797. (void)userdata;
  798. (void)_DirName;
  799. size_t count = 0;
  800. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  801. {
  802. if (count >= len)
  803. break;
  804. if (files[i] == NULL)
  805. continue;
  806. if (offset > 0) {
  807. offset --;
  808. continue;
  809. }
  810. ents[count].d_type = 0;
  811. strcpy(ents[count].d_name, files[i]->name);
  812. count ++;
  813. }
  814. return count;
  815. }
  816. int luat_vfs_ram_info(void* userdata, const char* path, luat_fs_info_t *conf) {
  817. (void)userdata;
  818. (void)path;
  819. memcpy(conf->filesystem, "ram", strlen("ram")+1);
  820. size_t ftotal = 0;
  821. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  822. {
  823. if (files[i] == NULL)
  824. continue;
  825. ftotal += files[i]->size;
  826. }
  827. size_t total; size_t used; size_t max_used;
  828. luat_meminfo_sys(&total, &used, &max_used);
  829. conf->type = 0;
  830. conf->total_block = 64;
  831. conf->block_used = (ftotal + 1023) / 1024;
  832. conf->block_size = 1024;
  833. return 0;
  834. }
  835. int luat_vfs_ram_truncate(void* fsdata, char const* path, size_t nsize) {
  836. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  837. {
  838. if (files[i] == NULL)
  839. continue;
  840. if (!strcmp(files[i]->name, path)) {
  841. if (files[i]->size > nsize) {
  842. files[i]->size = nsize;
  843. char* ptr = luat_heap_realloc(files[i], nsize + sizeof(ram_file_t));
  844. if (ptr) {
  845. files[i] = (ram_file_t*)ptr;
  846. }
  847. }
  848. return 0;
  849. }
  850. }
  851. return 0;
  852. }
  853. #endif
  854. #define T(name) .name = luat_vfs_ram_##name
  855. const struct luat_vfs_filesystem vfs_fs_ram = {
  856. .name = "ram",
  857. .opts = {
  858. .mkfs = NULL,
  859. T(mount),
  860. T(umount),
  861. .mkdir = NULL,
  862. .rmdir = NULL,
  863. T(lsdir),
  864. T(remove),
  865. T(rename),
  866. T(fsize),
  867. T(fexist),
  868. T(info),
  869. T(truncate)
  870. },
  871. .fopts = {
  872. T(fopen),
  873. T(getc),
  874. T(fseek),
  875. T(ftell),
  876. T(fclose),
  877. T(feof),
  878. T(ferror),
  879. T(fread),
  880. T(fwrite)
  881. }
  882. };