luat_fs_mem.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. #include "luat_base.h"
  2. #include "luat_fs.h"
  3. #include "luat_mem.h"
  4. #define LUAT_LOG_TAG "fs"
  5. #include "luat_log.h"
  6. typedef struct ram_file
  7. {
  8. size_t size; // 当前文件大小, 也是指针对应内存块的大小
  9. // size_t ptr_size; // 数值指针的大小
  10. char name[32]; // 文件名称
  11. char ptr[4];
  12. }ram_file_t;
  13. typedef struct luat_ram_fd
  14. {
  15. int fid;
  16. uint32_t offset;
  17. uint8_t readonly;
  18. }luat_raw_fd_t;
  19. #define RAM_FILE_MAX (64)
  20. static ram_file_t* files[RAM_FILE_MAX];
  21. FILE* luat_vfs_ram_fopen(void* userdata, const char *filename, const char *mode) {
  22. (void)userdata;
  23. // LLOGD("ram fs open %s %s", filename, mode);
  24. if (filename == NULL || mode == NULL || strlen(filename) > 31)
  25. return NULL;
  26. // 读文件
  27. if (!strcmp("r", mode) || !strcmp("rb", mode)) {
  28. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  29. {
  30. if (files[i]== NULL)
  31. continue;
  32. if (!strcmp(files[i]->name, filename)) {
  33. luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
  34. if (fd == NULL) {
  35. LLOGE("out of memory when malloc luat_raw_fd_t");
  36. return NULL;
  37. }
  38. fd->fid = i;
  39. fd->offset = 0;
  40. fd->readonly = 1;
  41. return (FILE*)fd;
  42. }
  43. }
  44. return NULL;
  45. }
  46. // 写文件
  47. if (!strcmp("w", mode) || !strcmp("wb", mode) || !strcmp("w+", mode) || !strcmp("wb+", mode) || !strcmp("w+b", mode) || !strcmp("r+", mode) || !strcmp("rb+", mode) || !strcmp("r+b", mode)) {
  48. // 先看看是否存在, 如果存在就重用老的
  49. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  50. {
  51. if (files[i]== NULL)
  52. continue;
  53. if (!strcmp(files[i]->name, filename)) {
  54. luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
  55. if (fd == NULL) {
  56. LLOGE("out of memory when malloc luat_raw_fd_t");
  57. return NULL;
  58. }
  59. fd->fid = i;
  60. fd->readonly = 0;
  61. fd->offset = 0;
  62. if (!strcmp("w+", mode) || !strcmp("wb+", mode) || !strcmp("w+b", mode)) {
  63. // 截断模式
  64. char* tmp = luat_heap_realloc(files[i], sizeof(ram_file_t));
  65. if (tmp) {
  66. files[i] = (ram_file_t*)tmp;
  67. }
  68. else {
  69. LLOGE("realloc ram_file_t failed");
  70. luat_heap_free(fd);
  71. return NULL;
  72. }
  73. files[i]->size = 0;
  74. }
  75. return (FILE*)fd;
  76. }
  77. }
  78. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  79. {
  80. if (files[i] != NULL)
  81. continue;
  82. ram_file_t *file = luat_heap_malloc(sizeof(ram_file_t));
  83. if (file == NULL) {
  84. LLOGE("out of memory when malloc ram_file_t");
  85. return NULL;
  86. }
  87. memset(file, 0, sizeof(ram_file_t));
  88. strcpy(file->name, filename);
  89. files[i] = file;
  90. luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
  91. if (fd == NULL) {
  92. LLOGE("out of memory when malloc luat_raw_fd_t");
  93. return NULL;
  94. }
  95. fd->fid = i;
  96. fd->offset = 0;
  97. fd->readonly = 0;
  98. return (FILE*)fd;
  99. }
  100. }
  101. // 追加模式
  102. if (!strcmp("a", mode) || !strcmp("ab", mode) || !strcmp("a+", mode) || !strcmp("ab+", mode) ) {
  103. // 先看看是否存在, 如果存在就重用老的
  104. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  105. {
  106. if (files[i] == NULL)
  107. continue;
  108. if (!strcmp(files[i]->name, filename)) {
  109. luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
  110. if (fd == NULL) {
  111. LLOGE("out of memory when malloc luat_raw_fd_t");
  112. return NULL;
  113. }
  114. fd->fid = i;
  115. fd->offset = files[i]->size;
  116. fd->readonly = 0;
  117. return (FILE*)fd;
  118. }
  119. }
  120. }
  121. LLOGE("too many ram files >= %d", RAM_FILE_MAX);
  122. return NULL;
  123. }
  124. int luat_vfs_ram_getc(void* userdata, FILE* stream) {
  125. (void)userdata;
  126. //LLOGD("getc %p %p", userdata, stream);
  127. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  128. //LLOGD("getc %p %p %d %d", userdata, stream, fd->offset, fd->size);
  129. if (fd->fid < 0 || fd->fid >= RAM_FILE_MAX) {
  130. return -1;
  131. }
  132. if (files[fd->fid] == NULL) {
  133. return -1;
  134. }
  135. if (fd->offset < files[fd->fid]->size) {
  136. uint8_t c = (uint8_t)files[fd->fid]->ptr[fd->offset];
  137. fd->offset ++;
  138. //LLOGD("getc %02X", c);
  139. return c;
  140. }
  141. return -1;
  142. }
  143. int luat_vfs_ram_fseek(void* userdata, FILE* stream, long int offset, int origin) {
  144. (void)userdata;
  145. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  146. if (origin == SEEK_CUR) {
  147. fd->offset += offset;
  148. return 0;
  149. }
  150. else if (origin == SEEK_SET) {
  151. fd->offset = offset;
  152. return 0;
  153. }
  154. else {
  155. fd->offset = files[fd->fid]->size - offset;
  156. return 0;
  157. }
  158. }
  159. int luat_vfs_ram_ftell(void* userdata, FILE* stream) {
  160. (void)userdata;
  161. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  162. // LLOGD("tell %p %p offset %d", userdata, stream, fd->offset);
  163. return fd->offset;
  164. }
  165. int luat_vfs_ram_fclose(void* userdata, FILE* stream) {
  166. (void)userdata;
  167. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  168. //LLOGD("fclose %p %p %d %d", userdata, stream, fd->size, fd->offset);
  169. luat_heap_free(fd);
  170. return 0;
  171. }
  172. int luat_vfs_ram_feof(void* userdata, FILE* stream) {
  173. (void)userdata;
  174. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  175. //LLOGD("feof %p %p %d %d", userdata, stream, fd->size, fd->offset);
  176. return fd->offset >= files[fd->fid]->size ? 1 : 0;
  177. }
  178. int luat_vfs_ram_ferror(void* userdata, FILE *stream) {
  179. (void)userdata;
  180. (void)stream;
  181. return 0;
  182. }
  183. size_t luat_vfs_ram_fread(void* userdata, void *ptr, size_t size, size_t nmemb, FILE *stream) {
  184. (void)userdata;
  185. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  186. //LLOGD("fread %p %p %d %d", userdata, stream, fd->size, fd->offset);
  187. //LLOGD("fread2 %p %p %d %d", userdata, stream, size * nmemb, fd->offset);
  188. size_t read_size = size*nmemb;
  189. if (fd->offset >= files[fd->fid]->size) {
  190. return 0;
  191. }
  192. if (fd->offset + read_size >= files[fd->fid]->size) {
  193. read_size = files[fd->fid]->size - fd->offset;
  194. }
  195. memcpy(ptr, files[fd->fid]->ptr + fd->offset, read_size);
  196. fd->offset += read_size;
  197. return read_size;
  198. }
  199. size_t luat_vfs_ram_fwrite(void* userdata, const void *ptr, size_t size, size_t nmemb, FILE *stream) {
  200. (void)userdata;
  201. luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
  202. size_t write_size = size*nmemb;
  203. if (write_size > 128 * 1024) {
  204. LLOGW("ramfs large write !! %ld", write_size);
  205. }
  206. if (fd->readonly) {
  207. LLOGW("readonly fd %d!! path %s", fd->fid, files[fd->fid]->name);
  208. return 0;
  209. }
  210. if (fd->offset + write_size > files[fd->fid]->size) {
  211. char* ptr = luat_heap_realloc(files[fd->fid], fd->offset + write_size + sizeof(ram_file_t));
  212. if (ptr == NULL) {
  213. LLOGW("/ram out of sys memory!! %ld", write_size);
  214. return 0;
  215. }
  216. files[fd->fid] = (ram_file_t*)ptr;
  217. files[fd->fid]->size = fd->offset + write_size;
  218. }
  219. memcpy(files[fd->fid]->ptr + fd->offset, ptr, write_size);
  220. fd->offset += write_size;
  221. return write_size;
  222. }
  223. int luat_vfs_ram_remove(void* userdata, const char *filename) {
  224. (void)userdata;
  225. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  226. {
  227. if (files[i] == NULL)
  228. continue;
  229. if (!strcmp(filename, files[i]->name)) {
  230. luat_heap_free(files[i]);
  231. files[i] = NULL;
  232. }
  233. }
  234. return 0;
  235. }
  236. int luat_vfs_ram_rename(void* userdata, const char *old_filename, const char *new_filename) {
  237. (void)userdata;
  238. if (old_filename == NULL || new_filename == NULL)
  239. return -1;
  240. if (strlen(old_filename) > 31 || strlen(new_filename) > 31)
  241. return -2;
  242. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  243. {
  244. if (files[i] == NULL)
  245. continue;
  246. if (!strcmp(old_filename, files[i]->name)) {
  247. strcpy(files[i]->name, new_filename);
  248. return 0;
  249. }
  250. }
  251. return 0;
  252. }
  253. int luat_vfs_ram_fexist(void* userdata, const char *filename) {
  254. (void)userdata;
  255. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  256. {
  257. if (files[i] == NULL)
  258. continue;
  259. if (!strcmp(filename, files[i]->name)) {
  260. return 1;
  261. }
  262. }
  263. return 0;
  264. }
  265. size_t luat_vfs_ram_fsize(void* userdata, const char *filename) {
  266. (void)userdata;
  267. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  268. {
  269. if (files[i] == NULL)
  270. continue;
  271. if (!strcmp(filename, files[i]->name)) {
  272. return files[i]->size;
  273. }
  274. }
  275. return 0;
  276. }
  277. void* luat_vfs_ram_mmap(void* userdata, FILE *stream) {
  278. (void)userdata;
  279. luat_raw_fd_t *fd = (luat_raw_fd_t*)(stream);
  280. //LLOGD("fsize %p %p %d %d", userdata, fd);
  281. return files[fd->fid]->ptr;
  282. }
  283. int luat_vfs_ram_mkfs(void* userdata, luat_fs_conf_t *conf) {
  284. (void)userdata;
  285. (void)conf;
  286. return -1;
  287. }
  288. int luat_vfs_ram_mount(void** userdata, luat_fs_conf_t *conf) {
  289. (void)userdata;
  290. (void)conf;
  291. return 0;
  292. }
  293. int luat_vfs_ram_umount(void* userdata, luat_fs_conf_t *conf) {
  294. (void)userdata;
  295. (void)conf;
  296. return 0;
  297. }
  298. int luat_vfs_ram_mkdir(void* userdata, char const* _DirName) {
  299. (void)userdata;
  300. (void)_DirName;
  301. return -1;
  302. }
  303. int luat_vfs_ram_rmdir(void* userdata, char const* _DirName) {
  304. (void)userdata;
  305. (void)_DirName;
  306. return -1;
  307. }
  308. int luat_vfs_ram_lsdir(void* userdata, char const* _DirName, luat_fs_dirent_t* ents, size_t offset, size_t len) {
  309. (void)userdata;
  310. (void)_DirName;
  311. size_t count = 0;
  312. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  313. {
  314. if (count >= len)
  315. break;
  316. if (files[i] == NULL)
  317. continue;
  318. if (offset > 0) {
  319. offset --;
  320. continue;
  321. }
  322. ents[count].d_type = 0;
  323. strcpy(ents[count].d_name, files[i]->name);
  324. count ++;
  325. }
  326. return count;
  327. }
  328. int luat_vfs_ram_info(void* userdata, const char* path, luat_fs_info_t *conf) {
  329. (void)userdata;
  330. (void)path;
  331. memcpy(conf->filesystem, "ram", strlen("ram")+1);
  332. size_t ftotal = 0;
  333. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  334. {
  335. if (files[i] == NULL)
  336. continue;
  337. ftotal += files[i]->size;
  338. }
  339. size_t total; size_t used; size_t max_used;
  340. luat_meminfo_sys(&total, &used, &max_used);
  341. conf->type = 0;
  342. conf->total_block = 64;
  343. conf->block_used = (ftotal + 1023) / 1024;
  344. conf->block_size = 1024;
  345. return 0;
  346. }
  347. int luat_vfs_ram_truncate(void* fsdata, char const* path, size_t nsize) {
  348. for (size_t i = 0; i < RAM_FILE_MAX; i++)
  349. {
  350. if (files[i] == NULL)
  351. continue;
  352. if (!strcmp(files[i]->name, path)) {
  353. if (files[i]->size > nsize) {
  354. files[i]->size = nsize;
  355. char* ptr = luat_heap_realloc(files[i], nsize + sizeof(ram_file_t));
  356. if (ptr) {
  357. files[i] = (ram_file_t*)ptr;
  358. }
  359. }
  360. return 0;
  361. }
  362. }
  363. return 0;
  364. }
  365. #define T(name) .name = luat_vfs_ram_##name
  366. const struct luat_vfs_filesystem vfs_fs_ram = {
  367. .name = "ram",
  368. .opts = {
  369. .mkfs = NULL,
  370. T(mount),
  371. T(umount),
  372. .mkdir = NULL,
  373. .rmdir = NULL,
  374. T(lsdir),
  375. T(remove),
  376. T(rename),
  377. T(fsize),
  378. T(fexist),
  379. T(info),
  380. T(truncate)
  381. },
  382. .fopts = {
  383. T(fopen),
  384. T(getc),
  385. T(fseek),
  386. T(ftell),
  387. T(fclose),
  388. T(feof),
  389. T(ferror),
  390. T(fread),
  391. T(fwrite)
  392. }
  393. };