8000 Adjust to be the same as FatFs · pycom/pycom-micropython-sigfox@39ca300 · 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 39ca300

Browse files
geza-pycompeter-pycom
authored andcommitted
Adjust to be the same as FatFs
1 parent 9253101 commit 39ca300

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

esp32/littlefs/vfs_littlefs_file.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ STATIC mp_uint_t file_obj_read(mp_obj_t self_in, void *buf, mp_uint_t size, int
3232

3333
pyb_file_obj_t *self = MP_OBJ_TO_PTR(self_in);
3434

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+
3541
xSemaphoreTake(self->littlefs->mutex, portMAX_DELAY);
3642
lfs_ssize_t sz_out = lfs_file_read(&self->littlefs->lfs ,&self->fp, buf, size);
3743
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
4753

4854
pyb_file_obj_t *self = MP_OBJ_TO_PTR(self_in);
4955

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+
5062
xSemaphoreTake(self->littlefs->mutex, portMAX_DELAY);
5163
lfs_ssize_t sz_out = lfs_file_write(&self->littlefs->lfs, &self->fp, buf, size);
5264
// 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,
90102

91103
} else if (request == MP_STREAM_FLUSH) {
92104

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+
93111
xSemaphoreTake(self->littlefs->mutex, portMAX_DELAY);
94112
int res = lfs_file_sync(&self->littlefs->lfs, &self->fp);
95113
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,
101119
return 0;
102120

103121
} else if (request == MP_STREAM_CLOSE) {
104-
// This check is needed here because calling close() twice makes LFS crash in lfs_file_close()
122+
105123
if (self->opened == false) {
124+
// Return 0 just as FatFs if the file is not opened
106125
return 0;
107126
}
108127

@@ -114,6 +133,7 @@ STATIC mp_uint_t file_obj_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg,
114133
*errcode = littleFsErrorToErrno(res);
115134
return MP_STREAM_ERROR;
116135
}
136+
117137
self->opened = false; // indicate a closed file
118138
return 0;
119139
} else {

0 commit comments

Comments
 (0)
0