Skip to content

Commit 4d3a92c

Browse files
tomlogicdpgeorge
authored andcommitted
extmod/vfs_fat: Add file size as 4th element of uos.ilistdir tuple.
1 parent 1345093 commit 4d3a92c

7 files changed

Lines changed: 18 additions & 15 deletions

File tree

docs/library/uos.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,22 @@ Filesystem access
4343

4444
.. function:: ilistdir([dir])
4545

46-
This function returns an iterator which then yields 3-tuples corresponding to
46+
This function returns an iterator which then yields tuples corresponding to
4747
the entries in the directory that it is listing. With no argument it lists the
4848
current directory, otherwise it lists the directory given by *dir*.
4949

50-
The 3-tuples have the form *(name, type, inode)*:
50+
The tuples have the form *(name, type, inode[, size])*:
5151

5252
- *name* is a string (or bytes if *dir* is a bytes object) and is the name of
5353
the entry;
5454
- *type* is an integer that specifies the type of the entry, with 0x4000 for
5555
directories and 0x8000 for regular files;
5656
- *inode* is an integer corresponding to the inode of the file, and may be 0
5757
for filesystems that don't have such a notion.
58+
- Some platforms may return a 4-tuple that includes the entry's *size*. For
59+
file entries, *size* is an integer representing the size of the file
60+
or -1 if unknown. Its meaning is currently undefined for directory
61+
entries.
5862

5963
.. function:: listdir([dir])
6064

extmod/vfs.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,7 @@ mp_obj_t mp_vfs_listdir(size_t n_args, const mp_obj_t *args) {
366366
mp_obj_t dir_list = mp_obj_new_list(0, NULL);
367367
mp_obj_t next;
368368
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
369-
mp_obj_t *items;
370-
mp_obj_get_array_fixed_n(next, 3, &items);
371-
mp_obj_list_append(dir_list, items[0]);
369+
mp_obj_list_append(dir_list, mp_obj_subscr(next, MP_OBJ_NEW_SMALL_INT(0), MP_OBJ_SENTINEL));
372370
}
373371
return dir_list;
374372
}

extmod/vfs_fat.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ STATIC mp_obj_t mp_vfs_fat_ilistdir_it_iternext(mp_obj_t self_in) {
142142

143143
// Note that FatFS already filters . and .., so we don't need to
144144

145-
// make 3-tuple with info about this entry
146-
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
145+
// make 4-tuple with info about this entry
146+
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(4, NULL));
147147
if (self->is_str) {
148148
t->items[0] = mp_obj_new_str(fn, strlen(fn));
149149
} else {
@@ -157,6 +157,7 @@ STATIC mp_obj_t mp_vfs_fat_ilistdir_it_iternext(mp_obj_t self_in) {
157157
t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFREG);
158158
}
159159
t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // no inode number
160+
t->items[3] = mp_obj_new_int_from_uint(fno.fsize);
160161

161162
return MP_OBJ_FROM_PTR(t);
162163
}

tests/extmod/vfs_fat_fileio1.py.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ e
1010
o
1111
d
1212
True
13-
[('foo_dir', 16384, 0)]
13+
[('foo_dir', 16384, 0, 0)]
1414
MemoryError
1515
x0
1616
x1

tests/extmod/vfs_fat_fileio2.py.exp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ True
33
True
44
b'data in file'
55
True
6-
[('sub_file.txt', 32768, 0), ('file.txt', 32768, 0)]
7-
[('foo_dir', 16384, 0), ('moved-to-root.txt', 32768, 0)]
8-
[('foo_dir', 16384, 0), ('moved-to-root.txt', 32768, 0)]
6+
[('sub_file.txt', 32768, 0, 11), ('file.txt', 32768, 0, 12)]
7+
[('foo_dir', 16384, 0, 0), ('moved-to-root.txt', 32768, 0, 12)]
8+
[('foo_dir', 16384, 0, 0), ('moved-to-root.txt', 32768, 0, 8)]
99
new text
10-
[('moved-to-root.txt', 32768, 0)]
10+
[('moved-to-root.txt', 32768, 0, 8)]
1111
ENOSPC: True
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
[('file.txt', 32768, 0)]
1+
[('file.txt', 32768, 0, 6)]
22
hello!
33
[]

tests/extmod/vfs_fat_ramdisk.py.exp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ True
33
statvfs: (512, 512, 16, 16, 16, 0, 0, 0, 0, 255)
44
getcwd: /
55
True
6-
[('foo_file.txt', 32768, 0)]
6+
[('foo_file.txt', 32768, 0, 6)]
77
stat root: (16384, 0, 0, 0, 0, 0, 0, 0, 0, 0)
88
stat file: (32768, 0, 0, 0, 0, 0, 6)
99
True
@@ -12,5 +12,5 @@ getcwd: /foo_dir
1212
[]
1313
True
1414
getcwd: /
15-
[(b'foo_file.txt', 32768, 0), (b'foo_dir', 16384, 0)]
15+
[(b'foo_file.txt', 32768, 0, 6), (b'foo_dir', 16384, 0, 0)]
1616
ENOENT: True

0 commit comments

Comments
 (0)