Add already opened file logic
This commit is contained in:
parent
9bebc0fb82
commit
a99f817c23
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
|
Loading…
Reference in New Issue