| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- // #include "luat_conf_bsp.h"
- #include "luat_base.h"
- #include "luat_fs.h"
- #define LUAT_LOG_TAG "luat.fs"
- #include "luat_log.h"
- #include "lfs_port.h"
- #include "wm_include.h"
- extern struct lfs_config lfs_cfg;
- extern lfs_t lfs;
- #ifndef LUAT_USE_FS_VFS
- FILE *luat_fs_fopen(const char *filename, const char *mode)
- {
- ////LLOGD("fopen %s %s", filename, mode);
- int ret;
- char *t = (char *)mode;
- int flags = 0;
- lfs_file_t *lfsfile = NULL;
- for (int i = 0; i < strlen(mode); i++)
- {
- switch (*(t++))
- {
- case 'w':
- flags |= LFS_O_RDWR;
- break;
- case 'r':
- flags |= LFS_O_RDONLY;
- break;
- case 'a':
- flags |= LFS_O_APPEND;
- break;
- case 'b':
- break;
- case '+':
- break;
- default:
- break;
- }
- }
- lfsfile = tls_mem_alloc(sizeof(lfs_file_t));
- if (!lfsfile)
- {
- return lfsfile;
- }
- ret = lfs_file_open(&lfs, lfsfile, filename, flags);
- if (ret != 0)
- {
- return NULL;
- }
- return lfsfile;
- }
- int luat_fs_getc(FILE *stream)
- {
- return getc(stream);
- }
- int luat_fs_fseek(FILE *stream, long int offset, int origin)
- {
- int ret;
- ret = lfs_file_seek(&lfs, stream, offset, origin);
- if (ret < 0)
- {
- return -1;
- }
- return ret;
- }
- int luat_fs_ftell(FILE *stream)
- {
- return ftell(stream);
- }
- int luat_fs_fclose(FILE *stream)
- {
- int ret;
- ret = lfs_file_close(&lfs, (lfs_dir_t *)stream);
- if (ret != 0)
- {
- return 1;
- }
- tls_mem_free(stream);
- return 0;
- }
- int luat_fs_feof(FILE *stream)
- {
- return feof(stream);
- }
- int luat_fs_ferror(FILE *stream)
- {
- return ferror(stream);
- }
- size_t luat_fs_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
- {
- int ret;
- ret = lfs_file_read(&lfs, stream,ptr, size * nmemb);
- if (ret < 0)
- {
- return 0;
- }
- return ret;
- }
- size_t luat_fs_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
- {
- int ret;
- ret = lfs_file_write(&lfs,stream, ptr, size * nmemb);
- if (ret < 0)
- {
- return 0;
- }
- return ret;
- }
- int luat_fs_remove(const char *filename)
- {
- int ret;
- ret = lfs_remove(&lfs,filename);
- if(ret != 0)
- {
- return 1;
- }
- return 0;
- }
- int luat_fs_rename(const char *old_filename, const char *new_filename)
- {
- int ret;
- ret = lfs_rename(&lfs, old_filename,new_filename);
- if (ret != 0)
- {
- return 1;
- }
- return 0;
- }
- int luat_fs_fexist(const char *filename)
- {
- FILE *fd = luat_fs_fopen(filename, "rb");
- if (fd)
- {
- luat_fs_fclose(fd);
- return 1;
- }
- return 0;
- }
- size_t luat_fs_fsize(const char *filename)
- {
- FILE *fd;
- size_t size = 0;
- fd = luat_fs_fopen(filename, "rb");
- if (fd)
- {
- luat_fs_fseek(fd, 0, SEEK_END);
- size = luat_fs_ftell(fd);
- luat_fs_fclose(fd);
- }
- return size;
- }
- int luat_fs_init(void)
- {
- LFS_Init();
- }
- #else
- extern const struct luat_vfs_filesystem vfs_fs_lfs2;
- #ifdef LUAT_USE_VFS_INLINE_LIB
- extern const char luadb_inline_sys[];
- extern const char luadb_inline[];
- extern const struct luat_vfs_filesystem vfs_fs_luadb;
- #endif
- #ifdef LUAT_USE_LVGL
- #include "lvgl.h"
- void luat_lv_fs_init(void);
- void lv_bmp_init(void);
- void lv_png_init(void);
- void lv_split_jpeg_init(void);
- #endif
- int luat_fs_init(void) {
- LFS_Init();
- luat_vfs_reg(&vfs_fs_lfs2);
- luat_fs_conf_t conf = {
- .busname = &lfs,
- .type = "lfs2",
- .filesystem = "lfs2",
- .mount_point = "/"
- };
- luat_fs_mount(&conf);
- #ifdef LUAT_USE_VFS_INLINE_LIB
- luat_vfs_reg(&vfs_fs_luadb);
- luat_fs_conf_t conf2 = {
- .busname = (char*)luadb_inline_sys,
- .type = "luadb",
- .filesystem = "luadb",
- .mount_point = "/luadb/",
- };
- luat_fs_mount(&conf2);
- #endif
- #ifdef LUAT_USE_LVGL
- luat_lv_fs_init();
- lv_bmp_init();
- lv_png_init();
- lv_split_jpeg_init();
- #endif
- return 0;
- }
- #endif
|