Skip to content

Commit dcf14c1

Browse files
hosakadpgeorge
authored andcommitted
extmod/vfs_fat: Add fat_vfs_statvfs(), reused from stmhal.
1 parent 791b65f commit dcf14c1

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

extmod/vfs_fat.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,36 @@ STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) {
250250
}
251251
STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_stat_obj, fat_vfs_stat);
252252

253+
// Get the status of a VFS.
254+
STATIC mp_obj_t fat_vfs_statvfs(mp_obj_t vfs_in, mp_obj_t path_in) {
255+
(void)vfs_in;
256+
const char *path = mp_obj_str_get_str(path_in);
257+
258+
FATFS *fatfs;
259+
DWORD nclst;
260+
FRESULT res = f_getfree(path, &nclst, &fatfs);
261+
if (FR_OK != res) {
262+
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError,
263+
MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
264+
}
265+
266+
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
267+
268+
t->items[0] = MP_OBJ_NEW_SMALL_INT(fatfs->csize * fatfs->ssize); // f_bsize
269+
t->items[1] = t->items[0]; // f_frsize
270+
t->items[2] = MP_OBJ_NEW_SMALL_INT((fatfs->n_fatent - 2) * fatfs->csize); // f_blocks
271+
t->items[3] = MP_OBJ_NEW_SMALL_INT(nclst); // f_bfree
272+
t->items[4] = t->items[3]; // f_bavail
273+
t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // f_files
274+
t->items[6] = MP_OBJ_NEW_SMALL_INT(0); // f_ffree
275+
t->items[7] = MP_OBJ_NEW_SMALL_INT(0); // f_favail
276+
t->items[8] = MP_OBJ_NEW_SMALL_INT(0); // f_flags
277+
t->items[9] = MP_OBJ_NEW_SMALL_INT(_MAX_LFN); // f_namemax
278+
279+
return MP_OBJ_FROM_PTR(t);
280+
}
281+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_statvfs_obj, fat_vfs_statvfs);
282+
253283
// Unmount the filesystem
254284
STATIC mp_obj_t fat_vfs_umount(mp_obj_t vfs_in) {
255285
fatfs_umount(((fs_user_mount_t *)vfs_in)->readblocks[1]);
@@ -268,6 +298,7 @@ STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = {
268298
{ MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&fat_vfs_remove_obj) },
269299
{ MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&fat_vfs_rename_obj) },
270300
{ MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&fat_vfs_stat_obj) },
301+
{ MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&fat_vfs_statvfs_obj) },
271302
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&fat_vfs_umount_obj) },
272303
};
273304
STATIC MP_DEFINE_CONST_DICT(fat_vfs_locals_dict, fat_vfs_locals_dict_table);

0 commit comments

Comments
 (0)