Skip to content

Commit 99b8564

Browse files
committed
Add VfsFat.readonly property for getting current state
Previously the only other way of determining whether the Vfs has been mounted read-write or read-only appears to be to attempt a write operation and detect a possible OSError. It wasn't possible for the user code to keep track of the state of the state since the boot VM has to decide whether to (re)mount read-write or read-only, but can't (easily) pass this information on to the runtime VM.
1 parent 295f7b4 commit 99b8564

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

extmod/vfs_fat.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,18 @@ STATIC mp_obj_t vfs_fat_utime(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t times_
429429
}
430430
STATIC MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_utime_obj, vfs_fat_utime);
431431

432+
STATIC mp_obj_t vfs_fat_getreadonly(mp_obj_t self_in) {
433+
fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
434+
return mp_obj_new_bool(!filesystem_is_writable_by_python(self));
435+
}
436+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_getreadonly_obj, vfs_fat_getreadonly);
437+
STATIC const mp_obj_property_t fat_vfs_readonly_obj = {
438+
.base.type = &mp_type_property,
439+
.proxy = {(mp_obj_t)&fat_vfs_getreadonly_obj,
440+
MP_ROM_NONE,
441+
MP_ROM_NONE},
442+
};
443+
432444
#if MICROPY_FATFS_USE_LABEL
433445
STATIC mp_obj_t vfs_fat_getlabel(mp_obj_t self_in) {
434446
fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
@@ -481,6 +493,7 @@ STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = {
481493
{ MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&vfs_fat_mount_obj) },
482494
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&fat_vfs_umount_obj) },
483495
{ MP_ROM_QSTR(MP_QSTR_utime), MP_ROM_PTR(&fat_vfs_utime_obj) },
496+
{ MP_ROM_QSTR(MP_QSTR_readonly), MP_ROM_PTR(&fat_vfs_readonly_obj) },
484497
#if MICROPY_FATFS_USE_LABEL
485498
{ MP_ROM_QSTR(MP_QSTR_label), MP_ROM_PTR(&fat_vfs_label_obj) },
486499
#endif

shared-bindings/storage/__init__.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ STATIC const mp_rom_map_elem_t storage_module_globals_table[] = {
258258
//| this property can only be set when the device is writable by the
259259
//| microcontroller."""
260260
//| ...
261+
//| readonly: bool
262+
//| """``True`` when the device is mounted as readonly by the microcontroller.
263+
//| This property cannot be changed, use `storage.remount` instead."""
264+
//| ...
261265
//|
262266
//| def mkfs(self) -> None:
263267
//| """Format the block device, deleting any data that may have been there"""

0 commit comments

Comments
 (0)