luat_fs_posix.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include "luat_base.h"
  2. #include "luat_fs.h"
  3. #define LUAT_LOG_TAG "fs"
  4. #include "luat_log.h"
  5. #define TAG "luat.fs"
  6. #ifdef LUA_USE_VFS_FILENAME_OFFSET
  7. #define FILENAME_OFFSET (filename[0] == '/' ? 1 : 0)
  8. #else
  9. #define FILENAME_OFFSET 0
  10. #endif
  11. // fs的默认实现, 指向poisx的stdio.h声明的方法
  12. #ifndef LUAT_USE_FS_VFS
  13. FILE* luat_fs_fopen(const char *filename, const char *mode) {
  14. //LLOGD("fopen %s %s", filename + FILENAME_OFFSET, mode);
  15. return fopen(filename + FILENAME_OFFSET, mode);
  16. }
  17. int luat_fs_getc(FILE* stream) {
  18. //LLOGD("posix_getc %p", stream);
  19. #ifdef LUAT_FS_NO_POSIX_GETC
  20. uint8_t buff = 0;
  21. int ret = luat_fs_fread(&buff, 1, 1, stream);
  22. if (ret == 1) {
  23. return buff;
  24. }
  25. return -1;
  26. #else
  27. return getc(stream);
  28. #endif
  29. }
  30. int luat_fs_fseek(FILE* stream, long int offset, int origin) {
  31. return fseek(stream, offset, origin);
  32. }
  33. int luat_fs_ftell(FILE* stream) {
  34. return ftell(stream);
  35. }
  36. int luat_fs_fclose(FILE* stream) {
  37. return fclose(stream);
  38. }
  39. int luat_fs_feof(FILE* stream) {
  40. return feof(stream);
  41. }
  42. int luat_fs_ferror(FILE *stream) {
  43. return ferror(stream);
  44. }
  45. size_t luat_fs_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
  46. //LLOGD("posix_fread %d %p", size*nmemb, stream);
  47. return fread(ptr, size, nmemb, stream);
  48. }
  49. size_t luat_fs_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
  50. return fwrite(ptr, size, nmemb, stream);
  51. }
  52. int luat_fs_remove(const char *filename) {
  53. return remove(filename + FILENAME_OFFSET);
  54. }
  55. int luat_fs_rename(const char *old_filename, const char *new_filename) {
  56. #if LUA_USE_VFS_FILENAME_OFFSET
  57. return rename(old_filename + (old_filename[0] == '/' ? 1 : 0), new_filename + (new_filename[0] == '/' ? 1 : 0));
  58. #else
  59. return rename(old_filename, new_filename);
  60. #endif
  61. }
  62. int luat_fs_fexist(const char *filename) {
  63. FILE* fd = luat_fs_fopen(filename, "rb");
  64. if (fd) {
  65. luat_fs_fclose(fd);
  66. return 1;
  67. }
  68. return 0;
  69. }
  70. size_t luat_fs_fsize(const char *filename) {
  71. FILE *fd;
  72. size_t size = 0;
  73. fd = luat_fs_fopen(filename, "rb");
  74. if (fd) {
  75. luat_fs_fseek(fd, 0, SEEK_END);
  76. size = luat_fs_ftell(fd);
  77. luat_fs_fclose(fd);
  78. }
  79. return size;
  80. }
  81. LUAT_WEAK int luat_fs_mkfs(luat_fs_conf_t *conf) {
  82. LLOGE("not support yet : mkfs");
  83. return -1;
  84. }
  85. LUAT_WEAK int luat_fs_mount(luat_fs_conf_t *conf) {
  86. LLOGE("not support yet : mount");
  87. return -1;
  88. }
  89. LUAT_WEAK int luat_fs_umount(luat_fs_conf_t *conf) {
  90. LLOGE("not support yet : umount");
  91. return -1;
  92. }
  93. int luat_fs_mkdir(char const* _DirName) {
  94. //return mkdir(_DirName);
  95. return -1;
  96. }
  97. int luat_fs_rmdir(char const* _DirName) {
  98. //return rmdir(_DirName);
  99. return -1;
  100. }
  101. #else
  102. FILE* luat_vfs_posix_fopen(void* userdata, const char *filename, const char *mode) {
  103. //LLOGD("fopen %s %s", filename + FILENAME_OFFSET, mode);
  104. return fopen(filename + FILENAME_OFFSET, mode);
  105. }
  106. int luat_vfs_posix_getc(void* userdata, FILE* stream) {
  107. //LLOGD("posix_getc %p", stream);
  108. #ifdef LUAT_FS_NO_POSIX_GETC
  109. uint8_t buff = 0;
  110. int ret = luat_fs_fread(&buff, 1, 1, stream);
  111. if (ret == 1) {
  112. return buff;
  113. }
  114. return -1;
  115. #else
  116. return getc(stream);
  117. #endif
  118. }
  119. int luat_vfs_posix_fseek(void* userdata, FILE* stream, long int offset, int origin) {
  120. return fseek(stream, offset, origin);
  121. }
  122. int luat_vfs_posix_ftell(void* userdata, FILE* stream) {
  123. return ftell(stream);
  124. }
  125. int luat_vfs_posix_fclose(void* userdata, FILE* stream) {
  126. return fclose(stream);
  127. }
  128. int luat_vfs_posix_feof(void* userdata, FILE* stream) {
  129. return feof(stream);
  130. }
  131. int luat_vfs_posix_ferror(void* userdata, FILE *stream) {
  132. return ferror(stream);
  133. }
  134. size_t luat_vfs_posix_fread(void* userdata, void *ptr, size_t size, size_t nmemb, FILE *stream) {
  135. return fread(ptr, size, nmemb, stream);
  136. }
  137. size_t luat_vfs_posix_fwrite(void* userdata, const void *ptr, size_t size, size_t nmemb, FILE *stream) {
  138. return fwrite(ptr, size, nmemb, stream);
  139. }
  140. int luat_vfs_posix_remove(void* userdata, const char *filename) {
  141. return remove(filename + FILENAME_OFFSET);
  142. }
  143. int luat_vfs_posix_rename(void* userdata, const char *old_filename, const char *new_filename) {
  144. #if LUA_USE_VFS_FILENAME_OFFSET
  145. return rename(old_filename + (old_filename[0] == '/' ? 1 : 0), new_filename + (new_filename[0] == '/' ? 1 : 0));
  146. #else
  147. return rename(old_filename, new_filename);
  148. #endif
  149. }
  150. int luat_vfs_posix_fexist(void* userdata, const char *filename) {
  151. FILE* fd = luat_vfs_posix_fopen(userdata, filename, "rb");
  152. if (fd) {
  153. luat_vfs_posix_fclose(userdata, fd);
  154. return 1;
  155. }
  156. return 0;
  157. }
  158. size_t luat_vfs_posix_fsize(void* userdata, const char *filename) {
  159. FILE *fd;
  160. size_t size = 0;
  161. fd = luat_vfs_posix_fopen(userdata, filename, "rb");
  162. if (fd) {
  163. luat_vfs_posix_fseek(userdata, fd, 0, SEEK_END);
  164. size = luat_vfs_posix_ftell(userdata, fd);
  165. luat_vfs_posix_fclose(userdata, fd);
  166. }
  167. return size;
  168. }
  169. int luat_vfs_posix_mkfs(void* userdata, luat_fs_conf_t *conf) {
  170. LLOGE("not support yet : mkfs");
  171. return -1;
  172. }
  173. int luat_vfs_posix_mount(void** userdata, luat_fs_conf_t *conf) {
  174. //LLOGE("not support yet : mount");
  175. return 0;
  176. }
  177. int luat_vfs_posix_umount(void* userdata, luat_fs_conf_t *conf) {
  178. //LLOGE("not support yet : umount");
  179. return 0;
  180. }
  181. #if defined(LUA_USE_LINUX) || defined(LUA_USE_WINDOWS) || defined(LUA_USE_MACOSX)
  182. #include <stdio.h>
  183. #include <sys/types.h>
  184. #include <dirent.h>
  185. #endif
  186. int luat_vfs_posix_mkdir(void* userdata, char const* _DirName) {
  187. #if defined(LUA_USE_LINUX) || defined(LUA_USE_WINDOWS) || defined(LUA_USE_MACOSX)
  188. return mkdir(_DirName);
  189. #else
  190. return -1;
  191. #endif
  192. }
  193. int luat_vfs_posix_rmdir(void* userdata, char const* _DirName) {
  194. #if defined(LUA_USE_LINUX) || defined(LUA_USE_WINDOWS) || defined(LUA_USE_MACOSX)
  195. return rmdir(_DirName);
  196. #else
  197. return -1;
  198. #endif
  199. }
  200. int luat_vfs_posix_info(void* userdata, const char* path, luat_fs_info_t *conf) {
  201. memcpy(conf->filesystem, "posix", strlen("posix")+1);
  202. conf->type = 0;
  203. conf->total_block = 0;
  204. conf->block_used = 0;
  205. conf->block_size = 512;
  206. return 0;
  207. }
  208. #if defined(LUA_USE_LINUX) || defined(LUA_USE_WINDOWS) || defined(LUA_USE_MACOSX)
  209. int luat_vfs_posix_lsdir(void* fsdata, char const* _DirName, luat_fs_dirent_t* ents, size_t offset, size_t len) {
  210. DIR *dp;
  211. struct dirent *ep;
  212. int index = 0;
  213. //LLOGW("opendir file %s %d %d", _DirName, offset, len);
  214. dp = opendir (_DirName);
  215. if (dp != NULL)
  216. {
  217. while ((ep = readdir (dp)) != NULL) {
  218. //LLOGW("offset %d len %d", offset, len);
  219. if (offset > 0) {
  220. offset --;
  221. continue;
  222. }
  223. if (len > 0) {
  224. memcpy(ents[index].d_name, ep->d_name, strlen(ep->d_name) + 1);
  225. if (ep->d_type == DT_REG) {
  226. ents[index].d_type = 0;
  227. }
  228. else {
  229. ents[index].d_type = 1;
  230. }
  231. index++;
  232. len --;
  233. }
  234. else {
  235. break;
  236. }
  237. }
  238. (void) closedir (dp);
  239. return index;
  240. }
  241. else {
  242. LLOGW("opendir file %s", _DirName);
  243. }
  244. return 0;
  245. }
  246. #else
  247. int luat_vfs_posix_lsdir(void* fsdata, char const* _DirName, luat_fs_dirent_t* ents, size_t offset, size_t len) {
  248. return 0;
  249. }
  250. #endif
  251. #define T(name) .name = luat_vfs_posix_##name
  252. const struct luat_vfs_filesystem vfs_fs_posix = {
  253. .name = "posix",
  254. .opts = {
  255. .mkfs = NULL,
  256. T(mount),
  257. T(umount),
  258. T(mkdir),
  259. T(rmdir),
  260. T(lsdir),
  261. T(remove),
  262. T(rename),
  263. T(fsize),
  264. T(fexist),
  265. T(info)
  266. },
  267. .fopts = {
  268. T(fopen),
  269. T(getc),
  270. T(fseek),
  271. T(ftell),
  272. T(fclose),
  273. T(feof),
  274. T(ferror),
  275. T(fread),
  276. T(fwrite)
  277. }
  278. };
  279. #endif