Skip to content

Commit ec7dc7f

Browse files
committed
extmod/vfs: Allow to mount a block device, not just a VFS object.
If the mounted object doesn't have a "mount" method then assume it's a block device and try to detect the filesystem. Since we currently only support FAT filesystems, the behaviour is to just try and create a VfsFat object automatically, using the given block device.
1 parent 181f7d1 commit ec7dc7f

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

extmod/vfs.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,24 @@ mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args
132132
mp_uint_t mnt_len;
133133
const char *mnt_str = mp_obj_str_get_data(pos_args[1], &mnt_len);
134134

135+
// see if we need to auto-detect and create the filesystem
136+
mp_obj_t vfs_obj = pos_args[0];
137+
mp_obj_t dest[2];
138+
mp_load_method_maybe(vfs_obj, MP_QSTR_mount, dest);
139+
if (dest[0] == MP_OBJ_NULL) {
140+
// Input object has no mount method, assume it's a block device and try to
141+
// auto-detect the filesystem and create the corresponding VFS entity.
142+
// (At the moment we only support FAT filesystems.)
143+
#if MICROPY_VFS_FAT
144+
vfs_obj = mp_fat_vfs_type.make_new(&mp_fat_vfs_type, 1, 0, &vfs_obj);
145+
#endif
146+
}
147+
135148
// create new object
136149
mp_vfs_mount_t *vfs = m_new_obj(mp_vfs_mount_t);
137150
vfs->str = mnt_str;
138151
vfs->len = mnt_len;
139-
vfs->obj = pos_args[0];
152+
vfs->obj = vfs_obj;
140153
vfs->next = NULL;
141154

142155
// call the underlying object to do any mounting operation

0 commit comments

Comments
 (0)