tools/mpremote: Support trailing slash on dest for non-recursive copy.

This fixes a regression in db59e55fe7a0b67d3af868990468e7b8056afe42: prior
to that commit `mpremote` supported trailing slashes on the destination of
a normal (non-recursive) copy.

Add back support for that, with the semantics that a trailing slash
requires the destination to be an existing directory.

Also add a test for this.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2024-11-06 12:45:06 +11:00
parent 159b54b7da
commit 4e78c611b4
3 changed files with 20 additions and 2 deletions

View File

@ -129,8 +129,15 @@ def _remote_path_basename(a):
def do_filesystem_cp(state, src, dest, multiple, check_hash=False): def do_filesystem_cp(state, src, dest, multiple, check_hash=False):
if dest.startswith(":"): if dest.startswith(":"):
dest_exists = state.transport.fs_exists(dest[1:]) dest_no_slash = dest.rstrip("/" + os.path.sep + (os.path.altsep or ""))
dest_isdir = dest_exists and state.transport.fs_isdir(dest[1:]) dest_exists = state.transport.fs_exists(dest_no_slash[1:])
dest_isdir = dest_exists and state.transport.fs_isdir(dest_no_slash[1:])
# A trailing / on dest forces it to be a directory.
if dest != dest_no_slash:
if not dest_isdir:
raise CommandError("cp: destination is not a directory")
dest = dest_no_slash
else: else:
dest_exists = os.path.exists(dest) dest_exists = os.path.exists(dest)
dest_isdir = dest_exists and os.path.isdir(dest) dest_isdir = dest_exists and os.path.isdir(dest)

View File

@ -71,6 +71,11 @@ echo -----
$MPREMOTE resume cp -f "${TMP}/a.py" :aaa $MPREMOTE resume cp -f "${TMP}/a.py" :aaa
$MPREMOTE resume cat :aaa/a.py $MPREMOTE resume cat :aaa/a.py
# Test cp where the destination has a trailing /.
echo -----
$MPREMOTE resume cp "${TMP}/a.py" :aaa/
$MPREMOTE resume cp "${TMP}/a.py" :aaa/a.py/ || echo "expect error"
echo ----- echo -----
$MPREMOTE resume rm :b.py c.py $MPREMOTE resume rm :b.py c.py
$MPREMOTE resume ls $MPREMOTE resume ls

View File

@ -42,6 +42,12 @@ cp ${TMP}/a.py :aaa
print("Hello") print("Hello")
print("World") print("World")
----- -----
cp ${TMP}/a.py :aaa/
Up to date: aaa/a.py
cp ${TMP}/a.py :aaa/a.py/
mpremote: cp: destination is not a directory
expect error
-----
rm :b.py rm :b.py
rm :c.py rm :c.py
ls : ls :