luat_vfs.c 9.3 KB

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