Add already opened file logic

This commit is contained in:
li_zan 2021-05-07 11:35:08 +08:00
parent 9bebc0fb82
commit a99f817c23
2 changed files with 23 additions and 3 deletions

View File

@ -62,6 +62,21 @@ LittleFsHandleStruct *GetFreeFd(int *fd)
return NULL; 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() lfs_dir_t *GetFreeDir()
{ {
pthread_mutex_lock(&g_FslocalMutex); pthread_mutex_lock(&g_FslocalMutex);
@ -205,16 +220,20 @@ int LfsClosedir(const DIR *dir)
return lfs_dir_close(&g_lfs, (lfs_dir_t *)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; int fd = INVALID_FD;
if (CheckFileIsOpen(pathName, &fd)) {
return fd;
}
LittleFsHandleStruct *fsHandle = GetFreeFd(&fd); LittleFsHandleStruct *fsHandle = GetFreeFd(&fd);
if (fd == INVALID_FD) { if (fd == INVALID_FD) {
goto errout; 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) { if (err != 0) {
goto errout; goto errout;
} }

View File

@ -55,6 +55,7 @@ typedef unsigned mode_t;
typedef struct { typedef struct {
uint8_t useFlag; uint8_t useFlag;
const char *pathName;
lfs_file_t file; lfs_file_t file;
} LittleFsHandleStruct; } LittleFsHandleStruct;
@ -121,7 +122,7 @@ int LfsRmdir(const char *dirName);
DIR *LfsOpendir(const char *dirName); DIR *LfsOpendir(const char *dirName);
struct dirent *LfsReaddir(DIR *dir); struct dirent *LfsReaddir(DIR *dir);
int LfsClosedir(const 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 LfsRead(int fd, void *buf, unsigned int len);
int LfsWrite(int fd, const void *buf, unsigned int len); int LfsWrite(int fd, const void *buf, unsigned int len);
int LfsSeek(int fd, off_t offset, int whence); int LfsSeek(int fd, off_t offset, int whence);