From 94c0727e1d1720a04da83232efd55fa7f2db4e8d Mon Sep 17 00:00:00 2001 From: li_zan <371442490@qq.com> Date: Fri, 7 May 2021 15:25:42 +0800 Subject: [PATCH] Add already opened file logic --- components/fs/littlefs/lfs_api.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/components/fs/littlefs/lfs_api.c b/components/fs/littlefs/lfs_api.c index e2a923e4..499b7e38 100644 --- a/components/fs/littlefs/lfs_api.c +++ b/components/fs/littlefs/lfs_api.c @@ -46,16 +46,22 @@ FileOpInfo GetFsOpInfo(void) return g_fsOp; } -LittleFsHandleStruct *GetFreeFd(int *fd) +LittleFsHandleStruct *GetFreeFd(const char *fileName, int *fd) { + int len = strlen() + 1; + pthread_mutex_lock(&g_FslocalMutex); for (int i = 0; i < LITTLE_FS_MAX_OPEN_FILES; i++) { if (g_handle[i].useFlag == 0) { *fd = i; g_handle[i].useFlag = 1; + g_handle[i].pathName = (char *)malloc(len); + if (g_handle[i].pathName) { + memcpy_s(g_handle[i].pathName, len, fileName, len); + } pthread_mutex_unlock(&g_FslocalMutex); return &(g_handle[i]); - } + } } pthread_mutex_unlock(&g_FslocalMutex); *fd = INVALID_FD; @@ -228,7 +234,7 @@ int LfsOpen(const char *pathName, int openFlag, int mode) return fd; } - LittleFsHandleStruct *fsHandle = GetFreeFd(&fd); + LittleFsHandleStruct *fsHandle = GetFreeFd(pathName, &fd); if (fd == INVALID_FD) { goto errout; }