summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-02-09 12:03:12 +1100
committerDamien George <damien.p.george@gmail.com>2017-02-09 12:03:12 +1100
commitec7dc7f8d796b5b67772d1f40863d13fb5e19be2 (patch)
treeaba56f771f007a0a670eca41cb087bb6f0a44ada
parent181f7d145002731e5baec0f2c43ac57818447f8b (diff)
downloadmicropython-ec7dc7f8d796b5b67772d1f40863d13fb5e19be2.tar.gz
micropython-ec7dc7f8d796b5b67772d1f40863d13fb5e19be2.zip
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.
-rw-r--r--extmod/vfs.c15
1 files changed, 14 insertions, 1 deletions
diff --git a/extmod/vfs.c b/extmod/vfs.c
index 1eb26acf16..c1e32e0522 100644
--- a/extmod/vfs.c
+++ b/extmod/vfs.c
@@ -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
mp_uint_t mnt_len;
const char *mnt_str = mp_obj_str_get_data(pos_args[1], &mnt_len);
+ // see if we need to auto-detect and create the filesystem
+ mp_obj_t vfs_obj = pos_args[0];
+ mp_obj_t dest[2];
+ mp_load_method_maybe(vfs_obj, MP_QSTR_mount, dest);
+ if (dest[0] == MP_OBJ_NULL) {
+ // Input object has no mount method, assume it's a block device and try to
+ // auto-detect the filesystem and create the corresponding VFS entity.
+ // (At the moment we only support FAT filesystems.)
+ #if MICROPY_VFS_FAT
+ vfs_obj = mp_fat_vfs_type.make_new(&mp_fat_vfs_type, 1, 0, &vfs_obj);
+ #endif
+ }
+
// create new object
mp_vfs_mount_t *vfs = m_new_obj(mp_vfs_mount_t);
vfs->str = mnt_str;
vfs->len = mnt_len;
- vfs->obj = pos_args[0];
+ vfs->obj = vfs_obj;
vfs->next = NULL;
// call the underlying object to do any mounting operation