luat_vfs.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #include "luat_base.h"
  2. #include "luat_fs.h"
  3. #define LUAT_LOG_TAG "luat.vfs"
  4. #include "luat_log.h"
  5. #ifdef LUAT_USE_FS_VFS
  6. #undef getc
  7. static luat_vfs_t vfs= {0};
  8. int luat_vfs_init(void* params) {
  9. memset(&vfs, 0, sizeof(vfs));
  10. }
  11. int luat_vfs_reg(const struct luat_vfs_filesystem* fs) {
  12. for (size_t i = 0; i < LUAT_VFS_FILESYSTEM_MAX; i++)
  13. {
  14. if (vfs.fsList[i] == NULL) {
  15. vfs.fsList[i] = (struct luat_vfs_filesystem*)fs;
  16. LLOGD("register fs %s", fs->name);
  17. return 0;
  18. }
  19. }
  20. LLOGE("too many filesystem !!!");
  21. return -1;
  22. }
  23. FILE* luat_vfs_add_fd(FILE* fd, luat_vfs_mount_t * mount) {
  24. for (size_t i = 1; i <= LUAT_VFS_FILESYSTEM_FD_MAX; i++)
  25. {
  26. if (vfs.fds[i].fsMount == NULL) {
  27. vfs.fds[i].fsMount = mount == NULL ? &vfs.mounted[0] : mount;
  28. vfs.fds[i].fd = fd;
  29. //LLOGD("luat_vfs_add_fd %p => %d", fd, i+1);
  30. return (FILE*)i;
  31. }
  32. }
  33. return NULL;
  34. }
  35. int luat_vfs_rm_fd(FILE* fd) {
  36. int _fd = (int)fd;
  37. if (_fd <= 0 || _fd > LUAT_VFS_FILESYSTEM_FD_MAX)
  38. return -1;
  39. //LLOGD("luat_vfs_rm_fd %d => %d", (int)fd, _fd);
  40. vfs.fds[_fd].fd = NULL;
  41. vfs.fds[_fd].fsMount = NULL;
  42. return -1;
  43. }
  44. luat_vfs_mount_t * getmount(const char* filename) {
  45. for (size_t j = LUAT_VFS_FILESYSTEM_MOUNT_MAX - 1; j >= 0; j--) {
  46. if (vfs.mounted[j].ok == 0)
  47. continue;
  48. if (strncmp(vfs.mounted[j].prefix, filename, strlen(vfs.mounted[j].prefix)) == 0) {
  49. return &vfs.mounted[j];
  50. }
  51. }
  52. LLOGW("not mount point match %s", filename);
  53. return NULL;
  54. }
  55. int luat_fs_mkfs(luat_fs_conf_t *conf) {
  56. return 0;
  57. }
  58. int luat_fs_mount(luat_fs_conf_t *conf) {
  59. LLOGD("mount %s %s", conf->filesystem, conf->mount_point);
  60. for (int i = 0; i < LUAT_VFS_FILESYSTEM_MAX; i++) {
  61. if (vfs.fsList[i] != NULL && strcmp(vfs.fsList[i]->name, conf->filesystem) == 0) {
  62. for (size_t j = 0; j < LUAT_VFS_FILESYSTEM_MOUNT_MAX; j++)
  63. {
  64. if (vfs.mounted[j].fs == NULL) {
  65. int ret = vfs.fsList[i]->opts.mount(&vfs.mounted[j].userdata, conf);
  66. if (ret == 0) {
  67. vfs.mounted[j].fs = vfs.fsList[i];
  68. vfs.mounted[j].ok = 1;
  69. memcpy(vfs.mounted[j].prefix, conf->mount_point, strlen(conf->mount_point) + 1);
  70. }
  71. //LLOGD("mount ret %d", ret);
  72. return ret;
  73. }
  74. }
  75. LLOGE("too many filesystem mounted!!");
  76. return -2;
  77. }
  78. }
  79. LLOGE("no such filesystem %s", conf->filesystem);
  80. return -1;
  81. }
  82. int luat_fs_umount(luat_fs_conf_t *conf) {
  83. for (size_t j = 0; j < LUAT_VFS_FILESYSTEM_MOUNT_MAX; j++) {
  84. if (vfs.mounted[j].ok == 0)
  85. continue;
  86. if (strcmp(vfs.mounted[j].prefix, conf->mount_point) == 0) {
  87. // TODO 关闭对应的FD
  88. return vfs.mounted[j].fs->opts.umount(vfs.mounted[j].userdata, conf);
  89. }
  90. }
  91. LLOGE("no such mount point %s", conf->mount_point);
  92. return -1;
  93. }
  94. int luat_fs_info(const char* path, luat_fs_info_t *conf) {
  95. for (size_t j = 0; j < LUAT_VFS_FILESYSTEM_MOUNT_MAX; j++) {
  96. if (vfs.mounted[j].ok == 0)
  97. continue;
  98. if (strcmp(vfs.mounted[j].prefix, path) == 0) {
  99. return vfs.mounted[j].fs->opts.info(vfs.mounted[j].userdata, path, conf);
  100. }
  101. }
  102. LLOGE("no such mount point %s", path);
  103. return -1;
  104. }
  105. static luat_vfs_fd_t* getfd(FILE* fd) {
  106. int _fd = (int)fd;
  107. //LLOGD("search for vfs.fd = %d %p", _fd, fd);
  108. if (_fd <= 0 || _fd > LUAT_VFS_FILESYSTEM_FD_MAX) return NULL;
  109. if (vfs.fds[_fd].fsMount == NULL) {
  110. LLOGD("vfs.fds[%d] is nil", _fd);
  111. return NULL;
  112. }
  113. return &(vfs.fds[_fd]);
  114. }
  115. FILE* luat_fs_fopen(const char *filename, const char *mode) {
  116. LLOGD("fopen %s %s", filename, mode);
  117. luat_vfs_mount_t *mount = getmount(filename);
  118. if (mount == NULL || mount->fs->fopts.fopen == NULL) return NULL;
  119. FILE* fd = mount->fs->fopts.fopen(mount->userdata, filename + strlen(mount->prefix), mode);
  120. if (fd) {
  121. for (size_t i = 1; i <= LUAT_VFS_FILESYSTEM_FD_MAX; i++)
  122. {
  123. if (vfs.fds[i].fsMount == NULL) {
  124. vfs.fds[i].fsMount = mount;
  125. vfs.fds[i].fd = fd;
  126. return (FILE*)i;
  127. }
  128. }
  129. mount->fs->fopts.fclose(mount->userdata, fd);
  130. LLOGE("too many open file!!!");
  131. }
  132. return NULL;
  133. }
  134. // #define vfs_fopt(name, ...) luat_fs_##name(FILE* stream) {\
  135. // luat_vfs_fd_t* fd = getfd(stream);\
  136. // if (fd == NULL || fd->fsMount->fs->fopts.name == NULL) \
  137. // return 0;\
  138. // return fd->fsMount->fs->fopts.name(fd->fsMount->userdata, fd->fd);\
  139. // }
  140. // int vfs_fopt(getc)
  141. // int vfs_fopt(ftell)
  142. // int vfs_fopt(fclose)
  143. // int vfs_fopt(feof)
  144. // int vfs_fopt(ferror)
  145. int luat_fs_feof(FILE* stream) {
  146. //LLOGD("call %s %d","feof", ((int)stream) - 1);
  147. luat_vfs_fd_t* fd = getfd(stream);
  148. if (fd == NULL)
  149. return 1;
  150. return fd->fsMount->fs->fopts.feof(fd->fsMount->userdata, fd->fd);
  151. }
  152. int luat_fs_ferror(FILE* stream) {
  153. //LLOGD("call %s %d","ferror", ((int)stream) - 1);
  154. luat_vfs_fd_t* fd = getfd(stream);
  155. if (fd == NULL || fd->fsMount->fs->fopts.ferror == NULL)
  156. return 0;
  157. return fd->fsMount->fs->fopts.ferror(fd->fsMount->userdata, fd->fd);
  158. }
  159. int luat_fs_ftell(FILE* stream) {
  160. //LLOGD("call %s %d","ftell", ((int)stream) - 1);
  161. luat_vfs_fd_t* fd = getfd(stream);
  162. if (fd == NULL || fd->fsMount->fs->fopts.ftell == NULL)
  163. return 0;
  164. return fd->fsMount->fs->fopts.ftell(fd->fsMount->userdata, fd->fd);
  165. }
  166. int luat_fs_getc(FILE* stream) {
  167. //LLOGD("call %s %d","getc", ((int)stream) - 1);
  168. luat_vfs_fd_t* fd = getfd(stream);
  169. if (fd == NULL) {
  170. LLOGD("FILE* stream is invaild!!!");
  171. return -1;
  172. }
  173. if (fd->fsMount->fs->fopts.getc == NULL) {
  174. LLOGD("miss getc");
  175. return -1;
  176. }
  177. return fd->fsMount->fs->fopts.getc(fd->fsMount->userdata, fd->fd);
  178. }
  179. // char luat_fs_getc(FILE* stream);
  180. // int luat_fs_ftell(FILE* stream);
  181. // int luat_fs_feof(FILE* stream);
  182. // int luat_fs_ferror(FILE *stream);
  183. int luat_fs_fclose(FILE* stream) {
  184. LLOGD("fclose %d", (int)stream);
  185. luat_vfs_fd_t* fd = getfd(stream);
  186. if (fd == NULL) {
  187. return 0;
  188. }
  189. int ret = fd->fsMount->fs->fopts.fclose(fd->fsMount->userdata, fd->fd);
  190. int _fd = (int)stream;
  191. vfs.fds[_fd].fsMount = NULL;
  192. vfs.fds[_fd].fd = NULL;
  193. return ret;
  194. }
  195. int luat_fs_fseek(FILE* stream, long int offset, int origin) {
  196. //LLOGD("call %s %d","fseek", ((int)stream) - 1);
  197. luat_vfs_fd_t* fd = getfd(stream);
  198. if (fd == NULL || fd->fsMount->fs->fopts.fseek == NULL)
  199. return 0;
  200. return fd->fsMount->fs->fopts.fseek(fd->fsMount->userdata, fd->fd, offset, origin);
  201. }
  202. size_t luat_fs_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
  203. //LLOGD("call %s %d","vfs_fread", ((int)stream) - 1);
  204. luat_vfs_fd_t* fd = getfd(stream);
  205. if (fd == NULL || fd->fsMount->fs->fopts.fread == NULL)
  206. return 0;
  207. return fd->fsMount->fs->fopts.fread(fd->fsMount->userdata, ptr, size, nmemb, fd->fd);
  208. }
  209. size_t luat_fs_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
  210. luat_vfs_fd_t* fd = getfd(stream);
  211. if (fd == NULL || fd->fsMount->fs->fopts.fwrite == NULL)
  212. return 0;
  213. return fd->fsMount->fs->fopts.fwrite(fd->fsMount->userdata, ptr, size, nmemb, fd->fd);
  214. }
  215. int luat_fs_remove(const char *filename) {
  216. luat_vfs_mount_t *mount = getmount(filename);
  217. if (mount == NULL || mount->fs->opts.remove == NULL) return -1;
  218. return mount->fs->opts.remove(mount->userdata, filename + strlen(mount->prefix));
  219. }
  220. int luat_fs_rename(const char *old_filename, const char *new_filename) {
  221. luat_vfs_mount_t *old_mount = getmount(old_filename);
  222. luat_vfs_mount_t *new_mount = getmount(new_filename);\
  223. if (old_filename == NULL || new_mount != old_mount) {
  224. return -1;
  225. }
  226. return old_mount->fs->opts.rename(old_mount->userdata, old_filename + strlen(old_mount->prefix),
  227. new_filename + strlen(old_mount->prefix));
  228. }
  229. size_t luat_fs_fsize(const char *filename) {
  230. luat_vfs_mount_t *mount = getmount(filename);
  231. if (mount == NULL || mount->fs->opts.fsize == NULL) return -1;
  232. return mount->fs->opts.fsize(mount->userdata, filename + strlen(mount->prefix));
  233. }
  234. int luat_fs_fexist(const char *filename) {
  235. //LLOGD("exist? %s", filename);
  236. luat_vfs_mount_t *mount = getmount(filename);
  237. if (mount == NULL || mount->fs->opts.fexist == NULL) return 0;
  238. return mount->fs->opts.fexist(mount->userdata, filename + strlen(mount->prefix));
  239. }
  240. // TODO 文件夹相关的API
  241. //int luat_fs_diropen(char const* _FileName);
  242. int luat_fs_mkdir(char const* _DirName) {
  243. luat_vfs_mount_t *mount = getmount(_DirName);
  244. if (mount == NULL || mount->fs->opts.mkdir == NULL) return 0;
  245. return mount->fs->opts.mkdir(mount->userdata, _DirName + strlen(mount->prefix));
  246. }
  247. int luat_fs_rmdir(char const* _DirName) {
  248. luat_vfs_mount_t *mount = getmount(_DirName);
  249. if (mount == NULL) return 0;
  250. return mount->fs->opts.rmdir(mount->userdata, _DirName + strlen(mount->prefix));
  251. }
  252. #endif