5 #include "luat_msgbus.h"
8 #define LUAT_LOG_TAG "fskv_sfd"
13 // TODO 应该对接vfs, 而非直接对接lfs
14 extern luat_sfd_lfs_t
* sfd_lfs
;
16 int luat_fskv_del(const char* key
) {
17 lfs_remove(&sfd_lfs
->lfs
, key
);
21 int luat_fskv_set(const char* key
, void* data
, size_t len
) {
24 ret
= lfs_file_open(&sfd_lfs
->lfs
, &fd
, key
, LFS_O_WRONLY
| LFS_O_CREAT
| LFS_O_TRUNC
);
25 if (ret
!= LFS_ERR_OK
) {
28 ret
= lfs_file_write(&sfd_lfs
->lfs
, &fd
, data
, len
);
29 lfs_file_close(&sfd_lfs
->lfs
, &fd
);
33 int luat_fskv_get(const char* key
, void* data
, size_t len
) {
36 ret
= lfs_file_open(&sfd_lfs
->lfs
, &fd
, key
, LFS_O_RDONLY
);
37 if (ret
!= LFS_ERR_OK
) {
40 ret
= lfs_file_read(&sfd_lfs
->lfs
, &fd
, data
, len
);
41 lfs_file_close(&sfd_lfs
->lfs
, &fd
);
42 return ret
> 0 ? ret
: 0;
45 int luat_fskv_clear(void) {
47 ret
= lfs_format(&sfd_lfs
->lfs
, &sfd_lfs
->conf
);
48 if (ret
!= LFS_ERR_OK
) {
49 LLOGE("fskv clear ret %d", ret
);
52 ret
= lfs_mount(&sfd_lfs
->lfs
, &sfd_lfs
->conf
);
53 if (ret
!= LFS_ERR_OK
) {
54 LLOGE("fskv reinit ret %d", ret
);
60 int luat_fskv_stat(size_t *using_sz
, size_t *total
, size_t *kv_count
) {
61 *using_sz
= lfs_fs_size(&sfd_lfs
->lfs
) * LFS_BLOCK_DEVICE_ERASE_SIZE
;
62 *total
= LFS_BLOCK_DEVICE_TOTOAL_SIZE
;
64 int ret
= lfs_dir_open(&sfd_lfs
->lfs
, &dir
, "");
65 if (ret
!= LFS_ERR_OK
) {
66 LLOGW("lfs_dir_open ret %d", ret
);
70 struct lfs_info info
= {0};
72 ret
= lfs_dir_read(&sfd_lfs
->lfs
, &dir
, &info
);
74 if (info
.type
== LFS_TYPE_REG
)
80 lfs_dir_close(&sfd_lfs
->lfs
, &dir
);
85 int luat_fskv_size(const char* key
, char buff
[4]) {
88 ret
= lfs_file_open(&sfd_lfs
->lfs
, &fd
, key
, LFS_O_RDONLY
);
89 if (ret
!= LFS_ERR_OK
) {
92 ret
= lfs_file_size(&sfd_lfs
->lfs
, &fd
);
93 if (ret
> 1 && ret
< 256) {
94 int ret2
= lfs_file_read(&sfd_lfs
->lfs
, &fd
, buff
, ret
);
96 ret
= -2; // 读取失败,肯定有问题
99 lfs_file_close(&sfd_lfs
->lfs
, &fd
);
103 int luat_fskv_next(char* buff
, size_t offset
) {
105 struct lfs_info info
= {0};
106 // offset要+2, 因为前2个值是"."和".."两个dir
108 int ret
= lfs_dir_open(&sfd_lfs
->lfs
, &dir
, "");
110 LLOGW("lfs_dir_open ret %d", ret
);
113 ret
= lfs_dir_seek(&sfd_lfs
->lfs
, &dir
, offset
);
115 lfs_dir_close(&sfd_lfs
->lfs
, &dir
);
118 ret
= lfs_dir_read(&sfd_lfs
->lfs
, &dir
, &info
);
120 lfs_dir_close(&sfd_lfs
->lfs
, &dir
);
123 memcpy(buff
, info
.name
, strlen(info
.name
) + 1);
124 lfs_dir_close(&sfd_lfs
->lfs
, &dir
);