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

@ -43,242 +43,250 @@ static pthread_mutex_t g_FslocalMutex = PTHREAD_MUTEX_INITIALIZER;
FileOpInfo GetFsOpInfo(void) FileOpInfo GetFsOpInfo(void)
{ {
return g_fsOp; return g_fsOp;
} }
LittleFsHandleStruct *GetFreeFd(int *fd) LittleFsHandleStruct *GetFreeFd(int *fd)
{ {
for (int i = 0; i < LITTLE_FS_MAX_OPEN_FILES; i++) { pthread_mutex_lock(&g_FslocalMutex);
if (g_handle[i].useFlag == 0) { for (int i = 0; i < LITTLE_FS_MAX_OPEN_FILES; i++) {
*fd = i; if (g_handle[i].useFlag == 0) {
g_handle[i].useFlag = 1; *fd = i;
return &(g_handle[i]); g_handle[i].useFlag = 1;
} pthread_mutex_unlock(&g_FslocalMutex);
} return &(g_handle[i]);
}
*fd = INVALID_FD; }
return NULL; pthread_mutex_unlock(&g_FslocalMutex);
*fd = INVALID_FD;
return NULL;
} }
lfs_dir_t *GetFreeDir() lfs_dir_t *GetFreeDir()
{ {
for (int i = 0; i < LFS_MAX_OPEN_DIRS; i++) { pthread_mutex_lock(&g_FslocalMutex);
if (g_lfsDir[i].useFlag == 0) { for (int i = 0; i < LFS_MAX_OPEN_DIRS; i++) {
g_lfsDir[i].useFlag = 1; if (g_lfsDir[i].useFlag == 0) {
return &(g_lfsDir[i].dir); g_lfsDir[i].useFlag = 1;
} pthread_mutex_unlock(&g_FslocalMutex);
} return &(g_lfsDir[i].dir);
}
return NULL; }
pthread_mutex_unlock(&g_FslocalMutex);
return NULL;
} }
int InitMountInfo(const char *fileSystemType, const struct MountOps *fsMops) int InitMountInfo(const char *fileSystemType, const struct MountOps *fsMops)
{ {
int len = strlen(fileSystemType) + 1; int len = strlen(fileSystemType) + 1;
for (int i = 0; i < MAX_FILE_SYSTEM_LEN; i++) { for (int i = 0; i < MAX_FILE_SYSTEM_LEN; i++) {
if (g_fsmap[i].fileSystemtype == NULL) { if (g_fsmap[i].fileSystemtype == NULL) {
g_fsmap[i].fileSystemtype = (char*)malloc(len); g_fsmap[i].fileSystemtype = (char*)malloc(len);
memcpy_s(g_fsmap[i].fileSystemtype, len, fileSystemType, len); memcpy_s(g_fsmap[i].fileSystemtype, len, fileSystemType, len);
g_fsmap[i].fs_mops = fsMops; g_fsmap[i].fs_mops = fsMops;
return VFS_OK; return VFS_OK;
} }
} }
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;
for (int i = 0; i < MAX_FILE_SYSTEM_LEN; i++) { for (int i = 0; i < MAX_FILE_SYSTEM_LEN; i++) {
m = &(g_fsmap[i]); m = &(g_fsmap[i]);
if (m->fileSystemtype && strcmp(fileSystemtype, m->fileSystemtype) == 0) { if (m->fileSystemtype && strcmp(fileSystemtype, m->fileSystemtype) == 0) {
return m; return m;
} }
} }
return (const struct fsmap_t *)NULL; return (const struct fsmap_t *)NULL;
} }
const struct MountOps g_fsMnt = { const struct MountOps g_fsMnt = {
.Mount = LfsMount, .Mount = LfsMount,
.Umount = LfsUmount, .Umount = LfsUmount,
}; };
const struct FileOps lfs_vops = { const struct FileOps lfs_vops = {
.Mkdir = LfsMkdir, .Mkdir = LfsMkdir,
.Unlink = LfsUnlink, .Unlink = LfsUnlink,
.Rmdir = LfsRmdir, .Rmdir = LfsRmdir,
.Opendir = LfsOpendir, .Opendir = LfsOpendir,
.Readdir = LfsReaddir, .Readdir = LfsReaddir,
.Closedir = LfsClosedir, .Closedir = LfsClosedir,
.Open = LfsOpen, .Open = LfsOpen,
.Close = LfsClose, .Close = LfsClose,
.Write = LfsWrite, .Write = LfsWrite,
.Read = LfsRead, .Read = LfsRead,
.Seek = LfsSeek, .Seek = LfsSeek,
.Rename = LfsRename, .Rename = LfsRename,
.Getattr = LfsStat, .Getattr = LfsStat,
.Fsync = LfsFsync, .Fsync = LfsFsync,
}; };
int LfsMount(const char * source, const char * target, const char * fileSystemType, unsigned long mountflags, int LfsMount(const char * source, const char * target, const char * fileSystemType, unsigned long mountflags,
const void * data) const void * data)
{ {
int ret; int ret;
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);
return ret; return ret;
} }
int LfsUmount(const char * target) int LfsUmount(const char * target)
{ {
return lfs_unmount(&g_lfs); return lfs_unmount(&g_lfs);
} }
int LfsUnlink(const char * fileName) int LfsUnlink(const char * fileName)
{ {
return lfs_remove(&g_lfs, fileName); return lfs_remove(&g_lfs, fileName);
} }
int LfsMkdir(const char * dirName, mode_t mode) int LfsMkdir(const char * dirName, mode_t mode)
{ {
return lfs_mkdir(&g_lfs, dirName); return lfs_mkdir(&g_lfs, dirName);
} }
int LfsRmdir(const char * dirName) int LfsRmdir(const char * dirName)
{ {
return lfs_remove(&g_lfs, dirName); return lfs_remove(&g_lfs, dirName);
} }
DIR *LfsOpendir(const char * dirName) DIR *LfsOpendir(const char * dirName)
{ {
int ret; int ret;
lfs_dir_t *dir = GetFreeDir(); lfs_dir_t *dir = GetFreeDir();
if (dir == NULL) { if (dir == NULL) {
return NULL; return NULL;
} }
ret = lfs_dir_open(&g_lfs, dir, dirName); ret = lfs_dir_open(&g_lfs, dir, dirName);
if (ret == 0) { if (ret == 0) {
return (DIR *)dir; return (DIR *)dir;
} else { } else {
return NULL; return NULL;
} }
} }
struct dirent *LfsReaddir(DIR * dir) struct dirent *LfsReaddir(DIR * dir)
{ {
int ret; int ret;
struct lfs_info lfsInfo; struct lfs_info lfsInfo;
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); pthread_mutex_lock(&g_FslocalMutex);
if (lfsInfo.type == LFS_TYPE_DIR) { (void)memcpy_s(g_nameValue.d_name, sizeof(g_nameValue.d_name), lfsInfo.name, strlen(lfsInfo.name) + 1);
g_nameValue.d_type = DT_DIR; if (lfsInfo.type == LFS_TYPE_DIR) {
} else if (lfsInfo.type == LFS_TYPE_REG) { g_nameValue.d_type = DT_DIR;
g_nameValue.d_type = DT_REG; } else if (lfsInfo.type == LFS_TYPE_REG) {
} g_nameValue.d_type = DT_REG;
}
g_nameValue.d_reclen = lfsInfo.size; g_nameValue.d_reclen = lfsInfo.size;
pthread_mutex_unlock(&g_FslocalMutex);
return &g_nameValue; return &g_nameValue;
} }
return NULL; return NULL;
} }
int LfsClosedir(DIR * dir) int LfsClosedir(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 * path, int openFlag, int mode)
{ {
int fd = INVALID_FD; int fd = INVALID_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), path, openFlag);
if (err != 0) { if (err != 0) {
goto errout; goto errout;
} }
return fd; return fd;
errout: errout:
return INVALID_FD; return INVALID_FD;
} }
int LfsRead(int fd, void * buf, unsigned int len) int LfsRead(int fd, void * buf, unsigned int len)
{ {
if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
return VFS_ERROR; return VFS_ERROR;
} }
return lfs_file_read(&g_lfs, &(g_handle[fd].file), buf, len); return lfs_file_read(&g_lfs, &(g_handle[fd].file), buf, len);
} }
int LfsWrite(int fd, const void * buf, unsigned int len) int LfsWrite(int fd, const void * buf, unsigned int len)
{ {
if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
return VFS_ERROR; return VFS_ERROR;
} }
return lfs_file_write(&g_lfs, &(g_handle[fd].file), buf, len); return lfs_file_write(&g_lfs, &(g_handle[fd].file), buf, len);
} }
int LfsSeek(int fd, off_t offset, int whence) int LfsSeek(int fd, off_t offset, int whence)
{ {
if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
return VFS_ERROR; return VFS_ERROR;
} }
return lfs_file_seek(&g_lfs, &(g_handle[fd].file), offset, whence); return lfs_file_seek(&g_lfs, &(g_handle[fd].file), offset, whence);
} }
int LfsClose(int fd) int LfsClose(int fd)
{ {
int ret = VFS_ERROR; 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 ret;
} }
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; pthread_mutex_lock(&g_FslocalMutex);
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)
{ {
return lfs_rename(&g_lfs, oldName, newName); return lfs_rename(&g_lfs, oldName, newName);
} }
int LfsStat(const char * path, struct stat * buf) int LfsStat(const char * path, struct stat * buf)
{ {
int ret; int ret;
struct lfs_info info; struct lfs_info info;
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;
} }
return ret; return ret;
} }
int LfsFsync(int fd) int LfsFsync(int fd)
{ {
return lfs_file_sync(&g_lfs, &(g_handle[fd].file)); return lfs_file_sync(&g_lfs, &(g_handle[fd].file));
} }

