LCOV - code coverage report
Current view: top level - extmod - vfs_fat_file.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.23.0-150-g6007f3e20.info Lines: 87 89 97.8 %
Date: 2024-07-26 11:35:34 Functions: 5 5 100.0 %
Branches: 33 36 91.7 %

           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) 2013, 2014 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/mpconfig.h"
      28                 :            : #if MICROPY_VFS && MICROPY_VFS_FAT
      29                 :            : 
      30                 :            : #include <stdio.h>
      31                 :            : 
      32                 :            : #include "py/runtime.h"
      33                 :            : #include "py/stream.h"
      34                 :            : #include "py/mperrno.h"
      35                 :            : #include "lib/oofatfs/ff.h"
      36                 :            : #include "extmod/vfs_fat.h"
      37                 :            : 
      38                 :            : // this table converts from FRESULT to POSIX errno
      39                 :            : const byte fresult_to_errno_table[20] = {
      40                 :            :     [FR_OK] = 0,
      41                 :            :     [FR_DISK_ERR] = MP_EIO,
      42                 :            :     [FR_INT_ERR] = MP_EIO,
      43                 :            :     [FR_NOT_READY] = MP_EBUSY,
      44                 :            :     [FR_NO_FILE] = MP_ENOENT,
      45                 :            :     [FR_NO_PATH] = MP_ENOENT,
      46                 :            :     [FR_INVALID_NAME] = MP_EINVAL,
      47                 :            :     [FR_DENIED] = MP_EACCES,
      48                 :            :     [FR_EXIST] = MP_EEXIST,
      49                 :            :     [FR_INVALID_OBJECT] = MP_EINVAL,
      50                 :            :     [FR_WRITE_PROTECTED] = MP_EROFS,
      51                 :            :     [FR_INVALID_DRIVE] = MP_ENODEV,
      52                 :            :     [FR_NOT_ENABLED] = MP_ENODEV,
      53                 :            :     [FR_NO_FILESYSTEM] = MP_ENODEV,
      54                 :            :     [FR_MKFS_ABORTED] = MP_EIO,
      55                 :            :     [FR_TIMEOUT] = MP_EIO,
      56                 :            :     [FR_LOCKED] = MP_EIO,
      57                 :            :     [FR_NOT_ENOUGH_CORE] = MP_ENOMEM,
      58                 :            :     [FR_TOO_MANY_OPEN_FILES] = MP_EMFILE,
      59                 :            :     [FR_INVALID_PARAMETER] = MP_EINVAL,
      60                 :            : };
      61                 :            : 
      62                 :            : typedef struct _pyb_file_obj_t {
      63                 :            :     mp_obj_base_t base;
      64                 :            :     FIL fp;
      65                 :            : } pyb_file_obj_t;
      66                 :            : 
      67                 :          4 : static void file_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
      68                 :          4 :     (void)kind;
      69                 :          4 :     mp_printf(print, "<io.%s %p>", mp_obj_get_type_str(self_in), MP_OBJ_TO_PTR(self_in));
      70                 :          4 : }
      71                 :            : 
      72                 :         56 : static mp_uint_t file_obj_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) {
      73                 :         56 :     pyb_file_obj_t *self = MP_OBJ_TO_PTR(self_in);
      74                 :         56 :     UINT sz_out;
      75                 :         56 :     FRESULT res = f_read(&self->fp, buf, size, &sz_out);
      76         [ +  + ]:         56 :     if (res != FR_OK) {
      77                 :          2 :         *errcode = fresult_to_errno_table[res];
      78                 :          2 :         return MP_STREAM_ERROR;
      79                 :            :     }
      80                 :         54 :     return sz_out;
      81                 :            : }
      82                 :            : 
      83                 :         56 : static mp_uint_t file_obj_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
      84                 :         56 :     pyb_file_obj_t *self = MP_OBJ_TO_PTR(self_in);
      85                 :         56 :     UINT sz_out;
      86                 :         56 :     FRESULT res = f_write(&self->fp, buf, size, &sz_out);
      87         [ +  + ]:         56 :     if (res != FR_OK) {
      88                 :          2 :         *errcode = fresult_to_errno_table[res];
      89                 :          2 :         return MP_STREAM_ERROR;
      90                 :            :     }
      91         [ +  + ]:         54 :     if (sz_out != size) {
      92                 :            :         // The FatFS documentation says that this means disk full.
      93                 :          2 :         *errcode = MP_ENOSPC;
      94                 :          2 :         return MP_STREAM_ERROR;
      95                 :            :     }
      96                 :            :     return sz_out;
      97                 :            : }
      98                 :            : 
      99                 :        176 : static mp_uint_t file_obj_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) {
     100                 :        176 :     pyb_file_obj_t *self = MP_OBJ_TO_PTR(o_in);
     101                 :            : 
     102         [ +  + ]:        176 :     if (request == MP_STREAM_SEEK) {
     103                 :         10 :         struct mp_stream_seek_t *s = (struct mp_stream_seek_t *)(uintptr_t)arg;
     104                 :            : 
     105   [ +  +  +  - ]:         10 :         switch (s->whence) {
     106                 :          2 :             case 0: // SEEK_SET
     107                 :          2 :                 f_lseek(&self->fp, s->offset);
     108                 :          2 :                 break;
     109                 :            : 
     110                 :          6 :             case 1: // SEEK_CUR
     111                 :          6 :                 f_lseek(&self->fp, f_tell(&self->fp) + s->offset);
     112                 :          6 :                 break;
     113                 :            : 
     114                 :          2 :             case 2: // SEEK_END
     115                 :          2 :                 f_lseek(&self->fp, f_size(&self->fp) + s->offset);
     116                 :          2 :                 break;
     117                 :            :         }
     118                 :            : 
     119                 :         10 :         s->offset = f_tell(&self->fp);
     120                 :         10 :         return 0;
     121                 :            : 
     122         [ +  + ]:        166 :     } else if (request == MP_STREAM_FLUSH) {
     123                 :          4 :         FRESULT res = f_sync(&self->fp);
     124         [ +  + ]:          4 :         if (res != FR_OK) {
     125                 :          2 :             *errcode = fresult_to_errno_table[res];
     126                 :          2 :             return MP_STREAM_ERROR;
     127                 :            :         }
     128                 :            :         return 0;
     129                 :            : 
     130         [ +  + ]:        162 :     } else if (request == MP_STREAM_CLOSE) {
     131                 :            :         // if fs==NULL then the file is closed and in that case this method is a no-op
     132         [ +  + ]:        160 :         if (self->fp.obj.fs != NULL) {
     133                 :         84 :             FRESULT res = f_close(&self->fp);
     134         [ -  + ]:         84 :             if (res != FR_OK) {
     135                 :          0 :                 *errcode = fresult_to_errno_table[res];
     136                 :          0 :                 return MP_STREAM_ERROR;
     137                 :            :             }
     138                 :            :         }
     139                 :        160 :         return 0;
     140                 :            : 
     141                 :            :     } else {
     142                 :          2 :         *errcode = MP_EINVAL;
     143                 :          2 :         return MP_STREAM_ERROR;
     144                 :            :     }
     145                 :            : }
     146                 :            : 
     147                 :            : // TODO gc hook to close the file if not already closed
     148                 :            : 
     149                 :            : static const mp_rom_map_elem_t vfs_fat_rawfile_locals_dict_table[] = {
     150                 :            :     { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
     151                 :            :     { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
     152                 :            :     { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
     153                 :            :     { MP_ROM_QSTR(MP_QSTR_readlines), MP_ROM_PTR(&mp_stream_unbuffered_readlines_obj) },
     154                 :            :     { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
     155                 :            :     { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) },
     156                 :            :     { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
     157                 :            :     { MP_ROM_QSTR(MP_QSTR_seek), MP_ROM_PTR(&mp_stream_seek_obj) },
     158                 :            :     { MP_ROM_QSTR(MP_QSTR_tell), MP_ROM_PTR(&mp_stream_tell_obj) },
     159                 :            :     { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mp_stream_close_obj) },
     160                 :            :     { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
     161                 :            :     { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&mp_stream___exit___obj) },
     162                 :            : };
     163                 :            : 
     164                 :            : static MP_DEFINE_CONST_DICT(vfs_fat_rawfile_locals_dict, vfs_fat_rawfile_locals_dict_table);
     165                 :            : 
     166                 :            : static const mp_stream_p_t vfs_fat_fileio_stream_p = {
     167                 :            :     .read = file_obj_read,
     168                 :            :     .write = file_obj_write,
     169                 :            :     .ioctl = file_obj_ioctl,
     170                 :            : };
     171                 :            : 
     172                 :            : MP_DEFINE_CONST_OBJ_TYPE(
     173                 :            :     mp_type_vfs_fat_fileio,
     174                 :            :     MP_QSTR_FileIO,
     175                 :            :     MP_TYPE_FLAG_ITER_IS_STREAM,
     176                 :            :     print, file_obj_print,
     177                 :            :     protocol, &vfs_fat_fileio_stream_p,
     178                 :            :     locals_dict, &vfs_fat_rawfile_locals_dict
     179                 :            :     );
     180                 :            : 
     181                 :            : static const mp_stream_p_t vfs_fat_textio_stream_p = {
     182                 :            :     .read = file_obj_read,
     183                 :            :     .write = file_obj_write,
     184                 :            :     .ioctl = file_obj_ioctl,
     185                 :            :     .is_text = true,
     186                 :            : };
     187                 :            : 
     188                 :            : MP_DEFINE_CONST_OBJ_TYPE(
     189                 :            :     mp_type_vfs_fat_textio,
     190                 :            :     MP_QSTR_TextIOWrapper,
     191                 :            :     MP_TYPE_FLAG_ITER_IS_STREAM,
     192                 :            :     print, file_obj_print,
     193                 :            :     protocol, &vfs_fat_textio_stream_p,
     194                 :            :     locals_dict, &vfs_fat_rawfile_locals_dict
     195                 :            :     );
     196                 :            : 
     197                 :            : // Factory function for I/O stream classes
     198                 :         88 : static mp_obj_t fat_vfs_open(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mode_in) {
     199                 :         88 :     fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
     200                 :            : 
     201                 :         88 :     const mp_obj_type_t *type = &mp_type_vfs_fat_textio;
     202                 :         88 :     int mode = 0;
     203                 :         88 :     const char *mode_s = mp_obj_str_get_str(mode_in);
     204                 :            :     // TODO make sure only one of r, w, x, a, and b, t are specified
     205         [ +  + ]:        192 :     while (*mode_s) {
     206   [ +  +  +  +  :        104 :         switch (*mode_s++) {
             +  +  +  - ]
     207                 :         26 :             case 'r':
     208                 :         26 :                 mode |= FA_READ;
     209                 :         26 :                 break;
     210                 :         58 :             case 'w':
     211                 :         58 :                 mode |= FA_WRITE | FA_CREATE_ALWAYS;
     212                 :         58 :                 break;
     213                 :          2 :             case 'x':
     214                 :          2 :                 mode |= FA_WRITE | FA_CREATE_NEW;
     215                 :          2 :                 break;
     216                 :          2 :             case 'a':
     217                 :          2 :                 mode |= FA_WRITE | FA_OPEN_ALWAYS;
     218                 :          2 :                 break;
     219                 :          4 :             case '+':
     220                 :          4 :                 mode |= FA_READ | FA_WRITE;
     221                 :          4 :                 break;
     222                 :          6 :             case 'b':
     223                 :          6 :                 type = &mp_type_vfs_fat_fileio;
     224                 :          6 :                 break;
     225                 :          6 :             case 't':
     226                 :          6 :                 type = &mp_type_vfs_fat_textio;
     227                 :          6 :                 break;
     228                 :            :         }
     229                 :            :     }
     230                 :            : 
     231                 :         88 :     pyb_file_obj_t *o = mp_obj_malloc_with_finaliser(pyb_file_obj_t, type);
     232                 :            : 
     233                 :         86 :     const char *fname = mp_obj_str_get_str(path_in);
     234                 :         86 :     FRESULT res = f_open(&self->fatfs, &o->fp, fname, mode);
     235         [ +  + ]:         86 :     if (res != FR_OK) {
     236                 :          2 :         m_del_obj(pyb_file_obj_t, o);
     237                 :          2 :         mp_raise_OSError(fresult_to_errno_table[res]);
     238                 :            :     }
     239                 :            : 
     240                 :            :     // for 'a' mode, we must begin at the end of the file
     241         [ +  + ]:         84 :     if ((mode & FA_OPEN_ALWAYS) != 0) {
     242                 :          2 :         f_lseek(&o->fp, f_size(&o->fp));
     243                 :            :     }
     244                 :            : 
     245                 :         84 :     return MP_OBJ_FROM_PTR(o);
     246                 :            : }
     247                 :            : MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_open_obj, fat_vfs_open);
     248                 :            : 
     249                 :            : #endif // MICROPY_VFS && MICROPY_VFS_FAT

Generated by: LCOV version 1.15-5-g462f71d