Skip to content

Commit fe7f405

Browse files
committed
Add VfsFat.label property
These allow accessing the filesystem label. For instance, in boot.py, you can set the label on the built-in storage with: storage.remount('/', False) storage.getmount('/').label = "NEWLABEL" storage.remount('/', True) Users with multiple CIRCUITPY boards may find it desirable to choose a different label for each board they own.
1 parent c08f5a3 commit fe7f405

4 files changed

Lines changed: 38 additions & 0 deletions

File tree

extmod/vfs_fat.c

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

3535
#include <string.h>
36+
#include "py/objproperty.h"
3637
#include "py/runtime.h"
3738
#include "py/mperrno.h"
3839
#include "lib/oofatfs/ff.h"
@@ -317,6 +318,36 @@ STATIC mp_obj_t vfs_fat_umount(mp_obj_t self_in) {
317318
}
318319
STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_umount_obj, vfs_fat_umount);
319320

321+
#if MICROPY_FATFS_USE_LABEL
322+
STATIC mp_obj_t vfs_fat_getlabel(mp_obj_t self_in) {
323+
fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
324+
char working_buf[12];
325+
FRESULT res = f_getlabel(&self->fatfs, working_buf, NULL);
326+
if (res != FR_OK) {
327+
mp_raise_OSError(fresult_to_errno_table[res]);
328+
}
329+
return mp_obj_new_str(working_buf, strlen(working_buf), false);
330+
}
331+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_getlabel_obj, vfs_fat_getlabel);
332+
333+
static mp_obj_t vfs_fat_setlabel(mp_obj_t self_in, mp_obj_t label_in) {
334+
fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
335+
const char *label_str = mp_obj_str_get_str(label_in);
336+
FRESULT res = f_setlabel(&self->fatfs, label_str);
337+
if (res != FR_OK) {
338+
mp_raise_OSError(fresult_to_errno_table[res]);
339+
}
340+
return mp_const_none;
341+
}
342+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_setlabel_obj, vfs_fat_setlabel);
343+
STATIC const mp_obj_property_t fat_vfs_label_obj = {
344+
.base.type = &mp_type_property,
345+
.proxy = {(mp_obj_t)&fat_vfs_getlabel_obj,
346+
(mp_obj_t)&fat_vfs_setlabel_obj,
347+
(mp_obj_t)&mp_const_none_obj},
348+
};
349+
#endif
350+
320351
STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = {
321352
{ MP_ROM_QSTR(MP_QSTR_mkfs), MP_ROM_PTR(&fat_vfs_mkfs_obj) },
322353
{ MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&fat_vfs_open_obj) },
@@ -331,6 +362,9 @@ STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = {
331362
{ MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&fat_vfs_statvfs_obj) },
332363
{ MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&vfs_fat_mount_obj) },
333364
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&fat_vfs_umount_obj) },
365+
#if MICROPY_FATFS_USE_LABEL
366+
{ MP_ROM_QSTR(MP_QSTR_label), MP_ROM_PTR(&fat_vfs_label_obj) },
367+
#endif
334368
};
335369
STATIC MP_DEFINE_CONST_DICT(fat_vfs_locals_dict, fat_vfs_locals_dict_table);
336370

ports/unix/mpconfigport_coverage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@
4343
#define MICROPY_PY_IO_BUFFEREDWRITER (1)
4444
#undef MICROPY_VFS_FAT
4545
#define MICROPY_VFS_FAT (1)
46+
#define MICROPY_FATFS_USE_LABEL (1)
4647
#define MICROPY_PY_FRAMEBUF (1)

tests/extmod/vfs_fat_ramdisk.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ def ioctl(self, op, arg):
5454
vfs = uos.VfsFat(bdev)
5555
uos.mount(vfs, "/ramdisk")
5656

57+
vfs.label = 'label test'
58+
print("label:", vfs.label)
5759
print("statvfs:", vfs.statvfs("/ramdisk"))
5860
print("getcwd:", vfs.getcwd())
5961

tests/extmod/vfs_fat_ramdisk.py.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
True
22
True
3+
label: LABEL TEST
34
statvfs: (512, 512, 16, 16, 16, 0, 0, 0, 0, 255)
45
getcwd: /
56
True

0 commit comments

Comments
 (0)