From 71adf506ce43b55b859f81f191ac0826928bbdd5 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 22 Sep 2020 15:05:00 +1000 Subject: [PATCH] extmod/vfs: Fix lookup of entry in root dir so it fails correctly. Prior to this commit, uos.chdir('/') followed by uos.stat('noexist') would succeed that stat even though the entry did not exist (some other functions like listdir would have similar issues). This is because, if the current directory was the root and the path was relative, mp_vfs_lookup_path would return success for bad paths. Signed-off-by: Damien George --- extmod/vfs.c | 8 ++------ tests/extmod/vfs_basic.py | 8 ++++++++ tests/extmod/vfs_basic.py.exp | 12 ++++++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/extmod/vfs.c b/extmod/vfs.c index d1291068ad..3cb7af1b43 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -83,12 +83,8 @@ mp_vfs_mount_t *mp_vfs_lookup_path(const char *path, const char **path_out) { } } - // if we get here then there's nothing mounted on / - - if (is_abs) { - // path began with / and was not found - return MP_VFS_NONE; - } + // if we get here then there's nothing mounted on /, so the path doesn't exist + return MP_VFS_NONE; } // a relative path within a mounted device diff --git a/tests/extmod/vfs_basic.py b/tests/extmod/vfs_basic.py index 62b2a27738..9a9ef2ca61 100644 --- a/tests/extmod/vfs_basic.py +++ b/tests/extmod/vfs_basic.py @@ -74,6 +74,14 @@ print(uos.statvfs("/")[9] >= 32) # getcwd when in root dir print(uos.getcwd()) +# test operations on the root directory with nothing mounted, they should all fail +for func in ("chdir", "listdir", "mkdir", "remove", "rmdir", "stat"): + for arg in ("x", "/x"): + try: + getattr(uos, func)(arg) + except OSError: + print(func, arg, "OSError") + # basic mounting and listdir uos.mount(Filesystem(1), "/test_mnt") print(uos.listdir()) diff --git a/tests/extmod/vfs_basic.py.exp b/tests/extmod/vfs_basic.py.exp index ebca310304..536bb4c805 100644 --- a/tests/extmod/vfs_basic.py.exp +++ b/tests/extmod/vfs_basic.py.exp @@ -1,6 +1,18 @@ (16384, 0, 0, 0, 0, 0, 0, 0, 0, 0) True / +chdir x OSError +chdir /x OSError +listdir x OSError +listdir /x OSError +mkdir x OSError +mkdir /x OSError +remove x OSError +remove /x OSError +rmdir x OSError +rmdir /x OSError +stat x OSError +stat /x OSError 1 mount False False ['test_mnt'] ('test_mnt', 16384, 0)