From 0f83ef395cccaa771543450ff8a4200da108e3d6 Mon Sep 17 00:00:00 2001 From: robert Date: Mon, 4 May 2020 15:34:12 +0200 Subject: [PATCH] extmod/vfs_lfsx: Fix rename to respect cur dir for new path. If the new name start with '/', cur_dir is not prepened any more, so that the current working directory is respected. And extend the test cases for rename to cover this functionality. --- extmod/vfs_lfsx.c | 7 +++++-- tests/extmod/vfs_lfs.py | 12 +++++++++--- tests/extmod/vfs_lfs.py.exp | 10 +++++++--- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/extmod/vfs_lfsx.c b/extmod/vfs_lfsx.c index 5dcf951bc7..843e706da4 100644 --- a/extmod/vfs_lfsx.c +++ b/extmod/vfs_lfsx.c @@ -236,10 +236,13 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(MP_VFS_LFSx(rmdir_obj), MP_VFS_LFSx(rmdir)); STATIC mp_obj_t MP_VFS_LFSx(rename)(mp_obj_t self_in, mp_obj_t path_old_in, mp_obj_t path_new_in) { MP_OBJ_VFS_LFSx *self = MP_OBJ_TO_PTR(self_in); const char *path_old = MP_VFS_LFSx(make_path)(self, path_old_in); + const char *path = mp_obj_str_get_str(path_new_in); vstr_t path_new; vstr_init(&path_new, vstr_len(&self->cur_dir)); - vstr_add_strn(&path_new, vstr_str(&self->cur_dir), vstr_len(&self->cur_dir)); - vstr_add_str(&path_new, mp_obj_str_get_str(path_new_in)); + if (path[0] != '/') { + vstr_add_strn(&path_new, vstr_str(&self->cur_dir), vstr_len(&self->cur_dir)); + } + vstr_add_str(&path_new, path); int ret = LFSx_API(rename)(&self->lfs, path_old, vstr_null_terminated_str(&path_new)); vstr_clear(&path_new); if (ret < 0) { diff --git a/tests/extmod/vfs_lfs.py b/tests/extmod/vfs_lfs.py index 82c8fead5a..609d9f949e 100644 --- a/tests/extmod/vfs_lfs.py +++ b/tests/extmod/vfs_lfs.py @@ -63,7 +63,7 @@ def test(bdev, vfs_class): # mkdir, rmdir vfs.mkdir("testdir") print(list(vfs.ilistdir())) - print(list(vfs.ilistdir("testdir"))) + print(sorted(list(vfs.ilistdir("testdir")))) vfs.rmdir("testdir") print(list(vfs.ilistdir())) vfs.mkdir("testdir") @@ -91,11 +91,17 @@ def test(bdev, vfs_class): # rename vfs.rename("testbig", "testbig2") - print(list(vfs.ilistdir())) + print(sorted(list(vfs.ilistdir()))) + vfs.chdir("testdir") + vfs.rename("/testbig2", "testbig2") + print(sorted(list(vfs.ilistdir()))) + vfs.rename("testbig2", "/testbig2") + vfs.chdir("/") + print(sorted(list(vfs.ilistdir()))) # remove vfs.remove("testbig2") - print(list(vfs.ilistdir())) + print(sorted(list(vfs.ilistdir()))) # getcwd, chdir vfs.mkdir("/testdir2") diff --git a/tests/extmod/vfs_lfs.py.exp b/tests/extmod/vfs_lfs.py.exp index 4cfd74b474..a702557744 100644 --- a/tests/extmod/vfs_lfs.py.exp +++ b/tests/extmod/vfs_lfs.py.exp @@ -16,7 +16,9 @@ write 1 write 2 write 3 (1024, 1024, 30, 6, 6, 0, 0, 0, 0, 255) -[('test', 32768, 0, 8), ('testdir', 16384, 0, 0), ('testbig2', 32768, 0, 16384)] +[('test', 32768, 0, 8), ('testbig2', 32768, 0, 16384), ('testdir', 16384, 0, 0)] +[('testbig2', 32768, 0, 16384)] +[('test', 32768, 0, 8), ('testbig2', 32768, 0, 16384), ('testdir', 16384, 0, 0)] [('test', 32768, 0, 8), ('testdir', 16384, 0, 0)] / /testdir @@ -51,8 +53,10 @@ write 1 write 2 write 3 (1024, 1024, 30, 7, 7, 0, 0, 0, 0, 255) -[('testbig2', 32768, 0, 16384), ('testdir', 16384, 0, 0), ('test', 32768, 0, 8)] -[('testdir', 16384, 0, 0), ('test', 32768, 0, 8)] +[('test', 32768, 0, 8), ('testbig2', 32768, 0, 16384), ('testdir', 16384, 0, 0)] +[('testbig2', 32768, 0, 16384)] +[('test', 32768, 0, 8), ('testbig2', 32768, 0, 16384), ('testdir', 16384, 0, 0)] +[('test', 32768, 0, 8), ('testdir', 16384, 0, 0)] / /testdir (32768, 0, 0, 0, 0, 0, 0, 0, 0, 0)