From cd6d189f48ccb02761ee286e1254c764ee732d7f Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 28 Feb 2016 17:17:24 +0200 Subject: [PATCH] extmod/vfs_fat: Move listdir() method from stmhal for reuse. --- extmod/vfs_fat.c | 17 ++++++++ extmod/vfs_fat_file.h | 2 + extmod/vfs_fat_misc.c | 97 +++++++++++++++++++++++++++++++++++++++++++ py/py.mk | 1 + py/qstrdefs.h | 1 + stmhal/moduos.c | 56 +------------------------ 6 files changed, 119 insertions(+), 55 deletions(-) create mode 100644 extmod/vfs_fat_misc.c diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index b408dca0c..77662ba98 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -58,9 +58,26 @@ STATIC mp_obj_t fat_vfs_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwar } MP_DEFINE_CONST_FUN_OBJ_KW(fat_vfs_open_obj, 2, fat_vfs_open); +STATIC mp_obj_t fat_vfs_listdir_func(size_t n_args, const mp_obj_t *args) { + bool is_str_type = true; + const char *path; + if (n_args == 2) { + if (mp_obj_get_type(args[1]) == &mp_type_bytes) { + is_str_type = false; + } + path = mp_obj_str_get_str(args[1]); + } else { + path = ""; + } + + return fat_vfs_listdir(path, is_str_type); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fat_vfs_listdir_obj, 1, 2, fat_vfs_listdir_func); + STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_mkfs), MP_ROM_PTR(&fat_vfs_mkfs_obj) }, { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&fat_vfs_open_obj) }, + { MP_ROM_QSTR(MP_QSTR_listdir), MP_ROM_PTR(&fat_vfs_listdir_obj) }, }; STATIC MP_DEFINE_CONST_DICT(fat_vfs_locals_dict, fat_vfs_locals_dict_table); diff --git a/extmod/vfs_fat_file.h b/extmod/vfs_fat_file.h index e62fd826f..6d6888955 100644 --- a/extmod/vfs_fat_file.h +++ b/extmod/vfs_fat_file.h @@ -28,3 +28,5 @@ extern const byte fresult_to_errno_table[20]; mp_obj_t fatfs_builtin_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_open_obj); + +mp_obj_t fat_vfs_listdir(const char *path, bool is_str_type); diff --git a/extmod/vfs_fat_misc.c b/extmod/vfs_fat_misc.c new file mode 100644 index 000000000..f9f49b0d3 --- /dev/null +++ b/extmod/vfs_fat_misc.c @@ -0,0 +1,97 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2014 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/mpconfig.h" +#if MICROPY_VFS_FAT || MICROPY_FSUSERMOUNT + +#include +#include "py/nlr.h" +#include "py/runtime.h" +#include "lib/fatfs/ff.h" +#include "lib/fatfs/diskio.h" +#include "extmod/vfs_fat_file.h" +#include "fsusermount.h" + +#if _USE_LFN +STATIC char lfn[_MAX_LFN + 1]; /* Buffer to store the LFN */ +#endif + +// TODO: actually, the core function should be ilistdir() +mp_obj_t fat_vfs_listdir(const char *path, bool is_str_type) { + FRESULT res; + FILINFO fno; + DIR dir; +#if _USE_LFN + fno.lfname = lfn; + fno.lfsize = sizeof lfn; +#endif + + res = f_opendir(&dir, path); /* Open the directory */ + if (res != FR_OK) { + // TODO should be mp_type_FileNotFoundError + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "No such file or directory: '%s'", path)); + } + + mp_obj_t dir_list = mp_obj_new_list(0, NULL); + + for (;;) { + res = f_readdir(&dir, &fno); /* Read a directory item */ + if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */ + if (fno.fname[0] == '.' && fno.fname[1] == 0) continue; /* Ignore . entry */ + if (fno.fname[0] == '.' && fno.fname[1] == '.' && fno.fname[2] == 0) continue; /* Ignore .. entry */ + +#if _USE_LFN + char *fn = *fno.lfname ? fno.lfname : fno.fname; +#else + char *fn = fno.fname; +#endif + + /* + if (fno.fattrib & AM_DIR) { + // dir + } else { + // file + } + */ + + // make a string object for this entry + mp_obj_t entry_o; + if (is_str_type) { + entry_o = mp_obj_new_str(fn, strlen(fn), false); + } else { + entry_o = mp_obj_new_bytes((const byte*)fn, strlen(fn)); + } + + // add the entry to the list + mp_obj_list_append(dir_list, entry_o); + } + + f_closedir(&dir); + + return dir_list; +} + +#endif // MICROPY_VFS_FAT diff --git a/py/py.mk b/py/py.mk index bbcd82566..9cf731b99 100644 --- a/py/py.mk +++ b/py/py.mk @@ -174,6 +174,7 @@ PY_O_BASENAME = \ ../extmod/vfs_fat_ffconf.o \ ../extmod/vfs_fat_diskio.o \ ../extmod/vfs_fat_file.o \ + ../extmod/vfs_fat_misc.o \ ../extmod/moduos_dupterm.o \ # prepend the build destination prefix to the py object files diff --git a/py/qstrdefs.h b/py/qstrdefs.h index ac0703a52..93eca3e82 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -718,6 +718,7 @@ Q(mount) Q(umount) Q(readonly) Q(mkfs) +Q(listdir) Q(readblocks) Q(writeblocks) Q(ioctl) diff --git a/stmhal/moduos.c b/stmhal/moduos.c index 75cb8986d..310b8d44f 100644 --- a/stmhal/moduos.c +++ b/stmhal/moduos.c @@ -54,10 +54,6 @@ /// On boot up, the current directory is `/flash` if no SD card is inserted, /// otherwise it is `/sd`. -#if _USE_LFN -static char lfn[_MAX_LFN + 1]; /* Buffer to store the LFN */ -#endif - STATIC const qstr os_uname_info_fields[] = { MP_QSTR_sysname, MP_QSTR_nodename, MP_QSTR_release, MP_QSTR_version, MP_QSTR_machine @@ -144,57 +140,7 @@ STATIC mp_obj_t os_listdir(mp_uint_t n_args, const mp_obj_t *args) { return dir_list; } - FRESULT res; - FILINFO fno; - DIR dir; -#if _USE_LFN - fno.lfname = lfn; - fno.lfsize = sizeof lfn; -#endif - - res = f_opendir(&dir, path); /* Open the directory */ - if (res != FR_OK) { - // TODO should be mp_type_FileNotFoundError - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "No such file or directory: '%s'", path)); - } - - mp_obj_t dir_list = mp_obj_new_list(0, NULL); - - for (;;) { - res = f_readdir(&dir, &fno); /* Read a directory item */ - if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */ - if (fno.fname[0] == '.' && fno.fname[1] == 0) continue; /* Ignore . entry */ - if (fno.fname[0] == '.' && fno.fname[1] == '.' && fno.fname[2] == 0) continue; /* Ignore .. entry */ - -#if _USE_LFN - char *fn = *fno.lfname ? fno.lfname : fno.fname; -#else - char *fn = fno.fname; -#endif - - /* - if (fno.fattrib & AM_DIR) { - // dir - } else { - // file - } - */ - - // make a string object for this entry - mp_obj_t entry_o; - if (is_str_type) { - entry_o = mp_obj_new_str(fn, strlen(fn), false); - } else { - entry_o = mp_obj_new_bytes((const byte*)fn, strlen(fn)); - } - - // add the entry to the list - mp_obj_list_append(dir_list, entry_o); - } - - f_closedir(&dir); - - return dir_list; + return fat_vfs_listdir(path, is_str_type); } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_listdir_obj, 0, 1, os_listdir);