From f5f4cdae89ed040ae9209a380cf968254434e819 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 1 Jun 2016 17:00:28 +0100 Subject: [PATCH] extmod/vfs_fat: Rework so it can optionally use OO version of FatFS. If MICROPY_VFS_FAT is enabled by a port then the port must switch to using MICROPY_FATFS_OO. Otherwise a port can continue to use the FatFs code without any changes. --- extmod/fsusermount.c | 41 +++++++++++++++++++-- extmod/vfs_fat.c | 81 ++++++++++++++++++----------------------- extmod/vfs_fat_diskio.c | 61 +++++++++++++++++++++++++++---- extmod/vfs_fat_ffconf.c | 40 +++++++++++++++++++- extmod/vfs_fat_file.c | 36 ++++++++++++++++-- extmod/vfs_fat_file.h | 6 +++ extmod/vfs_fat_misc.c | 30 ++++++++++++--- extmod/vfs_fat_reader.c | 13 +++++++ 8 files changed, 243 insertions(+), 65 deletions(-) diff --git a/extmod/fsusermount.c b/extmod/fsusermount.c index 5882aba991..4ca9b80a60 100644 --- a/extmod/fsusermount.c +++ b/extmod/fsusermount.c @@ -32,7 +32,11 @@ #include "py/nlr.h" #include "py/runtime.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" fs_user_mount_t *fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, bool mkfs) { @@ -57,7 +61,11 @@ fs_user_mount_t *fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp for (size_t i = 0; i < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount)); ++i) { fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i]; if (vfs != NULL && !memcmp(mnt_str, vfs->str, mnt_len + 1)) { + #if MICROPY_FATFS_OO + res = f_umount(&vfs->fatfs); + #else res = f_mount(NULL, vfs->str, 0); + #endif if (vfs->flags & FSUSER_FREE_OBJ) { m_del_obj(fs_user_mount_t, vfs); } @@ -86,6 +94,9 @@ fs_user_mount_t *fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp vfs->str = mnt_str; vfs->len = mnt_len; vfs->flags = FSUSER_FREE_OBJ; + #if MICROPY_FATFS_OO + vfs->fatfs.drv = vfs; + #endif // load block protocol methods mp_load_method(device, MP_QSTR_readblocks, vfs->readblocks); @@ -114,15 +125,30 @@ fs_user_mount_t *fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp MP_STATE_PORT(fs_user_mount)[i] = vfs; // mount the block device (if mkfs, only pre-mount) - FRESULT res = f_mount(&vfs->fatfs, vfs->str, !mkfs); + FRESULT res; + #if MICROPY_FATFS_OO + if (mkfs) { + res = FR_OK; + } else { + res = f_mount(&vfs->fatfs); + } + #else + res = f_mount(&vfs->fatfs, vfs->str, !mkfs); + #endif + // check the result if (res == FR_OK) { if (mkfs) { goto mkfs; } } else if (res == FR_NO_FILESYSTEM && args[1].u_bool) { -mkfs: +mkfs:; + #if MICROPY_FATFS_OO + uint8_t working_buf[_MAX_SS]; + res = f_mkfs(&vfs->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf)); + #else res = f_mkfs(vfs->str, 1, 0); + #endif if (res != FR_OK) { mkfs_error: MP_STATE_PORT(fs_user_mount)[i] = NULL; @@ -130,7 +156,11 @@ mkfs_error: } if (mkfs) { // If requested to only mkfs, unmount pre-mounted device + #if MICROPY_FATFS_OO + res = FR_OK; + #else res = f_mount(NULL, vfs->str, 0); + #endif if (res != FR_OK) { goto mkfs_error; } @@ -188,7 +218,12 @@ mp_obj_t fatfs_umount(mp_obj_t bdev_or_path_in) { } fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i]; - FRESULT res = f_mount(NULL, vfs->str, 0); + FRESULT res; + #if MICROPY_FATFS_OO + res = f_umount(&vfs->fatfs); + #else + res = f_mount(NULL, vfs->str, 0); + #endif if (vfs->flags & FSUSER_FREE_OBJ) { m_del_obj(fs_user_mount_t, vfs); } diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index bd88bcf1b1..36bdb5dbde 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -28,12 +28,15 @@ #include "py/mpconfig.h" #if MICROPY_VFS_FAT +#if !MICROPY_FATFS_OO +#error "with MICROPY_VFS_FAT enabled, must also enable MICROPY_FATFS_OO" +#endif + #include #include "py/nlr.h" #include "py/runtime.h" #include "py/mperrno.h" -#include "lib/fatfs/ff.h" -#include "lib/fatfs/diskio.h" +#include "lib/oofatfs/ff.h" #include "extmod/vfs_fat_file.h" #include "extmod/fsusermount.h" #include "timeutils.h" @@ -55,13 +58,10 @@ STATIC mp_obj_t fat_vfs_mkfs(mp_obj_t bdev_in) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_mkfs_fun_obj, fat_vfs_mkfs); STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(fat_vfs_mkfs_obj, MP_ROM_PTR(&fat_vfs_mkfs_fun_obj)); -STATIC mp_obj_t fat_vfs_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { - // Skip self - return fatfs_builtin_open(n_args - 1, args + 1, kwargs); -} -MP_DEFINE_CONST_FUN_OBJ_KW(fat_vfs_open_obj, 2, fat_vfs_open); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(fat_vfs_open_obj, 2, fatfs_builtin_open_self); STATIC mp_obj_t fat_vfs_listdir_func(size_t n_args, const mp_obj_t *args) { + mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(args[0]); bool is_str_type = true; const char *path; if (n_args == 2) { @@ -73,19 +73,16 @@ STATIC mp_obj_t fat_vfs_listdir_func(size_t n_args, const mp_obj_t *args) { path = ""; } - return fat_vfs_listdir(path, is_str_type); + return fat_vfs_listdir2(self, path, is_str_type); } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fat_vfs_listdir_obj, 1, 2, fat_vfs_listdir_func); -STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t path_in, mp_int_t attr) { +STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t vfs_in, mp_obj_t path_in, mp_int_t attr) { + mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); const char *path = mp_obj_str_get_str(path_in); FILINFO fno; -#if _USE_LFN - fno.lfname = NULL; - fno.lfsize = 0; -#endif - FRESULT res = f_stat(path, &fno); + FRESULT res = f_stat(&self->fatfs, path, &fno); if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); @@ -93,7 +90,7 @@ STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t path_in, mp_int_t attr) { // check if path is a file or directory if ((fno.fattrib & AM_DIR) == attr) { - res = f_unlink(path); + res = f_unlink(&self->fatfs, path); if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); @@ -105,27 +102,25 @@ STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t path_in, mp_int_t attr) { } STATIC mp_obj_t fat_vfs_remove(mp_obj_t vfs_in, mp_obj_t path_in) { - (void)vfs_in; - return fat_vfs_remove_internal(path_in, 0); // 0 == file attribute + return fat_vfs_remove_internal(vfs_in, path_in, 0); // 0 == file attribute } STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_remove_obj, fat_vfs_remove); STATIC mp_obj_t fat_vfs_rmdir(mp_obj_t vfs_in, mp_obj_t path_in) { - (void) vfs_in; - return fat_vfs_remove_internal(path_in, AM_DIR); + return fat_vfs_remove_internal(vfs_in, path_in, AM_DIR); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_rmdir_obj, fat_vfs_rmdir); STATIC mp_obj_t fat_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t path_out) { - (void)vfs_in; + mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); const char *old_path = mp_obj_str_get_str(path_in); const char *new_path = mp_obj_str_get_str(path_out); - FRESULT res = f_rename(old_path, new_path); + FRESULT res = f_rename(&self->fatfs, old_path, new_path); if (res == FR_EXIST) { // if new_path exists then try removing it (but only if it's a file) - fat_vfs_remove_internal(path_out, 0); // 0 == file attribute + fat_vfs_remove_internal(vfs_in, path_out, 0); // 0 == file attribute // try to rename again - res = f_rename(old_path, new_path); + res = f_rename(&self->fatfs, old_path, new_path); } if (res == FR_OK) { return mp_const_none; @@ -137,9 +132,9 @@ STATIC mp_obj_t fat_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t path_ STATIC MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_rename_obj, fat_vfs_rename); STATIC mp_obj_t fat_vfs_mkdir(mp_obj_t vfs_in, mp_obj_t path_o) { - (void)vfs_in; + mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); const char *path = mp_obj_str_get_str(path_o); - FRESULT res = f_mkdir(path); + FRESULT res = f_mkdir(&self->fatfs, path); if (res == FR_OK) { return mp_const_none; } else { @@ -150,15 +145,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_mkdir_obj, fat_vfs_mkdir); /// Change current directory. STATIC mp_obj_t fat_vfs_chdir(mp_obj_t vfs_in, mp_obj_t path_in) { - (void)vfs_in; + mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); const char *path; path = mp_obj_str_get_str(path_in); - FRESULT res = f_chdrive(path); - - if (res == FR_OK) { - res = f_chdir(path); - } + FRESULT res = f_chdir(&self->fatfs, path); if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); @@ -170,14 +161,18 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_chdir_obj, fat_vfs_chdir); /// Get the current directory. STATIC mp_obj_t fat_vfs_getcwd(mp_obj_t vfs_in) { - (void)vfs_in; + mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); char buf[MICROPY_ALLOC_PATH_MAX + 1]; - FRESULT res = f_getcwd(buf, sizeof buf); - + memcpy(buf, self->str, self->len); + FRESULT res = f_getcwd(&self->fatfs, buf + self->len, sizeof(buf) - self->len); if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); } - + // remove trailing / if in root dir, because we prepended the mount point + size_t l = strlen(buf); + if (res == FR_OK && buf[l - 1] == '/') { + buf[l - 1] = 0; + } return mp_obj_new_str(buf, strlen(buf), false); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_getcwd_obj, fat_vfs_getcwd); @@ -202,14 +197,10 @@ STATIC bool path_equal(const char *path, const char *path_canonical) { /// \function stat(path) /// Get the status of a file or directory. STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) { - (void)vfs_in; + mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); const char *path = mp_obj_str_get_str(path_in); FILINFO fno; -#if _USE_LFN - fno.lfname = NULL; - fno.lfsize = 0; -#endif FRESULT res; if (path_equal(path, "/")) { @@ -233,7 +224,7 @@ STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) { } if (res == FR_NO_PATH) { // stat normal file - res = f_stat(path, &fno); + res = f_stat(&self->fatfs, path, &fno); } if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); @@ -272,12 +263,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_stat_obj, fat_vfs_stat); // Get the status of a VFS. STATIC mp_obj_t fat_vfs_statvfs(mp_obj_t vfs_in, mp_obj_t path_in) { - (void)vfs_in; - const char *path = mp_obj_str_get_str(path_in); + mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); + (void)path_in; - FATFS *fatfs; DWORD nclst; - FRESULT res = f_getfree(path, &nclst, &fatfs); + FATFS *fatfs = &self->fatfs; + FRESULT res = f_getfree(fatfs, &nclst); if (FR_OK != res) { mp_raise_OSError(fresult_to_errno_table[res]); } diff --git a/extmod/vfs_fat_diskio.c b/extmod/vfs_fat_diskio.c index 3f1902b2e0..9e91b26f7e 100644 --- a/extmod/vfs_fat_diskio.c +++ b/extmod/vfs_fat_diskio.c @@ -36,8 +36,13 @@ #include "py/mphal.h" #include "py/runtime.h" +#if MICROPY_FATFS_OO +#include "lib/oofatfs/ff.h" +#include "lib/oofatfs/diskio.h" +#else #include "lib/fatfs/ff.h" /* FatFs lower layer API */ #include "lib/fatfs/diskio.h" /* FatFs lower layer API */ +#endif #include "extmod/fsusermount.h" #if _MAX_SS == _MIN_SS @@ -46,6 +51,13 @@ #define SECSIZE(fs) ((fs)->ssize) #endif +#if MICROPY_FATFS_OO +typedef void *bdev_t; +STATIC fs_user_mount_t *disk_get_device(void *bdev) { + return (fs_user_mount_t*)bdev; +} +#else +typedef BYTE bdev_t; STATIC fs_user_mount_t *disk_get_device(uint id) { if (id < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount))) { return MP_STATE_PORT(fs_user_mount)[id]; @@ -53,13 +65,17 @@ STATIC fs_user_mount_t *disk_get_device(uint id) { return NULL; } } +#endif /*-----------------------------------------------------------------------*/ /* Initialize a Drive */ /*-----------------------------------------------------------------------*/ +#if MICROPY_FATFS_OO +STATIC +#endif DSTATUS disk_initialize ( - BYTE pdrv /* Physical drive nmuber (0..) */ + bdev_t pdrv /* Physical drive nmuber (0..) */ ) { fs_user_mount_t *vfs = disk_get_device(pdrv); @@ -89,8 +105,11 @@ DSTATUS disk_initialize ( /* Get Disk Status */ /*-----------------------------------------------------------------------*/ +#if MICROPY_FATFS_OO +STATIC +#endif DSTATUS disk_status ( - BYTE pdrv /* Physical drive nmuber (0..) */ + bdev_t pdrv /* Physical drive nmuber (0..) */ ) { fs_user_mount_t *vfs = disk_get_device(pdrv); @@ -110,7 +129,7 @@ DSTATUS disk_status ( /*-----------------------------------------------------------------------*/ DRESULT disk_read ( - BYTE pdrv, /* Physical drive nmuber (0..) */ + bdev_t pdrv, /* Physical drive nmuber (0..) */ BYTE *buff, /* Data buffer to store read data */ DWORD sector, /* Sector address (LBA) */ UINT count /* Number of sectors to read (1..128) */ @@ -140,9 +159,9 @@ DRESULT disk_read ( /* Write Sector(s) */ /*-----------------------------------------------------------------------*/ -#if _USE_WRITE +#if MICROPY_FATFS_OO || _USE_WRITE DRESULT disk_write ( - BYTE pdrv, /* Physical drive nmuber (0..) */ + bdev_t pdrv, /* Physical drive nmuber (0..) */ const BYTE *buff, /* Data to be written */ DWORD sector, /* Sector address (LBA) */ UINT count /* Number of sectors to write (1..128) */ @@ -179,9 +198,9 @@ DRESULT disk_write ( /* Miscellaneous Functions */ /*-----------------------------------------------------------------------*/ -#if _USE_IOCTL +#if MICROPY_FATFS_OO || _USE_IOCTL DRESULT disk_ioctl ( - BYTE pdrv, /* Physical drive nmuber (0..) */ + bdev_t pdrv, /* Physical drive nmuber (0..) */ BYTE cmd, /* Control code */ void *buff /* Buffer to send/receive control data */ ) @@ -218,6 +237,10 @@ DRESULT disk_ioctl ( } else { *((WORD*)buff) = mp_obj_get_int(ret); } + #if MICROPY_FATFS_OO && _MAX_SS != _MIN_SS + // need to store ssize because we use it in disk_read/disk_write + vfs->fatfs.ssize = *((WORD*)buff); + #endif return RES_OK; } @@ -225,6 +248,16 @@ DRESULT disk_ioctl ( *((DWORD*)buff) = 1; // erase block size in units of sector size return RES_OK; + #if MICROPY_FATFS_OO + case IOCTL_INIT: + *((DSTATUS*)buff) = disk_initialize(pdrv); + return RES_OK; + + case IOCTL_STATUS: + *((DSTATUS*)buff) = disk_status(pdrv); + return RES_OK; + #endif + default: return RES_PARERR; } @@ -245,12 +278,26 @@ DRESULT disk_ioctl ( case GET_SECTOR_SIZE: *((WORD*)buff) = 512; // old protocol had fixed sector size + #if MICROPY_FATFS_OO && _MAX_SS != _MIN_SS + // need to store ssize because we use it in disk_read/disk_write + vfs->fatfs.ssize = 512; + #endif return RES_OK; case GET_BLOCK_SIZE: *((DWORD*)buff) = 1; // erase block size in units of sector size return RES_OK; + #if MICROPY_FATFS_OO + case IOCTL_INIT: + *((DSTATUS*)buff) = disk_initialize(pdrv); + return RES_OK; + + case IOCTL_STATUS: + *((DSTATUS*)buff) = disk_status(pdrv); + return RES_OK; + #endif + default: return RES_PARERR; } diff --git a/extmod/vfs_fat_ffconf.c b/extmod/vfs_fat_ffconf.c index f8935af751..ddcdd88444 100644 --- a/extmod/vfs_fat_ffconf.c +++ b/extmod/vfs_fat_ffconf.c @@ -30,10 +30,13 @@ #include #include "py/mpstate.h" +#if MICROPY_FATFS_OO +#include "lib/oofatfs/ff.h" +#else #include "lib/fatfs/ff.h" -#include "lib/fatfs/ffconf.h" -#include "lib/fatfs/diskio.h" +#endif #include "extmod/fsusermount.h" +#include "extmod/vfs_fat_file.h" STATIC bool check_path(const TCHAR **path, const char *mount_point_str, mp_uint_t mount_point_len) { if (strncmp(*path, mount_point_str, mount_point_len) == 0) { @@ -48,6 +51,37 @@ STATIC bool check_path(const TCHAR **path, const char *mount_point_str, mp_uint_ return false; } +#if MICROPY_FATFS_OO + +STATIC fs_user_mount_t *vfs_cur_obj = NULL; + +// "path" is the path to lookup; will advance this pointer beyond the volume name. +// Returns a pointer to the VFS object, NULL means path not found. +fs_user_mount_t *ff_get_vfs(const char **path) { + if (!(*path)) { + return NULL; + } + + if (**path != '/') { + #if _FS_RPATH + return vfs_cur_obj; + #else + return NULL; + #endif + } + + for (size_t i = 0; i < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount)); ++i) { + fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i]; + if (vfs != NULL && check_path(path, vfs->str, vfs->len)) { + return vfs; + } + } + + return NULL; +} + +#else + // "path" is the path to lookup; will advance this pointer beyond the volume name. // Returns logical drive number (-1 means invalid path). int ff_get_ldnumber (const TCHAR **path) { @@ -79,4 +113,6 @@ void ff_get_volname(BYTE vol, TCHAR **dest) { *dest += vfs->len; } +#endif + #endif // MICROPY_FSUSERMOUNT diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index 77848b5a53..6651d70d05 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 diff --git a/extmod/vfs_fat_file.h b/extmod/vfs_fat_file.h index 5c271b6ee6..9693aa04a2 100644 --- a/extmod/vfs_fat_file.h +++ b/extmod/vfs_fat_file.h @@ -24,9 +24,15 @@ * THE SOFTWARE. */ +struct _fs_user_mount_t; + extern const byte fresult_to_errno_table[20]; +struct _fs_user_mount_t *ff_get_vfs(const char **path); + mp_obj_t fatfs_builtin_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs); +mp_obj_t fatfs_builtin_open_self(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs); MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_open_obj); mp_obj_t fat_vfs_listdir(const char *path, bool is_str_type); +mp_obj_t fat_vfs_listdir2(struct _fs_user_mount_t *vfs, const char *path, bool is_str_type); diff --git a/extmod/vfs_fat_misc.c b/extmod/vfs_fat_misc.c index d3507a85f3..5e89009cd3 100644 --- a/extmod/vfs_fat_misc.c +++ b/extmod/vfs_fat_misc.c @@ -32,27 +32,39 @@ #include #include "py/nlr.h" #include "py/runtime.h" +#if MICROPY_FATFS_OO +#include "lib/oofatfs/ff.h" +#else #include "lib/fatfs/ff.h" -#include "lib/fatfs/diskio.h" +#endif #include "extmod/vfs_fat_file.h" #include "extmod/fsusermount.h" #include "py/lexer.h" -#if _USE_LFN +#if !MICROPY_FATFS_OO && _USE_LFN STATIC char lfn[_MAX_LFN + 1]; /* Buffer to store the LFN */ #endif // TODO: actually, the core function should be ilistdir() + mp_obj_t fat_vfs_listdir(const char *path, bool is_str_type) { + return fat_vfs_listdir2(NULL, path, is_str_type); +} + +mp_obj_t fat_vfs_listdir2(fs_user_mount_t *vfs, const char *path, bool is_str_type) { FRESULT res; FILINFO fno; DIR dir; -#if _USE_LFN +#if !MICROPY_FATFS_OO && _USE_LFN fno.lfname = lfn; fno.lfsize = sizeof lfn; #endif + #if MICROPY_FATFS_OO + res = f_opendir(&vfs->fatfs, &dir, path); + #else res = f_opendir(&dir, path); /* Open the directory */ + #endif if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); } @@ -65,7 +77,7 @@ mp_obj_t fat_vfs_listdir(const char *path, bool is_str_type) { if (fno.fname[0] == '.' && fno.fname[1] == 0) continue; /* Ignore . entry */ if (fno.fname[0] == '.' && fno.fname[1] == '.' && fno.fname[2] == 0) continue; /* Ignore .. entry */ -#if _USE_LFN +#if !MICROPY_FATFS_OO && _USE_LFN char *fn = *fno.lfname ? fno.lfname : fno.fname; #else char *fn = fno.fname; @@ -100,11 +112,19 @@ mp_import_stat_t fat_vfs_import_stat(const char *path); mp_import_stat_t fat_vfs_import_stat(const char *path) { FILINFO fno; -#if _USE_LFN +#if !MICROPY_FATFS_OO && _USE_LFN fno.lfname = NULL; fno.lfsize = 0; #endif + #if MICROPY_FATFS_OO + fs_user_mount_t *vfs = ff_get_vfs(&path); + if (vfs == NULL) { + return MP_IMPORT_STAT_NO_EXIST; + } + FRESULT res = f_stat(&vfs->fatfs, path, &fno); + #else FRESULT res = f_stat(path, &fno); + #endif if (res == FR_OK) { if ((fno.fattrib & AM_DIR) != 0) { return MP_IMPORT_STAT_DIR; diff --git a/extmod/vfs_fat_reader.c b/extmod/vfs_fat_reader.c index 7a00f18de3..efd2de0c16 100644 --- a/extmod/vfs_fat_reader.c +++ b/extmod/vfs_fat_reader.c @@ -32,7 +32,12 @@ #if MICROPY_READER_FATFS +#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" typedef struct _mp_reader_fatfs_t { @@ -71,7 +76,15 @@ int mp_reader_new_file(mp_reader_t *reader, const char *filename) { if (rf == NULL) { return MP_ENOMEM; } + #if MICROPY_FATFS_OO + fs_user_mount_t *vfs = ff_get_vfs(&filename); + if (vfs == NULL) { + return MP_ENOENT; + } + FRESULT res = f_open(&vfs->fatfs, &rf->fp, filename, FA_READ); + #else FRESULT res = f_open(&rf->fp, filename, FA_READ); + #endif if (res != FR_OK) { return fresult_to_errno_table[res]; }