8000 extmod/vfs_posix: Fix relative paths on non-root VFS. · micropython/micropython@bf7649c · GitHub
[go: up one dir, main page]

Skip to content

Commit bf7649c

Browse files
committed
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>
1 parent 0ccc82c commit bf7649c

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

extmod/vfs_posix.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,23 @@ typedef struct _mp_obj_vfs_posix_t {
5555
} mp_obj_vfs_posix_t;
5656

5757
STATIC const char *vfs_posix_get_path_str(mp_obj_vfs_posix_t *self, mp_obj_t path) {
58-
if (self->root_len == 0) {
59-
return mp_obj_str_get_str(path);
58+
const char *path_str = mp_obj_str_get_str(path);
59+
if (self->root_len == 0 || path_str[0] != '/') {
60+
return path_str;
6061
} else {
61-
self->root.len = self->root_len;
62-
vstr_add_str(&self->root, mp_obj_str_get_str(path));
62+
self->root.len = self->root_len - 1;
63+
vstr_add_str(&self->root, path_str);
6364
return vstr_null_terminated_str(&self->root);
6465
}
6566
}
6667

6768
STATIC mp_obj_t vfs_posix_get_path_obj(mp_obj_vfs_posix_t *self, mp_obj_t path) {
68-
if (self->root_len == 0) {
69+
const char *path_str = mp_obj_str_get_str(path);
70+
if (self->root_len == 0 || path_str[0] != '/') {
6971
return path;
7072
} else {
71-
self->root.len = self->root_len;
72-
vstr_add_str(&self->root, mp_obj_str_get_str(path));
73+
self->root.len = self->root_len - 1;
74+
vstr_add_str(&self->root, path_str);
7375
return mp_obj_new_str(self->root.buf, self->root.len);
7476
}
7577
}

0 commit comments

Comments
 (0)
0