@@ -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
6767void * 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) {
214217void 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 );
0 commit comments