From a99f817c234d28c04a49508f48e971888651838b Mon Sep 17 00:00:00 2001 From: li_zan <371442490@qq.com> Date: Fri, 7 May 2021 11:35:08 +0800 Subject: [PATCH] Add already opened file logic --- components/fs/littlefs/lfs_api.c | 23 +++++++++++++++++++++-- components/fs/littlefs/lfs_api.h | 3 ++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/components/fs/littlefs/lfs_api.c b/components/fs/littlefs/lfs_api.c index b36dee95..d335d9d3 100644 --- a/components/fs/littlefs/lfs_api.c +++ b/components/fs/littlefs/lfs_api.c @@ -62,6 +62,21 @@ LittleFsHandleStruct *GetFreeFd(int *fd) return NULL; } +bool CheckFileIsOpen(const char *fileName, int *fd) +{ + for(int i = 0; i < LITTLE_FS_MAX_OPEN_FILES; i++) { + if(g_handle[i].useFlag == 1) { + if(strcmp(g_handle[i].pathName, fileName) == 0) { + *fd = i; + return true; + } + } + } + + *fd = INVALID_FD; + return false; +} + lfs_dir_t *GetFreeDir() { pthread_mutex_lock(&g_FslocalMutex); @@ -205,16 +220,20 @@ int LfsClosedir(const DIR *dir) return lfs_dir_close(&g_lfs, (lfs_dir_t *)dir); } -int LfsOpen(const char *path, int openFlag, int mode) +int LfsOpen(const char *pathName, int openFlag, int mode) { int fd = INVALID_FD; + if (CheckFileIsOpen(pathName, &fd)) { + return fd; + } + LittleFsHandleStruct *fsHandle = GetFreeFd(&fd); if (fd == INVALID_FD) { goto errout; } - int err = lfs_file_open(&g_lfs, &(fsHandle->file), path, openFlag); + int err = lfs_file_open(&g_lfs, &(fsHandle->file), pathName, openFlag); if (err != 0) { goto errout; } diff --git a/components/fs/littlefs/lfs_api.h b/components/fs/littlefs/lfs_api.h index 3fed19c4..0f1a9c39 100644 --- a/components/fs/littlefs/lfs_api.h +++ b/components/fs/littlefs/lfs_api.h @@ -55,6 +55,7 @@ typedef unsigned mode_t; typedef struct { uint8_t useFlag; + const char *pathName; lfs_file_t file; } LittleFsHandleStruct; @@ -121,7 +122,7 @@ int LfsRmdir(const char *dirName); DIR *LfsOpendir(const char *dirName); struct dirent *LfsReaddir(DIR *dir); int LfsClosedir(const DIR *dir); -int LfsOpen(const char *path, int openFlag, int mode); +int LfsOpen(const char *pathName, int openFlag, int mode); int LfsRead(int fd, void *buf, unsigned int len); int LfsWrite(int fd, const void *buf, unsigned int len); int LfsSeek(int fd, off_t offset, int whence);