diff options
Diffstat (limited to 'extmod/vfs_fat_file.c')
-rw-r--r-- | extmod/vfs_fat_file.c | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index 77848b5a5..6651d70d0 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -36,7 +36,12 @@ #include "py/runtime.h" #include "py/stream.h" #include "py/mperrno.h" +#if MICROPY_FATFS_OO +#include "lib/oofatfs/ff.h" +#else #include "lib/fatfs/ff.h" +#endif +#include "extmod/fsusermount.h" #include "extmod/vfs_fat_file.h" #if MICROPY_VFS_FAT @@ -112,7 +117,11 @@ STATIC mp_uint_t file_obj_write(mp_obj_t self_in, const void *buf, mp_uint_t siz STATIC mp_obj_t file_obj_close(mp_obj_t self_in) { pyb_file_obj_t *self = MP_OBJ_TO_PTR(self_in); // if fs==NULL then the file is closed and in that case this method is a no-op + #if MICROPY_FATFS_OO + if (self->fp.obj.fs != NULL) { + #else if (self->fp.fs != NULL) { + #endif FRESULT res = f_close(&self->fp); if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); @@ -178,7 +187,7 @@ STATIC const mp_arg_t file_open_args[] = { }; #define FILE_OPEN_NUM_ARGS MP_ARRAY_SIZE(file_open_args) -STATIC mp_obj_t file_open(const mp_obj_type_t *type, mp_arg_val_t *args) { +STATIC mp_obj_t file_open(fs_user_mount_t *vfs, const mp_obj_type_t *type, mp_arg_val_t *args) { int mode = 0; const char *mode_s = mp_obj_str_get_str(args[1].u_obj); // TODO make sure only one of r, w, x, a, and b, t are specified @@ -214,7 +223,19 @@ STATIC mp_obj_t file_open(const mp_obj_type_t *type, mp_arg_val_t *args) { o->base.type = type; const char *fname = mp_obj_str_get_str(args[0].u_obj); + #if MICROPY_FATFS_OO + if (vfs == NULL) { + vfs = ff_get_vfs(&fname); + if (vfs == NULL) { + m_del_obj(pyb_file_obj_t, o); + mp_raise_OSError(MP_ENOENT); + } + } + FRESULT res = f_open(&vfs->fatfs, &o->fp, fname, mode); + #else + (void)vfs; FRESULT res = f_open(&o->fp, fname, mode); + #endif if (res != FR_OK) { m_del_obj(pyb_file_obj_t, o); mp_raise_OSError(fresult_to_errno_table[res]); @@ -231,7 +252,7 @@ STATIC mp_obj_t file_open(const mp_obj_type_t *type, mp_arg_val_t *args) { STATIC mp_obj_t file_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_val_t arg_vals[FILE_OPEN_NUM_ARGS]; mp_arg_parse_all_kw_array(n_args, n_kw, args, FILE_OPEN_NUM_ARGS, file_open_args, arg_vals); - return file_open(type, arg_vals); + return file_open(NULL, type, arg_vals); } // TODO gc hook to close the file if not already closed @@ -295,7 +316,16 @@ mp_obj_t fatfs_builtin_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw // TODO: analyze buffering args and instantiate appropriate type mp_arg_val_t arg_vals[FILE_OPEN_NUM_ARGS]; mp_arg_parse_all(n_args, args, kwargs, FILE_OPEN_NUM_ARGS, file_open_args, arg_vals); - return file_open(&mp_type_textio, arg_vals); + return file_open(NULL, &mp_type_textio, arg_vals); +} + +// Factory function for I/O stream classes +mp_obj_t fatfs_builtin_open_self(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { + // TODO: analyze buffering args and instantiate appropriate type + fs_user_mount_t *self = MP_OBJ_TO_PTR(args[0]); + mp_arg_val_t arg_vals[FILE_OPEN_NUM_ARGS]; + mp_arg_parse_all(n_args - 1, args + 1, kwargs, FILE_OPEN_NUM_ARGS, file_open_args, arg_vals); + return file_open(self, &mp_type_textio, arg_vals); } #endif // MICROPY_FSUSERMOUNT |