luat_fs_posix.c 9.0 KB

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