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

Commit 5b04a5e

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 8455e6b commit 5b04a5e

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
@@ -21,6 +21,7 @@ typedef struct _pyb_file_obj_t {
2121
vfs_lfs_struct_t* littlefs;
2222
struct lfs_file_config cfg; // Attributes of the file, e.g.: timestamp
2323
bool timestamp_update; // For requesting timestamp update when closing the file
24+
bool opened; // Indicate whether the file is opened
2425
} pyb_file_obj_t;
2526

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

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

@@ -113,7 +115,7 @@ STATIC mp_uint_t file_obj_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg,
113115
*errcode = littleFsErrorToErrno(res);
114116
return MP_STREAM_ERROR;
115117
}
116-
self->littlefs = NULL; // indicate a closed file
118+
self->opened = false; // indicate a closed file
117119
return 0;
118120
} else {
119121
*errcode = MP_EINVAL;
@@ -168,6 +170,7 @@ STATIC mp_obj_t file_open(fs_user_mount_t *vfs, const mp_obj_type_t *type, mp_ar
168170
pyb_file_obj_t *o = m_new_obj_with_finaliser(pyb_file_obj_t);
169171
o->base.type = type;
170172
o->timestamp_update = false;
173+
o->opened = false;
171174

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

183186
o->littlefs = &vfs->fs.littlefs;
187+
o->opened = true; // File is opened successfully
184188

185189
return MP_OBJ_FROM_PTR(o);
186190
}

0 commit comments

Comments
 (0)