dfs_lfs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. #include <rtdevice.h>
  2. #include <rtthread.h>
  3. #include <dfs_file.h>
  4. #include <dfs_fs.h>
  5. #include "lfs.h"
  6. #include <stdio.h>
  7. #include <string.h>
  8. #define RT_DFS_LFS_DRIVES 1
  9. #ifndef LFS_READ_SIZE
  10. #define LFS_READ_SIZE 128
  11. #endif
  12. #ifndef LFS_PROG_SIZE
  13. #define LFS_PROG_SIZE 256
  14. #endif
  15. #ifndef LFS_BLOCK_SIZE
  16. #define LFS_BLOCK_SIZE 512
  17. #endif
  18. #ifndef LFS_LOOKAHEAD
  19. #define LFS_LOOKAHEAD 512
  20. #endif
  21. typedef struct _dfs_lfs_s
  22. {
  23. struct lfs lfs;
  24. struct lfs_config cfg;
  25. } dfs_lfs_t;
  26. typedef struct _dfs_lfs_fd_s
  27. {
  28. struct lfs* lfs;
  29. union
  30. {
  31. struct lfs_file file;
  32. struct lfs_dir dir;
  33. } u;
  34. } dfs_lfs_fd_t;
  35. static struct _dfs_lfs_s* _lfs_mount_tbl[RT_DFS_LFS_DRIVES] = { 0 };
  36. // Read a region in a block. Negative error codes are propogated
  37. // to the user.
  38. static int _lfs_flash_read(const struct lfs_config* c, lfs_block_t block, lfs_off_t off, void* buffer, lfs_size_t size)
  39. {
  40. struct rt_mtd_nor_device* mtd_nor;
  41. RT_ASSERT(c != RT_NULL);
  42. RT_ASSERT(c->context != RT_NULL);
  43. mtd_nor = (struct rt_mtd_nor_device*)c->context;
  44. rt_mtd_nor_read(mtd_nor, block * c->block_size + off, buffer, size);
  45. return LFS_ERR_OK;
  46. }
  47. // Program a region in a block. The block must have previously
  48. // been erased. Negative error codes are propogated to the user.
  49. // May return LFS_ERR_CORRUPT if the block should be considered bad.
  50. static int _lfs_flash_prog(const struct lfs_config* c, lfs_block_t block, lfs_off_t off, const void* buffer, lfs_size_t size)
  51. {
  52. struct rt_mtd_nor_device* mtd_nor;
  53. RT_ASSERT(c != RT_NULL);
  54. RT_ASSERT(c->context != RT_NULL);
  55. mtd_nor = (struct rt_mtd_nor_device*)c->context;
  56. rt_mtd_nor_write(mtd_nor, block * c->block_size + off, buffer, size);
  57. return LFS_ERR_OK;
  58. }
  59. // Erase a block. A block must be erased before being programmed.
  60. // The state of an erased block is undefined. Negative error codes
  61. // are propogated to the user.
  62. // May return LFS_ERR_CORRUPT if the block should be considered bad.
  63. static int _lfs_flash_erase(const struct lfs_config* c, lfs_block_t block)
  64. {
  65. struct rt_mtd_nor_device* mtd_nor;
  66. RT_ASSERT(c != RT_NULL);
  67. RT_ASSERT(c->context != RT_NULL);
  68. mtd_nor = (struct rt_mtd_nor_device*)c->context;
  69. rt_mtd_nor_erase_block(mtd_nor, block * c->block_size, c->block_size);
  70. return LFS_ERR_OK;
  71. }
  72. // Sync the state of the underlying block device. Negative error codes
  73. // are propogated to the user.
  74. static int _lfs_flash_sync(const struct lfs_config* c)
  75. {
  76. return LFS_ERR_OK;
  77. }
  78. /* results:
  79. * -1, no space to install fatfs driver
  80. * >= 0, there is an space to install fatfs driver
  81. */
  82. static int get_disk(rt_device_t dev_id)
  83. {
  84. int index;
  85. if (dev_id == RT_NULL)
  86. {
  87. for (index = 0; index < RT_DFS_LFS_DRIVES; index++)
  88. {
  89. if (_lfs_mount_tbl[index] == RT_NULL)
  90. {
  91. return index;
  92. }
  93. }
  94. }
  95. else
  96. {
  97. for (index = 0; index < RT_DFS_LFS_DRIVES; index++)
  98. {
  99. if ((_lfs_mount_tbl[index] != RT_NULL)
  100. && (_lfs_mount_tbl[index]->cfg.context == (void*)dev_id))
  101. {
  102. return index;
  103. }
  104. }
  105. }
  106. return -1;
  107. }
  108. static int lfs_result_to_dfs(int result)
  109. {
  110. int status = 0;
  111. switch (result)
  112. {
  113. case LFS_ERR_OK:
  114. break;
  115. case LFS_ERR_IO:
  116. status = -EIO;
  117. break; // Error during device operation
  118. case LFS_ERR_NOENT:
  119. status = -ENOENT;
  120. break; // No directory entry
  121. case LFS_ERR_EXIST:
  122. status = -EEXIST;
  123. break; // Entry already exists
  124. case LFS_ERR_NOTDIR:
  125. status = -ENOTDIR;
  126. break; // Entry is not a dir
  127. case LFS_ERR_ISDIR:
  128. status = -EISDIR;
  129. break; // Entry is a dir
  130. case LFS_ERR_NOTEMPTY:
  131. status = -ENOTEMPTY;
  132. break; // Dir is not empty
  133. case LFS_ERR_BADF:
  134. status = -EBADF;
  135. break; // Bad file number
  136. case LFS_ERR_INVAL:
  137. status = -EINVAL;
  138. break; // Invalid parameter
  139. case LFS_ERR_NOSPC:
  140. status = -ENOSPC;
  141. break; // No space left on device
  142. case LFS_ERR_NOMEM:
  143. status = -ENOMEM;
  144. break; // No more memory available
  145. case LFS_ERR_CORRUPT:
  146. status = -52;
  147. break; // Corrupted
  148. default:
  149. status = -EIO;
  150. break;
  151. }
  152. return status;
  153. }
  154. static void _dfs_lfs_load_config(dfs_lfs_t* dfs_lfs, struct rt_mtd_nor_device* mtd_nor)
  155. {
  156. dfs_lfs->cfg.context = (void*)mtd_nor;
  157. // MTD device can access 1 byte....
  158. dfs_lfs->cfg.read_size = 1;
  159. if (dfs_lfs->cfg.read_size < LFS_READ_SIZE)
  160. {
  161. dfs_lfs->cfg.read_size = LFS_READ_SIZE;
  162. }
  163. // MTD device can access 1 byte....
  164. dfs_lfs->cfg.prog_size = 1;
  165. if (dfs_lfs->cfg.prog_size < LFS_PROG_SIZE)
  166. {
  167. dfs_lfs->cfg.prog_size = LFS_PROG_SIZE;
  168. }
  169. dfs_lfs->cfg.block_size = mtd_nor->block_size;
  170. if (dfs_lfs->cfg.block_size < LFS_BLOCK_SIZE)
  171. {
  172. dfs_lfs->cfg.block_size = LFS_BLOCK_SIZE;
  173. }
  174. dfs_lfs->cfg.block_count = mtd_nor->block_end - mtd_nor->block_start;
  175. dfs_lfs->cfg.lookahead = 32 * ((dfs_lfs->cfg.block_count + 31) / 32);
  176. if (dfs_lfs->cfg.lookahead > LFS_LOOKAHEAD)
  177. {
  178. dfs_lfs->cfg.lookahead = LFS_LOOKAHEAD;
  179. }
  180. dfs_lfs->cfg.read = &_lfs_flash_read;
  181. dfs_lfs->cfg.prog = &_lfs_flash_prog;
  182. dfs_lfs->cfg.erase = &_lfs_flash_erase;
  183. dfs_lfs->cfg.sync = &_lfs_flash_sync;
  184. }
  185. static int dfs_lfs_mount(struct dfs_filesystem* dfs, unsigned long rwflag, const void* data)
  186. {
  187. int result;
  188. dfs_lfs_t* dfs_lfs;
  189. int index;
  190. /* Check Device Type */
  191. if (dfs->dev_id->type != RT_Device_Class_MTD)
  192. {
  193. rt_kprintf("The flash device type must be MTD!\n");
  194. return -EINVAL;
  195. }
  196. /* get an empty position */
  197. index = get_disk(RT_NULL);
  198. if (index == -1)
  199. {
  200. return -EIO;
  201. }
  202. /*create lfs handle */
  203. dfs_lfs = rt_malloc(sizeof(dfs_lfs_t));
  204. if (dfs_lfs == RT_NULL)
  205. {
  206. rt_kprintf("ERROR:no memory!\n");
  207. _lfs_mount_tbl[index] = RT_NULL;
  208. return -ENOMEM;
  209. }
  210. rt_memset(dfs_lfs, 0, sizeof(dfs_lfs_t));
  211. { /* init cfg data */
  212. struct rt_mtd_nor_device* mtd_nor = (struct rt_mtd_nor_device*)dfs->dev_id;
  213. _dfs_lfs_load_config(dfs_lfs, mtd_nor);
  214. }
  215. /* mount lfs*/
  216. result = lfs_mount(&dfs_lfs->lfs, &dfs_lfs->cfg);
  217. if (result == LFS_ERR_OK)
  218. {
  219. /* save device */
  220. _lfs_mount_tbl[index] = dfs_lfs;
  221. /* mount succeed! */
  222. dfs->data = (void*)dfs_lfs;
  223. return RT_EOK;
  224. }
  225. /* release memory */
  226. rt_free(dfs_lfs);
  227. _lfs_mount_tbl[index] = RT_NULL;
  228. return -EIO;
  229. }
  230. static int dfs_lfs_unmount(struct dfs_filesystem* dfs)
  231. {
  232. int index;
  233. dfs_lfs_t* dfs_lfs = RT_NULL;
  234. RT_ASSERT(dfs != RT_NULL);
  235. RT_ASSERT(dfs->data != RT_NULL);
  236. dfs_lfs = (dfs_lfs_t*)dfs->data;
  237. /* find the device index and then umount it */
  238. index = get_disk(dfs->dev_id);
  239. if (index == -1) /* not found */
  240. {
  241. return -ENOENT;
  242. }
  243. dfs_lfs = _lfs_mount_tbl[index];
  244. _lfs_mount_tbl[index] = RT_NULL;
  245. dfs->data = RT_NULL;
  246. lfs_unmount(&dfs_lfs->lfs);
  247. rt_free(dfs_lfs);
  248. return RT_EOK;
  249. }
  250. static int dfs_lfs_mkfs(rt_device_t dev_id)
  251. {
  252. dfs_lfs_t* dfs_lfs = RT_NULL;
  253. int result;
  254. int index;
  255. if (dev_id == RT_NULL)
  256. {
  257. return -EINVAL;
  258. }
  259. /* Check Device Type */
  260. if (dev_id->type != RT_Device_Class_MTD)
  261. {
  262. rt_kprintf("The flash device type must be MTD!\n");
  263. return -EINVAL;
  264. }
  265. index = get_disk(dev_id);
  266. if (index == -1)
  267. {
  268. /* not found the device id */
  269. index = get_disk(RT_NULL);
  270. if (index == -1)
  271. {
  272. /* no space to store an temp driver */
  273. rt_kprintf("sorry, there is no space to do mkfs! \n");
  274. return -ENOSPC;
  275. }
  276. else
  277. {
  278. /*create lfs handle */
  279. dfs_lfs = rt_malloc(sizeof(dfs_lfs_t));
  280. if (dfs_lfs == RT_NULL)
  281. {
  282. rt_kprintf("ERROR:no memory!\n");
  283. _lfs_mount_tbl[index] = RT_NULL;
  284. return -ENOMEM;
  285. }
  286. rt_memset(dfs_lfs, 0, sizeof(dfs_lfs_t));
  287. { /* init cfg data */
  288. struct rt_mtd_nor_device* mtd_nor =
  289. (struct rt_mtd_nor_device*)dev_id;
  290. _dfs_lfs_load_config(dfs_lfs, mtd_nor);
  291. }
  292. }
  293. }
  294. else
  295. {
  296. dfs_lfs = _lfs_mount_tbl[index];
  297. _lfs_mount_tbl[index] = RT_NULL;
  298. /* unmount it */
  299. lfs_unmount(&dfs_lfs->lfs);
  300. }
  301. /* format flash device */
  302. result = lfs_format(&dfs_lfs->lfs, &dfs_lfs->cfg);
  303. if (result != LFS_ERR_OK)
  304. {
  305. return lfs_result_to_dfs(result);
  306. }
  307. /* release rt_spiffs */
  308. return RT_EOK;
  309. }
  310. static int _dfs_lfs_statfs_count(void* p, lfs_block_t b)
  311. {
  312. *(lfs_size_t*)p += 1;
  313. return 0;
  314. }
  315. static int dfs_lfs_statfs(struct dfs_filesystem* dfs, struct statfs* buf)
  316. {
  317. dfs_lfs_t* dfs_lfs;
  318. int result;
  319. lfs_size_t in_use = 0;
  320. RT_ASSERT(buf != RT_NULL);
  321. RT_ASSERT(dfs != RT_NULL);
  322. RT_ASSERT(dfs->data != RT_NULL);
  323. dfs_lfs = (dfs_lfs_t*)dfs->data;
  324. /* Get total sectors and free sectors */
  325. result = lfs_traverse(&dfs_lfs->lfs, _dfs_lfs_statfs_count, &in_use);
  326. if (result != LFS_ERR_OK)
  327. {
  328. return lfs_result_to_dfs(result);
  329. }
  330. buf->f_bsize = dfs_lfs->cfg.block_size;
  331. buf->f_blocks = dfs_lfs->cfg.block_count;
  332. buf->f_bfree = dfs_lfs->cfg.block_count - in_use;
  333. return RT_EOK;
  334. }
  335. static int dfs_lfs_unlink(struct dfs_filesystem* dfs, const char* path)
  336. {
  337. dfs_lfs_t* dfs_lfs;
  338. int result;
  339. RT_ASSERT(dfs != RT_NULL);
  340. RT_ASSERT(dfs->data != RT_NULL);
  341. dfs_lfs = (dfs_lfs_t*)dfs->data;
  342. result = lfs_remove(&dfs_lfs->lfs, path);
  343. return lfs_result_to_dfs(result);
  344. }
  345. static void _dfs_lfs_tostat(struct stat* st, struct lfs_info* info)
  346. {
  347. memset(st, 0, sizeof(struct stat));
  348. /* convert to dfs stat structure */
  349. st->st_dev = 0;
  350. st->st_size = info->size;
  351. st->st_mode = S_IRWXU | S_IRWXG | S_IRWXO;
  352. switch (info->type)
  353. {
  354. case LFS_TYPE_DIR:
  355. st->st_mode |= S_IFDIR;
  356. break;
  357. case LFS_TYPE_REG:
  358. st->st_mode |= S_IFREG;
  359. break;
  360. }
  361. }
  362. int dfs_lfs_stat(struct dfs_filesystem* dfs, const char* path, struct stat* st)
  363. {
  364. dfs_lfs_t* dfs_lfs;
  365. int result;
  366. struct lfs_info info;
  367. RT_ASSERT(dfs != RT_NULL);
  368. RT_ASSERT(dfs->data != RT_NULL);
  369. dfs_lfs = (dfs_lfs_t*)dfs->data;
  370. result = lfs_stat(&dfs_lfs->lfs, path, &info);
  371. if (result != LFS_ERR_OK)
  372. {
  373. return lfs_result_to_dfs(result);
  374. }
  375. _dfs_lfs_tostat(st, &info);
  376. return 0;
  377. }
  378. static int dfs_lfs_rename(struct dfs_filesystem* dfs, const char* from, const char* to)
  379. {
  380. dfs_lfs_t* dfs_lfs;
  381. int result;
  382. RT_ASSERT(dfs != RT_NULL);
  383. RT_ASSERT(dfs->data != RT_NULL);
  384. dfs_lfs = (dfs_lfs_t*)dfs->data;
  385. result = lfs_rename(&dfs_lfs->lfs, from, to);
  386. return lfs_result_to_dfs(result);
  387. }
  388. /******************************************************************************
  389. * file operations
  390. ******************************************************************************/
  391. static int dfs_lfs_open(struct dfs_fd* file)
  392. {
  393. struct dfs_filesystem* dfs;
  394. dfs_lfs_t* dfs_lfs;
  395. int result;
  396. int flags = 0;
  397. RT_ASSERT(file != RT_NULL);
  398. RT_ASSERT(file->data != RT_NULL);
  399. dfs = (struct dfs_filesystem*)file->data;
  400. dfs_lfs = (dfs_lfs_t*)dfs->data;
  401. if (file->flags & O_DIRECTORY)
  402. {
  403. dfs_lfs_fd_t* dfs_lfs_fd = rt_malloc(sizeof(dfs_lfs_fd_t));
  404. if (dfs_lfs_fd == RT_NULL)
  405. {
  406. rt_kprintf("ERROR:no memory!\n");
  407. result = -ENOMEM;
  408. goto _error_dir;
  409. }
  410. rt_memset(dfs_lfs_fd, 0, sizeof(dfs_lfs_fd_t));
  411. dfs_lfs_fd->lfs = &dfs_lfs->lfs;
  412. if (file->flags & O_CREAT)
  413. {
  414. result = lfs_mkdir(dfs_lfs_fd->lfs, file->path);
  415. if (result != LFS_ERR_OK)
  416. {
  417. goto _error_dir;
  418. }
  419. }
  420. result = lfs_dir_open(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.dir, file->path);
  421. if (result != LFS_ERR_OK)
  422. {
  423. goto _error_dir;
  424. }
  425. else
  426. {
  427. file->data = (void*)dfs_lfs_fd;
  428. return RT_EOK;
  429. }
  430. _error_dir:
  431. if (dfs_lfs_fd != RT_NULL)
  432. {
  433. rt_free(dfs_lfs_fd);
  434. }
  435. return lfs_result_to_dfs(result);
  436. }
  437. else
  438. {
  439. dfs_lfs_fd_t* dfs_lfs_fd = rt_malloc(sizeof(dfs_lfs_fd_t));
  440. if (dfs_lfs_fd == RT_NULL)
  441. {
  442. rt_kprintf("ERROR:no memory!\n");
  443. result = -ENOMEM;
  444. goto _error_file;
  445. }
  446. rt_memset(dfs_lfs_fd, 0, sizeof(dfs_lfs_fd_t));
  447. dfs_lfs_fd->lfs = &dfs_lfs->lfs;
  448. if ((file->flags & 3) == O_RDONLY)
  449. flags |= LFS_O_RDONLY;
  450. if ((file->flags & 3) == O_WRONLY)
  451. flags |= LFS_O_WRONLY;
  452. if ((file->flags & 3) == O_RDWR)
  453. flags |= LFS_O_RDWR;
  454. if (file->flags & O_CREAT)
  455. flags |= LFS_O_CREAT;
  456. if (file->flags & O_EXCL)
  457. flags |= LFS_O_EXCL;
  458. if (file->flags & O_TRUNC)
  459. flags |= LFS_O_TRUNC;
  460. if (file->flags & O_APPEND)
  461. flags |= LFS_O_APPEND;
  462. result = lfs_file_open(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, file->path, flags);
  463. if (result != LFS_ERR_OK)
  464. {
  465. goto _error_file;
  466. }
  467. else
  468. {
  469. file->data = (void*)dfs_lfs_fd;
  470. return RT_EOK;
  471. }
  472. _error_file:
  473. if (dfs_lfs_fd != RT_NULL)
  474. {
  475. rt_free(dfs_lfs_fd);
  476. }
  477. return lfs_result_to_dfs(result);
  478. }
  479. }
  480. static int dfs_lfs_close(struct dfs_fd* file)
  481. {
  482. int result;
  483. dfs_lfs_fd_t* dfs_lfs_fd;
  484. RT_ASSERT(file != RT_NULL);
  485. RT_ASSERT(file->data != RT_NULL);
  486. dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
  487. if (file->type == FT_DIRECTORY)
  488. {
  489. result = lfs_dir_close(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.dir);
  490. }
  491. else
  492. {
  493. result = lfs_file_close(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file);
  494. }
  495. rt_free(dfs_lfs_fd);
  496. return lfs_result_to_dfs(result);
  497. }
  498. static int dfs_lfs_ioctl(struct dfs_fd* file, int cmd, void* args)
  499. {
  500. return -ENOSYS;
  501. }
  502. int dfs_lfs_read(struct dfs_fd* file, void* buf, size_t len)
  503. {
  504. lfs_ssize_t ssize;
  505. dfs_lfs_fd_t* dfs_lfs_fd;
  506. RT_ASSERT(file != RT_NULL);
  507. RT_ASSERT(file->data != RT_NULL);
  508. dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
  509. if (file->type == FT_DIRECTORY)
  510. {
  511. return -EISDIR;
  512. }
  513. if (lfs_file_tell(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file) != file->pos)
  514. {
  515. lfs_soff_t soff = lfs_file_seek(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, file->pos, LFS_SEEK_SET);
  516. if (soff < 0)
  517. {
  518. return lfs_result_to_dfs(soff);
  519. }
  520. }
  521. ssize = lfs_file_read(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, buf, len);
  522. if (ssize < 0)
  523. {
  524. return lfs_result_to_dfs(ssize);
  525. }
  526. /* update position */
  527. file->pos = dfs_lfs_fd->u.file.pos;
  528. return ssize;
  529. }
  530. int dfs_lfs_write(struct dfs_fd* file, const void* buf, size_t len)
  531. {
  532. lfs_ssize_t ssize;
  533. dfs_lfs_fd_t* dfs_lfs_fd;
  534. RT_ASSERT(file != RT_NULL);
  535. RT_ASSERT(file->data != RT_NULL);
  536. if (file->type == FT_DIRECTORY)
  537. {
  538. return -EISDIR;
  539. }
  540. dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
  541. if (lfs_file_tell(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file) != file->pos)
  542. {
  543. lfs_soff_t soff = lfs_file_seek(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, file->pos, LFS_SEEK_SET);
  544. if (soff < 0)
  545. {
  546. return lfs_result_to_dfs(soff);
  547. }
  548. }
  549. ssize = lfs_file_write(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, buf, len);
  550. if (ssize < 0)
  551. {
  552. return lfs_result_to_dfs(ssize);
  553. }
  554. /* update position and file size */
  555. file->pos = dfs_lfs_fd->u.file.pos;
  556. file->size = dfs_lfs_fd->u.file.size;
  557. return ssize;
  558. }
  559. int dfs_lfs_flush(struct dfs_fd* file)
  560. {
  561. int result;
  562. dfs_lfs_fd_t* dfs_lfs_fd;
  563. RT_ASSERT(file != RT_NULL);
  564. RT_ASSERT(file->data != RT_NULL);
  565. dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
  566. result = lfs_file_sync(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file);
  567. return lfs_result_to_dfs(result);
  568. }
  569. int dfs_lfs_lseek(struct dfs_fd* file, rt_off_t offset)
  570. {
  571. dfs_lfs_fd_t* dfs_lfs_fd;
  572. RT_ASSERT(file != RT_NULL);
  573. RT_ASSERT(file->data != RT_NULL);
  574. dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
  575. if (file->type == FT_REGULAR)
  576. {
  577. lfs_soff_t soff = lfs_file_seek(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, offset, LFS_SEEK_SET);
  578. if (soff < 0)
  579. {
  580. return lfs_result_to_dfs(soff);
  581. }
  582. file->pos = dfs_lfs_fd->u.file.pos;
  583. }
  584. else if (file->type == FT_DIRECTORY)
  585. {
  586. lfs_soff_t soff = lfs_dir_seek(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.dir, offset);
  587. if (soff < 0)
  588. {
  589. return lfs_result_to_dfs(soff);
  590. }
  591. file->pos = dfs_lfs_fd->u.dir.pos;
  592. }
  593. return (file->pos);
  594. }
  595. int dfs_lfs_getdents(struct dfs_fd* file, struct dirent* dirp, uint32_t count)
  596. {
  597. dfs_lfs_fd_t* dfs_lfs_fd;
  598. int result;
  599. int index;
  600. struct dirent* d;
  601. struct lfs_info info;
  602. RT_ASSERT(file->data != RT_NULL);
  603. dfs_lfs_fd = (dfs_lfs_fd_t*)(file->data);
  604. /* make integer count */
  605. count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
  606. if (count == 0)
  607. {
  608. return -EINVAL;
  609. }
  610. index = 0;
  611. while (1)
  612. {
  613. d = dirp + index;
  614. result = lfs_dir_read(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.dir, &info);
  615. if ((result != 1) || (info.name[0] == 0))
  616. {
  617. return result;
  618. }
  619. d->d_type = DT_UNKNOWN;
  620. switch (info.type)
  621. {
  622. case LFS_TYPE_DIR:
  623. d->d_type |= DT_DIR;
  624. break;
  625. case LFS_TYPE_REG:
  626. d->d_type |= DT_REG;
  627. break;
  628. }
  629. d->d_namlen = (rt_uint8_t)rt_strlen(info.name);
  630. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  631. rt_strncpy(d->d_name, info.name, rt_strlen(info.name) + 1);
  632. index++;
  633. if (index * sizeof(struct dirent) >= count)
  634. {
  635. break;
  636. }
  637. }
  638. if (index == 0)
  639. {
  640. return lfs_result_to_dfs(result);
  641. }
  642. file->pos += index * sizeof(struct dirent);
  643. return index * sizeof(struct dirent);
  644. }
  645. static const struct dfs_file_ops _dfs_lfs_fops = {
  646. dfs_lfs_open,
  647. dfs_lfs_close,
  648. dfs_lfs_ioctl,
  649. dfs_lfs_read,
  650. dfs_lfs_write,
  651. dfs_lfs_flush,
  652. dfs_lfs_lseek,
  653. dfs_lfs_getdents,
  654. // RT_NULL, /* poll interface */
  655. };
  656. static const struct dfs_filesystem_ops _dfs_lfs_ops = {
  657. "lfs",
  658. DFS_FS_FLAG_DEFAULT,
  659. &_dfs_lfs_fops,
  660. dfs_lfs_mount,
  661. dfs_lfs_unmount,
  662. dfs_lfs_mkfs,
  663. dfs_lfs_statfs,
  664. dfs_lfs_unlink,
  665. dfs_lfs_stat,
  666. dfs_lfs_rename,
  667. };
  668. int dfs_lfs_init(void)
  669. {
  670. /* register ram file system */
  671. dfs_register(&_dfs_lfs_ops);
  672. return 0;
  673. }
  674. INIT_COMPONENT_EXPORT(dfs_lfs_init);