8000 Adjust the fix so calling other file operations (e.g. write, read etc… · pycom/pycom-micropython-sigfox@9253101 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 9253101

Browse files
geza-pycompeter-pycom
authored andcommitted
Adjust the fix so calling other file operations (e.g. write, read etc.) after close() not crash the system
1 parent 8baf148 commit 9253101

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

esp32/littlefs/vfs_littlefs_file.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ typedef struct _pyb_file_obj_t {
2020
vfs_lfs_struct_t* littlefs;
2121
struct lfs_file_config cfg; // Attributes of the file, e.g.: timestamp
2222
bool timestamp_update; // For requesting timestamp update when closing the file
23+
bool opened; // Indicate whether the file is opened
2324
} pyb_file_obj_t;
2425

2526
STATIC void file_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
@@ -100,7 +101,8 @@ STATIC mp_uint_t file_obj_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg,
100101
return 0;
101102

102103
} else if (request == MP_STREAM_CLOSE) {
103-
if (self->littlefs == NULL) {
104+
// This check is needed here because calling close() twice makes LFS crash in lfs_file_close()
105+
if (self->opened == false) {
104106
return 0;
105107
}
106108

@@ -112,7 +114,7 @@ STATIC mp_uint_t file_obj_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg,
112114
*errcode = littleFsErrorToErrno(res);
113115
return MP_STREAM_ERROR;
114116
}
115-
self->littlefs = NULL; // indicate a closed file
117+
self->opened = false; // indicate a closed file
116118
return 0;
117119
} else {
118120
*errcode = MP_EINVAL;
@@ -167,6 +169,7 @@ STATIC mp_obj_t file_open(fs_user_mount_t *vfs, const mp_obj_type_t *type, mp_ar
167169
pyb_file_obj_t *o = m_new_obj_with_finaliser(pyb_file_obj_t);
168170
o->base.type = type;
169171
o->timestamp_update = false;
172+
o->opened = false;
170173

171174
xSemaphoreTake(vfs->fs.littlefs.mutex, portMAX_DELAY);
172175
const char *fname = concat_with_cwd(&vfs->fs.littlefs, mp_obj_str_get_str(args[0].u_obj));
@@ -180,6 +183,7 @@ STATIC mp_obj_t file_open(fs_user_mount_t *vfs, const mp_obj_type_t *type, mp_ar
180183
}
181184

182185
o->littlefs = &vfs->fs.littlefs;
186+
o->opened = true; // File is opened successfully
183187

184188
return MP_OBJ_FROM_PTR(o);
185189
}

0 commit comments

Comments
 (0)
0