From 22806ed5df27c10131af0cedb2f7e8b134fe6e7a Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 28 May 2020 12:50:44 +1000 Subject: [PATCH] extmod/vfs: Retain previous working directory if chdir fails. Fixes issue #6069. --- extmod/vfs.c | 3 ++- tests/extmod/vfs_basic.py | 20 +++++++++++++++++++- tests/extmod/vfs_basic.py.exp | 6 ++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/extmod/vfs.c b/extmod/vfs.c index 79a8e8509d..d1291068ad 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -322,7 +322,6 @@ MP_DEFINE_CONST_FUN_OBJ_KW(mp_vfs_open_obj, 0, mp_vfs_open); mp_obj_t mp_vfs_chdir(mp_obj_t path_in) { mp_obj_t path_out; mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out); - MP_STATE_VM(vfs_cur) = vfs; if (vfs == MP_VFS_ROOT) { // If we change to the root dir and a VFS is mounted at the root then // we must change that VFS's current dir to the root dir so that any @@ -334,9 +333,11 @@ mp_obj_t mp_vfs_chdir(mp_obj_t path_in) { break; } } + vfs = MP_VFS_ROOT; } else { mp_vfs_proxy_call(vfs, MP_QSTR_chdir, 1, &path_out); } + MP_STATE_VM(vfs_cur) = vfs; return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_chdir_obj, mp_vfs_chdir); diff --git a/tests/extmod/vfs_basic.py b/tests/extmod/vfs_basic.py index c8e203b3df..62b2a27738 100644 --- a/tests/extmod/vfs_basic.py +++ b/tests/extmod/vfs_basic.py @@ -10,8 +10,9 @@ except (ImportError, AttributeError): class Filesystem: - def __init__(self, id): + def __init__(self, id, fail=0): self.id = id + self.fail = fail def mount(self, readonly, mkfs): print(self.id, "mount", readonly, mkfs) @@ -25,6 +26,8 @@ class Filesystem: def chdir(self, dir): print(self.id, "chdir", dir) + if self.fail: + raise OSError(self.fail) def getcwd(self): print(self.id, "getcwd") @@ -158,3 +161,18 @@ uos.chdir("/") uos.umount("/") print(uos.listdir("/")) uos.umount("/mnt") + +# chdir to a non-existent mount point (current directory should remain unchanged) +try: + uos.chdir("/foo") +except OSError: + print("OSError") +print(uos.getcwd()) + +# chdir to a non-existent subdirectory in a mounted filesystem +uos.mount(Filesystem(5, 1), "/mnt") +try: + uos.chdir("/mnt/subdir") +except OSError: + print("OSError") +print(uos.getcwd()) diff --git a/tests/extmod/vfs_basic.py.exp b/tests/extmod/vfs_basic.py.exp index 0ae2c2cc97..ebca310304 100644 --- a/tests/extmod/vfs_basic.py.exp +++ b/tests/extmod/vfs_basic.py.exp @@ -58,3 +58,9 @@ OSError 3 umount ['mnt'] 4 umount +OSError +/ +5 mount False False +5 chdir /subdir +OSError +/