@@ -32,6 +32,12 @@ STATIC mp_uint_t file_obj_read(mp_obj_t self_in, void *buf, mp_uint_t size, int
32
32
33
33
pyb_file_obj_t * self = MP_OBJ_TO_PTR (self_in );
34
34
35
+ if (self -> opened == false) {
36
+ // Return EINVAL just as FatFS if the file is not opened
37
+ * errcode = MP_EINVAL ;
38
+ return MP_STREAM_ERROR ;
39
+ }
40
+
35
41
xSemaphoreTake (self -> littlefs -> mutex , portMAX_DELAY );
36
42
lfs_ssize_t sz_out = lfs_file_read (& self -> littlefs -> lfs ,& self -> fp , buf , size );
37
43
xSemaphoreGive (self -> littlefs -> mutex );
@@ -47,6 +53,12 @@ STATIC mp_uint_t file_obj_write(mp_obj_t self_in, const void *buf, mp_uint_t siz
47
53
48
54
pyb_file_obj_t * self = MP_OBJ_TO_PTR (self_in );
49
55
56
+ if (self -> opened == false) {
57
+ // Return EINVAL just as FatFS if the file is not opened
58
+ * errcode = MP_EINVAL ;
59
+ return MP_STREAM_ERROR ;
60
+ }
61
+
50
62
xSemaphoreTake (self -> littlefs -> mutex , portMAX_DELAY );
51
63
lfs_ssize_t sz_out = lfs_file_write (& self -> littlefs -> lfs , & self -> fp , buf , size );
52
64
// Request timestamp update if file has been written successfully
@@ -90,6 +102,12 @@ STATIC mp_uint_t file_obj_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg,
90
102
91
103
} else if (request == MP_STREAM_FLUSH ) {
92
104
105
+ if (self -> opened == false) {
106
+ // Return EINVAL just as FatFS if the file is not opened
107
+ * errcode = MP_EINVAL ;
108
+ return MP_STREAM_ERROR ;
109
+ }
110
+
93
111
xSemaphoreTake (self -> littlefs -> mutex , portMAX_DELAY );
94
112
int res = lfs_file_sync (& self -> littlefs -> lfs , & self -> fp );
95
113
xSemaphoreGive (self -> littlefs -> mutex );
@@ -101,8 +119,9 @@ STATIC mp_uint_t file_obj_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg,
101
119
return 0 ;
102
120
103
121
} else if (request == MP_STREAM_CLOSE ) {
104
- // This check is needed here because calling close() twice makes LFS crash in lfs_file_close()
122
+
105
123
if (self -> opened == false) {
124
+ // Return 0 just as FatFs if the file is not opened
106
125
return 0 ;
107
126
}
108
127
@@ -114,6 +133,7 @@ STATIC mp_uint_t file_obj_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg,
114
133
* errcode = littleFsErrorToErrno (res );
115
134
return MP_STREAM_ERROR ;
116
135
}
136
+
117
137
self -> opened = false; // indicate a closed file
118
138
return 0 ;
119
139
} else {
0 commit comments