extmod/vfs_fat: Add file size as 4th element of uos.ilistdir tuple.

This commit is contained in:
Tom Collins 2018-03-08 16:02:26 -08:00 committed by Damien George
parent 1345093401
commit 4d3a92c67c
7 changed files with 18 additions and 15 deletions

View File

@ -43,11 +43,11 @@ Filesystem access
.. function:: ilistdir([dir])
This function returns an iterator which then yields 3-tuples corresponding to
This function returns an iterator which then yields tuples corresponding to
the entries in the directory that it is listing. With no argument it lists the
current directory, otherwise it lists the directory given by *dir*.
The 3-tuples have the form *(name, type, inode)*:
The tuples have the form *(name, type, inode[, size])*:
- *name* is a string (or bytes if *dir* is a bytes object) and is the name of
the entry;
@ -55,6 +55,10 @@ Filesystem access
directories and 0x8000 for regular files;
- *inode* is an integer corresponding to the inode of the file, and may be 0
for filesystems that don't have such a notion.
- Some platforms may return a 4-tuple that includes the entry's *size*. For
file entries, *size* is an integer representing the size of the file
or -1 if unknown. Its meaning is currently undefined for directory
entries.
.. function:: listdir([dir])

View File

@ -366,9 +366,7 @@ mp_obj_t mp_vfs_listdir(size_t n_args, const mp_obj_t *args) {
mp_obj_t dir_list = mp_obj_new_list(0, NULL);
mp_obj_t next;
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
mp_obj_t *items;
mp_obj_get_array_fixed_n(next, 3, &items);
mp_obj_list_append(dir_list, items[0]);
mp_obj_list_append(dir_list, mp_obj_subscr(next, MP_OBJ_NEW_SMALL_INT(0), MP_OBJ_SENTINEL));
}
return dir_list;
}

View File

@ -142,8 +142,8 @@ STATIC mp_obj_t mp_vfs_fat_ilistdir_it_iternext(mp_obj_t self_in) {
// Note that FatFS already filters . and .., so we don't need to
// make 3-tuple with info about this entry
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
// make 4-tuple with info about this entry
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(4, NULL));
if (self->is_str) {
t->items[0] = mp_obj_new_str(fn, strlen(fn));
} else {
@ -157,6 +157,7 @@ STATIC mp_obj_t mp_vfs_fat_ilistdir_it_iternext(mp_obj_t self_in) {
t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFREG);
}
t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // no inode number
t->items[3] = mp_obj_new_int_from_uint(fno.fsize);
return MP_OBJ_FROM_PTR(t);
}

View File

@ -10,7 +10,7 @@ e
o
d
True
[('foo_dir', 16384, 0)]
[('foo_dir', 16384, 0, 0)]
MemoryError
x0
x1

View File

@ -3,9 +3,9 @@ True
True
b'data in file'
True
[('sub_file.txt', 32768, 0), ('file.txt', 32768, 0)]
[('foo_dir', 16384, 0), ('moved-to-root.txt', 32768, 0)]
[('foo_dir', 16384, 0), ('moved-to-root.txt', 32768, 0)]
[('sub_file.txt', 32768, 0, 11), ('file.txt', 32768, 0, 12)]
[('foo_dir', 16384, 0, 0), ('moved-to-root.txt', 32768, 0, 12)]
[('foo_dir', 16384, 0, 0), ('moved-to-root.txt', 32768, 0, 8)]
new text
[('moved-to-root.txt', 32768, 0)]
[('moved-to-root.txt', 32768, 0, 8)]
ENOSPC: True

View File

@ -1,3 +1,3 @@
[('file.txt', 32768, 0)]
[('file.txt', 32768, 0, 6)]
hello!
[]

View File

@ -3,7 +3,7 @@ True
statvfs: (512, 512, 16, 16, 16, 0, 0, 0, 0, 255)
getcwd: /
True
[('foo_file.txt', 32768, 0)]
[('foo_file.txt', 32768, 0, 6)]
stat root: (16384, 0, 0, 0, 0, 0, 0, 0, 0, 0)
stat file: (32768, 0, 0, 0, 0, 0, 6)
True
@ -12,5 +12,5 @@ getcwd: /foo_dir
[]
True
getcwd: /
[(b'foo_file.txt', 32768, 0), (b'foo_dir', 16384, 0)]
[(b'foo_file.txt', 32768, 0, 6), (b'foo_dir', 16384, 0, 0)]
ENOENT: True