Skip to content

Commit

Permalink
extmod/vfs: Fix lookup of entry in root dir so it fails correctly.
Browse files Browse the repository at this point in the history
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 <damien@micropython.org>
  • Loading branch information
dpgeorge committed Sep 23, 2020
1 parent 3e16763 commit 71adf50
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
8 changes: 2 additions & 6 deletions extmod/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/extmod/vfs_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ def open(self, file, mode):
# 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())
Expand Down
12 changes: 12 additions & 0 deletions tests/extmod/vfs_basic.py.exp
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit 71adf50

Please sign in to comment.