summaryrefslogtreecommitdiff
path: root/extmod/vfs_posix.c
diff options
context:
space:
mode:
authorChristian Walther <cwalther@gmx.ch>2023-07-30 14:48:22 +0200
committerChristian Walther <cwalther@gmx.ch>2023-10-19 16:21:09 +0200
commit0c4fb1687193a6c6f5edbb9404ecb7e5d6f3bec3 (patch)
treed649af465106230190f1e57808e7012828367ef6 /extmod/vfs_posix.c
parent5f7065f57a1ce445266a566685ad451edecc58c3 (diff)
extmod/vfs_posix: Fix relative paths on non-root VFS.
The unwritten API contract expected of a VFS by mp_vfs_lookup_path() is that paths passed in are relative to the root of the VFS if they start with '/' and relative to the current directory of the VFS otherwise. This was not correctly implemented in VfsPosix for instances with a non-empty root - all paths were interpreted relative to the root. Fix that. Since VfsPosix tracks its CWD using the "external" CWD of the Unix process, the correct handling for relative paths is to pass them through unmodified. Also, when concatenating absolute paths, fix an off-by-one resulting in a harmless double slash (the root path already has a trailing slash). Signed-off-by: Christian Walther <cwalther@gmx.ch>
Diffstat (limited to 'extmod/vfs_posix.c')
-rw-r--r--extmod/vfs_posix.c16
1 files changed, 9 insertions, 7 deletions
diff --git a/extmod/vfs_posix.c b/extmod/vfs_posix.c
index 1505682f1..6df91a273 100644
--- a/extmod/vfs_posix.c
+++ b/extmod/vfs_posix.c
@@ -58,21 +58,23 @@ typedef struct _mp_obj_vfs_posix_t {
} mp_obj_vfs_posix_t;
STATIC const char *vfs_posix_get_path_str(mp_obj_vfs_posix_t *self, mp_obj_t path) {
- if (self->root_len == 0) {
- return mp_obj_str_get_str(path);
+ const char *path_str = mp_obj_str_get_str(path);
+ if (self->root_len == 0 || path_str[0] != '/') {
+ return path_str;
} else {
- self->root.len = self->root_len;
- vstr_add_str(&self->root, mp_obj_str_get_str(path));
+ self->root.len = self->root_len - 1;
+ vstr_add_str(&self->root, path_str);
return vstr_null_terminated_str(&self->root);
}
}
STATIC mp_obj_t vfs_posix_get_path_obj(mp_obj_vfs_posix_t *self, mp_obj_t path) {
- if (self->root_len == 0) {
+ const char *path_str = mp_obj_str_get_str(path);
+ if (self->root_len == 0 || path_str[0] != '/') {
return path;
} else {
- self->root.len = self->root_len;
- vstr_add_str(&self->root, mp_obj_str_get_str(path));
+ self->root.len = self->root_len - 1;
+ vstr_add_str(&self->root, path_str);
return mp_obj_new_str(self->root.buf, self->root.len);
}
}