Skip to content

Commit 6711c5c

Browse files
authored
Merge pull request adafruit#712 from jepler/fslabel
Add ability to get, set filesystem label from CircuitPython
2 parents 1eba580 + 34f5498 commit 6711c5c

7 files changed

Lines changed: 104 additions & 9 deletions

File tree

extmod/vfs_fat.c

Lines changed: 37 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,39 @@ 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+
if(res == FR_WRITE_PROTECTED) {
339+
mp_raise_msg(&mp_type_OSError, "Read-only filesystem");
340+
}
341+
mp_raise_OSError(fresult_to_errno_table[res]);
342+
}
343+
return mp_const_none;
344+
}
345+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_setlabel_obj, vfs_fat_setlabel);
346+
STATIC const mp_obj_property_t fat_vfs_label_obj = {
347+
.base.type = &mp_type_property,
348+
.proxy = {(mp_obj_t)&fat_vfs_getlabel_obj,
349+
(mp_obj_t)&fat_vfs_setlabel_obj,
350+
(mp_obj_t)&mp_const_none_obj},
351+
};
352+
#endif
353+
320354
STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = {
321355
{ MP_ROM_QSTR(MP_QSTR_mkfs), MP_ROM_PTR(&fat_vfs_mkfs_obj) },
322356
{ MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&fat_vfs_open_obj) },
@@ -331,6 +365,9 @@ STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = {
331365
{ MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&fat_vfs_statvfs_obj) },
332366
{ MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&vfs_fat_mount_obj) },
333367
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&fat_vfs_umount_obj) },
368+
#if MICROPY_FATFS_USE_LABEL
369+
{ MP_ROM_QSTR(MP_QSTR_label), MP_ROM_PTR(&fat_vfs_label_obj) },
370+
#endif
334371
};
335372
STATIC MP_DEFINE_CONST_DICT(fat_vfs_locals_dict, fat_vfs_locals_dict_table);
336373

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)

shared-bindings/storage/__init__.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,72 @@ mp_obj_t storage_remount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_a
122122
}
123123
MP_DEFINE_CONST_FUN_OBJ_KW(storage_remount_obj, 1, storage_remount);
124124

125+
//| .. function:: getmount(mount_path)
126+
//|
127+
//| Retrieves the mount object associated with the mount path
128+
//|
129+
mp_obj_t storage_getmount(const mp_obj_t mnt_in) {
130+
return common_hal_storage_getmount(mp_obj_str_get_str(mnt_in));
131+
}
132+
MP_DEFINE_CONST_FUN_OBJ_1(storage_getmount_obj, storage_getmount);
133+
125134
STATIC const mp_rom_map_elem_t storage_module_globals_table[] = {
126135
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_storage) },
127136

128137
{ MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&storage_mount_obj) },
129138
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&storage_umount_obj) },
130139
{ MP_ROM_QSTR(MP_QSTR_remount), MP_ROM_PTR(&storage_remount_obj) },
140+
{ MP_ROM_QSTR(MP_QSTR_getmount), MP_ROM_PTR(&storage_getmount_obj) },
131141

132142
//| .. class:: VfsFat(block_device)
133143
//|
134144
//| Create a new VfsFat filesystem around the given block device.
135145
//|
136146
//| :param block_device: Block device the the filesystem lives on
137147
//|
148+
//| .. attribute:: label
149+
//|
150+
//| The filesystem label, up to 11 case-insensitive bytes. Note that
151+
//| this property can only be set when the device is writable by the
152+
//| microcontroller.
153+
//|
154+
//| .. method:: mkfs
155+
//|
156+
//| Format the block device, deleting any data that may have been there
157+
//|
158+
//| .. method:: open(path, mode)
159+
//|
160+
//| Like builtin ``open()``
161+
//|
162+
//| .. method:: ilistdir([path])
163+
//|
164+
//| Return an iterator whose values describe files and folders within
165+
//| ``path``
166+
//|
167+
//| .. method:: mkdir(path)
168+
//|
169+
//| Like `os.mkdir`
170+
//|
171+
//| .. method:: rmdir(path)
172+
//|
173+
//| Like `os.rmdir`
174+
//|
175+
//| .. method:: stat(path)
176+
//|
177+
//| Like `os.stat`
178+
//|
179+
//| .. method:: statvfs(path)
180+
//|
181+
//| Like `os.statvfs`
182+
//|
183+
//| .. method:: mount(readonly, mkfs)
184+
//|
185+
//| Don't call this directly, call `storage.mount`.
186+
//|
187+
//| .. method:: umount
188+
//|
189+
//| Don't call this directly, call `storage.umount`.
190+
//|
138191
{ MP_ROM_QSTR(MP_QSTR_VfsFat), MP_ROM_PTR(&mp_fat_vfs_type) },
139192
};
140193

shared-bindings/storage/__init__.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ void common_hal_storage_mount(mp_obj_t vfs_obj, const char* path, bool readonly)
3434
void common_hal_storage_umount_path(const char* path);
3535
void common_hal_storage_umount_object(mp_obj_t vfs_obj);
3636
void common_hal_storage_remount(const char* path, bool readonly);
37+
mp_obj_t common_hal_storage_getmount(const char* path);
3738

3839
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_STORAGE___INIT___H

shared-module/storage/__init__.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,19 +109,19 @@ void common_hal_storage_umount_object(mp_obj_t vfs_obj) {
109109
mp_vfs_proxy_call(vfs, MP_QSTR_umount, 0, NULL);
110110
}
111111

112-
void common_hal_storage_umount_path(const char* mount_path) {
113-
// remove vfs from the mount table
114-
mp_obj_t *vfs_obj = NULL;
112+
STATIC mp_obj_t storage_object_from_path(const char* mount_path) {
115113
for (mp_vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table); *vfsp != NULL; vfsp = &(*vfsp)->next) {
116114
if (strcmp(mount_path, (*vfsp)->str) == 0) {
117-
vfs_obj = (*vfsp)->obj;
118-
break;
115+
return (*vfsp)->obj;
119116
}
120117
}
118+
mp_raise_OSError(MP_EINVAL);
119+
}
121120

122-
if (vfs_obj == NULL) {
123-
mp_raise_OSError(MP_EINVAL);
124-
}
121+
void common_hal_storage_umount_path(const char* mount_path) {
122+
common_hal_storage_umount_object(storage_object_from_path(mount_path));
123+
}
125124

126-
common_hal_storage_umount_object(vfs_obj);
125+
mp_obj_t common_hal_storage_getmount(const char *mount_path) {
126+
return storage_object_from_path(mount_path);
127127
}

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)