summaryrefslogtreecommitdiffstatshomepage
path: root/stmhal/moduos.c
diff options
context:
space:
mode:
authorDave Hylands <dhylands@gmail.com>2016-01-15 21:56:08 -0800
committerDamien George <damien.p.george@gmail.com>2016-01-19 12:29:47 +0000
commitf22844b4e5d1990f3be5433a45c7eb4e2c07a297 (patch)
treef8414b550aeec9cfb05470ccf4b79bfb221bc5e4 /stmhal/moduos.c
parenta17755ee8ba2982505d349e0f06acdabe943d33e (diff)
downloadmicropython-f22844b4e5d1990f3be5433a45c7eb4e2c07a297.tar.gz
micropython-f22844b4e5d1990f3be5433a45c7eb4e2c07a297.zip
stmhal: Add os.statvfs
Implement enough of statvfs to determine the amount of free space on a volume.
Diffstat (limited to 'stmhal/moduos.c')
-rw-r--r--stmhal/moduos.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/stmhal/moduos.c b/stmhal/moduos.c
index b4c0ba5297..d206ee1d83 100644
--- a/stmhal/moduos.c
+++ b/stmhal/moduos.c
@@ -349,6 +349,36 @@ error:
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_stat_obj, os_stat);
+STATIC mp_obj_t os_statvfs(mp_obj_t path_in) {
+ const char *path = mp_obj_str_get_str(path_in);
+
+ DWORD nclst;
+ FATFS *fatfs;
+ FRESULT res = f_getfree(path, &nclst, &fatfs);
+ if (res != FR_OK) {
+ goto error;
+ }
+
+ mp_obj_tuple_t *t = mp_obj_new_tuple(10, NULL);
+
+ t->items[0] = MP_OBJ_NEW_SMALL_INT(fatfs->csize * 512); // f_bsize - block size
+ t->items[1] = t->items[0]; // f_frsize - fragment size
+ t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // f_blocks - total number of blocks
+ t->items[3] = MP_OBJ_NEW_SMALL_INT(nclst); // f_bfree - number of free blocks
+ t->items[4] = t->items[3]; // f_bavail - free blocks avail to unpriviledged users
+ t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // f_files - # inodes
+ t->items[6] = MP_OBJ_NEW_SMALL_INT(0); // f_ffree - # free inodes
+ t->items[7] = MP_OBJ_NEW_SMALL_INT(0); // f_favail - # free inodes avail to unpriviledges users
+ t->items[8] = MP_OBJ_NEW_SMALL_INT(0); // f_flags
+ t->items[9] = MP_OBJ_NEW_SMALL_INT(_MAX_LFN); // f_namemax
+
+ return t;
+
+error:
+ nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_statvfs_obj, os_statvfs);
+
/// \function sync()
/// Sync all filesystems.
STATIC mp_obj_t os_sync(void) {
@@ -410,6 +440,7 @@ STATIC const mp_map_elem_t os_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_rename),(mp_obj_t)&os_rename_obj},
{ MP_OBJ_NEW_QSTR(MP_QSTR_rmdir), (mp_obj_t)&os_rmdir_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_stat), (mp_obj_t)&os_stat_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_statvfs), (mp_obj_t)&os_statvfs_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_unlink), (mp_obj_t)&os_remove_obj }, // unlink aliases to remove
{ MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&mod_os_sync_obj },