From d9467b07cb036dfc0d54f36fd74356001881d154 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 24 Apr 2025 13:15:40 +1000 Subject: [PATCH] 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 --- tools/mpremote/mpremote/main.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/tools/mpremote/mpremote/main.py b/tools/mpremote/mpremote/main.py index f8c913d26..d122e93c0 100644 --- a/tools/mpremote/mpremote/main.py +++ b/tools/mpremote/mpremote/main.py @@ -379,7 +379,27 @@ _BUILTIN_COMMAND_EXPANSIONS = { # Disk used/free. "df": [ "exec", - "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')", + """ +import os,vfs +_f = "{:<10}{:>9}{:>9}{:>9}{:>5} {}" +print(_f.format("filesystem", "size", "used", "avail", "use%", "mounted on")) +try: + _ms = vfs.mount() +except: + _ms = [] + for _m in [""] + os.listdir("/"): + _m = "/" + _m + _s = os.stat(_m) + if _s[0] & 1 << 14: + _ms.append(("",_m)) +for _v,_p in _ms: + _s = os.statvfs(_p) + _sz = _s[0]*_s[2] + if _sz: + _av = _s[0]*_s[3] + _us = 100*(_sz-_av)//_sz + print(_f.format(str(_v), _sz, _sz-_av, _av, _us, _p)) +""", ], # Other shortcuts. "reset": {