luat_fs_air101.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include "luat_conf_bsp.h"
  2. #include "luat_base.h"
  3. #include "luat_fs.h"
  4. #define LUAT_LOG_TAG "luat.fs"
  5. #include "luat_log.h"
  6. #include "lfs_port.h"
  7. #include "wm_include.h"
  8. extern struct lfs_config lfs_cfg;
  9. extern lfs_t lfs;
  10. #ifndef LUAT_USE_FS_VFS
  11. FILE *luat_fs_fopen(const char *filename, const char *mode)
  12. {
  13. ////LLOGD("fopen %s %s", filename, mode);
  14. int ret;
  15. char *t = (char *)mode;
  16. int flags = 0;
  17. lfs_file_t *lfsfile = NULL;
  18. for (int i = 0; i < strlen(mode); i++)
  19. {
  20. switch (*(t++))
  21. {
  22. case 'w':
  23. flags |= LFS_O_RDWR;
  24. break;
  25. case 'r':
  26. flags |= LFS_O_RDONLY;
  27. break;
  28. case 'a':
  29. flags |= LFS_O_APPEND;
  30. break;
  31. case 'b':
  32. break;
  33. case '+':
  34. break;
  35. default:
  36. break;
  37. }
  38. }
  39. lfsfile = tls_mem_alloc(sizeof(lfs_file_t));
  40. if (!lfsfile)
  41. {
  42. return lfsfile;
  43. }
  44. ret = lfs_file_open(&lfs, lfsfile, filename, flags);
  45. if (ret != 0)
  46. {
  47. return NULL;
  48. }
  49. return lfsfile;
  50. }
  51. int luat_fs_getc(FILE *stream)
  52. {
  53. return getc(stream);
  54. }
  55. int luat_fs_fseek(FILE *stream, long int offset, int origin)
  56. {
  57. int ret;
  58. ret = lfs_file_seek(&lfs, stream, offset, origin);
  59. if (ret < 0)
  60. {
  61. return -1;
  62. }
  63. return ret;
  64. }
  65. int luat_fs_ftell(FILE *stream)
  66. {
  67. return ftell(stream);
  68. }
  69. int luat_fs_fclose(FILE *stream)
  70. {
  71. int ret;
  72. ret = lfs_file_close(&lfs, (lfs_dir_t *)stream);
  73. if (ret != 0)
  74. {
  75. return 1;
  76. }
  77. tls_mem_free(stream);
  78. return 0;
  79. }
  80. int luat_fs_feof(FILE *stream)
  81. {
  82. return feof(stream);
  83. }
  84. int luat_fs_ferror(FILE *stream)
  85. {
  86. return ferror(stream);
  87. }
  88. size_t luat_fs_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
  89. {
  90. int ret;
  91. ret = lfs_file_read(&lfs, stream,ptr, size * nmemb);
  92. if (ret < 0)
  93. {
  94. return 0;
  95. }
  96. return ret;
  97. }
  98. size_t luat_fs_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
  99. {
  100. int ret;
  101. ret = lfs_file_write(&lfs,stream, ptr, size * nmemb);
  102. if (ret < 0)
  103. {
  104. return 0;
  105. }
  106. return ret;
  107. }
  108. int luat_fs_remove(const char *filename)
  109. {
  110. int ret;
  111. ret = lfs_remove(&lfs,filename);
  112. if(ret != 0)
  113. {
  114. return 1;
  115. }
  116. return 0;
  117. }
  118. int luat_fs_rename(const char *old_filename, const char *new_filename)
  119. {
  120. int ret;
  121. ret = lfs_rename(&lfs, old_filename,new_filename);
  122. if (ret != 0)
  123. {
  124. return 1;
  125. }
  126. return 0;
  127. }
  128. int luat_fs_fexist(const char *filename)
  129. {
  130. FILE *fd = luat_fs_fopen(filename, "rb");
  131. if (fd)
  132. {
  133. luat_fs_fclose(fd);
  134. return 1;
  135. }
  136. return 0;
  137. }
  138. size_t luat_fs_fsize(const char *filename)
  139. {
  140. FILE *fd;
  141. size_t size = 0;
  142. fd = luat_fs_fopen(filename, "rb");
  143. if (fd)
  144. {
  145. luat_fs_fseek(fd, 0, SEEK_END);
  146. size = luat_fs_ftell(fd);
  147. luat_fs_fclose(fd);
  148. }
  149. return size;
  150. }
  151. int luat_fs_init(void)
  152. {
  153. LFS_Init();
  154. }
  155. #else
  156. extern const struct luat_vfs_filesystem vfs_fs_lfs2;
  157. #ifdef LUAT_USE_VFS_INLINE_LIB
  158. extern const char luadb_inline_sys[];
  159. extern const char luadb_inline[];
  160. extern const struct luat_vfs_filesystem vfs_fs_luadb;
  161. #endif
  162. #ifdef LUAT_USE_LVGL
  163. #include "lvgl.h"
  164. void luat_lv_fs_init(void);
  165. void lv_bmp_init(void);
  166. void lv_png_init(void);
  167. void lv_split_jpeg_init(void);
  168. #endif
  169. int luat_fs_init(void) {
  170. LFS_Init();
  171. luat_vfs_reg(&vfs_fs_lfs2);
  172. luat_fs_conf_t conf = {
  173. .busname = &lfs,
  174. .type = "lfs2",
  175. .filesystem = "lfs2",
  176. .mount_point = "/"
  177. };
  178. luat_fs_mount(&conf);
  179. #ifdef LUAT_USE_VFS_INLINE_LIB
  180. luat_vfs_reg(&vfs_fs_luadb);
  181. luat_fs_conf_t conf2 = {
  182. .busname = (char*)luadb_inline_sys,
  183. .type = "luadb",
  184. .filesystem = "luadb",
  185. .mount_point = "/luadb/",
  186. };
  187. luat_fs_mount(&conf2);
  188. #endif
  189. #ifdef LUAT_USE_LVGL
  190. luat_lv_fs_init();
  191. lv_bmp_init();
  192. lv_png_init();
  193. lv_split_jpeg_init();
  194. #endif
  195. return 0;
  196. }
  197. #endif