Skip to content

Commit

Permalink
extmod/vfs_posix: Fix relative root path.
Browse files Browse the repository at this point in the history
A VfsPosix created with a relative root path would get confused when
chdir() was called on it and become unable to properly resolve absolute
paths, because changing directories effectively shifted its root. The
simplest fix for that would be to say "don't do that", but since the
unit tests themselves do it, fix it by making a relative path absolute
before storing it.

Signed-off-by: Christian Walther <cwalther@gmx.ch>
  • Loading branch information
cwalther committed Oct 19, 2023
1 parent 86c7b95 commit e3ba6f9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
26 changes: 25 additions & 1 deletion extmod/vfs_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
#ifdef _MSC_VER
#include <direct.h> // For mkdir etc.
#endif
#ifdef _WIN32
#include <windows.h>
#endif

typedef struct _mp_obj_vfs_posix_t {
mp_obj_base_t base;
Expand Down Expand Up @@ -107,7 +110,28 @@ STATIC mp_obj_t vfs_posix_make_new(const mp_obj_type_t *type, size_t n_args, siz
mp_obj_vfs_posix_t *vfs = mp_obj_malloc(mp_obj_vfs_posix_t, type);
vstr_init(&vfs->root, 0);
if (n_args == 1) {
vstr_add_str(&vfs->root, mp_obj_str_get_str(args[0]));
const char *root = mp_obj_str_get_str(args[0]);
// if the root is relative, make it absolute, otherwise we'll get confused by chdir
#ifdef _WIN32
char buf[MICROPY_ALLOC_PATH_MAX + 1];
DWORD result = GetFullPathNameA(root, sizeof(buf), buf, NULL);
if (result > 0 && result < sizeof(buf)) {
vstr_add_str(&vfs->root, buf);
} else {
mp_raise_OSError(GetLastError());
}
#else
if (root[0] != '\0' && root[0] != '/') {
char buf[MICROPY_ALLOC_PATH_MAX + 1];
const char *cwd = getcwd(buf, sizeof(buf));
if (cwd == NULL) {
mp_raise_OSError(errno);
}
vstr_add_str(&vfs->root, cwd);
vstr_add_char(&vfs->root, '/');
}
vstr_add_str(&vfs->root, root);
#endif
vstr_add_char(&vfs->root, '/');
}
vfs->root_len = vfs->root.len;
Expand Down
13 changes: 13 additions & 0 deletions tests/extmod/vfs_posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ def write_files_without_closing():
print(type(list(vfs.ilistdir("."))[0][0]))
print(type(list(vfs.ilistdir(b"."))[0][0]))

# chdir should not affect absolute paths (regression test)
vfs.mkdir("/subdir")
vfs.mkdir("/subdir/micropy_test_dir")
with vfs.open("/subdir/micropy_test_dir/test2", "w") as f:
f.write("wrong")
vfs.chdir("/subdir")
with vfs.open("/test2", "r") as f:
print(f.read())
os.chdir(curdir)
vfs.remove("/subdir/micropy_test_dir/test2")
vfs.rmdir("/subdir/micropy_test_dir")
vfs.rmdir("/subdir")

# remove
os.remove(temp_dir + "/test2")
print(os.listdir(temp_dir))
Expand Down
1 change: 1 addition & 0 deletions tests/extmod/vfs_posix.py.exp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ next_file_no <= base_file_no True
<class 'tuple'>
<class 'str'>
<class 'bytes'>
hello
[]
remove OSError
False
Expand Down

0 comments on commit e3ba6f9

Please sign in to comment.