Skip to content

Commit d9467b0

Browse files
committed
tools/mpremote: Improve df command to use new no-arg vfs.mount() query.
The existing `mpremote df` command is not very good, because it needs to assume that all directories in the root directory are mount points, and also doesn't correctly stat filesystems when the current directory is not the root. This leads to wrong results With the introduction of `vfs.mount()` to return a list of mounted filesystems and their path, a much better df can be implemented, as done in this commit. The new df will also fall back to using the old approach of listing the root directory if the no-arg `vfs.mount()` query is not supported. Signed-off-by: Damien George <[email protected]>
1 parent 171d751 commit d9467b0

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

tools/mpremote/mpremote/main.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,27 @@ def argparse_none(description):
379379
# Disk used/free.
380380
"df": [
381381
"exec",
382-
"import os\nprint('mount \\tsize \\tused \\tavail \\tuse%')\nfor _m in [''] + os.listdir('/'):\n _s = os.stat('/' + _m)\n if not _s[0] & 1 << 14: continue\n _s = os.statvfs(_m)\n if _s[0]:\n _size = _s[0] * _s[2]; _free = _s[0] * _s[3]; print(_m, _size, _size - _free, _free, int(100 * (_size - _free) / _size), sep='\\t')",
382+
"""
383+
import os,vfs
384+
_f = "{:<10}{:>9}{:>9}{:>9}{:>5} {}"
385+
print(_f.format("filesystem", "size", "used", "avail", "use%", "mounted on"))
386+
try:
387+
_ms = vfs.mount()
388+
except:
389+
_ms = []
390+
for _m in [""] + os.listdir("/"):
391+
_m = "/" + _m
392+
_s = os.stat(_m)
393+
if _s[0] & 1 << 14:
394+
_ms.append(("<unknown>",_m))
395+
for _v,_p in _ms:
396+
_s = os.statvfs(_p)
397+
_sz = _s[0]*_s[2]
398+
if _sz:
399+
_av = _s[0]*_s[3]
400+
_us = 100*(_sz-_av)//_sz
401+
print(_f.format(str(_v), _sz, _sz-_av, _av, _us, _p))
402+
""",
383403
],
384404
# Other shortcuts.
385405
"reset": {

0 commit comments

Comments
 (0)