Skip to content

Commit 0f83ef3

Browse files
robert-hhdpgeorge
authored andcommitted
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.
1 parent d3ea28d commit 0f83ef3

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

extmod/vfs_lfsx.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,13 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(MP_VFS_LFSx(rmdir_obj), MP_VFS_LFSx(rmdir));
236236
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) {
237237
MP_OBJ_VFS_LFSx *self = MP_OBJ_TO_PTR(self_in);
238238
const char *path_old = MP_VFS_LFSx(make_path)(self, path_old_in);
239+
const char *path = mp_obj_str_get_str(path_new_in);
239240
vstr_t path_new;
240241
vstr_init(&path_new, vstr_len(&self->cur_dir));
241-
vstr_add_strn(&path_new, vstr_str(&self->cur_dir), vstr_len(&self->cur_dir));
242-
vstr_add_str(&path_new, mp_obj_str_get_str(path_new_in));
242+
if (path[0] != '/') {
243+
vstr_add_strn(&path_new, vstr_str(&self->cur_dir), vstr_len(&self->cur_dir));
244+
}
245+
vstr_add_str(&path_new, path);
243246
int ret = LFSx_API(rename)(&self->lfs, path_old, vstr_null_terminated_str(&path_new));
244247
vstr_clear(&path_new);
245248
if (ret < 0) {

tests/extmod/vfs_lfs.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test(bdev, vfs_class):
6363
# mkdir, rmdir
6464
vfs.mkdir("testdir")
6565
print(list(vfs.ilistdir()))
66-
print(list(vfs.ilistdir("testdir")))
66+
print(sorted(list(vfs.ilistdir("testdir"))))
6767
vfs.rmdir("testdir")
6868
print(list(vfs.ilistdir()))
6969
vfs.mkdir("testdir")
@@ -91,11 +91,17 @@ def test(bdev, vfs_class):
9191

9292
# rename
9393
vfs.rename("testbig", "testbig2")
94-
print(list(vfs.ilistdir()))
94+
print(sorted(list(vfs.ilistdir())))
95+
vfs.chdir("testdir")
96+
vfs.rename("/testbig2", "testbig2")
97+
print(sorted(list(vfs.ilistdir())))
98+
vfs.rename("testbig2", "/testbig2")
99+
vfs.chdir("/")
100+
print(sorted(list(vfs.ilistdir())))
95101

96102
# remove
97103
vfs.remove("testbig2")
98-
print(list(vfs.ilistdir()))
104+
print(sorted(list(vfs.ilistdir())))
99105

100106
# getcwd, chdir
101107
vfs.mkdir("/testdir2")

tests/extmod/vfs_lfs.py.exp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ write 1
1616
write 2
1717
write 3
1818
(1024, 1024, 30, 6, 6, 0, 0, 0, 0, 255)
19-
[('test', 32768, 0, 8), ('testdir', 16384, 0, 0), ('testbig2', 32768, 0, 16384)]
19+
[('test', 32768, 0, 8), ('testbig2', 32768, 0, 16384), ('testdir', 16384, 0, 0)]
20+
[('testbig2', 32768, 0, 16384)]
21+
[('test', 32768, 0, 8), ('testbig2', 32768, 0, 16384), ('testdir', 16384, 0, 0)]
2022
[('test', 32768, 0, 8), ('testdir', 16384, 0, 0)]
2123
/
2224
/testdir
@@ -51,8 +53,10 @@ write 1
5153
write 2
5254
write 3
5355
(1024, 1024, 30, 7, 7, 0, 0, 0, 0, 255)
54-
[('testbig2', 32768, 0, 16384), ('testdir', 16384, 0, 0), ('test', 32768, 0, 8)]
55-
[('testdir', 16384, 0, 0), ('test', 32768, 0, 8)]
56+
[('test', 32768, 0, 8), ('testbig2', 32768, 0, 16384), ('testdir', 16384, 0, 0)]
57+
[('testbig2', 32768, 0, 16384)]
58+
[('test', 32768, 0, 8), ('testbig2', 32768, 0, 16384), ('testdir', 16384, 0, 0)]
59+
[('test', 32768, 0, 8), ('testdir', 16384, 0, 0)]
5660
/
5761
/testdir
5862
(32768, 0, 0, 0, 0, 0, 0, 0, 0, 0)

0 commit comments

Comments
 (0)