Skip to content

Commit 6c23c75

Browse files
committed
extmod/vfs: Add ability for VFS sub-system to import using VfsFat.
1 parent fb3ae17 commit 6c23c75

4 files changed

Lines changed: 15 additions & 10 deletions

File tree

extmod/vfs.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "py/objstr.h"
3232
#include "py/mperrno.h"
3333
#include "extmod/vfs.h"
34+
#include "extmod/vfs_fat.h"
3435

3536
#if MICROPY_VFS
3637

@@ -114,6 +115,12 @@ mp_import_stat_t mp_vfs_import_stat(const char *path) {
114115
if (vfs == VFS_NONE || vfs == VFS_ROOT) {
115116
return MP_IMPORT_STAT_NO_EXIST;
116117
}
118+
#if MICROPY_VFS_FAT
119+
// fast paths for known VFS types
120+
if (mp_obj_get_type(vfs->obj) == &mp_fat_vfs_type) {
121+
return fat_vfs_import_stat(MP_OBJ_TO_PTR(vfs->obj), path_out);
122+
}
123+
#endif
117124
// TODO delegate to vfs.stat() method
118125
return MP_IMPORT_STAT_NO_EXIST;
119126
}

extmod/vfs_fat.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "py/lexer.h"
28+
2729
struct _fs_user_mount_t;
2830

2931
extern const byte fresult_to_errno_table[20];
3032
extern const mp_obj_type_t mp_fat_vfs_type;
3133

3234
struct _fs_user_mount_t *ff_get_vfs(const char **path);
3335

36+
mp_import_stat_t fat_vfs_import_stat(struct _fs_user_mount_t *vfs, const char *path);
3437
mp_obj_t fatfs_builtin_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
3538
mp_obj_t fatfs_builtin_open_self(mp_obj_t self_in, mp_obj_t path, mp_obj_t mode);
3639
MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_open_obj);

extmod/vfs_fat_misc.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,17 @@ mp_obj_t fat_vfs_listdir2(fs_user_mount_t *vfs, const char *path, bool is_str_ty
108108
return dir_list;
109109
}
110110

111-
mp_import_stat_t fat_vfs_import_stat(const char *path);
112-
113-
mp_import_stat_t fat_vfs_import_stat(const char *path) {
111+
mp_import_stat_t fat_vfs_import_stat(fs_user_mount_t *vfs, const char *path) {
114112
FILINFO fno;
115113
#if !MICROPY_FATFS_OO && _USE_LFN
116114
fno.lfname = NULL;
117115
fno.lfsize = 0;
118116
#endif
119117
#if MICROPY_FATFS_OO
120-
fs_user_mount_t *vfs = ff_get_vfs(&path);
121-
if (vfs == NULL) {
122-
return MP_IMPORT_STAT_NO_EXIST;
123-
}
118+
assert(vfs != NULL);
124119
FRESULT res = f_stat(&vfs->fatfs, path, &fno);
125120
#else
121+
(void)vfs;
126122
FRESULT res = f_stat(path, &fno);
127123
#endif
128124
if (res == FR_OK) {

stmhal/import.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@
2828

2929
#include "py/lexer.h"
3030
#include "lib/fatfs/ff.h"
31-
32-
mp_import_stat_t fat_vfs_import_stat(const char *path);
31+
#include "extmod/vfs_fat.h"
3332

3433
mp_import_stat_t mp_import_stat(const char *path) {
35-
return fat_vfs_import_stat(path);
34+
return fat_vfs_import_stat(NULL, path);
3635
}

0 commit comments

Comments
 (0)