luat_vfs.c 9.6 KB

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