add littlefs code

This commit is contained in:
li_zan 2021-04-29 15:09:13 +08:00
parent ac46317e0f
commit 580ef55f7e
2 changed files with 162 additions and 154 deletions

View File

@ -48,27 +48,31 @@ FileOpInfo GetFsOpInfo(void)
LittleFsHandleStruct *GetFreeFd(int *fd) LittleFsHandleStruct *GetFreeFd(int *fd)
{ {
pthread_mutex_lock(&g_FslocalMutex);
for (int i = 0; i < LITTLE_FS_MAX_OPEN_FILES; i++) { for (int i = 0; i < LITTLE_FS_MAX_OPEN_FILES; i++) {
if (g_handle[i].useFlag == 0) { if (g_handle[i].useFlag == 0) {
*fd = i; *fd = i;
g_handle[i].useFlag = 1; g_handle[i].useFlag = 1;
pthread_mutex_unlock(&g_FslocalMutex);
return &(g_handle[i]); return &(g_handle[i]);
} }
} }
pthread_mutex_unlock(&g_FslocalMutex);
*fd = INVALID_FD; *fd = INVALID_FD;
return NULL; return NULL;
} }
lfs_dir_t *GetFreeDir() lfs_dir_t *GetFreeDir()
{ {
pthread_mutex_lock(&g_FslocalMutex);
for (int i = 0; i < LFS_MAX_OPEN_DIRS; i++) { for (int i = 0; i < LFS_MAX_OPEN_DIRS; i++) {
if (g_lfsDir[i].useFlag == 0) { if (g_lfsDir[i].useFlag == 0) {
g_lfsDir[i].useFlag = 1; g_lfsDir[i].useFlag = 1;
pthread_mutex_unlock(&g_FslocalMutex);
return &(g_lfsDir[i].dir); return &(g_lfsDir[i].dir);
} }
} }
pthread_mutex_unlock(&g_FslocalMutex);
return NULL; return NULL;
} }
@ -87,7 +91,7 @@ int InitMountInfo(const char *fileSystemType, const struct MountOps *fsMops)
return VFS_ERROR; return VFS_ERROR;
} }
const struct fsmap_t *mount_findfs(const char*fileSystemtype) const struct fsmap_t *MountFindfs(const char*fileSystemtype)
{ {
struct fsmap_t *m = NULL; struct fsmap_t *m = NULL;
@ -179,6 +183,7 @@ struct dirent *LfsReaddir(DIR * dir)
ret = lfs_dir_read(&g_lfs, (lfs_dir_t *)dir, &lfsInfo); ret = lfs_dir_read(&g_lfs, (lfs_dir_t *)dir, &lfsInfo);
if (ret == 0) { if (ret == 0) {
pthread_mutex_lock(&g_FslocalMutex);
(void)memcpy_s(g_nameValue.d_name, sizeof(g_nameValue.d_name), lfsInfo.name, strlen(lfsInfo.name) + 1); (void)memcpy_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;
@ -187,6 +192,7 @@ struct dirent *LfsReaddir(DIR * dir)
} }
g_nameValue.d_reclen = lfsInfo.size; g_nameValue.d_reclen = lfsInfo.size;
pthread_mutex_unlock(&g_FslocalMutex);
return &g_nameValue; return &g_nameValue;
} }
@ -254,7 +260,9 @@ int LfsClose(int fd)
} }
ret = lfs_file_close(&g_lfs, &(g_handle[fd].file)); ret = lfs_file_close(&g_lfs, &(g_handle[fd].file));
pthread_mutex_lock(&g_FslocalMutex);
g_handle[fd].useFlag = 0; g_handle[fd].useFlag = 0;
pthread_mutex_unlock(&g_FslocalMutex);
return ret; return ret;
} }

View File

@ -127,6 +127,6 @@ int LfsStat(const char *path, struct stat *buf);
int LfsFsync(int fd); int LfsFsync(int fd);
FileOpInfo GetFsOpInfo(void); FileOpInfo GetFsOpInfo(void);
const struct fsmap_t *mount_findfs(const char *filesystemtype); const struct fsmap_t *MountFindfs(const char *filesystemtype);