Skip to content

Commit f22844b

Browse files
dhylandsdpgeorge
authored andcommitted
stmhal: Add os.statvfs
Implement enough of statvfs to determine the amount of free space on a volume.
1 parent a17755e commit f22844b

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

stmhal/moduos.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,36 @@ STATIC mp_obj_t os_stat(mp_obj_t path_in) {
349349
}
350350
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_stat_obj, os_stat);
351351

352+
STATIC mp_obj_t os_statvfs(mp_obj_t path_in) {
353+
const char *path = mp_obj_str_get_str(path_in);
354+
355+
DWORD nclst;
356+
FATFS *fatfs;
357+
FRESULT res = f_getfree(path, &nclst, &fatfs);
358+
if (res != FR_OK) {
359+
goto error;
360+
}
361+
362+
mp_obj_tuple_t *t = mp_obj_new_tuple(10, NULL);
363+
364+
t->items[0] = MP_OBJ_NEW_SMALL_INT(fatfs->csize * 512); // f_bsize - block size
365+
t->items[1] = t->items[0]; // f_frsize - fragment size
366+
t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // f_blocks - total number of blocks
367+
t->items[3] = MP_OBJ_NEW_SMALL_INT(nclst); // f_bfree - number of free blocks
368+
t->items[4] = t->items[3]; // f_bavail - free blocks avail to unpriviledged users
369+
t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // f_files - # inodes
370+
t->items[6] = MP_OBJ_NEW_SMALL_INT(0); // f_ffree - # free inodes
371+
t->items[7] = MP_OBJ_NEW_SMALL_INT(0); // f_favail - # free inodes avail to unpriviledges users
372+
t->items[8] = MP_OBJ_NEW_SMALL_INT(0); // f_flags
373+
t->items[9] = MP_OBJ_NEW_SMALL_INT(_MAX_LFN); // f_namemax
374+
375+
return t;
376+
377+
error:
378+
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
379+
}
380+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_statvfs_obj, os_statvfs);
381+
352382
/// \function sync()
353383
/// Sync all filesystems.
354384
STATIC mp_obj_t os_sync(void) {
@@ -410,6 +440,7 @@ STATIC const mp_map_elem_t os_module_globals_table[] = {
410440
{ MP_OBJ_NEW_QSTR(MP_QSTR_rename),(mp_obj_t)&os_rename_obj},
411441
{ MP_OBJ_NEW_QSTR(MP_QSTR_rmdir), (mp_obj_t)&os_rmdir_obj },
412442
{ MP_OBJ_NEW_QSTR(MP_QSTR_stat), (mp_obj_t)&os_stat_obj },
443+
{ MP_OBJ_NEW_QSTR(MP_QSTR_statvfs), (mp_obj_t)&os_statvfs_obj },
413444
{ MP_OBJ_NEW_QSTR(MP_QSTR_unlink), (mp_obj_t)&os_remove_obj }, // unlink aliases to remove
414445

415446
{ MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&mod_os_sync_obj },

stmhal/qstrdefsport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ Q(rmdir)
396396
Q(unlink)
397397
Q(sep)
398398
Q(stat)
399+
Q(statvfs)
399400
Q(urandom)
400401
Q(dupterm)
401402

0 commit comments

Comments
 (0)