From a95ae06badb9bdc852bb1eddcd6b4fe82c9ebba7 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Wed, 11 Oct 2017 23:23:49 +0200 Subject: [PATCH] extmod/vfs: Implement import from non-FAT filesystems. --- extmod/vfs.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/extmod/vfs.c b/extmod/vfs.c index a1cd8d5f67578..e02a2e68898a7 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -125,8 +125,39 @@ mp_import_stat_t mp_vfs_import_stat(const char *path) { 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; + + mp_obj_t dest[3]; + dest[2] = mp_obj_new_str(path_out, strlen(path_out), false); + mp_load_method_maybe(vfs->obj, MP_QSTR_stat, dest); + if (dest[0] == MP_OBJ_NULL) { + return MP_IMPORT_STAT_NO_EXIST; + } + + mp_obj_t st; + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + st = mp_call_method_n_kw(1, 0, dest); + nlr_pop(); + } else { + // Uncaught exception - file doesn't exist? + return MP_IMPORT_STAT_NO_EXIST; + } + + mp_obj_tuple_t *t = MP_OBJ_TO_PTR(st); + if (t->base.type != &mp_type_tuple || t->len != 10) { + // Not a valid tuple? + return MP_IMPORT_STAT_NO_EXIST; + } + + mp_int_t filetype = mp_obj_get_int(t->items[0]); + if (filetype & MP_S_IFDIR) { + return MP_IMPORT_STAT_DIR; + } else if (filetype & MP_S_IFREG) { + return MP_IMPORT_STAT_FILE; + } else { + // some other file type + return MP_IMPORT_STAT_NO_EXIST; + } } mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {