Fix: littlefs some bug

Signed-off-by: li_zan <371442490@qq.com>
This commit is contained in:
li_zan 2021-06-03 15:52:03 +08:00
parent 05937ee49c
commit 51335629d0
2 changed files with 80 additions and 11 deletions

View File

@ -92,6 +92,22 @@ FileDirInfo *GetFreeDir(const char *dirName)
return NULL; return NULL;
} }
void FreeDirInfo(const char *dirName)
{
pthread_mutex_lock(&g_FslocalMutex);
for (int i = 0; i < LFS_MAX_OPEN_DIRS; i++) {
if (g_lfsDir[i].useFlag == 1 && strcmp(g_lfsDir[i].dirName, dirName) == 0) {
g_lfsDir[i].useFlag = 0;
if (g_lfsDir[i].dirName) {
free(g_lfsDir[i].dirName);
g_lfsDir[i].dirName = NULL;
}
pthread_mutex_unlock(&g_FslocalMutex);
}
}
pthread_mutex_unlock(&g_FslocalMutex);
}
BOOL CheckDirIsOpen(const char *dirName) BOOL CheckDirIsOpen(const char *dirName)
{ {
pthread_mutex_lock(&g_FslocalMutex); pthread_mutex_lock(&g_FslocalMutex);
@ -110,7 +126,7 @@ BOOL CheckDirIsOpen(const char *dirName)
BOOL CheckPathIsMounted(const char *pathName, struct FileOpInfo **fileOpInfo) BOOL CheckPathIsMounted(const char *pathName, struct FileOpInfo **fileOpInfo)
{ {
char tmpName[LITTLEFS_MAX_LFN_LEN] = {0}; char tmpName[LITTLEFS_MAX_LFN_LEN] = {0};
int mountPathNameLen = 0; int mountPathNameLen;
int len = strlen(pathName) + 1; int len = strlen(pathName) + 1;
pthread_mutex_lock(&g_FslocalMutex); pthread_mutex_lock(&g_FslocalMutex);
@ -206,6 +222,41 @@ int FreeMountRes(const char *target)
return VFS_ERROR; return VFS_ERROR;
} }
int ConvertFlagToLfsOpenFlag (int oflags)
{
int lfsOpenFlag = 0;
if (oflags & O_CREAT) {
lfsOpenFlag |= LFS_O_CREAT;
}
if (oflags & O_EXCL) {
lfsOpenFlag |= LFS_O_EXCL;
}
if (oflags & O_TRUNC) {
lfsOpenFlag |= LFS_O_TRUNC;
}
if (oflags & O_APPEND) {
lfsOpenFlag |= LFS_O_APPEND;
}
if (oflags & O_RDWR) {
lfsOpenFlag |= LFS_O_RDWR;
}
if (oflags & O_WRONLY) {
lfsOpenFlag |= LFS_O_WRONLY;
}
if (oflags & O_RDONLY) {
lfsOpenFlag |= LFS_O_RDONLY;
}
return lfsOpenFlag;
}
const struct MountOps g_lfsMnt = { const struct MountOps g_lfsMnt = {
.Mount = LfsMount, .Mount = LfsMount,
.Umount = LfsUmount, .Umount = LfsUmount,
@ -256,14 +307,23 @@ int LfsMount(const char *source, const char *target, const char *fileSystemType,
goto errout; goto errout;
} }
return lfs_mount(&(fileOpInfo->lfsInfo), (struct lfs_config*)data); ret = lfs_mount(&(fileOpInfo->lfsInfo), (struct lfs_config*)data);
if (ret != 0) {
ret = lfs_format(&(fileOpInfo->lfsInfo), (struct lfs_config*)data);
if (ret == 0) {
ret = lfs_mount(&(fileOpInfo->lfsInfo), (struct lfs_config*)data);
}
}
return ret;
errout: errout:
return ret; return ret;
} }
int LfsUmount(const char *target) int LfsUmount(const char *target)
{ {
int ret, mountIndex = -1; int ret;
int mountIndex = -1;
struct FileOpInfo *fileOpInfo = NULL; struct FileOpInfo *fileOpInfo = NULL;
if (target == NULL) { if (target == NULL) {
@ -376,7 +436,7 @@ struct dirent *LfsReaddir(DIR *dir)
ret = lfs_dir_read(dirInfo->lfsHandle, (lfs_dir_t *)(&(dirInfo->dir)), &lfsInfo); ret = lfs_dir_read(dirInfo->lfsHandle, (lfs_dir_t *)(&(dirInfo->dir)), &lfsInfo);
if (ret == 0) { if (ret == 0) {
pthread_mutex_lock(&g_FslocalMutex); pthread_mutex_lock(&g_FslocalMutex);
g_nameValue.d_name = strdup(lfsInfo.name); (void)strncpy_s(g_nameValue.d_name, sizeof(g_nameValue.d_name), lfsInfo.name, strlen(lfsInfo.name) + 1);
if (lfsInfo.type == LFS_TYPE_DIR) { if (lfsInfo.type == LFS_TYPE_DIR) {
g_nameValue.d_type = DT_DIR; g_nameValue.d_type = DT_DIR;
} else if (lfsInfo.type == LFS_TYPE_REG) { } else if (lfsInfo.type == LFS_TYPE_REG) {
@ -394,18 +454,25 @@ struct dirent *LfsReaddir(DIR *dir)
int LfsClosedir(const DIR *dir) int LfsClosedir(const DIR *dir)
{ {
int ret;
FileDirInfo *dirInfo = (FileDirInfo *)dir; FileDirInfo *dirInfo = (FileDirInfo *)dir;
if (dirInfo == NULL || dirInfo->lfsHandle == NULL) { if (dirInfo == NULL || dirInfo->lfsHandle == NULL) {
return VFS_ERROR; return VFS_ERROR;
} }
return lfs_dir_close(dirInfo->lfsHandle, (lfs_dir_t *)(&(dirInfo->dir))); ret = lfs_dir_close(dirInfo->lfsHandle, (lfs_dir_t *)(&(dirInfo->dir)));
FreeDirInfo(dirInfo->dirName);
return ret;
} }
int LfsOpen(const char *pathName, int openFlag, int mode) int LfsOpen(const char *pathName, int openFlag, int mode)
{ {
int fd = INVALID_FD; int fd = INVALID_FD;
int err = INVALID_FD;
struct FileOpInfo *fileOpInfo = NULL; struct FileOpInfo *fileOpInfo = NULL;
if (pathName == NULL) { if (pathName == NULL) {
@ -425,7 +492,8 @@ int LfsOpen(const char *pathName, int openFlag, int mode)
goto errout; goto errout;
} }
int err = lfs_file_open(&(fileOpInfo->lfsInfo), &(fsHandle->file), pathName, openFlag); int lfsOpenFlag = ConvertFlagToLfsOpenFlag(openFlag);
err = lfs_file_open(&(fileOpInfo->lfsInfo), &(fsHandle->file), pathName, lfsOpenFlag);
if (err != 0) { if (err != 0) {
goto errout; goto errout;
} }
@ -433,7 +501,7 @@ int LfsOpen(const char *pathName, int openFlag, int mode)
g_handle[fd].lfsHandle = &(fileOpInfo->lfsInfo); g_handle[fd].lfsHandle = &(fileOpInfo->lfsInfo);
return fd; return fd;
errout: errout:
return INVALID_FD; return err;
} }
int LfsRead(int fd, void *buf, unsigned int len) int LfsRead(int fd, void *buf, unsigned int len)

View File

@ -33,6 +33,7 @@
#define _LFS_API_H_ #define _LFS_API_H_
#include "bits/alltypes.h" #include "bits/alltypes.h"
#include "fcntl.h"
#include "sys/stat.h" #include "sys/stat.h"
#include "dirent.h" #include "dirent.h"