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 1/9] 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); From cd292f84288722db6d46d0a8fa718b49f4ec3fe1 Mon Sep 17 00:00:00 2001 From: li_zan <371442490@qq.com> Date: Fri, 7 May 2021 11:35:51 +0800 Subject: [PATCH 2/9] Add already opened file logic --- components/fs/littlefs/lfs_api.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/fs/littlefs/lfs_api.c b/components/fs/littlefs/lfs_api.c index d335d9d3..e2a923e4 100644 --- a/components/fs/littlefs/lfs_api.c +++ b/components/fs/littlefs/lfs_api.c @@ -64,9 +64,9 @@ LittleFsHandleStruct *GetFreeFd(int *fd) 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) { + 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; } 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 3/9] 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; } From 49e9044529321d175d766fbace0a26b74f08aa32 Mon Sep 17 00:00:00 2001 From: li_zan <371442490@qq.com> Date: Sat, 8 May 2021 11:14:33 +0800 Subject: [PATCH 4/9] Add already opened file logic --- components/fs/littlefs/lfs_api.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/components/fs/littlefs/lfs_api.c b/components/fs/littlefs/lfs_api.c index 499b7e38..ad995851 100644 --- a/components/fs/littlefs/lfs_api.c +++ b/components/fs/littlefs/lfs_api.c @@ -246,7 +246,7 @@ int LfsOpen(const char *pathName, int openFlag, int mode) return fd; errout: - return INVALID_FD; + return INVALID_FD; } int LfsRead(int fd, void *buf, unsigned int len) @@ -287,6 +287,9 @@ int LfsClose(int fd) ret = lfs_file_close(&g_lfs, &(g_handle[fd].file)); pthread_mutex_lock(&g_FslocalMutex); g_handle[fd].useFlag = 0; + if (g_handle[fd].pathName != NULL) { + free(g_handle[fd].pathName); + } pthread_mutex_unlock(&g_FslocalMutex); return ret; From 4aeaf030a256061833f5b960ad537840fc55473c Mon Sep 17 00:00:00 2001 From: li_zan <371442490@qq.com> Date: Sat, 8 May 2021 11:31:27 +0800 Subject: [PATCH 5/9] Add already opened file logic --- components/fs/littlefs/lfs_api.c | 1 + 1 file changed, 1 insertion(+) diff --git a/components/fs/littlefs/lfs_api.c b/components/fs/littlefs/lfs_api.c index ad995851..c09b7a47 100644 --- a/components/fs/littlefs/lfs_api.c +++ b/components/fs/littlefs/lfs_api.c @@ -289,6 +289,7 @@ int LfsClose(int fd) g_handle[fd].useFlag = 0; if (g_handle[fd].pathName != NULL) { free(g_handle[fd].pathName); + g_handle[fd].pathName = NULL; } pthread_mutex_unlock(&g_FslocalMutex); From 3a4a8b0680d6df7d4bf20fe9ca633d8b7aa33a45 Mon Sep 17 00:00:00 2001 From: li_zan <371442490@qq.com> Date: Sat, 8 May 2021 15:14:45 +0800 Subject: [PATCH 6/9] Add already opened file logic --- components/fs/littlefs/lfs_api.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/components/fs/littlefs/lfs_api.c b/components/fs/littlefs/lfs_api.c index c09b7a47..8299d72c 100644 --- a/components/fs/littlefs/lfs_api.c +++ b/components/fs/littlefs/lfs_api.c @@ -68,18 +68,16 @@ LittleFsHandleStruct *GetFreeFd(const char *fileName, int *fd) return NULL; } -bool CheckFileIsOpen(const char *fileName, int *fd) +bool CheckFileIsOpen(const char *fileName) { 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; } @@ -230,8 +228,9 @@ int LfsOpen(const char *pathName, int openFlag, int mode) { int fd = INVALID_FD; - if (CheckFileIsOpen(pathName, &fd)) { - return fd; + // if file is already open, return invalid fd + if (CheckFileIsOpen(pathName)) { + goto errout; } LittleFsHandleStruct *fsHandle = GetFreeFd(pathName, &fd); From 96d2f2910ed46ed40032910ce9b1b5ed187978e8 Mon Sep 17 00:00:00 2001 From: li_zan <371442490@qq.com> Date: Sat, 8 May 2021 15:18:01 +0800 Subject: [PATCH 7/9] Add already opened file logic --- components/fs/littlefs/lfs_api.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/fs/littlefs/lfs_api.c b/components/fs/littlefs/lfs_api.c index 8299d72c..da18286d 100644 --- a/components/fs/littlefs/lfs_api.c +++ b/components/fs/littlefs/lfs_api.c @@ -48,7 +48,7 @@ FileOpInfo GetFsOpInfo(void) LittleFsHandleStruct *GetFreeFd(const char *fileName, int *fd) { - int len = strlen() + 1; + int len = strlen(fileName) + 1; pthread_mutex_lock(&g_FslocalMutex); for (int i = 0; i < LITTLE_FS_MAX_OPEN_FILES; i++) { From 602c9f6dfae1e6a59553dbfb1f501b945ddf26f0 Mon Sep 17 00:00:00 2001 From: li_zan <371442490@qq.com> Date: Sat, 8 May 2021 17:00:11 +0800 Subject: [PATCH 8/9] Add already opened file logic --- components/fs/littlefs/lfs_api.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/components/fs/littlefs/lfs_api.c b/components/fs/littlefs/lfs_api.c index da18286d..eb2b527f 100644 --- a/components/fs/littlefs/lfs_api.c +++ b/components/fs/littlefs/lfs_api.c @@ -46,7 +46,7 @@ FileOpInfo GetFsOpInfo(void) return g_fsOp; } -LittleFsHandleStruct *GetFreeFd(const char *fileName, int *fd) +LittleFsHandleStruct *LfsAllocFd(const char *fileName, int *fd) { int len = strlen(fileName) + 1; @@ -229,11 +229,11 @@ int LfsOpen(const char *pathName, int openFlag, int mode) int fd = INVALID_FD; // if file is already open, return invalid fd - if (CheckFileIsOpen(pathName)) { + if (pathName == NULL || CheckFileIsOpen(pathName)) { goto errout; } - LittleFsHandleStruct *fsHandle = GetFreeFd(pathName, &fd); + LittleFsHandleStruct *fsHandle = LfsAllocFd(pathName, &fd); if (fd == INVALID_FD) { goto errout; } @@ -283,8 +283,8 @@ int LfsClose(int fd) return ret; } - ret = lfs_file_close(&g_lfs, &(g_handle[fd].file)); pthread_mutex_lock(&g_FslocalMutex); + ret = lfs_file_close(&g_lfs, &(g_handle[fd].file)); g_handle[fd].useFlag = 0; if (g_handle[fd].pathName != NULL) { free(g_handle[fd].pathName); From 7d9976cd90cac52d4b4eb91e771e681d26e703e0 Mon Sep 17 00:00:00 2001 From: li_zan <371442490@qq.com> Date: Tue, 11 May 2021 20:30:31 +0800 Subject: [PATCH 9/9] Add already opened file logic --- components/fs/littlefs/lfs_api.c | 11 +++++------ components/fs/littlefs/lfs_api.h | 1 + 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/components/fs/littlefs/lfs_api.c b/components/fs/littlefs/lfs_api.c index eb2b527f..e30b3946 100644 --- a/components/fs/littlefs/lfs_api.c +++ b/components/fs/littlefs/lfs_api.c @@ -30,7 +30,6 @@ */ #include "lfs_api.h" -#include "iCunit.h" lfs_t g_lfs; FileDirInfo g_lfsDir[LFS_MAX_OPEN_DIRS] = {0}; @@ -57,7 +56,7 @@ LittleFsHandleStruct *LfsAllocFd(const char *fileName, int *fd) 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); + memcpy_s(g_handle[i].pathName, LITTLE_FS_MAX_NAME_LEN, fileName, len); } pthread_mutex_unlock(&g_FslocalMutex); return &(g_handle[i]); @@ -68,17 +67,17 @@ LittleFsHandleStruct *LfsAllocFd(const char *fileName, int *fd) return NULL; } -bool CheckFileIsOpen(const char *fileName) +BOOL CheckFileIsOpen(const char *fileName) { 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) { - return true; + return TRUE; } } } - return false; + return FALSE; } lfs_dir_t *GetFreeDir() @@ -101,7 +100,7 @@ int InitMountInfo(const char *fileSystemType, const struct MountOps *fsMops) for (int i = 0; i < MAX_FILE_SYSTEM_LEN; i++) { if (g_fsmap[i].fileSystemtype == NULL) { g_fsmap[i].fileSystemtype = (char*)malloc(len); - memcpy_s(g_fsmap[i].fileSystemtype, len, fileSystemType, len); + memcpy_s(g_fsmap[i].fileSystemtype, LITTLE_FS_MAX_NAME_LEN, fileSystemType, len); g_fsmap[i].fsMops = fsMops; return VFS_OK; } diff --git a/components/fs/littlefs/lfs_api.h b/components/fs/littlefs/lfs_api.h index 0f1a9c39..8f87d108 100644 --- a/components/fs/littlefs/lfs_api.h +++ b/components/fs/littlefs/lfs_api.h @@ -98,6 +98,7 @@ typedef struct { #define LITTLE_FS_MAX_OPEN_FILES 100 #define LITTLE_FS_STANDARD_NAME_LENGTH 50 +#define LITTLE_FS_MAX_NAME_LEN 255 #define MAX_DEF_BUF_NUM 21 #define MAX_BUFFER_LEN 100