Skip to content

Commit 3654a91

Browse files
committed
_vfs_stream.c: Fix cannot open files with relative paths.
Signed-off-by: lbuque <[email protected]>
1 parent 6266a29 commit 3654a91

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

m5stack/_vfs_stream.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static int lfs2_get_mode(int flags) {
4646
ret |= flags & VFS_READ ? LFS2_O_RDONLY : 0;
4747
ret = flags & VFS_WRITE ? LFS2_O_WRONLY : 0;
4848
ret |= flags & VFS_APPEND ? LFS2_O_APPEND : 0;
49-
ret |= flags & VFS_CREATE ? LFS2_O_CREAT : 0;
49+
ret |= flags & VFS_CREATE ? LFS2_O_CREAT | LFS2_O_TRUNC: 0;
5050

5151
return ret;
5252
}
@@ -65,26 +65,29 @@ static int fatfs_get_mode(int flags) {
6565

6666

6767
void *vfs_stream_open(const char *path, int flags) {
68-
ESP_LOGI(TAG, "vfs_stream_open: path=%s, mode=%d\n", path, flags);
68+
ESP_LOGI(TAG, "vfs_stream_open: path=%s, mode=%d", path, flags);
6969

7070
vfs_stream_t *vfs = calloc(1, sizeof(vfs_stream_t));
7171

72-
const char *path_out;
72+
const char *path_out = {'\0'};
7373
mp_vfs_mount_t *existing_mount = mp_vfs_lookup_path(path, &path_out);
7474
if (existing_mount == MP_VFS_NONE || existing_mount == MP_VFS_ROOT) {
7575
ESP_LOGE(TAG, "No vfs mount");
7676
goto _vfs_init_exit;
7777
}
7878

79-
if (strstr(path, "flash")) {
79+
if (strstr(path_out, "flash")) {
8080
ESP_LOGD(TAG, "in flash");
8181
vfs->lfs2 = &((mp_obj_vfs_lfs2_t *)MP_OBJ_TO_PTR(existing_mount->obj))->lfs;
82-
} else if (strstr(path, "system")) {
82+
} else if (strstr(path_out, "system")) {
8383
ESP_LOGD(TAG, "in system");
8484
vfs->lfs2 = &((mp_obj_vfs_lfs2_t *)MP_OBJ_TO_PTR(existing_mount->obj))->lfs;
85-
} else if (strstr(path, "sd")) {
85+
} else if (strstr(path_out, "sd")) {
8686
ESP_LOGD(TAG, "in sd");
8787
vfs->fatfs = &((fs_user_mount_t *)MP_OBJ_TO_PTR(existing_mount->obj))->fatfs;
88+
} else {
89+
ESP_LOGI(TAG, "default in flash");
90+
vfs->lfs2 = &((mp_obj_vfs_lfs2_t *)MP_OBJ_TO_PTR(existing_mount->obj))->lfs;
8891
}
8992

9093
if (vfs->lfs2) {
@@ -128,9 +131,9 @@ void *vfs_stream_open(const char *path, int flags) {
128131
}
129132

130133

131-
uint32_t vfs_stream_read(void *file_p, void *buf, uint32_t btr) {
134+
int32_t vfs_stream_read(void *file_p, void *buf, uint32_t btr) {
132135
vfs_stream_t *vfs = file_p;
133-
uint32_t br = 0;
136+
int32_t br = 0;
134137

135138
if (vfs->lfs2) {
136139
br = lfs2_file_read(vfs->lfs2, vfs->file.lfs2_file, (uint8_t *)buf, btr);
@@ -214,6 +217,10 @@ int32_t vfs_stream_tell(void *file_p) {
214217
void vfs_stream_close(void *file_p) {
215218
vfs_stream_t *vfs = file_p;
216219

220+
if (vfs == NULL) {
221+
return;
222+
}
223+
217224
if (vfs->lfs2) {
218225
lfs2_file_close(vfs->lfs2, vfs->file.lfs2_file);
219226
free(vfs->file.lfs2_file);

m5stack/_vfs_stream.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#define VFS_CREATE 0x08
2929

3030
void *vfs_stream_open(const char *path, int flags);
31-
uint32_t vfs_stream_read(void *file_p, void *buf, uint32_t btr);
31+
int32_t vfs_stream_read(void *file_p, void *buf, uint32_t btr);
3232
ssize_t vfs_stream_write(void *file_p, const void *buf, size_t len);
3333
int32_t vfs_stream_seek(void *file_p, uint32_t pos, int whence);
3434
int32_t vfs_stream_tell(void *file_p);

m5stack/cmodules/lv_utils/modlv_utils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static void *lv_utils_fs_open_cb(lv_fs_drv_t *drv, const char *path, lv_fs_mode_
4646
LV_UNUSED(drv);
4747
lv_fs_res_t res = LV_FS_RES_NOT_IMP;
4848

49-
ESP_LOGI("lv_utils", "fs_open_cb: path=%s, mode=%d\n", path, mode);
49+
ESP_LOGI("lv_utils", "fs_open_cb: path=%s, mode=%d", path, mode);
5050

5151
vfs_stream_t *vfs = lv_malloc(sizeof(vfs_stream_t));
5252

0 commit comments

Comments
 (0)