tools/mpremote: Add recursive remove functionality to filesystem cmds.

mpremote now supports `mpremote rm -r`.

Addresses #9802 and #16845.

Signed-off-by: Jos Verlinde <Jos_Verlinde@hotmail.com>
This commit is contained in:
Jos Verlinde 2025-04-07 23:01:06 +02:00 committed by Damien George
parent 037f2dad72
commit 1aa9b3d94b
3 changed files with 33 additions and 3 deletions

View file

@ -1,4 +1,5 @@
import binascii
import errno
import hashlib
import os
import sys
@ -300,6 +301,28 @@ def do_filesystem_recursive_cp(state, src, dest, multiple, check_hash):
do_filesystem_cp(state, src_path_joined, dest_path_joined, False, check_hash)
def do_filesystem_recursive_rm(state, path, args):
if state.transport.fs_isdir(path):
for entry in state.transport.fs_listdir(path):
do_filesystem_recursive_rm(state, _remote_path_join(path, entry.name), args)
if path:
try:
state.transport.fs_rmdir(path)
if args.verbose:
print(f"removed directory: '{path}'")
except OSError as e:
if e.errno != errno.EINVAL: # not vfs mountpoint
raise CommandError(
f"rm -r: cannot remove :{path} {os.strerror(e.errno) if e.errno else ''}"
) from e
if args.verbose:
print(f"skipped: '{path}' (vfs mountpoint)")
else:
state.transport.fs_rmfile(path)
if args.verbose:
print(f"removed: '{path}'")
def do_filesystem(state, args):
state.ensure_raw_repl()
state.did_action()
@ -352,7 +375,10 @@ def do_filesystem(state, args):
elif command == "mkdir":
state.transport.fs_mkdir(path)
elif command == "rm":
state.transport.fs_rmfile(path)
if args.recursive:
do_filesystem_recursive_rm(state, path, args)
else:
state.transport.fs_rmfile(path)
elif command == "rmdir":
state.transport.fs_rmdir(path)
elif command == "touch":

View file

@ -182,7 +182,7 @@ def argparse_rtc():
def argparse_filesystem():
cmd_parser = argparse.ArgumentParser(description="execute filesystem commands on the device")
_bool_flag(cmd_parser, "recursive", "r", False, "recursive copy (for cp command only)")
_bool_flag(cmd_parser, "recursive", "r", False, "recursive (for cp and rm commands)")
_bool_flag(
cmd_parser,
"force",

View file

@ -24,7 +24,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import ast, hashlib, os, sys
import ast, errno, hashlib, os, sys
from collections import namedtuple
@ -63,6 +63,10 @@ def _convert_filesystem_error(e, info):
return FileExistsError(info)
if "OSError" in e.error_output and "ENODEV" in e.error_output:
return FileNotFoundError(info)
if "OSError" in e.error_output and "EINVAL" in e.error_output:
return OSError(errno.EINVAL, info)
if "OSError" in e.error_output and "EPERM" in e.error_output:
return OSError(errno.EPERM, info)
return e