fix: 对不支持weak的bsp, luat_http_client_onevent报重复定义了
[LuatOS.git] / components / fskv / luat_fskv_sfd.c
blob30fe3017bf222a504de3ba41f9eaba5aaead8618
1 #ifdef __LUATOS__
2 #include "luat_base.h"
3 #include "luat_fskv.h"
4 #include "luat_mem.h"
5 #include "luat_msgbus.h"
6 #include "luat_sfd.h"
8 #define LUAT_LOG_TAG "fskv_sfd"
9 #include "luat_log.h"
11 #include "lfs.h"
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);
18 return 0;
21 int luat_fskv_set(const char* key, void* data, size_t len) {
22 lfs_file_t fd = {0};
23 int ret = 0;
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) {
26 return -1;
28 ret = lfs_file_write(&sfd_lfs->lfs, &fd, data, len);
29 lfs_file_close(&sfd_lfs->lfs, &fd);
30 return ret;
33 int luat_fskv_get(const char* key, void* data, size_t len) {
34 lfs_file_t fd = {0};
35 int ret = 0;
36 ret = lfs_file_open(&sfd_lfs->lfs, &fd, key, LFS_O_RDONLY);
37 if (ret != LFS_ERR_OK) {
38 return 0;
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) {
46 int ret = 0;
47 ret = lfs_format(&sfd_lfs->lfs, &sfd_lfs->conf);
48 if (ret != LFS_ERR_OK) {
49 LLOGE("fskv clear ret %d", ret);
50 return ret;
52 ret = lfs_mount(&sfd_lfs->lfs, &sfd_lfs->conf);
53 if (ret != LFS_ERR_OK) {
54 LLOGE("fskv reinit ret %d", ret);
55 return ret;
57 return 0;
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;
63 lfs_dir_t dir = {0};
64 int ret = lfs_dir_open(&sfd_lfs->lfs, &dir, "");
65 if (ret != LFS_ERR_OK) {
66 LLOGW("lfs_dir_open ret %d", ret);
67 return -1;
69 size_t count = 0;
70 struct lfs_info info = {0};
71 while (1) {
72 ret = lfs_dir_read(&sfd_lfs->lfs, &dir, &info);
73 if (ret > 0) {
74 if (info.type == LFS_TYPE_REG)
75 count ++;
77 else
78 break;
80 lfs_dir_close(&sfd_lfs->lfs, &dir);
81 *kv_count = count;
82 return 0;
85 int luat_fskv_size(const char* key, char buff[4]) {
86 lfs_file_t fd = {0};
87 int ret = 0;
88 ret = lfs_file_open(&sfd_lfs->lfs, &fd, key, LFS_O_RDONLY);
89 if (ret != LFS_ERR_OK) {
90 return 0;
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);
95 if (ret2 != ret) {
96 ret = -2; // 读取失败,肯定有问题
99 lfs_file_close(&sfd_lfs->lfs, &fd);
100 return ret;
103 int luat_fskv_next(char* buff, size_t offset) {
104 lfs_dir_t dir = {0};
105 struct lfs_info info = {0};
106 // offset要+2, 因为前2个值是"."和".."两个dir
107 offset += 2;
108 int ret = lfs_dir_open(&sfd_lfs->lfs, &dir, "");
109 if (ret < 0) {
110 LLOGW("lfs_dir_open ret %d", ret);
111 return -1;
113 ret = lfs_dir_seek(&sfd_lfs->lfs, &dir, offset);
114 if (ret < 0) {
115 lfs_dir_close(&sfd_lfs->lfs, &dir);
116 return -2;
118 ret = lfs_dir_read(&sfd_lfs->lfs, &dir, &info);
119 if (ret <= 0) {
120 lfs_dir_close(&sfd_lfs->lfs, &dir);
121 return -3;
123 memcpy(buff, info.name, strlen(info.name) + 1);
124 lfs_dir_close(&sfd_lfs->lfs, &dir);
125 return 0;
127 #endif