Skip to content

Commit c3000b6

Browse files
committed
unix/modos: Add statvfs() function.
Another function (like stat) which is problematic to deal with on ABI level (FFI), as struct statvfs layout may differ unpredictably between OSes and even different versions of a same OS. So, implement it in C, returning a 10-element tuple of f_bsize, f_frsize, f_blocks, f_bfree, f_bavail, f_files, f_ffree, f_favail, f_flag, f_namemax. This is exactly the order described in Python3 docs, https://docs.python.org/3/library/os.html#os.statvfs (but note that os.statvfs() should make these values available as attributes).
1 parent 6ec6f51 commit c3000b6

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

unix/modos.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <unistd.h>
3131
#include <errno.h>
3232
#include <stdlib.h>
33+
#include <sys/statvfs.h>
3334

3435
#include "py/nlr.h"
3536
#include "py/runtime.h"
@@ -62,6 +63,31 @@ STATIC mp_obj_t mod_os_stat(mp_obj_t path_in) {
6263
}
6364
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_os_stat_obj, mod_os_stat);
6465

66+
#if MICROPY_PY_OS_STATVFS
67+
STATIC mp_obj_t mod_os_statvfs(mp_obj_t path_in) {
68+
struct statvfs sb;
69+
mp_uint_t len;
70+
const char *path = mp_obj_str_get_data(path_in, &len);
71+
72+
int res = statvfs(path, &sb);
73+
RAISE_ERRNO(res, errno);
74+
75+
mp_obj_tuple_t *t = mp_obj_new_tuple(10, NULL);
76+
t->items[0] = MP_OBJ_NEW_SMALL_INT(sb.f_bsize);
77+
t->items[1] = MP_OBJ_NEW_SMALL_INT(sb.f_frsize);
78+
t->items[2] = MP_OBJ_NEW_SMALL_INT(sb.f_blocks);
79+
t->items[3] = MP_OBJ_NEW_SMALL_INT(sb.f_bfree);
80+
t->items[4] = MP_OBJ_NEW_SMALL_INT(sb.f_bavail);
81+
t->items[5] = MP_OBJ_NEW_SMALL_INT(sb.f_files);
82+
t->items[6] = MP_OBJ_NEW_SMALL_INT(sb.f_ffree);
83+
t->items[7] = MP_OBJ_NEW_SMALL_INT(sb.f_favail);
84+
t->items[8] = MP_OBJ_NEW_SMALL_INT(sb.f_flag);
85+
t->items[9] = MP_OBJ_NEW_SMALL_INT(sb.f_namemax);
86+
return t;
87+
}
88+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_os_statvfs_obj, mod_os_statvfs);
89+
#endif
90+
6591
STATIC mp_obj_t mod_os_unlink(mp_obj_t path_in) {
6692
mp_uint_t len;
6793
const char *path = mp_obj_str_get_data(path_in, &len);
@@ -88,6 +114,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_os_system_obj, mod_os_system);
88114
STATIC const mp_map_elem_t mp_module_os_globals_table[] = {
89115
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR__os) },
90116
{ MP_OBJ_NEW_QSTR(MP_QSTR_stat), (mp_obj_t)&mod_os_stat_obj },
117+
#if MICROPY_PY_OS_STATVFS
118+
{ MP_OBJ_NEW_QSTR(MP_QSTR_statvfs), (mp_obj_t)&mod_os_statvfs_obj },
119+
#endif
91120
{ MP_OBJ_NEW_QSTR(MP_QSTR_system), (mp_obj_t)&mod_os_system_obj },
92121
{ MP_OBJ_NEW_QSTR(MP_QSTR_unlink),(mp_obj_t)&mod_os_unlink_obj},
93122
};

unix/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
#define MICROPY_STACKLESS (0)
9494
#define MICROPY_STACKLESS_STRICT (0)
9595

96+
#define MICROPY_PY_OS_STATVFS (1)
9697
#define MICROPY_PY_UCTYPES (1)
9798
#define MICROPY_PY_UZLIB (1)
9899
#define MICROPY_PY_UJSON (1)

unix/qstrdefsport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ Q(flush)
3636

3737
Q(_os)
3838
Q(stat)
39+
#if MICROPY_PY_OS_STATVFS
40+
Q(statvfs)
41+
#endif
3942
Q(system)
4043
Q(unlink)
4144

0 commit comments

Comments
 (0)