LCOV - code coverage report
Current view: top level - extmod - vfs_lfsx_file.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-344-ge60e8079a.info Lines: 104 108 96.3 %
Date: 2024-04-26 14:58:11 Functions: 12 12 100.0 %
Branches: 41 46 89.1 %

           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) 2019-2020 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                 :            : // This file should be compiled when included from vfs_lfs.c.
      28                 :            : #if defined(LFS_BUILD_VERSION)
      29                 :            : 
      30                 :            : #include <stdio.h>
      31                 :            : #include <string.h>
      32                 :            : 
      33                 :            : #include "py/runtime.h"
      34                 :            : #include "py/stream.h"
      35                 :            : #include "py/mperrno.h"
      36                 :            : #include "extmod/vfs.h"
      37                 :            : 
      38                 :       2208 : static void MP_VFS_LFSx(check_open)(MP_OBJ_VFS_LFSx_FILE * self) {
      39         [ -  + ]:       2208 :     if (self->vfs == NULL) {
      40                 :          0 :         mp_raise_ValueError(NULL);
      41                 :            :     }
      42                 :       2208 : }
      43                 :            : 
      44                 :          8 : static void MP_VFS_LFSx(file_print)(const mp_print_t * print, mp_obj_t self_in, mp_print_kind_t kind) {
      45                 :          8 :     (void)self_in;
      46                 :          8 :     (void)kind;
      47                 :          8 :     mp_printf(print, "<io.%s>", mp_obj_get_type_str(self_in));
      48                 :          8 : }
      49                 :            : 
      50                 :        178 : mp_obj_t MP_VFS_LFSx(file_open)(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mode_in) {
      51                 :        178 :     MP_OBJ_VFS_LFSx *self = MP_OBJ_TO_PTR(self_in);
      52                 :            : 
      53                 :        178 :     int flags = 0;
      54                 :        178 :     const mp_obj_type_t *type = &MP_TYPE_VFS_LFSx_(_textio);
      55                 :        178 :     const char *mode_str = mp_obj_str_get_str(mode_in);
      56         [ +  + ]:        584 :     for (; *mode_str; ++mode_str) {
      57                 :        228 :         int new_flags = 0;
      58   [ +  +  +  +  :        228 :         switch (*mode_str) {
             +  +  +  - ]
      59                 :         56 :             case 'r':
      60                 :         56 :                 new_flags = LFSx_MACRO(_O_RDONLY);
      61                 :         56 :                 break;
      62                 :         94 :             case 'w':
      63                 :         94 :                 new_flags = LFSx_MACRO(_O_WRONLY) | LFSx_MACRO(_O_CREAT) | LFSx_MACRO(_O_TRUNC);
      64                 :         94 :                 break;
      65                 :          8 :             case 'x':
      66                 :          8 :                 new_flags = LFSx_MACRO(_O_WRONLY) | LFSx_MACRO(_O_CREAT) | LFSx_MACRO(_O_EXCL);
      67                 :          8 :                 break;
      68                 :          8 :             case 'a':
      69                 :          8 :                 new_flags = LFSx_MACRO(_O_WRONLY) | LFSx_MACRO(_O_CREAT) | LFSx_MACRO(_O_APPEND);
      70                 :          8 :                 break;
      71                 :          4 :             case '+':
      72                 :          4 :                 flags |= LFSx_MACRO(_O_RDWR);
      73                 :          4 :                 break;
      74                 :         40 :             case 'b':
      75                 :         40 :                 type = &MP_TYPE_VFS_LFSx_(_fileio);
      76                 :         40 :                 break;
      77                 :         18 :             case 't':
      78                 :         18 :                 type = &MP_TYPE_VFS_LFSx_(_textio);
      79                 :         18 :                 break;
      80                 :            :         }
      81         [ +  + ]:        228 :         if (new_flags) {
      82         [ -  + ]:        166 :             if (flags) {
      83                 :          0 :                 mp_raise_ValueError(NULL);
      84                 :            :             }
      85                 :            :             flags = new_flags;
      86                 :            :         }
      87                 :            :     }
      88         [ +  + ]:        178 :     if (flags == 0) {
      89                 :         12 :         flags = LFSx_MACRO(_O_RDONLY);
      90                 :            :     }
      91                 :            : 
      92                 :            :     #if LFS_BUILD_VERSION == 1
      93                 :         72 :     MP_OBJ_VFS_LFSx_FILE *o = mp_obj_malloc_var_with_finaliser(MP_OBJ_VFS_LFSx_FILE, uint8_t, self->lfs.cfg->prog_size, type);
      94                 :            :     #else
      95                 :        106 :     MP_OBJ_VFS_LFSx_FILE *o = mp_obj_malloc_var_with_finaliser(MP_OBJ_VFS_LFSx_FILE, uint8_t, self->lfs.cfg->cache_size, type);
      96                 :            :     #endif
      97                 :        178 :     o->vfs = self;
      98                 :            :     #if !MICROPY_GC_CONSERVATIVE_CLEAR
      99                 :            :     memset(&o->file, 0, sizeof(o->file));
     100                 :            :     memset(&o->cfg, 0, sizeof(o->cfg));
     101                 :            :     #endif
     102                 :        178 :     o->cfg.buffer = &o->file_buffer[0];
     103                 :            : 
     104                 :            :     #if LFS_BUILD_VERSION == 2
     105         [ +  + ]:        106 :     if (self->enable_mtime) {
     106                 :        104 :         lfs_get_mtime(&o->mtime[0]);
     107                 :        104 :         o->attrs[0].type = LFS_ATTR_MTIME;
     108                 :        104 :         o->attrs[0].buffer = &o->mtime[0];
     109                 :        104 :         o->attrs[0].size = sizeof(o->mtime);
     110                 :        104 :         o->cfg.attrs = &o->attrs[0];
     111                 :        104 :         o->cfg.attr_count = MP_ARRAY_SIZE(o->attrs);
     112                 :            :     }
     113                 :            :     #endif
     114                 :            : 
     115                 :        178 :     const char *path = MP_VFS_LFSx(make_path)(self, path_in);
     116                 :        178 :     int ret = LFSx_API(file_opencfg)(&self->lfs, &o->file, path, flags, &o->cfg);
     117         [ +  + ]:        178 :     if (ret < 0) {
     118                 :         12 :         o->vfs = NULL;
     119                 :         12 :         mp_raise_OSError(-ret);
     120                 :            :     }
     121                 :            : 
     122                 :        166 :     return MP_OBJ_FROM_PTR(o);
     123                 :            : }
     124                 :            : 
     125                 :         88 : static mp_uint_t MP_VFS_LFSx(file_read)(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) {
     126                 :         88 :     MP_OBJ_VFS_LFSx_FILE *self = MP_OBJ_TO_PTR(self_in);
     127                 :         88 :     MP_VFS_LFSx(check_open)(self);
     128                 :         88 :     LFSx_API(ssize_t) sz = LFSx_API(file_read)(&self->vfs->lfs, &self->file, buf, size);
     129         [ +  + ]:         88 :     if (sz < 0) {
     130                 :          4 :         *errcode = -sz;
     131                 :          4 :         return MP_STREAM_ERROR;
     132                 :            :     }
     133                 :         84 :     return sz;
     134                 :            : }
     135                 :            : 
     136                 :       2080 : static mp_uint_t MP_VFS_LFSx(file_write)(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
     137                 :       2080 :     MP_OBJ_VFS_LFSx_FILE *self = MP_OBJ_TO_PTR(self_in);
     138                 :       2080 :     MP_VFS_LFSx(check_open)(self);
     139                 :            :     #if LFS_BUILD_VERSION == 2
     140         [ +  - ]:       1050 :     if (self->vfs->enable_mtime) {
     141                 :       1050 :         lfs_get_mtime(&self->mtime[0]);
     142                 :            :     }
     143                 :            :     #endif
     144                 :       2080 :     LFSx_API(ssize_t) sz = LFSx_API(file_write)(&self->vfs->lfs, &self->file, buf, size);
     145         [ +  + ]:       2080 :     if (sz < 0) {
     146                 :          4 :         *errcode = -sz;
     147                 :          4 :         return MP_STREAM_ERROR;
     148                 :            :     }
     149                 :       2076 :     return sz;
     150                 :            : }
     151                 :            : 
     152                 :        380 : static mp_uint_t MP_VFS_LFSx(file_ioctl)(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
     153                 :        380 :     MP_OBJ_VFS_LFSx_FILE *self = MP_OBJ_TO_PTR(self_in);
     154                 :            : 
     155         [ +  + ]:        380 :     if (request != MP_STREAM_CLOSE) {
     156                 :         40 :         MP_VFS_LFSx(check_open)(self);
     157                 :            :     }
     158                 :            : 
     159         [ +  + ]:         40 :     if (request == MP_STREAM_SEEK) {
     160                 :         20 :         struct mp_stream_seek_t *s = (struct mp_stream_seek_t *)(uintptr_t)arg;
     161                 :         20 :         int res = LFSx_API(file_seek)(&self->vfs->lfs, &self->file, s->offset, s->whence);
     162         [ +  + ]:         20 :         if (res < 0) {
     163                 :          4 :             *errcode = -res;
     164                 :          4 :             return MP_STREAM_ERROR;
     165                 :            :         }
     166                 :         16 :         res = LFSx_API(file_tell)(&self->vfs->lfs, &self->file);
     167         [ -  + ]:         16 :         if (res < 0) {
     168                 :          0 :             *errcode = -res;
     169                 :          0 :             return MP_STREAM_ERROR;
     170                 :            :         }
     171                 :         16 :         s->offset = res;
     172                 :         16 :         return 0;
     173         [ +  + ]:        360 :     } else if (request == MP_STREAM_FLUSH) {
     174                 :          8 :         int res = LFSx_API(file_sync)(&self->vfs->lfs, &self->file);
     175         [ +  + ]:          8 :         if (res < 0) {
     176                 :          4 :             *errcode = -res;
     177                 :          4 :             return MP_STREAM_ERROR;
     178                 :            :         }
     179                 :            :         return 0;
     180         [ +  + ]:        352 :     } else if (request == MP_STREAM_CLOSE) {
     181         [ +  + ]:        340 :         if (self->vfs == NULL) {
     182                 :            :             return 0;
     183                 :            :         }
     184                 :        166 :         int res = LFSx_API(file_close)(&self->vfs->lfs, &self->file);
     185                 :        166 :         self->vfs = NULL; // indicate a closed file
     186         [ +  + ]:        166 :         if (res < 0) {
     187                 :          4 :             *errcode = -res;
     188                 :          4 :             return MP_STREAM_ERROR;
     189                 :            :         }
     190                 :            :         return 0;
     191                 :            :     } else {
     192                 :         12 :         *errcode = MP_EINVAL;
     193                 :         12 :         return MP_STREAM_ERROR;
     194                 :            :     }
     195                 :            : }
     196                 :            : 
     197                 :            : static const mp_rom_map_elem_t MP_VFS_LFSx(file_locals_dict_table)[] = {
     198                 :            :     { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
     199                 :            :     { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
     200                 :            :     { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
     201                 :            :     { MP_ROM_QSTR(MP_QSTR_readlines), MP_ROM_PTR(&mp_stream_unbuffered_readlines_obj) },
     202                 :            :     { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
     203                 :            :     { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) },
     204                 :            :     { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
     205                 :            :     { MP_ROM_QSTR(MP_QSTR_seek), MP_ROM_PTR(&mp_stream_seek_obj) },
     206                 :            :     { MP_ROM_QSTR(MP_QSTR_tell), MP_ROM_PTR(&mp_stream_tell_obj) },
     207                 :            :     { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mp_stream_close_obj) },
     208                 :            :     { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
     209                 :            :     { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&mp_stream___exit___obj) },
     210                 :            : };
     211                 :            : static MP_DEFINE_CONST_DICT(MP_VFS_LFSx(file_locals_dict), MP_VFS_LFSx(file_locals_dict_table));
     212                 :            : 
     213                 :            : static const mp_stream_p_t MP_VFS_LFSx(fileio_stream_p) = {
     214                 :            :     .read = MP_VFS_LFSx(file_read),
     215                 :            :     .write = MP_VFS_LFSx(file_write),
     216                 :            :     .ioctl = MP_VFS_LFSx(file_ioctl),
     217                 :            : };
     218                 :            : 
     219                 :            : MP_DEFINE_CONST_OBJ_TYPE(
     220                 :            :     MP_TYPE_VFS_LFSx_(_fileio),
     221                 :            :     MP_QSTR_FileIO,
     222                 :            :     MP_TYPE_FLAG_ITER_IS_STREAM,
     223                 :            :     print, MP_VFS_LFSx(file_print),
     224                 :            :     protocol, &MP_VFS_LFSx(fileio_stream_p),
     225                 :            :     locals_dict, &MP_VFS_LFSx(file_locals_dict)
     226                 :            :     );
     227                 :            : 
     228                 :            : static const mp_stream_p_t MP_VFS_LFSx(textio_stream_p) = {
     229                 :            :     .read = MP_VFS_LFSx(file_read),
     230                 :            :     .write = MP_VFS_LFSx(file_write),
     231                 :            :     .ioctl = MP_VFS_LFSx(file_ioctl),
     232                 :            :     .is_text = true,
     233                 :            : };
     234                 :            : 
     235                 :            : MP_DEFINE_CONST_OBJ_TYPE(
     236                 :            :     MP_TYPE_VFS_LFSx_(_textio),
     237                 :            :     MP_QSTR_TextIOWrapper,
     238                 :            :     MP_TYPE_FLAG_ITER_IS_STREAM,
     239                 :            :     print, MP_VFS_LFSx(file_print),
     240                 :            :     protocol, &MP_VFS_LFSx(textio_stream_p),
     241                 :            :     locals_dict, &MP_VFS_LFSx(file_locals_dict)
     242                 :            :     );
     243                 :            : 
     244                 :            : #endif // defined(LFS_BUILD_VERSION)

Generated by: LCOV version 1.15-5-g462f71d