1+ from typing import Tuple
2+ from typing_extensions import TypeAlias
3+
4+ # TODO ? Move into OS or vfs module ?
5+
6+ stat_result : TypeAlias = Tuple [int , int , int , int , int , int , int , int , int , int ]
7+ """A 10-tuple containing the result of an os.stat() call.
8+ The tuple contains the following values:
9+ - 0 st_mode: File mode (permissions and type)
10+ - 1 st_ino: always 0
11+ - 2 st_dev: always 0
12+ - 3 st_nlink: always 0
13+ - 4 st_uid: always 0
14+ - 5 st_gid: always 0
15+ - 6 st_size: Size of file in bytes
16+ - 7 st_atime: Last access time (in seconds since epoch)
17+ - 8 st_mtime: Last modification time (in seconds since epoch)
18+ - 9 st_ctime: Last status change time (in seconds since epoch)
19+
20+ https://docs.micropython.org/en/latest/library/os.html#os.stat
21+ https://docs.python.org/3/library/os.html#os.stat_result
22+ """
23+ statvfs_result : TypeAlias = Tuple [int , int , int , int , int , int , int , int , int , int ]
24+ """A 10-tuple containing the result of an os.statvfs() call.
25+ The tuple contains the following values:
26+ f_bsize – file system block size
27+ f_frsize – fragment size
28+ f_blocks – size of fs in f_frsize units
29+ f_bfree – number of free blocks
30+ f_bavail – number of free blocks for unprivileged users
31+ f_files – number of inodes
32+ f_ffree – number of free inodes
33+ f_favail – number of free inodes for unprivileged users
34+ f_flag – mount flags
35+ f_namemax – maximum filename length
36+
37+ https://docs.micropython.org/en/latest/library/os.html#os.statvfs
38+ https://docs.python.org/3/library/os.html#os.statvfs
39+ """
40+
41+ # defined in extmod/vfs_posix.c
42+
43+ # function vfs_posix_stat
44+
45+ static mp_obj_t vfs_posix_stat (mp_obj_t self_in , mp_obj_t path_in ) {
46+ mp_obj_vfs_posix_t * self = MP_OBJ_TO_PTR (self_in );
47+ struct stat sb ;
48+ const char * path = vfs_posix_get_path_str (self , path_in );
49+ int ret ;
50+ MP_HAL_RETRY_SYSCALL (ret , stat (path , & sb ), mp_raise_OSError (err ));
51+ mp_obj_tuple_t * t = MP_OBJ_TO_PTR (mp_obj_new_tuple (10 , NULL ));
52+ t - > items [0 ] = MP_OBJ_NEW_SMALL_INT (sb .st_mode );
53+ t - > items [1 ] = mp_obj_new_int_from_uint (sb .st_ino );
54+ t - > items [2 ] = mp_obj_new_int_from_uint (sb .st_dev );
55+ t - > items [3 ] = mp_obj_new_int_from_uint (sb .st_nlink );
56+ t - > items [4 ] = mp_obj_new_int_from_uint (sb .st_uid );
57+ t - > items [5 ] = mp_obj_new_int_from_uint (sb .st_gid );
58+ t - > items [6 ] = mp_obj_new_int_from_uint (sb .st_size );
59+ t - > items [7 ] = mp_obj_new_int_from_uint (sb .st_atime );
60+ t - > items [8 ] = mp_obj_new_int_from_uint (sb .st_mtime );
61+ t - > items [9 ] = mp_obj_new_int_from_uint (sb .st_ctime );
62+ return MP_OBJ_FROM_PTR (t );
63+ }
64+
65+
66+ # function vfs_posix_statvfs
67+ #define F_FAVAIL sb.f_favail
68+ #define F_NAMEMAX sb.f_namemax
69+ #define F_FLAG sb.f_flag
70+
71+
72+ static mp_obj_t vfs_posix_statvfs (mp_obj_t self_in , mp_obj_t path_in ) {
73+ mp_obj_vfs_posix_t * self = MP_OBJ_TO_PTR (self_in );
74+ STRUCT_STATVFS sb ;
75+ const char * path = vfs_posix_get_path_str (self , path_in );
76+ int ret ;
77+ MP_HAL_RETRY_SYSCALL (ret , STATVFS (path , & sb ), mp_raise_OSError (err ));
78+ mp_obj_tuple_t * t = MP_OBJ_TO_PTR (mp_obj_new_tuple (10 , NULL ));
79+ t - > items [0 ] = MP_OBJ_NEW_SMALL_INT (sb .f_bsize );
80+ t - > items [1 ] = MP_OBJ_NEW_SMALL_INT (sb .f_frsize );
81+ t - > items [2 ] = MP_OBJ_NEW_SMALL_INT (sb .f_blocks );
82+ t - > items [3 ] = MP_OBJ_NEW_SMALL_INT (sb .f_bfree );
83+ t - > items [4 ] = MP_OBJ_NEW_SMALL_INT (sb .f_bavail );
84+ t - > items [5 ] = MP_OBJ_NEW_SMALL_INT (sb .f_files );
85+ t - > items [6 ] = MP_OBJ_NEW_SMALL_INT (sb .f_ffree );
86+ t - > items [7 ] = MP_OBJ_NEW_SMALL_INT (F_FAVAIL );
87+ t - > items [8 ] = MP_OBJ_NEW_SMALL_INT (F_FLAG );
88+ t - > items [9 ] = MP_OBJ_NEW_SMALL_INT (F_NAMEMAX );
89+ return MP_OBJ_FROM_PTR (t );
90+ }
91+
92+
93+
94+
95+
96+ # def foo() -> stat_result:
97+ # return (0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
98+
99+
100+ # x = foo
0 commit comments