dfs_lfs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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. #ifndef RT_DEF_LFS_DRIVERS
  9. #define RT_DEF_LFS_DRIVERS 1
  10. #endif
  11. #if (RT_DEF_LFS_DRIVERS < 1)
  12. #error "#define RT_DEF_LFS_DRIVERS must > 0"
  13. #endif
  14. #ifndef LFS_READ_SIZE
  15. #define LFS_READ_SIZE 256
  16. #endif
  17. #ifndef LFS_PROG_SIZE
  18. #define LFS_PROG_SIZE 256
  19. #endif
  20. #ifndef LFS_BLOCK_SIZE
  21. #define LFS_BLOCK_SIZE 4096
  22. #endif
  23. #ifndef LFS_LOOKAHEAD
  24. #define LFS_LOOKAHEAD 512
  25. #endif
  26. typedef struct _dfs_lfs_s
  27. {
  28. struct lfs lfs;
  29. struct lfs_config cfg;
  30. } dfs_lfs_t;
  31. typedef struct _dfs_lfs_fd_s
  32. {
  33. struct lfs* lfs;
  34. union
  35. {
  36. struct lfs_file file;
  37. struct lfs_dir dir;
  38. } u;
  39. } dfs_lfs_fd_t;
  40. static struct _dfs_lfs_s* _lfs_mount_tbl[RT_DEF_LFS_DRIVERS] = {0};
  41. // Read a region in a block. Negative error codes are propogated
  42. // to the user.
  43. static int _lfs_flash_read(const struct lfs_config* c, lfs_block_t block, lfs_off_t off, void* buffer, lfs_size_t size)
  44. {
  45. struct rt_mtd_nor_device* mtd_nor;
  46. RT_ASSERT(c != RT_NULL);
  47. RT_ASSERT(c->context != RT_NULL);
  48. mtd_nor = (struct rt_mtd_nor_device*)c->context;
  49. if (rt_mtd_nor_read(mtd_nor, block * c->block_size + off, buffer, size) != size)
  50. {
  51. return LFS_ERR_IO;
  52. }
  53. return LFS_ERR_OK;
  54. }
  55. // Program a region in a block. The block must have previously
  56. // been erased. Negative error codes are propogated to the user.
  57. // May return LFS_ERR_CORRUPT if the block should be considered bad.
  58. 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)
  59. {
  60. struct rt_mtd_nor_device* mtd_nor;
  61. RT_ASSERT(c != RT_NULL);
  62. RT_ASSERT(c->context != RT_NULL);
  63. mtd_nor = (struct rt_mtd_nor_device*)c->context;
  64. if (rt_mtd_nor_write(mtd_nor, block * c->block_size + off, buffer, size) != size)
  65. {
  66. return LFS_ERR_IO;
  67. }
  68. return LFS_ERR_OK;
  69. }
  70. // Erase a block. A block must be erased before being programmed.
  71. // The state of an erased block is undefined. Negative error codes
  72. // are propogated to the user.
  73. // May return LFS_ERR_CORRUPT if the block should be considered bad.
  74. static int _lfs_flash_erase(const struct lfs_config* c, lfs_block_t block)
  75. {
  76. struct rt_mtd_nor_device* mtd_nor;
  77. RT_ASSERT(c != RT_NULL);
  78. RT_ASSERT(c->context != RT_NULL);
  79. mtd_nor = (struct rt_mtd_nor_device*)c->context;
  80. if (rt_mtd_nor_erase_block(mtd_nor, block * c->block_size, c->block_size) != RT_EOK)
  81. {
  82. return LFS_ERR_IO;
  83. }
  84. return LFS_ERR_OK;
  85. }
  86. // Sync the state of the underlying block device. Negative error codes
  87. // are propogated to the user.
  88. static int _lfs_flash_sync(const struct lfs_config* c)
  89. {
  90. return LFS_ERR_OK;
  91. }
  92. /* results:
  93. * -1, no space to install fatfs driver
  94. * >= 0, there is an space to install littlefs driver
  95. */
  96. static int _get_disk(rt_device_t dev_id)
  97. {
  98. int index;
  99. if (dev_id == RT_NULL)
  100. {
  101. for (index = 0; index < RT_DEF_LFS_DRIVERS; index ++)
  102. {
  103. if(_lfs_mount_tbl[index] == RT_NULL)
  104. {
  105. return index;
  106. }
  107. }
  108. }
  109. else
  110. {
  111. for (index = 0; index < RT_DEF_LFS_DRIVERS; index ++)
  112. {
  113. if ((_lfs_mount_tbl[index] != RT_NULL) && (_lfs_mount_tbl[index]->cfg.context == (void *)dev_id))
  114. {
  115. return index;
  116. }
  117. }
  118. }
  119. return -1;
  120. }
  121. static int _lfs_result_to_dfs(int result)
  122. {
  123. int status = 0;
  124. switch (result)
  125. {
  126. case LFS_ERR_OK:
  127. break;
  128. case LFS_ERR_IO:
  129. status = -EIO;
  130. break; // Error during device operation
  131. case LFS_ERR_NOENT:
  132. status = -ENOENT;
  133. break; // No directory entry
  134. case LFS_ERR_EXIST:
  135. status = -EEXIST;
  136. break; // Entry already exists
  137. case LFS_ERR_NOTDIR:
  138. status = -ENOTDIR;
  139. break; // Entry is not a dir
  140. case LFS_ERR_ISDIR:
  141. status = -EISDIR;
  142. break; // Entry is a dir
  143. case LFS_ERR_NOTEMPTY:
  144. status = -ENOTEMPTY;
  145. break; // Dir is not empty
  146. case LFS_ERR_BADF:
  147. status = -EBADF;
  148. break; // Bad file number
  149. case LFS_ERR_INVAL:
  150. status = -EINVAL;
  151. break; // Invalid parameter
  152. case LFS_ERR_NOSPC:
  153. status = -ENOSPC;
  154. break; // No space left on device
  155. case LFS_ERR_NOMEM:
  156. status = -ENOMEM;
  157. break; // No more memory available
  158. case LFS_ERR_CORRUPT:
  159. status = -52;
  160. break; // Corrupted
  161. default:
  162. status = -EIO;
  163. break;
  164. }
  165. return status;
  166. }
  167. static void _lfs_load_config(struct lfs_config* lfs_cfg, struct rt_mtd_nor_device* mtd_nor)
  168. {
  169. lfs_cfg->context = (void*)mtd_nor;
  170. lfs_cfg->read_size = LFS_READ_SIZE;
  171. lfs_cfg->prog_size = LFS_PROG_SIZE;
  172. lfs_cfg->block_size = mtd_nor->block_size;
  173. if (lfs_cfg->block_size < LFS_BLOCK_SIZE)
  174. {
  175. lfs_cfg->block_size = LFS_BLOCK_SIZE;
  176. }
  177. lfs_cfg->block_count = mtd_nor->block_end - mtd_nor->block_start;
  178. lfs_cfg->lookahead = 32 * ((lfs_cfg->block_count + 31) / 32);
  179. if (lfs_cfg->lookahead > LFS_LOOKAHEAD)
  180. {
  181. lfs_cfg->lookahead = LFS_LOOKAHEAD;
  182. }
  183. lfs_cfg->read = &_lfs_flash_read;
  184. lfs_cfg->prog = &_lfs_flash_prog;
  185. lfs_cfg->erase = &_lfs_flash_erase;
  186. lfs_cfg->sync = &_lfs_flash_sync;
  187. }
  188. static int _dfs_lfs_mount(struct dfs_filesystem* dfs, unsigned long rwflag, const void* data)
  189. {
  190. int result;
  191. int index;
  192. dfs_lfs_t* dfs_lfs;
  193. /* Check Device Type */
  194. if (dfs->dev_id->type != RT_Device_Class_MTD)
  195. {
  196. rt_kprintf("The flash device type must be MTD!\n");
  197. return -EINVAL;
  198. }
  199. /* get an empty position */
  200. index = _get_disk(RT_NULL);
  201. if (index == -1)
  202. {
  203. return -EIO;
  204. }
  205. /*create lfs handle */
  206. dfs_lfs = (dfs_lfs_t*)rt_malloc(sizeof(dfs_lfs_t));
  207. if (dfs_lfs == RT_NULL)
  208. {
  209. rt_kprintf("ERROR:no memory!\n");
  210. return -ENOMEM;
  211. }
  212. rt_memset(dfs_lfs, 0, sizeof(dfs_lfs_t));
  213. _lfs_load_config(&dfs_lfs->cfg, (struct rt_mtd_nor_device*)dfs->dev_id);
  214. /* mount lfs*/
  215. result = lfs_mount(&dfs_lfs->lfs, &dfs_lfs->cfg);
  216. if (result != LFS_ERR_OK)
  217. {
  218. /* release memory */
  219. rt_free(dfs_lfs);
  220. return -EIO;
  221. }
  222. /* mount succeed! */
  223. dfs->data = (void*)dfs_lfs;
  224. _lfs_mount_tbl[index] = dfs_lfs;
  225. return RT_EOK;
  226. }
  227. static int _dfs_lfs_unmount(struct dfs_filesystem* dfs)
  228. {
  229. int result;
  230. int index;
  231. dfs_lfs_t* dfs_lfs;
  232. RT_ASSERT(dfs != RT_NULL);
  233. RT_ASSERT(dfs->data != RT_NULL);
  234. /* find the device index and then umount it */
  235. index = _get_disk(dfs->dev_id);
  236. if (index == -1)
  237. {
  238. return -ENOENT;
  239. }
  240. _lfs_mount_tbl[index] = RT_NULL;
  241. dfs_lfs = (dfs_lfs_t*)dfs->data;
  242. dfs->data = RT_NULL;
  243. result = lfs_unmount(&dfs_lfs->lfs);
  244. rt_free(dfs_lfs);
  245. return _lfs_result_to_dfs(result);
  246. }
  247. static int _dfs_lfs_mkfs(rt_device_t dev_id)
  248. {
  249. int result;
  250. int index;
  251. dfs_lfs_t* dfs_lfs;
  252. if (dev_id == RT_NULL)
  253. {
  254. return -EINVAL;
  255. }
  256. /* Check Device Type */
  257. if (dev_id->type != RT_Device_Class_MTD)
  258. {
  259. rt_kprintf("The flash device type must be MTD!\n");
  260. return -EINVAL;
  261. }
  262. index = _get_disk(dev_id);
  263. if (index == -1)
  264. {
  265. /* not found the device id */
  266. index = _get_disk(RT_NULL);
  267. if (index == -1)
  268. {
  269. /* no space to store an temp driver */
  270. rt_kprintf("sorry, there is no space to do mkfs! \n");
  271. return -ENOSPC;
  272. }
  273. /* create lfs handle */
  274. dfs_lfs = rt_malloc(sizeof(dfs_lfs_t));
  275. if (dfs_lfs == RT_NULL)
  276. {
  277. rt_kprintf("ERROR:no memory!\n");
  278. _lfs_mount_tbl[index] = RT_NULL;
  279. return -ENOMEM;
  280. }
  281. rt_memset(dfs_lfs, 0, sizeof(dfs_lfs_t));
  282. _lfs_load_config(&dfs_lfs->cfg, (struct rt_mtd_nor_device*)dev_id);
  283. /* format flash device */
  284. result = lfs_format(&dfs_lfs->lfs, &dfs_lfs->cfg);
  285. return _lfs_result_to_dfs(result);
  286. }
  287. dfs_lfs = _lfs_mount_tbl[index];
  288. /* unmount it */
  289. result = lfs_unmount(&dfs_lfs->lfs);
  290. if (result != LFS_ERR_OK)
  291. {
  292. return _lfs_result_to_dfs(result);
  293. }
  294. _lfs_mount_tbl[index] = RT_NULL;
  295. /* format flash device */
  296. result = lfs_format(&dfs_lfs->lfs, &dfs_lfs->cfg);
  297. if (result != LFS_ERR_OK)
  298. {
  299. return _lfs_result_to_dfs(result);
  300. }
  301. _lfs_load_config(&dfs_lfs->cfg, (struct rt_mtd_nor_device*)dev_id);
  302. /* mount lfs*/
  303. result = lfs_mount(&dfs_lfs->lfs, &dfs_lfs->cfg);
  304. if (result == LFS_ERR_OK)
  305. {
  306. _lfs_mount_tbl[index] = dfs_lfs;
  307. }
  308. return _lfs_result_to_dfs(result);
  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. static 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. file->pos = dfs_lfs_fd->u.file.pos;
  471. file->size = dfs_lfs_fd->u.file.size;
  472. return RT_EOK;
  473. }
  474. _error_file:
  475. if (dfs_lfs_fd != RT_NULL)
  476. {
  477. rt_free(dfs_lfs_fd);
  478. }
  479. return _lfs_result_to_dfs(result);
  480. }
  481. }
  482. static int _dfs_lfs_close(struct dfs_fd* file)
  483. {
  484. int result;
  485. dfs_lfs_fd_t* dfs_lfs_fd;
  486. RT_ASSERT(file != RT_NULL);
  487. RT_ASSERT(file->data != RT_NULL);
  488. dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
  489. if (file->type == FT_DIRECTORY)
  490. {
  491. result = lfs_dir_close(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.dir);
  492. }
  493. else
  494. {
  495. result = lfs_file_close(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file);
  496. }
  497. rt_free(dfs_lfs_fd);
  498. return _lfs_result_to_dfs(result);
  499. }
  500. static int _dfs_lfs_ioctl(struct dfs_fd* file, int cmd, void* args)
  501. {
  502. return -ENOSYS;
  503. }
  504. static int _dfs_lfs_read(struct dfs_fd* file, void* buf, size_t len)
  505. {
  506. lfs_ssize_t ssize;
  507. dfs_lfs_fd_t* dfs_lfs_fd;
  508. RT_ASSERT(file != RT_NULL);
  509. RT_ASSERT(file->data != RT_NULL);
  510. if (file->type == FT_DIRECTORY)
  511. {
  512. return -EISDIR;
  513. }
  514. dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
  515. #if 0
  516. if (lfs_file_tell(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file) != file->pos)
  517. {
  518. lfs_soff_t soff = lfs_file_seek(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, file->pos, LFS_SEEK_SET);
  519. if (soff < 0)
  520. {
  521. return _lfs_result_to_dfs(soff);
  522. }
  523. }
  524. #endif
  525. ssize = lfs_file_read(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, buf, len);
  526. if (ssize < 0)
  527. {
  528. return _lfs_result_to_dfs(ssize);
  529. }
  530. /* update position */
  531. file->pos = dfs_lfs_fd->u.file.pos;
  532. return ssize;
  533. }
  534. static int _dfs_lfs_write(struct dfs_fd* file, const void* buf, size_t len)
  535. {
  536. lfs_ssize_t ssize;
  537. dfs_lfs_fd_t* dfs_lfs_fd;
  538. RT_ASSERT(file != RT_NULL);
  539. RT_ASSERT(file->data != RT_NULL);
  540. if (file->type == FT_DIRECTORY)
  541. {
  542. return -EISDIR;
  543. }
  544. dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
  545. #if 0
  546. if (lfs_file_tell(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file) != file->pos)
  547. {
  548. lfs_soff_t soff = lfs_file_seek(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, file->pos, LFS_SEEK_SET);
  549. if (soff < 0)
  550. {
  551. return _lfs_result_to_dfs(soff);
  552. }
  553. }
  554. #endif
  555. ssize = lfs_file_write(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, buf, len);
  556. if (ssize < 0)
  557. {
  558. return _lfs_result_to_dfs(ssize);
  559. }
  560. /* update position and file size */
  561. file->pos = dfs_lfs_fd->u.file.pos;
  562. file->size = dfs_lfs_fd->u.file.size;
  563. return ssize;
  564. }
  565. static int _dfs_lfs_flush(struct dfs_fd* file)
  566. {
  567. int result;
  568. dfs_lfs_fd_t* dfs_lfs_fd;
  569. RT_ASSERT(file != RT_NULL);
  570. RT_ASSERT(file->data != RT_NULL);
  571. dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
  572. result = lfs_file_sync(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file);
  573. return _lfs_result_to_dfs(result);
  574. }
  575. static int _dfs_lfs_lseek(struct dfs_fd* file, rt_off_t offset)
  576. {
  577. dfs_lfs_fd_t* dfs_lfs_fd;
  578. RT_ASSERT(file != RT_NULL);
  579. RT_ASSERT(file->data != RT_NULL);
  580. dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
  581. if (file->type == FT_REGULAR)
  582. {
  583. lfs_soff_t soff = lfs_file_seek(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, offset, LFS_SEEK_SET);
  584. if (soff < 0)
  585. {
  586. return _lfs_result_to_dfs(soff);
  587. }
  588. file->pos = dfs_lfs_fd->u.file.pos;
  589. }
  590. else if (file->type == FT_DIRECTORY)
  591. {
  592. lfs_soff_t soff = lfs_dir_seek(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.dir, offset);
  593. if (soff < 0)
  594. {
  595. return _lfs_result_to_dfs(soff);
  596. }
  597. file->pos = dfs_lfs_fd->u.dir.pos;
  598. }
  599. return (file->pos);
  600. }
  601. static int _dfs_lfs_getdents(struct dfs_fd* file, struct dirent* dirp, uint32_t count)
  602. {
  603. dfs_lfs_fd_t* dfs_lfs_fd;
  604. int result;
  605. int index;
  606. struct dirent* d;
  607. struct lfs_info info;
  608. RT_ASSERT(file->data != RT_NULL);
  609. dfs_lfs_fd = (dfs_lfs_fd_t*)(file->data);
  610. /* make integer count */
  611. count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
  612. if (count == 0)
  613. {
  614. return -EINVAL;
  615. }
  616. index = 0;
  617. while (1)
  618. {
  619. d = dirp + index;
  620. result = lfs_dir_read(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.dir, &info);
  621. if ((result != 1) || (info.name[0] == 0))
  622. {
  623. break;
  624. }
  625. d->d_type = DT_UNKNOWN;
  626. switch (info.type)
  627. {
  628. case LFS_TYPE_DIR:
  629. d->d_type |= DT_DIR;
  630. break;
  631. case LFS_TYPE_REG:
  632. d->d_type |= DT_REG;
  633. break;
  634. }
  635. d->d_namlen = (rt_uint8_t)rt_strlen(info.name);
  636. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  637. rt_strncpy(d->d_name, info.name, rt_strlen(info.name) + 1);
  638. index++;
  639. if (index * sizeof(struct dirent) >= count)
  640. {
  641. break;
  642. }
  643. }
  644. if (index == 0)
  645. {
  646. return _lfs_result_to_dfs(result);
  647. }
  648. file->pos += index * sizeof(struct dirent);
  649. return index * sizeof(struct dirent);
  650. }
  651. static const struct dfs_file_ops _dfs_lfs_fops = {
  652. _dfs_lfs_open,
  653. _dfs_lfs_close,
  654. _dfs_lfs_ioctl,
  655. _dfs_lfs_read,
  656. _dfs_lfs_write,
  657. _dfs_lfs_flush,
  658. _dfs_lfs_lseek,
  659. _dfs_lfs_getdents,
  660. // RT_NULL, /* poll interface */
  661. };
  662. static const struct dfs_filesystem_ops _dfs_lfs_ops = {
  663. "lfs",
  664. DFS_FS_FLAG_DEFAULT,
  665. &_dfs_lfs_fops,
  666. _dfs_lfs_mount,
  667. _dfs_lfs_unmount,
  668. _dfs_lfs_mkfs,
  669. _dfs_lfs_statfs,
  670. _dfs_lfs_unlink,
  671. _dfs_lfs_stat,
  672. _dfs_lfs_rename,
  673. };
  674. int dfs_lfs_init(void)
  675. {
  676. /* register ram file system */
  677. dfs_register(&_dfs_lfs_ops);
  678. return 0;
  679. }
  680. INIT_COMPONENT_EXPORT(dfs_lfs_init);