summaryrefslogtreecommitdiff
path: root/extmod/vfs_fat_file.c
diff options
context:
space:
mode:
Diffstat (limited to 'extmod/vfs_fat_file.c')
-rw-r--r--extmod/vfs_fat_file.c34
1 files changed, 19 insertions, 15 deletions
diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c
index ef57225a9..9c166e163 100644
--- a/extmod/vfs_fat_file.c
+++ b/extmod/vfs_fat_file.c
@@ -24,11 +24,10 @@
* THE SOFTWARE.
*/
-#include "extmod/vfs_fat_file.h"
+#include "py/mpconfig.h"
+#if MICROPY_VFS && MICROPY_VFS_FAT
-// *_ADHOC part is for cc3200 port which doesn't use general uPy
-// infrastructure and instead duplicates code. TODO: Resolve.
-#if MICROPY_FSUSERMOUNT || MICROPY_FSUSERMOUNT_ADHOC
+#include "extmod/vfs_fat_file.h"
#include <stdio.h>
#include <errno.h>
@@ -37,7 +36,8 @@
#include "py/runtime.h"
#include "py/stream.h"
#include "py/mperrno.h"
-#include "lib/fatfs/ff.h"
+#include "lib/oofatfs/ff.h"
+#include "extmod/vfs_fat.h"
// this table converts from FRESULT to POSIX errno
const byte fresult_to_errno_table[20] = {
@@ -99,7 +99,7 @@ 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 (self->fp.fs != NULL) {
+ if (self->fp.obj.fs != NULL) {
FRESULT res = f_close(&self->fp);
if (res != FR_OK) {
mp_raise_OSError(fresult_to_errno_table[res]);
@@ -165,7 +165,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
@@ -201,7 +201,8 @@ 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);
- FRESULT res = f_open(&o->fp, fname, mode);
+ assert(vfs != NULL);
+ FRESULT res = f_open(&vfs->fatfs, &o->fp, fname, mode);
if (res != FR_OK) {
m_del_obj(pyb_file_obj_t, o);
mp_raise_OSError(fresult_to_errno_table[res]);
@@ -218,7 +219,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
@@ -252,7 +253,7 @@ const mp_obj_type_t mp_type_fileio = {
.name = MP_QSTR_FileIO,
.print = file_obj_print,
.make_new = file_obj_make_new,
- .getiter = mp_identity,
+ .getiter = mp_identity_getiter,
.iternext = mp_stream_unbuffered_iter,
.protocol = &fileio_stream_p,
.locals_dict = (mp_obj_dict_t*)&rawfile_locals_dict,
@@ -271,18 +272,21 @@ const mp_obj_type_t mp_type_textio = {
.name = MP_QSTR_TextIOWrapper,
.print = file_obj_print,
.make_new = file_obj_make_new,
- .getiter = mp_identity,
+ .getiter = mp_identity_getiter,
.iternext = mp_stream_unbuffered_iter,
.protocol = &textio_stream_p,
.locals_dict = (mp_obj_dict_t*)&rawfile_locals_dict,
};
// Factory function for I/O stream classes
-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(mp_obj_t self_in, mp_obj_t path, mp_obj_t mode) {
// TODO: analyze buffering args and instantiate appropriate type
+ fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
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);
+ arg_vals[0].u_obj = path;
+ arg_vals[1].u_obj = mode;
+ arg_vals[2].u_obj = mp_const_none;
+ return file_open(self, &mp_type_textio, arg_vals);
}
-#endif // MICROPY_FSUSERMOUNT
+#endif // MICROPY_VFS && MICROPY_VFS_FAT