View File

@ -50,45 +50,45 @@ typedef unsigned mode_t;
#endif #endif
typedef struct { typedef struct {
uint8_t useFlag; uint8_t useFlag;
lfs_file_t file; lfs_file_t file;
} LittleFsHandleStruct; } LittleFsHandleStruct;
struct MountOps { struct MountOps {
int (*Mount)(const char *source, const char *target, const char *filesystemtype, unsigned long mountflags, int (*Mount)(const char *source, const char *target, const char *filesystemtype, unsigned long mountflags,
const void *data); const void *data);
int (*Umount)(const char* target); int (*Umount)(const char* target);
}; };
struct fsmap_t { struct fsmap_t {
const char *fileSystemtype; const char *fileSystemtype;
const struct MountOps *fs_mops; const struct MountOps *fs_mops;
}; };
struct FileOps { struct FileOps {
int (*Open)(const char *path, int openFlag, int mode); int (*Open)(const char *path, int openFlag, int mode);
int (*Close)(int fd); int (*Close)(int fd);
int (*Unlink)(const char *fileName); int (*Unlink)(const char *fileName);
int (*Rmdir)(const char *dirName); int (*Rmdir)(const char *dirName);
int (*Mkdir)(const char *dirName, mode_t mode); int (*Mkdir)(const char *dirName, mode_t mode);
struct dirent *(*Readdir)(DIR *dir); struct dirent *(*Readdir)(DIR *dir);
DIR *(*Opendir)(const char *dirName); DIR *(*Opendir)(const char *dirName);
int (*Closedir)(DIR *dir); int (*Closedir)(DIR *dir);
int (*Read)(int fd, void *buf, size_t len); int (*Read)(int fd, void *buf, size_t len);
int (*Write)(int fd, const void *buf, size_t len); int (*Write)(int fd, const void *buf, size_t len);
int (*Seek)(int fd, off_t offset, int whence); int (*Seek)(int fd, off_t offset, int whence);
int (*Getattr)(const char *path, struct stat *buf); int (*Getattr)(const char *path, struct stat *buf);
int (*Rename)(const char *oldName, const char *newName); int (*Rename)(const char *oldName, const char *newName);
int (*Fsync)(int fd); int (*Fsync)(int fd);
}; };
typedef struct { typedef struct {
struct FileOps *fsVops; struct FileOps *fsVops;
} FileOpInfo; } FileOpInfo;
typedef struct { typedef struct {
uint8_t useFlag; uint8_t useFlag;
lfs_dir_t dir; lfs_dir_t dir;
} FileDirInfo; } FileDirInfo;
#define LITTLE_FS_MAX_OPEN_FILES 100 #define LITTLE_FS_MAX_OPEN_FILES 100
@ -108,7 +108,7 @@ LittleFsHandleStruct *GetFreeFd(int *fd);
int InitMountInfo(const char *fileSystemType, const struct MountOps *fsMops); int InitMountInfo(const char *fileSystemType, const struct MountOps *fsMops);
int LfsMount(const char *source, const char *target, const char *fileSystemType, unsigned long mountflags, int LfsMount(const char *source, const char *target, const char *fileSystemType, unsigned long mountflags,
const void *data); const void *data);
int LfsUmount(const char *target); int LfsUmount(const char *target);
int LfsUnlink(const char *fileName); int LfsUnlink(const char *fileName);
@ -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);