luat_vfs.c 11 KB

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