add littlefs code

This commit is contained in:
li_zan 2021-04-25 20:42:35 +08:00
parent 10b416e74f
commit ac46317e0f
1 changed files with 13 additions and 87 deletions

View File

@ -128,56 +128,30 @@ int LfsMount(const char * source, const char * target, const char * fileSystemTy
{ {
int ret; int ret;
pthread_mutex_lock(&g_FslocalMutex);
g_fsOp.fsVops = &lfs_vops; g_fsOp.fsVops = &lfs_vops;
ret = lfs_mount(&g_lfs, (struct lfs_config*)data); ret = lfs_mount(&g_lfs, (struct lfs_config*)data);
pthread_mutex_unlock(&g_FslocalMutex);
return ret; return ret;
} }
int LfsUmount(const char * target) int LfsUmount(const char * target)
{ {
int ret; return lfs_unmount(&g_lfs);
pthread_mutex_lock(&g_FslocalMutex);
ret = lfs_unmount(&g_lfs);
pthread_mutex_unlock(&g_FslocalMutex);
return ret;
} }
int LfsUnlink(const char * fileName) int LfsUnlink(const char * fileName)
{ {
int ret; return lfs_remove(&g_lfs, fileName);
pthread_mutex_lock(&g_FslocalMutex);
ret = lfs_remove(&g_lfs, fileName);
pthread_mutex_unlock(&g_FslocalMutex);
return ret;
} }
int LfsMkdir(const char * dirName, mode_t mode) int LfsMkdir(const char * dirName, mode_t mode)
{ {
int ret; return lfs_mkdir(&g_lfs, dirName);
pthread_mutex_lock(&g_FslocalMutex);
ret = lfs_mkdir(&g_lfs, dirName);
pthread_mutex_unlock(&g_FslocalMutex);
return ret;
} }
int LfsRmdir(const char * dirName) int LfsRmdir(const char * dirName)
{ {
int ret; return lfs_remove(&g_lfs, dirName);
pthread_mutex_lock(&g_FslocalMutex);
ret = lfs_remove(&g_lfs, dirName);
pthread_mutex_unlock(&g_FslocalMutex);
return ret;
} }
DIR *LfsOpendir(const char * dirName) DIR *LfsOpendir(const char * dirName)
@ -189,9 +163,7 @@ DIR *LfsOpendir(const char * dirName)
return NULL; return NULL;
} }
pthread_mutex_lock(&g_FslocalMutex);
ret = lfs_dir_open(&g_lfs, dir, dirName); ret = lfs_dir_open(&g_lfs, dir, dirName);
pthread_mutex_unlock(&g_FslocalMutex);
if (ret == 0) { if (ret == 0) {
return (DIR *)dir; return (DIR *)dir;
@ -205,7 +177,6 @@ struct dirent *LfsReaddir(DIR * dir)
int ret; int ret;
struct lfs_info lfsInfo; struct lfs_info lfsInfo;
pthread_mutex_lock(&g_FslocalMutex);
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) {
(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);
@ -216,31 +187,22 @@ 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;
} }
pthread_mutex_unlock(&g_FslocalMutex);
return NULL; return NULL;
} }
int LfsClosedir(DIR * dir) int LfsClosedir(DIR * dir)
{ {
int ret; return lfs_dir_close(&g_lfs, (lfs_dir_t *)dir);
pthread_mutex_lock(&g_FslocalMutex);
ret = lfs_dir_close(&g_lfs, (lfs_dir_t *)dir);
pthread_mutex_unlock(&g_FslocalMutex);
return ret;
} }
int LfsOpen(const char * path, int openFlag, int mode) int LfsOpen(const char * path, int openFlag, int mode)
{ {
int fd = INVALID_FD; int fd = INVALID_FD;
pthread_mutex_lock(&g_FslocalMutex);
LittleFsHandleStruct *fsHandle = GetFreeFd(&fd); LittleFsHandleStruct *fsHandle = GetFreeFd(&fd);
if (fd == INVALID_FD) { if (fd == INVALID_FD) {
goto errout; goto errout;
@ -251,56 +213,36 @@ int LfsOpen(const char * path, int openFlag, int mode)
goto errout; goto errout;
} }
pthread_mutex_unlock(&g_FslocalMutex);
return fd; return fd;
errout: errout:
pthread_mutex_unlock(&g_FslocalMutex);
return INVALID_FD; return INVALID_FD;
} }
int LfsRead(int fd, void * buf, unsigned int len) int LfsRead(int fd, void * buf, unsigned int len)
{ {
int ret = VFS_ERROR;
if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
return ret; return VFS_ERROR;
} }
pthread_mutex_lock(&g_FslocalMutex); return lfs_file_read(&g_lfs, &(g_handle[fd].file), buf, len);
ret = lfs_file_read(&g_lfs, &(g_handle[fd].file), buf, len);
pthread_mutex_unlock(&g_FslocalMutex);
return ret;
} }
int LfsWrite(int fd, const void * buf, unsigned int len) int LfsWrite(int fd, const void * buf, unsigned int len)
{ {
int ret = VFS_ERROR;
if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
return ret; return VFS_ERROR;
} }
pthread_mutex_lock(&g_FslocalMutex); return lfs_file_write(&g_lfs, &(g_handle[fd].file), buf, len);
ret = lfs_file_write(&g_lfs, &(g_handle[fd].file), buf, len);
pthread_mutex_unlock(&g_FslocalMutex);
return ret;
} }
int LfsSeek(int fd, off_t offset, int whence) int LfsSeek(int fd, off_t offset, int whence)
{ {
int ret = VFS_ERROR;
if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
return ret; return VFS_ERROR;
} }
pthread_mutex_lock(&g_FslocalMutex); return lfs_file_seek(&g_lfs, &(g_handle[fd].file), offset, whence);
ret = lfs_file_seek(&g_lfs, &(g_handle[fd].file), offset, whence);
pthread_mutex_unlock(&g_FslocalMutex);
return ret;
} }
int LfsClose(int fd) int LfsClose(int fd)
@ -311,23 +253,15 @@ int LfsClose(int fd)
return ret; return ret;
} }
pthread_mutex_lock(&g_FslocalMutex);
ret = lfs_file_close(&g_lfs, &(g_handle[fd].file)); ret = lfs_file_close(&g_lfs, &(g_handle[fd].file));
g_handle[fd].useFlag = 0; g_handle[fd].useFlag = 0;
pthread_mutex_unlock(&g_FslocalMutex);
return ret; return ret;
} }
int LfsRename(const char * oldName, const char * newName) int LfsRename(const char * oldName, const char * newName)
{ {
int ret; return lfs_rename(&g_lfs, oldName, newName);
pthread_mutex_lock(&g_FslocalMutex);
ret = lfs_rename(&g_lfs, oldName, newName);
pthread_mutex_unlock(&g_FslocalMutex);
return ret;
} }
int LfsStat(const char * path, struct stat * buf) int LfsStat(const char * path, struct stat * buf)
@ -335,24 +269,16 @@ int LfsStat(const char * path, struct stat * buf)
int ret; int ret;
struct lfs_info info; struct lfs_info info;
pthread_mutex_lock(&g_FslocalMutex);
ret = lfs_stat(&g_lfs, path, &info); ret = lfs_stat(&g_lfs, path, &info);
if (ret == 0) { if (ret == 0) {
buf->st_size = info.size; buf->st_size = info.size;
} }
pthread_mutex_unlock(&g_FslocalMutex);
return ret; return ret;
} }
int LfsFsync(int fd) int LfsFsync(int fd)
{ {
int ret; return lfs_file_sync(&g_lfs, &(g_handle[fd].file));
pthread_mutex_lock(&g_FslocalMutex);
ret = lfs_file_sync(&g_lfs, &(g_handle[fd].file));
pthread_mutex_unlock(&g_FslocalMutex);
return ret;
} }