From 6c23c7587f1c02f58e9246ec59fe4f6544728b50 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Fri, 27 Jan 2017 17:17:54 +1100
Subject: [PATCH] extmod/vfs: Add ability for VFS sub-system to import using
 VfsFat.

---
 extmod/vfs.c          |  7 +++++++
 extmod/vfs_fat.h      |  3 +++
 extmod/vfs_fat_misc.c | 10 +++-------
 stmhal/import.c       |  5 ++---
 4 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/extmod/vfs.c b/extmod/vfs.c
index 2880271c6..0cba0bc58 100644
--- a/extmod/vfs.c
+++ b/extmod/vfs.c
@@ -31,6 +31,7 @@
 #include "py/objstr.h"
 #include "py/mperrno.h"
 #include "extmod/vfs.h"
+#include "extmod/vfs_fat.h"
 
 #if MICROPY_VFS
 
@@ -114,6 +115,12 @@ mp_import_stat_t mp_vfs_import_stat(const char *path) {
     if (vfs == VFS_NONE || vfs == VFS_ROOT) {
         return MP_IMPORT_STAT_NO_EXIST;
     }
+    #if MICROPY_VFS_FAT
+    // fast paths for known VFS types
+    if (mp_obj_get_type(vfs->obj) == &mp_fat_vfs_type) {
+        return fat_vfs_import_stat(MP_OBJ_TO_PTR(vfs->obj), path_out);
+    }
+    #endif
     // TODO delegate to vfs.stat() method
     return MP_IMPORT_STAT_NO_EXIST;
 }
diff --git a/extmod/vfs_fat.h b/extmod/vfs_fat.h
index 52674eb6e..1ea8a9637 100644
--- a/extmod/vfs_fat.h
+++ b/extmod/vfs_fat.h
@@ -24,6 +24,8 @@
  * THE SOFTWARE.
  */
 
+#include "py/lexer.h"
+
 struct _fs_user_mount_t;
 
 extern const byte fresult_to_errno_table[20];
@@ -31,6 +33,7 @@ extern const mp_obj_type_t mp_fat_vfs_type;
 
 struct _fs_user_mount_t *ff_get_vfs(const char **path);
 
+mp_import_stat_t fat_vfs_import_stat(struct _fs_user_mount_t *vfs, const char *path);
 mp_obj_t fatfs_builtin_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
 mp_obj_t fatfs_builtin_open_self(mp_obj_t self_in, mp_obj_t path, mp_obj_t mode);
 MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_open_obj);
diff --git a/extmod/vfs_fat_misc.c b/extmod/vfs_fat_misc.c
index ea267a15f..489e53586 100644
--- a/extmod/vfs_fat_misc.c
+++ b/extmod/vfs_fat_misc.c
@@ -108,21 +108,17 @@ mp_obj_t fat_vfs_listdir2(fs_user_mount_t *vfs, const char *path, bool is_str_ty
     return dir_list;
 }
 
-mp_import_stat_t fat_vfs_import_stat(const char *path);
-
-mp_import_stat_t fat_vfs_import_stat(const char *path) {
+mp_import_stat_t fat_vfs_import_stat(fs_user_mount_t *vfs, const char *path) {
     FILINFO fno;
 #if !MICROPY_FATFS_OO && _USE_LFN
     fno.lfname = NULL;
     fno.lfsize = 0;
 #endif
     #if MICROPY_FATFS_OO
-    fs_user_mount_t *vfs = ff_get_vfs(&path);
-    if (vfs == NULL) {
-        return MP_IMPORT_STAT_NO_EXIST;
-    }
+    assert(vfs != NULL);
     FRESULT res = f_stat(&vfs->fatfs, path, &fno);
     #else
+    (void)vfs;
     FRESULT res = f_stat(path, &fno);
     #endif
     if (res == FR_OK) {
diff --git a/stmhal/import.c b/stmhal/import.c
index 1edbe2caa..2bc282e7b 100644
--- a/stmhal/import.c
+++ b/stmhal/import.c
@@ -28,9 +28,8 @@
 
 #include "py/lexer.h"
 #include "lib/fatfs/ff.h"
-
-mp_import_stat_t fat_vfs_import_stat(const char *path);
+#include "extmod/vfs_fat.h"
 
 mp_import_stat_t mp_import_stat(const char *path) {
-    return fat_vfs_import_stat(path);
+    return fat_vfs_import_stat(NULL, path);
 }
-- 
GitLab