luat_vfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. int ret = vfs.mounted[j].fs->opts.umount(vfs.mounted[j].userdata, conf);
  125. vfs.mounted[j].fs = NULL;
  126. vfs.mounted[j].ok = 0;
  127. return ret;
  128. }
  129. }
  130. LLOGE("no such mount point %s", conf->mount_point);
  131. return -1;
  132. }
  133. int luat_fs_info(const char* path, luat_fs_info_t *conf) {
  134. luat_vfs_mount_t * mf = getmount(path);
  135. if (mf != NULL && mf->fs->opts.info != NULL) {
  136. return mf->fs->opts.info(mf->userdata, ((char*)path) + strlen(mf->prefix), conf);
  137. }
  138. LLOGE("no such mount point %s", path);
  139. return -1;
  140. }
  141. static luat_vfs_fd_t* getfd(FILE* fd) {
  142. int _fd = (int)fd;
  143. //LLOGD("search for vfs.fd = %d %p", _fd, fd);
  144. if (_fd <= 0 || _fd > LUAT_VFS_FILESYSTEM_FD_MAX) return NULL;
  145. if (vfs.fds[_fd].fsMount == NULL) {
  146. LLOGD("vfs.fds[%d] is nil", _fd);
  147. return NULL;
  148. }
  149. return &(vfs.fds[_fd]);
  150. }
  151. FILE* luat_fs_fopen(const char *filename, const char *mode) {
  152. luat_vfs_mount_t *mount = getmount(filename);
  153. if (mount == NULL || mount->fs->fopts.fopen == NULL) {
  154. LLOGD("fopen %s %s NOT matched mount", filename, mode);
  155. return NULL;
  156. }
  157. FILE* fd = mount->fs->fopts.fopen(mount->userdata, filename + strlen(mount->prefix), mode);
  158. if (fd) {
  159. for (size_t i = 1; i <= LUAT_VFS_FILESYSTEM_FD_MAX; i++)
  160. {
  161. if (vfs.fds[i].fsMount == NULL) {
  162. vfs.fds[i].fsMount = mount;
  163. vfs.fds[i].fd = fd;
  164. //LLOGD("fopen %s %s vfd=%ld fd=%ld", filename, mode, i, fd);
  165. return (FILE*)i;
  166. }
  167. }
  168. mount->fs->fopts.fclose(mount->userdata, fd);
  169. LLOGE("fopen %s %s too many open file!!!", filename, mode);
  170. }
  171. LLOGD("fopen %s %s not found", filename, mode);
  172. return NULL;
  173. }
  174. int luat_fs_feof(FILE* stream) {
  175. //LLOGD("call %s %d","feof", ((int)stream) - 1);
  176. luat_vfs_fd_t* fd = getfd(stream);
  177. if (fd == NULL)
  178. return 1;
  179. return fd->fsMount->fs->fopts.feof(fd->fsMount->userdata, fd->fd);
  180. }
  181. int luat_fs_ferror(FILE* stream) {
  182. //LLOGD("call %s %d","ferror", ((int)stream) - 1);
  183. luat_vfs_fd_t* fd = getfd(stream);
  184. if (fd == NULL || fd->fsMount->fs->fopts.ferror == NULL)
  185. return 0;
  186. return fd->fsMount->fs->fopts.ferror(fd->fsMount->userdata, fd->fd);
  187. }
  188. int luat_fs_ftell(FILE* stream) {
  189. //LLOGD("call %s %d","ftell", ((int)stream) - 1);
  190. luat_vfs_fd_t* fd = getfd(stream);
  191. if (fd == NULL || fd->fsMount->fs->fopts.ftell == NULL)
  192. return 0;
  193. return fd->fsMount->fs->fopts.ftell(fd->fsMount->userdata, fd->fd);
  194. }
  195. int luat_fs_getc(FILE* stream) {
  196. //LLOGD("call %s %d","getc", ((int)stream) - 1);
  197. luat_vfs_fd_t* fd = getfd(stream);
  198. if (fd == NULL) {
  199. LLOGD("FILE* stream is invaild!!!");
  200. return -1;
  201. }
  202. if (fd->fsMount->fs->fopts.getc == NULL) {
  203. LLOGD("miss getc");
  204. return -1;
  205. }
  206. return fd->fsMount->fs->fopts.getc(fd->fsMount->userdata, fd->fd);
  207. }
  208. // char luat_fs_getc(FILE* stream);
  209. // int luat_fs_ftell(FILE* stream);
  210. // int luat_fs_feof(FILE* stream);
  211. // int luat_fs_ferror(FILE *stream);
  212. int luat_fs_fclose(FILE* stream) {
  213. //LLOGD("fclose %d", (int)stream);
  214. luat_vfs_fd_t* fd = getfd(stream);
  215. if (fd == NULL) {
  216. return 0;
  217. }
  218. int ret = fd->fsMount->fs->fopts.fclose(fd->fsMount->userdata, fd->fd);
  219. int _fd = (int)stream;
  220. vfs.fds[_fd].fsMount = NULL;
  221. vfs.fds[_fd].fd = NULL;
  222. return ret;
  223. }
  224. int luat_fs_fseek(FILE* stream, long int offset, int origin) {
  225. //LLOGD("call %s %d","fseek", ((int)stream) - 1);
  226. luat_vfs_fd_t* fd = getfd(stream);
  227. if (fd == NULL || fd->fsMount->fs->fopts.fseek == NULL)
  228. return -1;
  229. return fd->fsMount->fs->fopts.fseek(fd->fsMount->userdata, fd->fd, offset, origin);
  230. }
  231. size_t luat_fs_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
  232. //LLOGD("call %s %d","vfs_fread", ((int)stream) - 1);
  233. luat_vfs_fd_t* fd = getfd(stream);
  234. if (fd == NULL || fd->fsMount->fs->fopts.fread == NULL)
  235. return 0;
  236. return fd->fsMount->fs->fopts.fread(fd->fsMount->userdata, ptr, size, nmemb, fd->fd);
  237. }
  238. size_t luat_fs_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
  239. luat_vfs_fd_t* fd = getfd(stream);
  240. if (fd == NULL || fd->fsMount->fs->fopts.fwrite == NULL)
  241. return 0;
  242. return fd->fsMount->fs->fopts.fwrite(fd->fsMount->userdata, ptr, size, nmemb, fd->fd);
  243. }
  244. int luat_fs_remove(const char *filename) {
  245. luat_vfs_mount_t *mount = getmount(filename);
  246. if (mount == NULL || mount->fs->opts.remove == NULL) return -1;
  247. return mount->fs->opts.remove(mount->userdata, filename + strlen(mount->prefix));
  248. }
  249. int luat_fs_rename(const char *old_filename, const char *new_filename) {
  250. luat_vfs_mount_t *old_mount = getmount(old_filename);
  251. luat_vfs_mount_t *new_mount = getmount(new_filename);\
  252. if (old_filename == NULL || new_mount != old_mount || old_mount->fs->opts.rename == NULL) {
  253. return -1;
  254. }
  255. return old_mount->fs->opts.rename(old_mount->userdata, old_filename + strlen(old_mount->prefix),
  256. new_filename + strlen(old_mount->prefix));
  257. }
  258. size_t luat_fs_fsize(const char *filename) {
  259. luat_vfs_mount_t *mount = getmount(filename);
  260. if (mount == NULL || mount->fs->opts.fsize == NULL) return 0;
  261. return mount->fs->opts.fsize(mount->userdata, filename + strlen(mount->prefix));
  262. }
  263. int luat_fs_fexist(const char *filename) {
  264. //LLOGD("exist? %s", filename);
  265. luat_vfs_mount_t *mount = getmount(filename);
  266. if (mount == NULL || mount->fs->opts.fexist == NULL) return 0;
  267. return mount->fs->opts.fexist(mount->userdata, filename + strlen(mount->prefix));
  268. }
  269. int luat_fs_readline(char * buf, int bufsize, FILE * stream){
  270. int get_len = 0;
  271. char buff[2];
  272. if (buf == NULL || bufsize < 1) {
  273. return 0;
  274. }
  275. size_t tmp = (size_t)bufsize;
  276. for (size_t i = 0; i <= tmp; i++){
  277. memset(buff, 0, 2);
  278. int len = luat_fs_fread(buff, sizeof(char), 1, stream);
  279. if (len>0){
  280. get_len = get_len+len;
  281. memcpy(buf+i, buff, len);
  282. if (memcmp(buff, "\n", 1)==0){
  283. break;
  284. }
  285. }else{
  286. break;
  287. }
  288. }
  289. return get_len;
  290. }
  291. int luat_fs_mkdir(char const* _DirName) {
  292. luat_vfs_mount_t *mount = getmount(_DirName);
  293. if (mount == NULL || mount->fs->opts.mkdir == NULL) return 0;
  294. return mount->fs->opts.mkdir(mount->userdata, _DirName + strlen(mount->prefix));
  295. }
  296. int luat_fs_rmdir(char const* _DirName) {
  297. luat_vfs_mount_t *mount = getmount(_DirName);
  298. if (mount == NULL || mount->fs->opts.rmdir == NULL) return 0;
  299. return mount->fs->opts.rmdir(mount->userdata, _DirName + strlen(mount->prefix));
  300. }
  301. int luat_fs_lsdir(char const* _DirName, luat_fs_dirent_t* ents, size_t offset, size_t len) {
  302. if (len == 0)
  303. return 0;
  304. luat_vfs_mount_t *mount = getmount(_DirName);
  305. if (mount == NULL) {
  306. LLOGD("no such mount");
  307. return 0;
  308. }
  309. if (mount->fs->opts.lsdir == NULL) {
  310. LLOGD("such mount not support lsdir");
  311. return 0;
  312. }
  313. // LLOGD("luat_fs_lsdir _DirName:%s mount->prefix:%s dir:%s", _DirName,mount->prefix,_DirName + strlen(mount->prefix));
  314. int ret = mount->fs->opts.lsdir(mount->userdata, _DirName + strlen(mount->prefix), ents, offset, len);
  315. char file_path[256] = {0};
  316. size_t file_path_len = strlen(_DirName);
  317. memcpy(file_path, _DirName, file_path_len + 1);
  318. if (strlen(_DirName + strlen(mount->prefix))!=1){
  319. file_path[file_path_len] = '/';
  320. file_path[file_path_len + 1] = 0;
  321. file_path_len++;
  322. }
  323. for (size_t i = 0; i < ret; i++){
  324. if (ents[i].d_type==0){
  325. memcpy(file_path+file_path_len, ents[i].d_name, strlen(ents[i].d_name) + 1);
  326. ents[i].d_size = luat_fs_fsize(file_path);
  327. }
  328. }
  329. return ret;
  330. }
  331. int luat_fs_dexist(const char *_DirName){
  332. // LLOGD("dexist? %s", _DirName);
  333. luat_vfs_mount_t *mount = getmount(_DirName);
  334. if (mount == NULL) {
  335. LLOGD("no such mount");
  336. return 0;
  337. }
  338. if (strlen(mount->prefix) == strlen(_DirName)){
  339. return 1;
  340. }
  341. if (mount->fs->opts.opendir == NULL) {
  342. LLOGD("such mount not support opendir");
  343. return 0;
  344. }
  345. void* dir = mount->fs->opts.opendir(mount->userdata, _DirName + strlen(mount->prefix));
  346. // LLOGD("opendir dir:%p",dir);
  347. mount->fs->opts.closedir(mount->userdata,dir);
  348. return dir == NULL ? 0 : 1;
  349. }
  350. void* luat_fs_mmap(FILE* stream) {
  351. luat_vfs_fd_t* fd = getfd(stream);
  352. if (fd == NULL)
  353. return NULL;
  354. if (fd->fsMount->fs->fopts.mmap != NULL) {
  355. return fd->fsMount->fs->fopts.mmap(fd->fsMount->userdata, fd->fd);
  356. }
  357. return NULL;
  358. }
  359. luat_vfs_t* luat_vfs_self(void) {
  360. return &vfs;
  361. }
  362. int luat_fs_truncate(const char* filename, size_t len) {
  363. luat_vfs_mount_t *mount = getmount(filename);
  364. if (mount == NULL ) return -1;
  365. if (mount->fs->opts.truncate) {
  366. return mount->fs->opts.truncate(mount->userdata, filename, len);
  367. }
  368. return -1;
  369. }
  370. int luat_fs_fflush(FILE *stream) {
  371. luat_vfs_fd_t* fd = getfd(stream);
  372. if (fd == NULL)
  373. return -1;
  374. if (fd->fsMount->fs->fopts.fflush != NULL) {
  375. return fd->fsMount->fs->fopts.fflush(fd->fsMount->userdata, fd->fd);
  376. }
  377. return -1;
  378. }
  379. #endif