extmod/vfs_posix: Add size field to ilistdir() entries.#19275
Conversation
Previously the POSIX ilistdir() iterator returned 3-tuples (name, type, inode), while the MCU VFS implementations (FAT, LFS, ROM) return 4-tuples that also include the entry size. This caused mpremote's ls/tree commands to display zero for all file sizes when talking to a MicroPython Unix port. This change makes vfs_posix return 4-tuples (name, type, inode, size), matching the other VFS implementations. The size is retrieved via a stat() call on each entry, with the directory path cached in the iterator state to avoid recomputation. If stat() fails the size is reported as 0, consistent with how MCU ports treat missing size info. Fixes micropython#18661. Signed-off-by: Finn Glas <finn@finnmglas.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #19275 +/- ##
=======================================
Coverage 98.47% 98.47%
=======================================
Files 176 176
Lines 22845 22862 +17
=======================================
+ Hits 22497 22514 +17
Misses 348 348 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Code size report: |
|
Thanks for the contribution. The main downside of this change is the performance impact: each iteration through Might be worth measuring the actual performance impact of this change on a large directory listing, to see how significant it is. |
|
Options to mitigate that perf hit A. fstatat(dirfd(self->dir), fn, ...) instead of stat(full_path, ...) - maybe a bit faster Not sure |

Summary
Previously the POSIX ilistdir() iterator returned 3-tuples (name, type, inode), while the MCU VFS implementations (FAT, LFS, ROM) return 4-tuples that also include the entry size. This caused mpremote's ls/tree commands to display zero for all file sizes when talking to a MicroPython Unix port.
This change makes vfs_posix return 4-tuples (name, type, inode, size), matching the other VFS implementations. The size is retrieved via a stat() call on each entry, with the directory path cached in the iterator state to avoid recomputation. If stat() fails the size is reported as 0, consistent with how MCU ports treat missing size info.
Fixes #18661.
Testing
tests/extmod/vfs_basic.py,vfs_posix_paths.py,vfs_posix_enoent.py,vfs_posix_ilistdir_filter.py, andvfs_posix_ilistdir_del.pyallpass.
tests/extmod/suite is unaffected by this change (the onlyfailure,
select_poll_fd.py, also fails on master and is unrelated).paths still return
bytesnames, that mountedvfs.VfsPosixworks,and that a nonexistent directory still raises
OSError.Generative AI
I used generative AI tools when creating this PR, but a human has checked the
code and is responsible for the code and the description above.