luat_fs_posix.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. int luat_vfs_posix_mkdir(void* userdata, char const* _DirName) {
  182. //return mkdir(_DirName);
  183. return -1;
  184. }
  185. int luat_vfs_posix_rmdir(void* userdata, char const* _DirName) {
  186. //return rmdir(_DirName);
  187. return -1;
  188. }
  189. int luat_vfs_posix_info(void* userdata, const char* path, luat_fs_info_t *conf) {
  190. memcpy(conf->filesystem, "posix", strlen("posix")+1);
  191. conf->type = 0;
  192. conf->total_block = 0;
  193. conf->block_used = 0;
  194. conf->block_size = 512;
  195. return 0;
  196. }
  197. #define T(name) .name = luat_vfs_posix_##name
  198. const struct luat_vfs_filesystem vfs_fs_posix = {
  199. .name = "posix",
  200. .opts = {
  201. .mkfs = NULL,
  202. T(mount),
  203. T(umount),
  204. .mkdir = NULL,
  205. .rmdir = NULL,
  206. .lsdir = NULL,
  207. T(remove),
  208. T(rename),
  209. T(fsize),
  210. T(fexist),
  211. T(info)
  212. },
  213. .fopts = {
  214. T(fopen),
  215. T(getc),
  216. T(fseek),
  217. T(ftell),
  218. T(fclose),
  219. T(feof),
  220. T(ferror),
  221. T(fread),
  222. T(fwrite)
  223. }
  224. };
  225. #endif