Skip to content

Commit 1d40f12

Browse files
committed
unix: Support MICROPY_VFS_POSIX and enable it in coverage build.
The unix coverage build is now switched fully to the VFS implementation, ie the uos module is the uos_vfs module. For example, one can now sandbox uPy to their home directory via: $ ./micropython_coverage >>> import uos >>> uos.umount('/') # unmount existing root VFS >>> vfs = uos.VfsPosix('/home/user') # create new POSIX VFS >>> uos.mount(vfs, '/') # mount new POSIX VFS at root Some filesystem/OS features may no longer work with the coverage build due to this change, and these need to be gradually fixed. The standard unix port remains unchanged, it still uses the traditional uos module which directly accesses the underlying host filesystem.
1 parent d4ce57e commit 1d40f12

5 files changed

Lines changed: 36 additions & 6 deletions

File tree

ports/unix/file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#include "py/mphal.h"
3838
#include "fdfile.h"
3939

40-
#if MICROPY_PY_IO
40+
#if MICROPY_PY_IO && !MICROPY_VFS
4141

4242
#ifdef _WIN32
4343
#define fsync _commit
@@ -277,4 +277,4 @@ const mp_obj_fdfile_t mp_sys_stdin_obj = { .base = {&mp_type_textio}, .fd = STD
277277
const mp_obj_fdfile_t mp_sys_stdout_obj = { .base = {&mp_type_textio}, .fd = STDOUT_FILENO };
278278
const mp_obj_fdfile_t mp_sys_stderr_obj = { .base = {&mp_type_textio}, .fd = STDERR_FILENO };
279279

280-
#endif // MICROPY_PY_IO
280+
#endif // MICROPY_PY_IO && !MICROPY_VFS

ports/unix/main.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
#include "py/mphal.h"
4747
#include "py/mpthread.h"
4848
#include "extmod/misc.h"
49+
#include "extmod/vfs.h"
50+
#include "extmod/vfs_posix.h"
4951
#include "genhdr/mpversion.h"
5052
#include "input.h"
5153

@@ -447,6 +449,18 @@ MP_NOINLINE int main_(int argc, char **argv) {
447449

448450
mp_init();
449451

452+
#if MICROPY_VFS_POSIX
453+
{
454+
// Mount the host FS at the root of our internal VFS
455+
mp_obj_t args[2] = {
456+
mp_type_vfs_posix.make_new(&mp_type_vfs_posix, 0, 0, NULL),
457+
MP_OBJ_NEW_QSTR(MP_QSTR__slash_),
458+
};
459+
mp_vfs_mount(2, args, (mp_map_t*)&mp_const_empty_map);
460+
MP_STATE_VM(vfs_cur) = MP_STATE_VM(vfs_mount_table);
461+
}
462+
#endif
463+
450464
char *home = getenv("HOME");
451465
char *path = getenv("MICROPYPATH");
452466
if (path == NULL) {
@@ -645,6 +659,7 @@ MP_NOINLINE int main_(int argc, char **argv) {
645659
return ret & 0xff;
646660
}
647661

662+
#if !MICROPY_VFS
648663
uint mp_import_stat(const char *path) {
649664
struct stat st;
650665
if (stat(path, &st) == 0) {
@@ -656,6 +671,7 @@ uint mp_import_stat(const char *path) {
656671
}
657672
return MP_IMPORT_STAT_NO_EXIST;
658673
}
674+
#endif
659675

660676
void nlr_jump_fail(void *val) {
661677
printf("FATAL: uncaught NLR %p\n", val);

ports/unix/moduos_vfs.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <string.h>
2929

3030
#include "extmod/vfs.h"
31+
#include "extmod/vfs_posix.h"
3132
#include "extmod/vfs_fat.h"
3233

3334
#if MICROPY_VFS
@@ -52,6 +53,9 @@ STATIC const mp_rom_map_elem_t uos_vfs_module_globals_table[] = {
5253
{ MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&mp_vfs_statvfs_obj) },
5354
{ MP_ROM_QSTR(MP_QSTR_unlink), MP_ROM_PTR(&mp_vfs_remove_obj) }, // unlink aliases to remove
5455

56+
#if MICROPY_VFS_POSIX
57+
{ MP_ROM_QSTR(MP_QSTR_VfsPosix), MP_ROM_PTR(&mp_type_vfs_posix) },
58+
#endif
5559
#if MICROPY_VFS_FAT
5660
{ MP_ROM_QSTR(MP_QSTR_VfsFat), MP_ROM_PTR(&mp_fat_vfs_type) },
5761
#endif

ports/unix/mpconfigport.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ extern const struct _mp_obj_module_t mp_module_ffi;
180180
extern const struct _mp_obj_module_t mp_module_jni;
181181

182182
#if MICROPY_PY_UOS_VFS
183-
#define MICROPY_PY_UOS_VFS_DEF { MP_ROM_QSTR(MP_QSTR_uos_vfs), MP_ROM_PTR(&mp_module_uos_vfs) },
183+
#define MICROPY_PY_UOS_DEF { MP_ROM_QSTR(MP_QSTR_uos), MP_ROM_PTR(&mp_module_uos_vfs) },
184184
#else
185-
#define MICROPY_PY_UOS_VFS_DEF
185+
#define MICROPY_PY_UOS_DEF { MP_ROM_QSTR(MP_QSTR_uos), MP_ROM_PTR(&mp_module_os) },
186186
#endif
187187
#if MICROPY_PY_FFI
188188
#define MICROPY_PY_FFI_DEF { MP_ROM_QSTR(MP_QSTR_ffi), MP_ROM_PTR(&mp_module_ffi) },
@@ -221,8 +221,7 @@ extern const struct _mp_obj_module_t mp_module_jni;
221221
MICROPY_PY_UTIME_DEF \
222222
MICROPY_PY_SOCKET_DEF \
223223
{ MP_ROM_QSTR(MP_QSTR_umachine), MP_ROM_PTR(&mp_module_machine) }, \
224-
{ MP_ROM_QSTR(MP_QSTR_uos), MP_ROM_PTR(&mp_module_os) }, \
225-
MICROPY_PY_UOS_VFS_DEF \
224+
MICROPY_PY_UOS_DEF \
226225
MICROPY_PY_USELECT_DEF \
227226
MICROPY_PY_TERMIOS_DEF \
228227

ports/unix/mpconfigport_coverage.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#define MICROPY_FLOAT_HIGH_QUALITY_HASH (1)
3636
#define MICROPY_ENABLE_SCHEDULER (1)
37+
#define MICROPY_READER_VFS (1)
3738
#define MICROPY_PY_DELATTR_SETATTR (1)
3839
#define MICROPY_PY_REVERSE_SPECIAL_METHODS (1)
3940
#define MICROPY_PY_BUILTINS_RANGE_BINOP (1)
@@ -43,7 +44,17 @@
4344
#define MICROPY_PY_URANDOM_EXTRA_FUNCS (1)
4445
#define MICROPY_PY_IO_BUFFEREDWRITER (1)
4546
#define MICROPY_PY_IO_RESOURCE_STREAM (1)
47+
#define MICROPY_VFS_POSIX (1)
4648
#undef MICROPY_VFS_FAT
4749
#define MICROPY_VFS_FAT (1)
4850
#define MICROPY_PY_FRAMEBUF (1)
4951
#define MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT (1)
52+
53+
// TODO these should be generic, not bound to fatfs
54+
#define mp_type_fileio mp_type_vfs_posix_fileio
55+
#define mp_type_textio mp_type_vfs_posix_textio
56+
57+
// use vfs's functions for import stat and builtin open
58+
#define mp_import_stat mp_vfs_import_stat
59+
#define mp_builtin_open mp_vfs_open
60+
#define mp_builtin_open_obj mp_vfs_open_obj

0 commit comments

Comments
 (0)