luat_vfs.c 12 KB

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