Skip to content

Commit ae4a077

Browse files
committed
extmod/vfs_fat: Move ilistdir implementation from misc to main file.
The fat_vfs_ilistdir2() function was only used by fat_vfs_ilistdir_func() so moving the former into the same file as the latter allows it to be placed directly into the latter function, thus saving code size.
1 parent 989fc16 commit ae4a077

3 files changed

Lines changed: 57 additions & 62 deletions

File tree

extmod/vfs_fat.c

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,52 @@ STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(fat_vfs_mkfs_obj, MP_ROM_PTR(&fat_vfs_mk
109109

110110
STATIC MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_open_obj, fatfs_builtin_open_self);
111111

112+
typedef struct _mp_vfs_fat_ilistdir_it_t {
113+
mp_obj_base_t base;
114+
mp_fun_1_t iternext;
115+
bool is_str;
116+
FF_DIR dir;
117+
} mp_vfs_fat_ilistdir_it_t;
118+
119+
STATIC mp_obj_t mp_vfs_fat_ilistdir_it_iternext(mp_obj_t self_in) {
120+
mp_vfs_fat_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in);
121+
122+
for (;;) {
123+
FILINFO fno;
124+
FRESULT res = f_readdir(&self->dir, &fno);
125+
char *fn = fno.fname;
126+
if (res != FR_OK || fn[0] == 0) {
127+
// stop on error or end of dir
128+
break;
129+
}
130+
131+
// Note that FatFS already filters . and .., so we don't need to
132+
133+
// make 3-tuple with info about this entry
134+
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
135+
if (self->is_str) {
136+
t->items[0] = mp_obj_new_str(fn, strlen(fn));
137+
} else {
138+
t->items[0] = mp_obj_new_bytes((const byte*)fn, strlen(fn));
139+
}
140+
if (fno.fattrib & AM_DIR) {
141+
// dir
142+
t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFDIR);
143+
} else {
144+
// file
145+
t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFREG);
146+
}
147+
t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // no inode number
148+
149+
return MP_OBJ_FROM_PTR(t);
150+
}
151+
152+
// ignore error because we may be closing a second time
153+
f_closedir(&self->dir);
154+
155+
return MP_OBJ_STOP_ITERATION;
156+
}
157+
112158
STATIC mp_obj_t fat_vfs_ilistdir_func(size_t n_args, const mp_obj_t *args) {
113159
mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(args[0]);
114160
bool is_str_type = true;
@@ -122,7 +168,17 @@ STATIC mp_obj_t fat_vfs_ilistdir_func(size_t n_args, const mp_obj_t *args) {
122168
path = "";
123169
}
124170

125-
return fat_vfs_ilistdir2(self, path, is_str_type);
171+
// Create a new iterator object to list the dir
172+
mp_vfs_fat_ilistdir_it_t *iter = m_new_obj(mp_vfs_fat_ilistdir_it_t);
173+
iter->base.type = &mp_type_polymorph_iter;
174+
iter->iternext = mp_vfs_fat_ilistdir_it_iternext;
175+
iter->is_str = is_str_type;
176+
FRESULT res = f_opendir(&self->fatfs, &iter->dir, path);
177+
if (res != FR_OK) {
178+
mp_raise_OSError(fresult_to_errno_table[res]);
179+
}
180+
181+
return MP_OBJ_FROM_PTR(iter);
126182
}
127183
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fat_vfs_ilistdir_obj, 1, 2, fat_vfs_ilistdir_func);
128184

extmod/vfs_fat.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,4 @@ mp_import_stat_t fat_vfs_import_stat(struct _fs_user_mount_t *vfs, const char *p
6060
mp_obj_t fatfs_builtin_open_self(mp_obj_t self_in, mp_obj_t path, mp_obj_t mode);
6161
MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_open_obj);
6262

63-
mp_obj_t fat_vfs_ilistdir2(struct _fs_user_mount_t *vfs, const char *path, bool is_str_type);
64-
6563
#endif // MICROPY_INCLUDED_EXTMOD_VFS_FAT_H

extmod/vfs_fat_misc.c

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -31,65 +31,6 @@
3131
#include "py/runtime.h"
3232
#include "lib/oofatfs/ff.h"
3333
#include "extmod/vfs_fat.h"
34-
#include "py/lexer.h"
35-
36-
typedef struct _mp_vfs_fat_ilistdir_it_t {
37-
mp_obj_base_t base;
38-
mp_fun_1_t iternext;
39-
bool is_str;
40-
FF_DIR dir;
41-
} mp_vfs_fat_ilistdir_it_t;
42-
43-
STATIC mp_obj_t mp_vfs_fat_ilistdir_it_iternext(mp_obj_t self_in) {
44-
mp_vfs_fat_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in);
45-
46-
for (;;) {
47-
FILINFO fno;
48-
FRESULT res = f_readdir(&self->dir, &fno);
49-
char *fn = fno.fname;
50-
if (res != FR_OK || fn[0] == 0) {
51-
// stop on error or end of dir
52-
break;
53-
}
54-
55-
// Note that FatFS already filters . and .., so we don't need to
56-
57-
// make 3-tuple with info about this entry
58-
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
59-
if (self->is_str) {
60-
t->items[0] = mp_obj_new_str(fn, strlen(fn));
61-
} else {
62-
t->items[0] = mp_obj_new_bytes((const byte*)fn, strlen(fn));
63-
}
64-
if (fno.fattrib & AM_DIR) {
65-
// dir
66-
t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFDIR);
67-
} else {
68-
// file
69-
t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFREG);
70-
}
71-
t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // no inode number
72-
73-
return MP_OBJ_FROM_PTR(t);
74-
}
75-
76-
// ignore error because we may be closing a second time
77-
f_closedir(&self->dir);
78-
79-
return MP_OBJ_STOP_ITERATION;
80-
}
81-
82-
mp_obj_t fat_vfs_ilistdir2(fs_user_mount_t *vfs, const char *path, bool is_str_type) {
83-
mp_vfs_fat_ilistdir_it_t *iter = m_new_obj(mp_vfs_fat_ilistdir_it_t);
84-
iter->base.type = &mp_type_polymorph_iter;
85-
iter->iternext = mp_vfs_fat_ilistdir_it_iternext;
86-
iter->is_str = is_str_type;
87-
FRESULT res = f_opendir(&vfs->fatfs, &iter->dir, path);
88-
if (res != FR_OK) {
89-
mp_raise_OSError(fresult_to_errno_table[res]);
90-
}
91-
return MP_OBJ_FROM_PTR(iter);
92-
}
9334

9435
mp_import_stat_t fat_vfs_import_stat(fs_user_mount_t *vfs, const char *path) {
9536
FILINFO fno;

0 commit comments

Comments
 (0)