LCOV - code coverage report
Current view: top level - extmod - vfs_posix.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-314-gc3e37d1fa.info Lines: 170 180 94.4 %
Date: 2024-03-29 04:35:19 Functions: 19 19 100.0 %
Branches: 55 74 74.3 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * This file is part of the MicroPython project, http://micropython.org/
       3                 :            :  *
       4                 :            :  * The MIT License (MIT)
       5                 :            :  *
       6                 :            :  * Copyright (c) 2017-2018 Damien P. George
       7                 :            :  *
       8                 :            :  * Permission is hereby granted, free of charge, to any person obtaining a copy
       9                 :            :  * of this software and associated documentation files (the "Software"), to deal
      10                 :            :  * in the Software without restriction, including without limitation the rights
      11                 :            :  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      12                 :            :  * copies of the Software, and to permit persons to whom the Software is
      13                 :            :  * furnished to do so, subject to the following conditions:
      14                 :            :  *
      15                 :            :  * The above copyright notice and this permission notice shall be included in
      16                 :            :  * all copies or substantial portions of the Software.
      17                 :            :  *
      18                 :            :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      19                 :            :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      20                 :            :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      21                 :            :  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      22                 :            :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      23                 :            :  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      24                 :            :  * THE SOFTWARE.
      25                 :            :  */
      26                 :            : 
      27                 :            : #include "py/runtime.h"
      28                 :            : #include "py/mperrno.h"
      29                 :            : #include "py/mphal.h"
      30                 :            : #include "py/mpthread.h"
      31                 :            : #include "extmod/vfs.h"
      32                 :            : #include "extmod/vfs_posix.h"
      33                 :            : 
      34                 :            : #if MICROPY_VFS_POSIX
      35                 :            : 
      36                 :            : #if !MICROPY_ENABLE_FINALISER
      37                 :            : #error "MICROPY_VFS_POSIX requires MICROPY_ENABLE_FINALISER"
      38                 :            : #endif
      39                 :            : 
      40                 :            : #include <errno.h>
      41                 :            : #include <stdio.h>
      42                 :            : #include <string.h>
      43                 :            : #include <sys/stat.h>
      44                 :            : #include <unistd.h>
      45                 :            : #include <dirent.h>
      46                 :            : #ifdef _MSC_VER
      47                 :            : #include <direct.h> // For mkdir etc.
      48                 :            : #endif
      49                 :            : #ifdef _WIN32
      50                 :            : #include <windows.h>
      51                 :            : #endif
      52                 :            : 
      53                 :            : typedef struct _mp_obj_vfs_posix_t {
      54                 :            :     mp_obj_base_t base;
      55                 :            :     vstr_t root;
      56                 :            :     size_t root_len;
      57                 :            :     bool readonly;
      58                 :            : } mp_obj_vfs_posix_t;
      59                 :            : 
      60                 :        320 : static const char *vfs_posix_get_path_str(mp_obj_vfs_posix_t *self, mp_obj_t path) {
      61                 :        320 :     const char *path_str = mp_obj_str_get_str(path);
      62   [ +  +  +  + ]:        320 :     if (self->root_len == 0 || path_str[0] != '/') {
      63                 :            :         return path_str;
      64                 :            :     } else {
      65                 :        122 :         self->root.len = self->root_len - 1;
      66                 :        122 :         vstr_add_str(&self->root, path_str);
      67                 :        122 :         return vstr_null_terminated_str(&self->root);
      68                 :            :     }
      69                 :            : }
      70                 :            : 
      71                 :       3632 : static mp_obj_t vfs_posix_get_path_obj(mp_obj_vfs_posix_t *self, mp_obj_t path) {
      72                 :       3632 :     const char *path_str = mp_obj_str_get_str(path);
      73   [ +  +  +  + ]:       3632 :     if (self->root_len == 0 || path_str[0] != '/') {
      74                 :            :         return path;
      75                 :            :     } else {
      76                 :         26 :         self->root.len = self->root_len - 1;
      77                 :         26 :         vstr_add_str(&self->root, path_str);
      78                 :         26 :         return mp_obj_new_str(self->root.buf, self->root.len);
      79                 :            :     }
      80                 :            : }
      81                 :            : 
      82                 :        150 : static mp_obj_t vfs_posix_fun1_helper(mp_obj_t self_in, mp_obj_t path_in, int (*f)(const char *)) {
      83                 :        150 :     mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
      84                 :        150 :     int ret = f(vfs_posix_get_path_str(self, path_in));
      85         [ +  + ]:        150 :     if (ret != 0) {
      86                 :          8 :         mp_raise_OSError(errno);
      87                 :            :     }
      88                 :        142 :     return mp_const_none;
      89                 :            : }
      90                 :            : 
      91                 :       9407 : static mp_import_stat_t mp_vfs_posix_import_stat(void *self_in, const char *path) {
      92                 :       9407 :     mp_obj_vfs_posix_t *self = self_in;
      93         [ -  + ]:       9407 :     if (self->root_len != 0) {
      94                 :          0 :         self->root.len = self->root_len;
      95                 :          0 :         vstr_add_str(&self->root, path);
      96                 :          0 :         path = vstr_null_terminated_str(&self->root);
      97                 :            :     }
      98                 :       9407 :     struct stat st;
      99         [ +  + ]:       9407 :     if (stat(path, &st) == 0) {
     100         [ +  + ]:       1640 :         if (S_ISDIR(st.st_mode)) {
     101                 :            :             return MP_IMPORT_STAT_DIR;
     102         [ +  - ]:       1552 :         } else if (S_ISREG(st.st_mode)) {
     103                 :       1552 :             return MP_IMPORT_STAT_FILE;
     104                 :            :         }
     105                 :            :     }
     106                 :            :     return MP_IMPORT_STAT_NO_EXIST;
     107                 :            : }
     108                 :            : 
     109                 :       3320 : static mp_obj_t vfs_posix_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
     110                 :       3320 :     mp_arg_check_num(n_args, n_kw, 0, 1, false);
     111                 :            : 
     112                 :       3320 :     mp_obj_vfs_posix_t *vfs = mp_obj_malloc(mp_obj_vfs_posix_t, type);
     113                 :       3320 :     vstr_init(&vfs->root, 0);
     114         [ +  + ]:       3320 :     if (n_args == 1) {
     115                 :         12 :         const char *root = mp_obj_str_get_str(args[0]);
     116                 :            :         // if the root is relative, make it absolute, otherwise we'll get confused by chdir
     117                 :            :         #ifdef _WIN32
     118                 :            :         char buf[MICROPY_ALLOC_PATH_MAX + 1];
     119                 :            :         DWORD result = GetFullPathNameA(root, sizeof(buf), buf, NULL);
     120                 :            :         if (result > 0 && result < sizeof(buf)) {
     121                 :            :             vstr_add_str(&vfs->root, buf);
     122                 :            :         } else {
     123                 :            :             mp_raise_OSError(GetLastError());
     124                 :            :         }
     125                 :            :         #else
     126         [ +  + ]:         12 :         if (root[0] != '\0' && root[0] != '/') {
     127                 :          8 :             char buf[MICROPY_ALLOC_PATH_MAX + 1];
     128                 :          8 :             const char *cwd = getcwd(buf, sizeof(buf));
     129         [ +  + ]:          8 :             if (cwd == NULL) {
     130                 :          2 :                 mp_raise_OSError(errno);
     131                 :            :             }
     132                 :          6 :             vstr_add_str(&vfs->root, cwd);
     133                 :          6 :             vstr_add_char(&vfs->root, '/');
     134                 :            :         }
     135                 :         10 :         vstr_add_str(&vfs->root, root);
     136                 :            :         #endif
     137                 :         10 :         vstr_add_char(&vfs->root, '/');
     138                 :            :     }
     139                 :       3318 :     vfs->root_len = vfs->root.len;
     140                 :       3318 :     vfs->readonly = false;
     141                 :            : 
     142                 :       3318 :     return MP_OBJ_FROM_PTR(vfs);
     143                 :            : }
     144                 :            : 
     145                 :       3310 : static mp_obj_t vfs_posix_mount(mp_obj_t self_in, mp_obj_t readonly, mp_obj_t mkfs) {
     146                 :       3310 :     mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
     147         [ -  + ]:       3310 :     if (mp_obj_is_true(readonly)) {
     148                 :          0 :         self->readonly = true;
     149                 :            :     }
     150         [ -  + ]:       3310 :     if (mp_obj_is_true(mkfs)) {
     151                 :          0 :         mp_raise_OSError(MP_EPERM);
     152                 :            :     }
     153                 :       3310 :     return mp_const_none;
     154                 :            : }
     155                 :            : static MP_DEFINE_CONST_FUN_OBJ_3(vfs_posix_mount_obj, vfs_posix_mount);
     156                 :            : 
     157                 :         10 : static mp_obj_t vfs_posix_umount(mp_obj_t self_in) {
     158                 :         10 :     (void)self_in;
     159                 :         10 :     return mp_const_none;
     160                 :            : }
     161                 :            : static MP_DEFINE_CONST_FUN_OBJ_1(vfs_posix_umount_obj, vfs_posix_umount);
     162                 :            : 
     163                 :       3632 : static mp_obj_t vfs_posix_open(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mode_in) {
     164                 :       3632 :     mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
     165                 :       3632 :     const char *mode = mp_obj_str_get_str(mode_in);
     166         [ -  + ]:       3632 :     if (self->readonly
     167   [ #  #  #  #  :          0 :         && (strchr(mode, 'w') != NULL || strchr(mode, 'a') != NULL || strchr(mode, '+') != NULL)) {
                   #  # ]
     168                 :          0 :         mp_raise_OSError(MP_EROFS);
     169                 :            :     }
     170         [ +  - ]:       3632 :     if (!mp_obj_is_small_int(path_in)) {
     171                 :       3632 :         path_in = vfs_posix_get_path_obj(self, path_in);
     172                 :            :     }
     173                 :       3632 :     return mp_vfs_posix_file_open(&mp_type_vfs_posix_textio, path_in, mode_in);
     174                 :            : }
     175                 :            : static MP_DEFINE_CONST_FUN_OBJ_3(vfs_posix_open_obj, vfs_posix_open);
     176                 :            : 
     177                 :         42 : static mp_obj_t vfs_posix_chdir(mp_obj_t self_in, mp_obj_t path_in) {
     178                 :         42 :     return vfs_posix_fun1_helper(self_in, path_in, chdir);
     179                 :            : }
     180                 :            : static MP_DEFINE_CONST_FUN_OBJ_2(vfs_posix_chdir_obj, vfs_posix_chdir);
     181                 :            : 
     182                 :         28 : static mp_obj_t vfs_posix_getcwd(mp_obj_t self_in) {
     183                 :         28 :     mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
     184                 :         28 :     char buf[MICROPY_ALLOC_PATH_MAX + 1];
     185                 :         28 :     const char *ret = getcwd(buf, sizeof(buf));
     186         [ +  + ]:         28 :     if (ret == NULL) {
     187                 :          2 :         mp_raise_OSError(errno);
     188                 :            :     }
     189         [ +  + ]:         26 :     if (self->root_len > 0) {
     190                 :         12 :         ret += self->root_len - 1;
     191                 :            :         #ifdef _WIN32
     192                 :            :         if (*ret == '\\') {
     193                 :            :             *(char *)ret = '/';
     194                 :            :         }
     195                 :            :         #endif
     196                 :            :     }
     197                 :         26 :     return mp_obj_new_str(ret, strlen(ret));
     198                 :            : }
     199                 :            : static MP_DEFINE_CONST_FUN_OBJ_1(vfs_posix_getcwd_obj, vfs_posix_getcwd);
     200                 :            : 
     201                 :            : typedef struct _vfs_posix_ilistdir_it_t {
     202                 :            :     mp_obj_base_t base;
     203                 :            :     mp_fun_1_t iternext;
     204                 :            :     mp_fun_1_t finaliser;
     205                 :            :     bool is_str;
     206                 :            :     DIR *dir;
     207                 :            : } vfs_posix_ilistdir_it_t;
     208                 :            : 
     209                 :       1398 : static mp_obj_t vfs_posix_ilistdir_it_iternext(mp_obj_t self_in) {
     210                 :       1398 :     vfs_posix_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in);
     211                 :            : 
     212         [ +  + ]:       1398 :     if (self->dir == NULL) {
     213                 :            :         return MP_OBJ_STOP_ITERATION;
     214                 :            :     }
     215                 :            : 
     216                 :       1586 :     for (;;) {
     217                 :       1482 :         MP_THREAD_GIL_EXIT();
     218                 :       1482 :         struct dirent *dirent = readdir(self->dir);
     219         [ +  + ]:       1482 :         if (dirent == NULL) {
     220                 :         52 :             closedir(self->dir);
     221                 :         52 :             MP_THREAD_GIL_ENTER();
     222                 :         52 :             self->dir = NULL;
     223                 :         52 :             return MP_OBJ_STOP_ITERATION;
     224                 :            :         }
     225                 :       1430 :         MP_THREAD_GIL_ENTER();
     226                 :       1430 :         const char *fn = dirent->d_name;
     227                 :            : 
     228   [ +  +  +  +  :       1430 :         if (fn[0] == '.' && (fn[1] == 0 || (fn[1] == '.' && fn[2] == 0))) {
             +  +  +  + ]
     229                 :            :             // skip . and ..
     230                 :        104 :             continue;
     231                 :            :         }
     232                 :            : 
     233                 :            :         // make 3-tuple with info about this entry
     234                 :       1326 :         mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
     235                 :            : 
     236         [ +  + ]:       1326 :         if (self->is_str) {
     237                 :       1324 :             t->items[0] = mp_obj_new_str(fn, strlen(fn));
     238                 :            :         } else {
     239                 :          2 :             t->items[0] = mp_obj_new_bytes((const byte *)fn, strlen(fn));
     240                 :            :         }
     241                 :            : 
     242                 :            :         #ifdef _DIRENT_HAVE_D_TYPE
     243                 :            :         #ifdef DTTOIF
     244                 :       1326 :         t->items[1] = MP_OBJ_NEW_SMALL_INT(DTTOIF(dirent->d_type));
     245                 :            :         #else
     246                 :            :         if (dirent->d_type == DT_DIR) {
     247                 :            :             t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFDIR);
     248                 :            :         } else if (dirent->d_type == DT_REG) {
     249                 :            :             t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFREG);
     250                 :            :         } else {
     251                 :            :             t->items[1] = MP_OBJ_NEW_SMALL_INT(dirent->d_type);
     252                 :            :         }
     253                 :            :         #endif
     254                 :            :         #else
     255                 :            :         // DT_UNKNOWN should have 0 value on any reasonable system
     256                 :            :         t->items[1] = MP_OBJ_NEW_SMALL_INT(0);
     257                 :            :         #endif
     258                 :            : 
     259                 :            :         #ifdef _DIRENT_HAVE_D_INO
     260                 :       1326 :         t->items[2] = MP_OBJ_NEW_SMALL_INT(dirent->d_ino);
     261                 :            :         #else
     262                 :            :         t->items[2] = MP_OBJ_NEW_SMALL_INT(0);
     263                 :            :         #endif
     264                 :            : 
     265                 :       1326 :         return MP_OBJ_FROM_PTR(t);
     266                 :            :     }
     267                 :            : }
     268                 :            : 
     269                 :         92 : static mp_obj_t vfs_posix_ilistdir_it_del(mp_obj_t self_in) {
     270                 :         92 :     vfs_posix_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in);
     271         [ +  + ]:         92 :     if (self->dir != NULL) {
     272                 :         40 :         MP_THREAD_GIL_EXIT();
     273                 :         40 :         closedir(self->dir);
     274                 :         92 :         MP_THREAD_GIL_ENTER();
     275                 :            :     }
     276                 :         92 :     return mp_const_none;
     277                 :            : }
     278                 :            : 
     279                 :         92 : static mp_obj_t vfs_posix_ilistdir(mp_obj_t self_in, mp_obj_t path_in) {
     280                 :         92 :     mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
     281                 :         92 :     vfs_posix_ilistdir_it_t *iter = mp_obj_malloc_with_finaliser(vfs_posix_ilistdir_it_t, &mp_type_polymorph_iter_with_finaliser);
     282                 :         92 :     iter->iternext = vfs_posix_ilistdir_it_iternext;
     283                 :         92 :     iter->finaliser = vfs_posix_ilistdir_it_del;
     284                 :         92 :     iter->is_str = mp_obj_get_type(path_in) == &mp_type_str;
     285                 :         92 :     const char *path = vfs_posix_get_path_str(self, path_in);
     286         [ +  + ]:         92 :     if (path[0] == '\0') {
     287                 :          4 :         path = ".";
     288                 :            :     }
     289                 :         92 :     MP_THREAD_GIL_EXIT();
     290                 :         92 :     iter->dir = opendir(path);
     291                 :         92 :     MP_THREAD_GIL_ENTER();
     292         [ -  + ]:         92 :     if (iter->dir == NULL) {
     293                 :          0 :         mp_raise_OSError(errno);
     294                 :            :     }
     295                 :         92 :     return MP_OBJ_FROM_PTR(iter);
     296                 :            : }
     297                 :            : static MP_DEFINE_CONST_FUN_OBJ_2(vfs_posix_ilistdir_obj, vfs_posix_ilistdir);
     298                 :            : 
     299                 :            : typedef struct _mp_obj_listdir_t {
     300                 :            :     mp_obj_base_t base;
     301                 :            :     mp_fun_1_t iternext;
     302                 :            :     DIR *dir;
     303                 :            : } mp_obj_listdir_t;
     304                 :            : 
     305                 :         58 : static mp_obj_t vfs_posix_mkdir(mp_obj_t self_in, mp_obj_t path_in) {
     306                 :         58 :     mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
     307                 :         58 :     const char *path = vfs_posix_get_path_str(self, path_in);
     308                 :         58 :     MP_THREAD_GIL_EXIT();
     309                 :            :     #ifdef _WIN32
     310                 :            :     int ret = mkdir(path);
     311                 :            :     #else
     312                 :         58 :     int ret = mkdir(path, 0777);
     313                 :            :     #endif
     314                 :         58 :     MP_THREAD_GIL_ENTER();
     315         [ -  + ]:         58 :     if (ret != 0) {
     316                 :          0 :         mp_raise_OSError(errno);
     317                 :            :     }
     318                 :         58 :     return mp_const_none;
     319                 :            : }
     320                 :            : static MP_DEFINE_CONST_FUN_OBJ_2(vfs_posix_mkdir_obj, vfs_posix_mkdir);
     321                 :            : 
     322                 :         48 : static mp_obj_t vfs_posix_remove(mp_obj_t self_in, mp_obj_t path_in) {
     323                 :         48 :     return vfs_posix_fun1_helper(self_in, path_in, unlink);
     324                 :            : }
     325                 :            : static MP_DEFINE_CONST_FUN_OBJ_2(vfs_posix_remove_obj, vfs_posix_remove);
     326                 :            : 
     327                 :          2 : static mp_obj_t vfs_posix_rename(mp_obj_t self_in, mp_obj_t old_path_in, mp_obj_t new_path_in) {
     328                 :          2 :     mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
     329                 :          2 :     const char *old_path = vfs_posix_get_path_str(self, old_path_in);
     330                 :          2 :     const char *new_path = vfs_posix_get_path_str(self, new_path_in);
     331                 :          2 :     MP_THREAD_GIL_EXIT();
     332                 :          2 :     int ret = rename(old_path, new_path);
     333                 :          2 :     MP_THREAD_GIL_ENTER();
     334         [ -  + ]:          2 :     if (ret != 0) {
     335                 :          0 :         mp_raise_OSError(errno);
     336                 :            :     }
     337                 :          2 :     return mp_const_none;
     338                 :            : }
     339                 :            : static MP_DEFINE_CONST_FUN_OBJ_3(vfs_posix_rename_obj, vfs_posix_rename);
     340                 :            : 
     341                 :         60 : static mp_obj_t vfs_posix_rmdir(mp_obj_t self_in, mp_obj_t path_in) {
     342                 :         60 :     return vfs_posix_fun1_helper(self_in, path_in, rmdir);
     343                 :            : }
     344                 :            : static MP_DEFINE_CONST_FUN_OBJ_2(vfs_posix_rmdir_obj, vfs_posix_rmdir);
     345                 :            : 
     346                 :         14 : static mp_obj_t vfs_posix_stat(mp_obj_t self_in, mp_obj_t path_in) {
     347                 :         14 :     mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
     348                 :         14 :     struct stat sb;
     349                 :         14 :     const char *path = vfs_posix_get_path_str(self, path_in);
     350                 :         14 :     int ret;
     351   [ +  +  -  + ]:         14 :     MP_HAL_RETRY_SYSCALL(ret, stat(path, &sb), mp_raise_OSError(err));
     352                 :          2 :     mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
     353                 :          2 :     t->items[0] = MP_OBJ_NEW_SMALL_INT(sb.st_mode);
     354                 :          2 :     t->items[1] = mp_obj_new_int_from_uint(sb.st_ino);
     355                 :          2 :     t->items[2] = mp_obj_new_int_from_uint(sb.st_dev);
     356                 :          2 :     t->items[3] = mp_obj_new_int_from_uint(sb.st_nlink);
     357                 :          2 :     t->items[4] = mp_obj_new_int_from_uint(sb.st_uid);
     358                 :          2 :     t->items[5] = mp_obj_new_int_from_uint(sb.st_gid);
     359                 :          2 :     t->items[6] = mp_obj_new_int_from_uint(sb.st_size);
     360                 :          2 :     t->items[7] = mp_obj_new_int_from_uint(sb.st_atime);
     361                 :          2 :     t->items[8] = mp_obj_new_int_from_uint(sb.st_mtime);
     362                 :          2 :     t->items[9] = mp_obj_new_int_from_uint(sb.st_ctime);
     363                 :          2 :     return MP_OBJ_FROM_PTR(t);
     364                 :            : }
     365                 :            : static MP_DEFINE_CONST_FUN_OBJ_2(vfs_posix_stat_obj, vfs_posix_stat);
     366                 :            : 
     367                 :            : #if MICROPY_PY_OS_STATVFS
     368                 :            : 
     369                 :            : #ifdef __ANDROID__
     370                 :            : #define USE_STATFS 1
     371                 :            : #endif
     372                 :            : 
     373                 :            : #if USE_STATFS
     374                 :            : #include <sys/vfs.h>
     375                 :            : #define STRUCT_STATVFS struct statfs
     376                 :            : #define STATVFS statfs
     377                 :            : #define F_FAVAIL sb.f_ffree
     378                 :            : #define F_NAMEMAX sb.f_namelen
     379                 :            : #define F_FLAG sb.f_flags
     380                 :            : #else
     381                 :            : #include <sys/statvfs.h>
     382                 :            : #define STRUCT_STATVFS struct statvfs
     383                 :            : #define STATVFS statvfs
     384                 :            : #define F_FAVAIL sb.f_favail
     385                 :            : #define F_NAMEMAX sb.f_namemax
     386                 :            : #define F_FLAG sb.f_flag
     387                 :            : #endif
     388                 :            : 
     389                 :          2 : static mp_obj_t vfs_posix_statvfs(mp_obj_t self_in, mp_obj_t path_in) {
     390                 :          2 :     mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
     391                 :          2 :     STRUCT_STATVFS sb;
     392                 :          2 :     const char *path = vfs_posix_get_path_str(self, path_in);
     393                 :          2 :     int ret;
     394   [ -  +  -  - ]:          2 :     MP_HAL_RETRY_SYSCALL(ret, STATVFS(path, &sb), mp_raise_OSError(err));
     395                 :          2 :     mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
     396                 :          2 :     t->items[0] = MP_OBJ_NEW_SMALL_INT(sb.f_bsize);
     397                 :          2 :     t->items[1] = MP_OBJ_NEW_SMALL_INT(sb.f_frsize);
     398                 :          2 :     t->items[2] = MP_OBJ_NEW_SMALL_INT(sb.f_blocks);
     399                 :          2 :     t->items[3] = MP_OBJ_NEW_SMALL_INT(sb.f_bfree);
     400                 :          2 :     t->items[4] = MP_OBJ_NEW_SMALL_INT(sb.f_bavail);
     401                 :          2 :     t->items[5] = MP_OBJ_NEW_SMALL_INT(sb.f_files);
     402                 :          2 :     t->items[6] = MP_OBJ_NEW_SMALL_INT(sb.f_ffree);
     403                 :          2 :     t->items[7] = MP_OBJ_NEW_SMALL_INT(F_FAVAIL);
     404                 :          2 :     t->items[8] = MP_OBJ_NEW_SMALL_INT(F_FLAG);
     405                 :          2 :     t->items[9] = MP_OBJ_NEW_SMALL_INT(F_NAMEMAX);
     406                 :          2 :     return MP_OBJ_FROM_PTR(t);
     407                 :            : }
     408                 :            : static MP_DEFINE_CONST_FUN_OBJ_2(vfs_posix_statvfs_obj, vfs_posix_statvfs);
     409                 :            : 
     410                 :            : #endif
     411                 :            : 
     412                 :            : static const mp_rom_map_elem_t vfs_posix_locals_dict_table[] = {
     413                 :            :     { MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&vfs_posix_mount_obj) },
     414                 :            :     { MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&vfs_posix_umount_obj) },
     415                 :            :     { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&vfs_posix_open_obj) },
     416                 :            : 
     417                 :            :     { MP_ROM_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&vfs_posix_chdir_obj) },
     418                 :            :     { MP_ROM_QSTR(MP_QSTR_getcwd), MP_ROM_PTR(&vfs_posix_getcwd_obj) },
     419                 :            :     { MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&vfs_posix_ilistdir_obj) },
     420                 :            :     { MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&vfs_posix_mkdir_obj) },
     421                 :            :     { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&vfs_posix_remove_obj) },
     422                 :            :     { MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&vfs_posix_rename_obj) },
     423                 :            :     { MP_ROM_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&vfs_posix_rmdir_obj) },
     424                 :            :     { MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&vfs_posix_stat_obj) },
     425                 :            :     #if MICROPY_PY_OS_STATVFS
     426                 :            :     { MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&vfs_posix_statvfs_obj) },
     427                 :            :     #endif
     428                 :            : };
     429                 :            : static MP_DEFINE_CONST_DICT(vfs_posix_locals_dict, vfs_posix_locals_dict_table);
     430                 :            : 
     431                 :            : static const mp_vfs_proto_t vfs_posix_proto = {
     432                 :            :     .import_stat = mp_vfs_posix_import_stat,
     433                 :            : };
     434                 :            : 
     435                 :            : MP_DEFINE_CONST_OBJ_TYPE(
     436                 :            :     mp_type_vfs_posix,
     437                 :            :     MP_QSTR_VfsPosix,
     438                 :            :     MP_TYPE_FLAG_NONE,
     439                 :            :     make_new, vfs_posix_make_new,
     440                 :            :     protocol, &vfs_posix_proto,
     441                 :            :     locals_dict, &vfs_posix_locals_dict
     442                 :            :     );
     443                 :            : 
     444                 :            : #endif // MICROPY_VFS_POSIX

Generated by: LCOV version 1.15-5-g462f71d