Skip to content

Commit cd6d189

Browse files
committed
extmod/vfs_fat: Move listdir() method from stmhal for reuse.
1 parent 8a18084 commit cd6d189

6 files changed

Lines changed: 119 additions & 55 deletions

File tree

extmod/vfs_fat.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,26 @@ STATIC mp_obj_t fat_vfs_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwar
5858
}
5959
MP_DEFINE_CONST_FUN_OBJ_KW(fat_vfs_open_obj, 2, fat_vfs_open);
6060

61+
STATIC mp_obj_t fat_vfs_listdir_func(size_t n_args, const mp_obj_t *args) {
62+
bool is_str_type = true;
63+
const char *path;
64+
if (n_args == 2) {
65+
if (mp_obj_get_type(args[1]) == &mp_type_bytes) {
66+
is_str_type = false;
67+
}
68+
path = mp_obj_str_get_str(args[1]);
69+
} else {
70+
path = "";
71+
}
72+
73+
return fat_vfs_listdir(path, is_str_type);
74+
}
75+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fat_vfs_listdir_obj, 1, 2, fat_vfs_listdir_func);
76+
6177
STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = {
6278
{ MP_ROM_QSTR(MP_QSTR_mkfs), MP_ROM_PTR(&fat_vfs_mkfs_obj) },
6379
{ MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&fat_vfs_open_obj) },
80+
{ MP_ROM_QSTR(MP_QSTR_listdir), MP_ROM_PTR(&fat_vfs_listdir_obj) },
6481
};
6582
STATIC MP_DEFINE_CONST_DICT(fat_vfs_locals_dict, fat_vfs_locals_dict_table);
6683

extmod/vfs_fat_file.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ extern const byte fresult_to_errno_table[20];
2828

2929
mp_obj_t fatfs_builtin_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
3030
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_open_obj);
31+
32+
mp_obj_t fat_vfs_listdir(const char *path, bool is_str_type);

extmod/vfs_fat_misc.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2014 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/mpconfig.h"
28+
#if MICROPY_VFS_FAT || MICROPY_FSUSERMOUNT
29+
30+
#include <string.h>
31+
#include "py/nlr.h"
32+
#include "py/runtime.h"
33+
#include "lib/fatfs/ff.h"
34+
#include "lib/fatfs/diskio.h"
35+
#include "extmod/vfs_fat_file.h"
36+
#include "fsusermount.h"
37+
38+
#if _USE_LFN
39+
STATIC char lfn[_MAX_LFN + 1]; /* Buffer to store the LFN */
40+
#endif
41+
42+
// TODO: actually, the core function should be ilistdir()
43+
mp_obj_t fat_vfs_listdir(const char *path, bool is_str_type) {
44+
FRESULT res;
45+
FILINFO fno;
46+
DIR dir;
47+
#if _USE_LFN
48+
fno.lfname = lfn;
49+
fno.lfsize = sizeof lfn;
50+
#endif
51+
52+
res = f_opendir(&dir, path); /* Open the directory */
53+
if (res != FR_OK) {
54+
// TODO should be mp_type_FileNotFoundError
55+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "No such file or directory: '%s'", path));
56+
}
57+
58+
mp_obj_t dir_list = mp_obj_new_list(0, NULL);
59+
60+
for (;;) {
61+
res = f_readdir(&dir, &fno); /* Read a directory item */
62+
if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */
63+
if (fno.fname[0] == '.' && fno.fname[1] == 0) continue; /* Ignore . entry */
64+
if (fno.fname[0] == '.' && fno.fname[1] == '.' && fno.fname[2] == 0) continue; /* Ignore .. entry */
65+
66+
#if _USE_LFN
67+
char *fn = *fno.lfname ? fno.lfname : fno.fname;
68+
#else
69+
char *fn = fno.fname;
70+
#endif
71+
72+
/*
73+
if (fno.fattrib & AM_DIR) {
74+
// dir
75+
} else {
76+
// file
77+
}
78+
*/
79+
80+
// make a string object for this entry
81+
mp_obj_t entry_o;
82+
if (is_str_type) {
83+
entry_o = mp_obj_new_str(fn, strlen(fn), false);
84+
} else {
85+
entry_o = mp_obj_new_bytes((const byte*)fn, strlen(fn));
86+
}
87+
88+
// add the entry to the list
89+
mp_obj_list_append(dir_list, entry_o);
90+
}
91+
92+
f_closedir(&dir);
93+
94+
return dir_list;
95+
}
96+
97+
#endif // MICROPY_VFS_FAT

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ PY_O_BASENAME = \
174174
../extmod/vfs_fat_ffconf.o \
175175
../extmod/vfs_fat_diskio.o \
176176
../extmod/vfs_fat_file.o \
177+
../extmod/vfs_fat_misc.o \
177178
../extmod/moduos_dupterm.o \
178179

179180
# prepend the build destination prefix to the py object files

py/qstrdefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@ Q(mount)
718718
Q(umount)
719719
Q(readonly)
720720
Q(mkfs)
721+
Q(listdir)
721722
Q(readblocks)
722723
Q(writeblocks)
723724
Q(ioctl)

stmhal/moduos.c

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@
5454
/// On boot up, the current directory is `/flash` if no SD card is inserted,
5555
/// otherwise it is `/sd`.
5656

57-
#if _USE_LFN
58-
static char lfn[_MAX_LFN + 1]; /* Buffer to store the LFN */
59-
#endif
60-
6157
STATIC const qstr os_uname_info_fields[] = {
6258
MP_QSTR_sysname, MP_QSTR_nodename,
6359
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) {
144140
return dir_list;
145141
}
146142

147-
FRESULT res;
148-
FILINFO fno;
149-
DIR dir;
150-
#if _USE_LFN
151-
fno.lfname = lfn;
152-
fno.lfsize = sizeof lfn;
153-
#endif
154-
155-
res = f_opendir(&dir, path); /* Open the directory */
156-
if (res != FR_OK) {
157-
// TODO should be mp_type_FileNotFoundError
158-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "No such file or directory: '%s'", path));
159-
}
160-
161-
mp_obj_t dir_list = mp_obj_new_list(0, NULL);
162-
163-
for (;;) {
164-
res = f_readdir(&dir, &fno); /* Read a directory item */
165-
if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */
166-
if (fno.fname[0] == '.' && fno.fname[1] == 0) continue; /* Ignore . entry */
167-
if (fno.fname[0] == '.' && fno.fname[1] == '.' && fno.fname[2] == 0) continue; /* Ignore .. entry */
168-
169-
#if _USE_LFN
170-
char *fn = *fno.lfname ? fno.lfname : fno.fname;
171-
#else
172-
char *fn = fno.fname;
173-
#endif
174-
175-
/*
176-
if (fno.fattrib & AM_DIR) {
177-
// dir
178-
} else {
179-
// file
180-
}
181-
*/
182-
183-
// make a string object for this entry
184-
mp_obj_t entry_o;
185-
if (is_str_type) {
186-
entry_o = mp_obj_new_str(fn, strlen(fn), false);
187-
} else {
188-
entry_o = mp_obj_new_bytes((const byte*)fn, strlen(fn));
189-
}
190-
191-
// add the entry to the list
192-
mp_obj_list_append(dir_list, entry_o);
193-
}
194-
195-
f_closedir(&dir);
196-
197-
return dir_list;
143+
return fat_vfs_listdir(path, is_str_type);
198144
}
199145
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_listdir_obj, 0, 1, os_listdir);
200146

0 commit comments

Comments
 (0)