From 606bd9ca08c560e7825e56be9afa95f9f5aa15df Mon Sep 17 00:00:00 2001 From: li_zan <371442490@qq.com> Date: Sat, 24 Apr 2021 11:23:49 +0800 Subject: [PATCH 1/8] add littlefs code --- components/fs/BUILD.gn | 4 + components/fs/littlefs/BUILD.gn | 45 ++++ components/fs/littlefs/lfs_api.c | 343 +++++++++++++++++++++++++++++++ components/fs/littlefs/lfs_api.h | 132 ++++++++++++ 4 files changed, 524 insertions(+) create mode 100644 components/fs/littlefs/BUILD.gn create mode 100644 components/fs/littlefs/lfs_api.c create mode 100644 components/fs/littlefs/lfs_api.h diff --git a/components/fs/BUILD.gn b/components/fs/BUILD.gn index 094d2edc..5114fdd3 100644 --- a/components/fs/BUILD.gn +++ b/components/fs/BUILD.gn @@ -29,6 +29,7 @@ declare_args() { enable_ohos_kernel_liteos_m_fatfs = true + enable_ohos_kernel_liteos_m_littlefs = false } group("fs") { @@ -36,4 +37,7 @@ group("fs") { if (enable_ohos_kernel_liteos_m_fatfs == true) { deps += [ "fatfs:fatfs" ] } + if (enable_ohos_kernel_liteos_m_littlefs == true) { + deps += [ "littlefs:littlefs" ] + } } diff --git a/components/fs/littlefs/BUILD.gn b/components/fs/littlefs/BUILD.gn new file mode 100644 index 00000000..f3d8c9ea --- /dev/null +++ b/components/fs/littlefs/BUILD.gn @@ -0,0 +1,45 @@ +# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. +# Copyright (c) 2020-2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +static_library("littlefs") { + sources = [ + "lfs_api.c", + ] + + include_dirs = [ + "../../../kernel/arch/include", + "../../../kernel/include", + "../../../utils", + "../../../kal/cmsis", + "../../../kal", + "../../../kal/posix/include", + "./", + "//third_party/littlefs/include", + ] +} diff --git a/components/fs/littlefs/lfs_api.c b/components/fs/littlefs/lfs_api.c new file mode 100644 index 00000000..d96c8624 --- /dev/null +++ b/components/fs/littlefs/lfs_api.c @@ -0,0 +1,343 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "lfs_api.h" +#include "iCunit.h" + +lfs_t g_lfs; +FileDirInfo g_lfsDir[LFS_MAX_OPEN_DIRS]; + +FileOpInfo g_fsOp; +static LittleFsHandleStruct g_handle[LITTLE_FS_MAX_OPEN_FILES]; +struct dirent g_nameValue; +struct fsmap_t g_fsmap[MAX_FILE_SYSTEM_LEN]; +static pthread_mutex_t g_FslocalMutex = PTHREAD_MUTEX_INITIALIZER; + +FileOpInfo GetFsOpInfo(void) +{ + return g_fsOp; +} + +LittleFsHandleStruct *GetFreeFd(int *fd) +{ + for (int i = 0; i < LITTLE_FS_MAX_OPEN_FILES; i++) { + if (g_handle[i].useFlag == 0) { + *fd = i; + g_handle[i].useFlag = 1; + return &(g_handle[i]); + } + } + + *fd = INVALID_FD; + return NULL; +} + +lfs_dir_t *GetFreeDir() +{ + for (int i = 0; i < LFS_MAX_OPEN_DIRS; i++) { + if (g_lfsDir[i].useFlag == 0) { + g_lfsDir[i].useFlag = 1; + return &(g_lfsDir[i].dir); + } + } + + return NULL; +} + +int InitMountInfo(const char *fileSystemType, const struct MountOps *fsMops) +{ + int len = strlen(fileSystemType) + 1; + for (int i = 0; i < MAX_FILE_SYSTEM_LEN; i++) { + if (g_fsmap[i].fileSystemtype == NULL) { + g_fsmap[i].fileSystemtype = (char*)malloc(len); + memcpy_s(g_fsmap[i].fileSystemtype, len, fileSystemType, len); + g_fsmap[i].fs_mops = fsMops; + return VFS_OK; + } + } + + return VFS_ERROR; +} + +const struct fsmap_t *mount_finds(const char*fileSystemtype) +{ + struct fsmap_t *m = NULL; + + for (int i = 0; i < MAX_FILE_SYSTEM_LEN; i++) { + m = &(g_fsmap[i]); + if (m->fileSystemtype && strcmp(fileSystemtype, m->fileSystemtype) == 0) { + return m; + } + } + + return (const struct fsmap_t *)NULL; +} + +const struct MountOps g_fsMnt = { + +}; + +struct FileOps lfs_vops = { + +}; + +int LfsMount(const char * source, const char * target, const char * fileSystemType, unsigned long mountflags, + const void * data) +{ + int ret; + + pthread_mutex_lock(&g_FslocalMutex); + g_fsOp.fsVops = &lfs_vops; + ret = lfs_mount(&g_lfs, (struct lfs_config*)data); + pthread_mutex_unlock(&g_FslocalMutex); + + return ret; +} + +int LfsUmount(const char * target) +{ + int ret; + + pthread_mutex_lock(&g_FslocalMutex); + ret = lfs_unmount(&g_lfs); + pthread_mutex_unlock(&g_FslocalMutex); + + return ret; +} + +int LfsUnlink(const char * fileName) +{ + int ret; + + 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 ret; + + pthread_mutex_lock(&g_FslocalMutex); + ret = lfs_mkdir(&g_lfs, dirName); + pthread_mutex_unlock(&g_FslocalMutex); + + return ret; +} + +int LfsRmdir(const char * dirName) +{ + int ret; + + pthread_mutex_lock(&g_FslocalMutex); + ret = lfs_remove(&g_lfs, dirName); + pthread_mutex_unlock(&g_FslocalMutex); + + return ret; +} + +DIR *LfsOpendir(const char * dirName) +{ + int ret; + + lfs_dir_t *dir = GetFreeDir(); + if (dir == NULL) { + return NULL; + } + + pthread_mutex_lock(&g_FslocalMutex); + ret = lfs_dir_open(&g_lfs, dir, dirName); + pthread_mutex_unlock(&g_FslocalMutex); + + if (ret == 0) { + return (DIR *)dir; + } else { + return NULL; + } +} + +struct direct *LfsReaddir(DIR * dir) +{ + int ret; + struct lfs_info lfsInfo; + + pthread_mutex_lock(&g_FslocalMutex); + ret = lfs_dir_read(&g_lfs, (lfs_dir_t *)dir, &lfsInfo); + if (ret == 0) { + (void)memcpy_s(g_nameValue.d_name, sizeof(g_nameValue.d_name), lfsInfo.name, strlen(lfsInfo.name) + 1); + if (lfsInfo.type == LFS_TYPE_DIR) { + g_nameValue.d_type = DT_DIR; + } else if (lfsInfo.type == LFS_TYPE_REG) { + g_nameValue.d_type = DT_REG; + } + + g_nameValue.d_reclen = lfsInfo.size; + pthread_mutex_unlock(&g_FslocalMutex); + + return &g_nameValue; + } + + pthread_mutex_unlock(&g_FslocalMutex); + return NULL; +} + +int LfsCloseDir(DIR * dir) +{ + int ret; + + pthread_mutex_lock(&g_FslocalMutex); + ret = lfs_remove(&g_lfs, dirName); + pthread_mutex_unlock(&g_FslocalMutex); + + return ret; +} + +int LfsOpen(const char * path, int openFlag, int mode) +{ + int fd = INVALID_FD; + + pthread_mutex_lock(&g_FslocalMutex); + LittleFsHandleStruct *fsHandle = GetFreeDir(&fd); + if (fd == INVALID_FD) { + goto errout; + } + + int err = lfs_file_open(lfs_t * lfs, lfs_file_t * file, const char * path, int flags); + if (err != 0) { + goto errout; + } + + pthread_mutex_unlock(&g_FslocalMutex); + return fd; +errout: + pthread_mutex_unlock(&g_FslocalMutex); + return INVALID_FD; +} + +int LfsRead(int fd, void * buf, unsigned int len) +{ + int ret = VFS_ERROR; + + if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { + return ret; + } + + pthread_mutex_lock(&g_FslocalMutex); + 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 ret = VFS_ERROR; + + if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { + return ret; + } + + pthread_mutex_lock(&g_FslocalMutex); + 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 ret = VFS_ERROR; + + if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { + return ret; + } + + pthread_mutex_lock(&g_FslocalMutex); + ret = lfs_file_seek(&g_lfs, &(g_handle[fd].file), offset, whence); + pthread_mutex_unlock(&g_FslocalMutex); + + return ret; +} + +int LfsClose(int fd) +{ + int ret = VFS_ERROR; + + if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { + return ret; + } + + pthread_mutex_lock(&g_FslocalMutex); + ret = lfs_file_close(&g_lfs, &(g_handle[fd].file)); + pthread_mutex_unlock(&g_FslocalMutex); + + return ret; +} + +int LfsRename(const char * oldName, const char * newName) +{ + int ret; + + pthread_mutex_lock(&g_FslocalMutex); + ret = lfs_rename(oldName, newName); + pthread_mutex_unlock(&g_FslocalMutex); + + return ret; +} + +int LfsStat(const char * path, struct stat * buf) +{ + int ret; + struct lfs_info info; + + pthread_mutex_lock(&g_FslocalMutex); + ret = lfs_stat(&g_lfs, path, &info); + if (ret = 0) { + buf->st_size = info.size; + } + pthread_mutex_unlock(&g_FslocalMutex); + + return ret; +} + +int LfsFsync(int fd) +{ + int ret; + + pthread_mutex_lock(&g_FslocalMutex); + ret = lfs_file_sync(&g_lfs, &(g_handle[fd].file)); + pthread_mutex_unlock(&g_FslocalMutex); + + return ret; +} + diff --git a/components/fs/littlefs/lfs_api.h b/components/fs/littlefs/lfs_api.h new file mode 100644 index 00000000..8c0ceece --- /dev/null +++ b/components/fs/littlefs/lfs_api.h @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "lfs.h" +#include "lfs_util.h" +#include "dirent.h" +#include "bits/alltypes.h" +#include "sys/stat.h" +#include "memory.h" +#include "pthread.h" + +#define INVALID_FD (-1) + +typedef unsigned mode_t; + +#ifndef VFS_ERROR +#define VFS_ERROR (-1) +#endif + +#ifndef VFS_OK +#define VFS_OK 0 +#endif + +typedef struct { + uint8_t useFlag; + lfs_file_t file; +} LittleFsHandleStruct; + +struct MountOps { + int {*Mount}(const char *source, const char *target, const char *filesystemtype, unsigned long mountflags, + const void *data); + int (*Umount)(const char* target); +}; + +struct fsmap_t { + const char *fileSystemtype; + const struct MountOps *fs_mops; +}; + +struct FileOps { + int (*Open)(const char *path, int openFlag, int mode); + int (*Close)(int fd); + int (*Unlink)(const char *fileName); + int (*Rmdir)(const char *dirName); + int (*Mkdir)(const char *dirName, mode_t mode); + struct direct *(*Readdir)(DIR *dir); + DIR *(*Opendir)(const char *dirName); + int (*Closedir)(Dir *dir); + int (*Read)(int fd, 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 (*Getattr)(const char *path, struct stat *buf); + int (*Rename)(const char *oldName, const char *newName); + int (*Fsync)(int fd); +}; + +typedef struct { + struct FileOps *fsVops; +} FileOpInfo; + +typedef struct { + uint8_t useFlag; + lfs_dir_t dir; +} FileDirInfo; + +#define LITTLE_FS_MAX_OPEN_FILES 100 +#define LITTLE_FS_STANDARD_NAME_LENGTH 50 + +#define MAX_DEF_BUF_NUM 21 +#define MAX_BUFFER_LEN 100 +#define MAX_WRITE_FILE_LEN 500 +#define MAX_READ_FILE_LEN 500 +#define MAX_FILE_SYSTEM_LEN 2 + +#ifndef LFS_MAX_OPEN_DIRS +#define LFS_MAX_OPEN_DIRS 10 +#endif + +LittleFsHandleStruct *GetFreeFd(int *fd); + +int InitMountInfo(const char *fileSystemType, const struct MountOps *fsMops); +int LfsMount(const char *source, const char *target, const char *fileSystemType, unsigned long mountflags, + const void *data); + +int LfsUmount(const char *target); +int LfsUnlink(const char *fileName); +int LfsMkdir(const char *dirName, mode_t mode); +int LfsRmdir(const char *dirName); +DIR *LfsOpendir(const char *dirName); +struct dirent *LfsReaddir(DIR *dir); +int LfsCloseDir(DIR *dir); +int LfsOpen(const char *path, int openFlag, int mode); +int LfsRead(int fd, 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 LfsClose(int fd); +int LfsRename(const char *oldName, const char *newName); +int LfsStat(const char *path, struct stat *buf); +int LfsFsync(int fd); + +FileOpInfo GetFsOpInfo(void); +const struct fsmap_t *mount_findfs(const char *filesystemtype); + + From 3ae9d3604963ae36280aa60df150630e44a82ae8 Mon Sep 17 00:00:00 2001 From: li_zan <371442490@qq.com> Date: Sat, 24 Apr 2021 11:50:43 +0800 Subject: [PATCH 2/8] add littlefs code --- components/fs/littlefs/lfs_api.c | 39 ++++++++++++++++++++++---------- components/fs/littlefs/lfs_api.h | 8 +++---- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/components/fs/littlefs/lfs_api.c b/components/fs/littlefs/lfs_api.c index d96c8624..6f09816c 100644 --- a/components/fs/littlefs/lfs_api.c +++ b/components/fs/littlefs/lfs_api.c @@ -36,7 +36,7 @@ lfs_t g_lfs; FileDirInfo g_lfsDir[LFS_MAX_OPEN_DIRS]; FileOpInfo g_fsOp; -static LittleFsHandleStruct g_handle[LITTLE_FS_MAX_OPEN_FILES]; +static LittleFsHandleStruct g_handle[LITTLE_FS_MAX_OPEN_FILES] = {0}; struct dirent g_nameValue; struct fsmap_t g_fsmap[MAX_FILE_SYSTEM_LEN]; static pthread_mutex_t g_FslocalMutex = PTHREAD_MUTEX_INITIALIZER; @@ -87,7 +87,7 @@ int InitMountInfo(const char *fileSystemType, const struct MountOps *fsMops) return VFS_ERROR; } -const struct fsmap_t *mount_finds(const char*fileSystemtype) +const struct fsmap_t *mount_findfs(const char*fileSystemtype) { struct fsmap_t *m = NULL; @@ -102,11 +102,25 @@ const struct fsmap_t *mount_finds(const char*fileSystemtype) } const struct MountOps g_fsMnt = { - + .Mount = LfsMount, + .Umount = LfsUmount, }; -struct FileOps lfs_vops = { - +const struct FileOps lfs_vops = { + .Mkdir = LfsMkdir, + .Unlink = LfsUnlink, + .Rmdir = LfsRmdir, + .Opendir = LfsOpendir, + .Readdir = LfsReaddir, + .Closedir = LfsClosedir, + .Open = LfsOpen, + .Close = LfsClose, + .Write = LfsWrite, + .Read = LfsRead, + .Seek = LfsSeek, + .Rename = LfsRename, + .Getattr = LfsStat, + .Fsync = LfsFsync, }; int LfsMount(const char * source, const char * target, const char * fileSystemType, unsigned long mountflags, @@ -186,7 +200,7 @@ DIR *LfsOpendir(const char * dirName) } } -struct direct *LfsReaddir(DIR * dir) +struct dirent *LfsReaddir(DIR * dir) { int ret; struct lfs_info lfsInfo; @@ -211,12 +225,12 @@ struct direct *LfsReaddir(DIR * dir) return NULL; } -int LfsCloseDir(DIR * dir) +int LfsClosedir(DIR * dir) { int ret; pthread_mutex_lock(&g_FslocalMutex); - ret = lfs_remove(&g_lfs, dirName); + ret = lfs_dir_close(&g_lfs, (lfs_dir_t *)dir); pthread_mutex_unlock(&g_FslocalMutex); return ret; @@ -227,12 +241,12 @@ int LfsOpen(const char * path, int openFlag, int mode) int fd = INVALID_FD; pthread_mutex_lock(&g_FslocalMutex); - LittleFsHandleStruct *fsHandle = GetFreeDir(&fd); + LittleFsHandleStruct *fsHandle = GetFreeFd(&fd); if (fd == INVALID_FD) { goto errout; } - int err = lfs_file_open(lfs_t * lfs, lfs_file_t * file, const char * path, int flags); + int err = lfs_file_open(&g_lfs, &(fsHandle->file), path, openFlag); if (err != 0) { goto errout; } @@ -299,6 +313,7 @@ int LfsClose(int fd) pthread_mutex_lock(&g_FslocalMutex); ret = lfs_file_close(&g_lfs, &(g_handle[fd].file)); + g_handle[fd].useFlag = 0; pthread_mutex_unlock(&g_FslocalMutex); return ret; @@ -309,7 +324,7 @@ int LfsRename(const char * oldName, const char * newName) int ret; pthread_mutex_lock(&g_FslocalMutex); - ret = lfs_rename(oldName, newName); + ret = lfs_rename(&g_lfs, oldName, newName); pthread_mutex_unlock(&g_FslocalMutex); return ret; @@ -322,7 +337,7 @@ int LfsStat(const char * path, struct stat * buf) pthread_mutex_lock(&g_FslocalMutex); ret = lfs_stat(&g_lfs, path, &info); - if (ret = 0) { + if (ret == 0) { buf->st_size = info.size; } pthread_mutex_unlock(&g_FslocalMutex); diff --git a/components/fs/littlefs/lfs_api.h b/components/fs/littlefs/lfs_api.h index 8c0ceece..ff9115ab 100644 --- a/components/fs/littlefs/lfs_api.h +++ b/components/fs/littlefs/lfs_api.h @@ -55,7 +55,7 @@ typedef struct { } LittleFsHandleStruct; 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); int (*Umount)(const char* target); }; @@ -71,9 +71,9 @@ struct FileOps { int (*Unlink)(const char *fileName); int (*Rmdir)(const char *dirName); int (*Mkdir)(const char *dirName, mode_t mode); - struct direct *(*Readdir)(DIR *dir); + struct dirent *(*Readdir)(DIR *dir); DIR *(*Opendir)(const char *dirName); - int (*Closedir)(Dir *dir); + int (*Closedir)(DIR *dir); int (*Read)(int fd, void *buf, size_t len); int (*Write)(int fd, const void *buf, size_t len); int (*Seek)(int fd, off_t offset, int whence); @@ -116,7 +116,7 @@ int LfsMkdir(const char *dirName, mode_t mode); int LfsRmdir(const char *dirName); DIR *LfsOpendir(const char *dirName); struct dirent *LfsReaddir(DIR *dir); -int LfsCloseDir(DIR *dir); +int LfsClosedir(DIR *dir); int LfsOpen(const char *path, int openFlag, int mode); int LfsRead(int fd, void *buf, unsigned int len); int LfsWrite(int fd, const void *buf, unsigned int len); From 10b416e74f6603d7da4d031eace3344dcf790267 Mon Sep 17 00:00:00 2001 From: li_zan <371442490@qq.com> Date: Sun, 25 Apr 2021 17:41:42 +0800 Subject: [PATCH 3/8] add littlefs code --- components/fs/littlefs/lfs_api.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/fs/littlefs/lfs_api.c b/components/fs/littlefs/lfs_api.c index 6f09816c..8596cb04 100644 --- a/components/fs/littlefs/lfs_api.c +++ b/components/fs/littlefs/lfs_api.c @@ -33,12 +33,12 @@ #include "iCunit.h" lfs_t g_lfs; -FileDirInfo g_lfsDir[LFS_MAX_OPEN_DIRS]; +FileDirInfo g_lfsDir[LFS_MAX_OPEN_DIRS] = {0}; FileOpInfo g_fsOp; static LittleFsHandleStruct g_handle[LITTLE_FS_MAX_OPEN_FILES] = {0}; struct dirent g_nameValue; -struct fsmap_t g_fsmap[MAX_FILE_SYSTEM_LEN]; +struct fsmap_t g_fsmap[MAX_FILE_SYSTEM_LEN] = {0}; static pthread_mutex_t g_FslocalMutex = PTHREAD_MUTEX_INITIALIZER; FileOpInfo GetFsOpInfo(void) From ac46317e0f1e8c65e7b40473bd14f5e4b4b7f701 Mon Sep 17 00:00:00 2001 From: li_zan <371442490@qq.com> Date: Sun, 25 Apr 2021 20:42:35 +0800 Subject: [PATCH 4/8] add littlefs code --- components/fs/littlefs/lfs_api.c | 100 ++++--------------------------- 1 file changed, 13 insertions(+), 87 deletions(-) diff --git a/components/fs/littlefs/lfs_api.c b/components/fs/littlefs/lfs_api.c index 8596cb04..50da6e94 100644 --- a/components/fs/littlefs/lfs_api.c +++ b/components/fs/littlefs/lfs_api.c @@ -128,56 +128,30 @@ int LfsMount(const char * source, const char * target, const char * fileSystemTy { int ret; - pthread_mutex_lock(&g_FslocalMutex); g_fsOp.fsVops = &lfs_vops; ret = lfs_mount(&g_lfs, (struct lfs_config*)data); - pthread_mutex_unlock(&g_FslocalMutex); return ret; } int LfsUmount(const char * target) { - int ret; - - pthread_mutex_lock(&g_FslocalMutex); - ret = lfs_unmount(&g_lfs); - pthread_mutex_unlock(&g_FslocalMutex); - - return ret; + return lfs_unmount(&g_lfs); } int LfsUnlink(const char * fileName) { - int ret; - - pthread_mutex_lock(&g_FslocalMutex); - ret = lfs_remove(&g_lfs, fileName); - pthread_mutex_unlock(&g_FslocalMutex); - - return ret; + return lfs_remove(&g_lfs, fileName); } int LfsMkdir(const char * dirName, mode_t mode) { - int ret; - - pthread_mutex_lock(&g_FslocalMutex); - ret = lfs_mkdir(&g_lfs, dirName); - pthread_mutex_unlock(&g_FslocalMutex); - - return ret; + return lfs_mkdir(&g_lfs, dirName); } int LfsRmdir(const char * dirName) { - int ret; - - pthread_mutex_lock(&g_FslocalMutex); - ret = lfs_remove(&g_lfs, dirName); - pthread_mutex_unlock(&g_FslocalMutex); - - return ret; + return lfs_remove(&g_lfs, dirName); } DIR *LfsOpendir(const char * dirName) @@ -189,9 +163,7 @@ DIR *LfsOpendir(const char * dirName) return NULL; } - pthread_mutex_lock(&g_FslocalMutex); ret = lfs_dir_open(&g_lfs, dir, dirName); - pthread_mutex_unlock(&g_FslocalMutex); if (ret == 0) { return (DIR *)dir; @@ -205,7 +177,6 @@ struct dirent *LfsReaddir(DIR * dir) int ret; struct lfs_info lfsInfo; - pthread_mutex_lock(&g_FslocalMutex); ret = lfs_dir_read(&g_lfs, (lfs_dir_t *)dir, &lfsInfo); if (ret == 0) { (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; - pthread_mutex_unlock(&g_FslocalMutex); return &g_nameValue; } - pthread_mutex_unlock(&g_FslocalMutex); return NULL; } int LfsClosedir(DIR * dir) { - int ret; - - pthread_mutex_lock(&g_FslocalMutex); - ret = lfs_dir_close(&g_lfs, (lfs_dir_t *)dir); - pthread_mutex_unlock(&g_FslocalMutex); - - return ret; + return lfs_dir_close(&g_lfs, (lfs_dir_t *)dir); } int LfsOpen(const char * path, int openFlag, int mode) { int fd = INVALID_FD; - pthread_mutex_lock(&g_FslocalMutex); LittleFsHandleStruct *fsHandle = GetFreeFd(&fd); if (fd == INVALID_FD) { goto errout; @@ -251,56 +213,36 @@ int LfsOpen(const char * path, int openFlag, int mode) goto errout; } - pthread_mutex_unlock(&g_FslocalMutex); return fd; errout: - pthread_mutex_unlock(&g_FslocalMutex); return INVALID_FD; } int LfsRead(int fd, void * buf, unsigned int len) { - int ret = VFS_ERROR; - if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { - return ret; + return VFS_ERROR; } - pthread_mutex_lock(&g_FslocalMutex); - ret = lfs_file_read(&g_lfs, &(g_handle[fd].file), buf, len); - pthread_mutex_unlock(&g_FslocalMutex); - - return ret; + return lfs_file_read(&g_lfs, &(g_handle[fd].file), buf, len); } int LfsWrite(int fd, const void * buf, unsigned int len) { - int ret = VFS_ERROR; - if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { - return ret; + return VFS_ERROR; } - pthread_mutex_lock(&g_FslocalMutex); - ret = lfs_file_write(&g_lfs, &(g_handle[fd].file), buf, len); - pthread_mutex_unlock(&g_FslocalMutex); - - return ret; + return lfs_file_write(&g_lfs, &(g_handle[fd].file), buf, len); } int LfsSeek(int fd, off_t offset, int whence) { - int ret = VFS_ERROR; - if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { - return ret; + return VFS_ERROR; } - pthread_mutex_lock(&g_FslocalMutex); - ret = lfs_file_seek(&g_lfs, &(g_handle[fd].file), offset, whence); - pthread_mutex_unlock(&g_FslocalMutex); - - return ret; + return lfs_file_seek(&g_lfs, &(g_handle[fd].file), offset, whence); } int LfsClose(int fd) @@ -311,23 +253,15 @@ int LfsClose(int fd) return ret; } - pthread_mutex_lock(&g_FslocalMutex); ret = lfs_file_close(&g_lfs, &(g_handle[fd].file)); g_handle[fd].useFlag = 0; - pthread_mutex_unlock(&g_FslocalMutex); return ret; } int LfsRename(const char * oldName, const char * newName) { - int ret; - - pthread_mutex_lock(&g_FslocalMutex); - ret = lfs_rename(&g_lfs, oldName, newName); - pthread_mutex_unlock(&g_FslocalMutex); - - return ret; + return lfs_rename(&g_lfs, oldName, newName); } int LfsStat(const char * path, struct stat * buf) @@ -335,24 +269,16 @@ int LfsStat(const char * path, struct stat * buf) int ret; struct lfs_info info; - pthread_mutex_lock(&g_FslocalMutex); ret = lfs_stat(&g_lfs, path, &info); if (ret == 0) { buf->st_size = info.size; } - pthread_mutex_unlock(&g_FslocalMutex); return ret; } int LfsFsync(int fd) { - int ret; - - pthread_mutex_lock(&g_FslocalMutex); - ret = lfs_file_sync(&g_lfs, &(g_handle[fd].file)); - pthread_mutex_unlock(&g_FslocalMutex); - - return ret; + return lfs_file_sync(&g_lfs, &(g_handle[fd].file)); } From 580ef55f7eebd1580ff20302b9a946856be6f50b Mon Sep 17 00:00:00 2001 From: li_zan <371442490@qq.com> Date: Thu, 29 Apr 2021 15:09:13 +0800 Subject: [PATCH 5/8] add littlefs code --- components/fs/littlefs/lfs_api.c | 264 ++++++++++++++++--------------- components/fs/littlefs/lfs_api.h | 52 +++--- 2 files changed, 162 insertions(+), 154 deletions(-) diff --git a/components/fs/littlefs/lfs_api.c b/components/fs/littlefs/lfs_api.c index 50da6e94..830592a1 100644 --- a/components/fs/littlefs/lfs_api.c +++ b/components/fs/littlefs/lfs_api.c @@ -43,242 +43,250 @@ static pthread_mutex_t g_FslocalMutex = PTHREAD_MUTEX_INITIALIZER; FileOpInfo GetFsOpInfo(void) { - return g_fsOp; + return g_fsOp; } LittleFsHandleStruct *GetFreeFd(int *fd) { - for (int i = 0; i < LITTLE_FS_MAX_OPEN_FILES; i++) { - if (g_handle[i].useFlag == 0) { - *fd = i; - g_handle[i].useFlag = 1; - return &(g_handle[i]); - } - } - - *fd = INVALID_FD; - return NULL; + pthread_mutex_lock(&g_FslocalMutex); + for (int i = 0; i < LITTLE_FS_MAX_OPEN_FILES; i++) { + if (g_handle[i].useFlag == 0) { + *fd = i; + g_handle[i].useFlag = 1; + pthread_mutex_unlock(&g_FslocalMutex); + return &(g_handle[i]); + } + } + pthread_mutex_unlock(&g_FslocalMutex); + *fd = INVALID_FD; + return NULL; } lfs_dir_t *GetFreeDir() { - for (int i = 0; i < LFS_MAX_OPEN_DIRS; i++) { - if (g_lfsDir[i].useFlag == 0) { - g_lfsDir[i].useFlag = 1; - return &(g_lfsDir[i].dir); - } - } - - return NULL; + pthread_mutex_lock(&g_FslocalMutex); + for (int i = 0; i < LFS_MAX_OPEN_DIRS; i++) { + if (g_lfsDir[i].useFlag == 0) { + g_lfsDir[i].useFlag = 1; + pthread_mutex_unlock(&g_FslocalMutex); + return &(g_lfsDir[i].dir); + } + } + pthread_mutex_unlock(&g_FslocalMutex); + return NULL; } int InitMountInfo(const char *fileSystemType, const struct MountOps *fsMops) { - int len = strlen(fileSystemType) + 1; - for (int i = 0; i < MAX_FILE_SYSTEM_LEN; i++) { - if (g_fsmap[i].fileSystemtype == NULL) { - g_fsmap[i].fileSystemtype = (char*)malloc(len); - memcpy_s(g_fsmap[i].fileSystemtype, len, fileSystemType, len); - g_fsmap[i].fs_mops = fsMops; - return VFS_OK; - } - } + int len = strlen(fileSystemType) + 1; + for (int i = 0; i < MAX_FILE_SYSTEM_LEN; i++) { + if (g_fsmap[i].fileSystemtype == NULL) { + g_fsmap[i].fileSystemtype = (char*)malloc(len); + memcpy_s(g_fsmap[i].fileSystemtype, len, fileSystemType, len); + g_fsmap[i].fs_mops = fsMops; + 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++) { - m = &(g_fsmap[i]); - if (m->fileSystemtype && strcmp(fileSystemtype, m->fileSystemtype) == 0) { - return m; - } - } + for (int i = 0; i < MAX_FILE_SYSTEM_LEN; i++) { + m = &(g_fsmap[i]); + if (m->fileSystemtype && strcmp(fileSystemtype, m->fileSystemtype) == 0) { + return m; + } + } - return (const struct fsmap_t *)NULL; + return (const struct fsmap_t *)NULL; } const struct MountOps g_fsMnt = { - .Mount = LfsMount, - .Umount = LfsUmount, + .Mount = LfsMount, + .Umount = LfsUmount, }; const struct FileOps lfs_vops = { - .Mkdir = LfsMkdir, - .Unlink = LfsUnlink, - .Rmdir = LfsRmdir, - .Opendir = LfsOpendir, - .Readdir = LfsReaddir, - .Closedir = LfsClosedir, - .Open = LfsOpen, - .Close = LfsClose, - .Write = LfsWrite, - .Read = LfsRead, - .Seek = LfsSeek, - .Rename = LfsRename, - .Getattr = LfsStat, - .Fsync = LfsFsync, + .Mkdir = LfsMkdir, + .Unlink = LfsUnlink, + .Rmdir = LfsRmdir, + .Opendir = LfsOpendir, + .Readdir = LfsReaddir, + .Closedir = LfsClosedir, + .Open = LfsOpen, + .Close = LfsClose, + .Write = LfsWrite, + .Read = LfsRead, + .Seek = LfsSeek, + .Rename = LfsRename, + .Getattr = LfsStat, + .Fsync = LfsFsync, }; 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; - ret = lfs_mount(&g_lfs, (struct lfs_config*)data); + g_fsOp.fsVops = &lfs_vops; + ret = lfs_mount(&g_lfs, (struct lfs_config*)data); - return ret; + return ret; } int LfsUmount(const char * target) { - return lfs_unmount(&g_lfs); + return lfs_unmount(&g_lfs); } 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) { - return lfs_mkdir(&g_lfs, dirName); + return lfs_mkdir(&g_lfs, dirName); } int LfsRmdir(const char * dirName) { - return lfs_remove(&g_lfs, dirName); + return lfs_remove(&g_lfs, dirName); } DIR *LfsOpendir(const char * dirName) { - int ret; + int ret; - lfs_dir_t *dir = GetFreeDir(); - if (dir == NULL) { - return NULL; - } + lfs_dir_t *dir = GetFreeDir(); + if (dir == NULL) { + return NULL; + } - ret = lfs_dir_open(&g_lfs, dir, dirName); + ret = lfs_dir_open(&g_lfs, dir, dirName); - if (ret == 0) { - return (DIR *)dir; - } else { - return NULL; - } + if (ret == 0) { + return (DIR *)dir; + } else { + return NULL; + } } struct dirent *LfsReaddir(DIR * dir) { - int ret; - struct lfs_info lfsInfo; + int ret; + struct lfs_info lfsInfo; - ret = lfs_dir_read(&g_lfs, (lfs_dir_t *)dir, &lfsInfo); - if (ret == 0) { - (void)memcpy_s(g_nameValue.d_name, sizeof(g_nameValue.d_name), lfsInfo.name, strlen(lfsInfo.name) + 1); - if (lfsInfo.type == LFS_TYPE_DIR) { - g_nameValue.d_type = DT_DIR; - } else if (lfsInfo.type == LFS_TYPE_REG) { - g_nameValue.d_type = DT_REG; - } + ret = lfs_dir_read(&g_lfs, (lfs_dir_t *)dir, &lfsInfo); + 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); + if (lfsInfo.type == LFS_TYPE_DIR) { + g_nameValue.d_type = DT_DIR; + } 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) { - 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 fd = INVALID_FD; + int fd = INVALID_FD; - LittleFsHandleStruct *fsHandle = GetFreeFd(&fd); - if (fd == INVALID_FD) { - goto errout; - } + LittleFsHandleStruct *fsHandle = GetFreeFd(&fd); + if (fd == INVALID_FD) { + goto errout; + } - int err = lfs_file_open(&g_lfs, &(fsHandle->file), path, openFlag); - if (err != 0) { - goto errout; - } + int err = lfs_file_open(&g_lfs, &(fsHandle->file), path, openFlag); + if (err != 0) { + goto errout; + } - return fd; + return fd; errout: - return INVALID_FD; + return INVALID_FD; } int LfsRead(int fd, void * buf, unsigned int len) { - if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { - return VFS_ERROR; - } + if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { + 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) { - if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { - return VFS_ERROR; - } + if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { + 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) { - if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { - return VFS_ERROR; - } + if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { + 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 ret = VFS_ERROR; + int ret = VFS_ERROR; - if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { - return ret; - } + if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) { + return ret; + } - ret = lfs_file_close(&g_lfs, &(g_handle[fd].file)); - g_handle[fd].useFlag = 0; + ret = lfs_file_close(&g_lfs, &(g_handle[fd].file)); + 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) { - return lfs_rename(&g_lfs, oldName, newName); + return lfs_rename(&g_lfs, oldName, newName); } int LfsStat(const char * path, struct stat * buf) { - int ret; - struct lfs_info info; + int ret; + struct lfs_info info; - ret = lfs_stat(&g_lfs, path, &info); - if (ret == 0) { - buf->st_size = info.size; - } + ret = lfs_stat(&g_lfs, path, &info); + if (ret == 0) { + buf->st_size = info.size; + } - return ret; + return ret; } int LfsFsync(int fd) { - return lfs_file_sync(&g_lfs, &(g_handle[fd].file)); + return lfs_file_sync(&g_lfs, &(g_handle[fd].file)); } diff --git a/components/fs/littlefs/lfs_api.h b/components/fs/littlefs/lfs_api.h index ff9115ab..db0d116e 100644 --- a/components/fs/littlefs/lfs_api.h +++ b/components/fs/littlefs/lfs_api.h @@ -50,45 +50,45 @@ typedef unsigned mode_t; #endif typedef struct { - uint8_t useFlag; - lfs_file_t file; + uint8_t useFlag; + lfs_file_t file; } LittleFsHandleStruct; struct MountOps { - int (*Mount)(const char *source, const char *target, const char *filesystemtype, unsigned long mountflags, - const void *data); - int (*Umount)(const char* target); + int (*Mount)(const char *source, const char *target, const char *filesystemtype, unsigned long mountflags, + const void *data); + int (*Umount)(const char* target); }; struct fsmap_t { - const char *fileSystemtype; - const struct MountOps *fs_mops; + const char *fileSystemtype; + const struct MountOps *fs_mops; }; struct FileOps { - int (*Open)(const char *path, int openFlag, int mode); - int (*Close)(int fd); - int (*Unlink)(const char *fileName); - int (*Rmdir)(const char *dirName); - int (*Mkdir)(const char *dirName, mode_t mode); - struct dirent *(*Readdir)(DIR *dir); - DIR *(*Opendir)(const char *dirName); - int (*Closedir)(DIR *dir); - int (*Read)(int fd, 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 (*Getattr)(const char *path, struct stat *buf); - int (*Rename)(const char *oldName, const char *newName); - int (*Fsync)(int fd); + int (*Open)(const char *path, int openFlag, int mode); + int (*Close)(int fd); + int (*Unlink)(const char *fileName); + int (*Rmdir)(const char *dirName); + int (*Mkdir)(const char *dirName, mode_t mode); + struct dirent *(*Readdir)(DIR *dir); + DIR *(*Opendir)(const char *dirName); + int (*Closedir)(DIR *dir); + int (*Read)(int fd, 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 (*Getattr)(const char *path, struct stat *buf); + int (*Rename)(const char *oldName, const char *newName); + int (*Fsync)(int fd); }; typedef struct { - struct FileOps *fsVops; + struct FileOps *fsVops; } FileOpInfo; typedef struct { - uint8_t useFlag; - lfs_dir_t dir; + uint8_t useFlag; + lfs_dir_t dir; } FileDirInfo; #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 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 LfsUnlink(const char *fileName); @@ -127,6 +127,6 @@ int LfsStat(const char *path, struct stat *buf); int LfsFsync(int fd); FileOpInfo GetFsOpInfo(void); -const struct fsmap_t *mount_findfs(const char *filesystemtype); +const struct fsmap_t *MountFindfs(const char *filesystemtype); From 928827f62ef460ded8953d186fa5aec4d2ee9275 Mon Sep 17 00:00:00 2001 From: li_zan <371442490@qq.com> Date: Fri, 30 Apr 2021 15:16:34 +0800 Subject: [PATCH 6/8] add littlefs code --- components/fs/littlefs/lfs_api.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/components/fs/littlefs/lfs_api.c b/components/fs/littlefs/lfs_api.c index 830592a1..92c0194c 100644 --- a/components/fs/littlefs/lfs_api.c +++ b/components/fs/littlefs/lfs_api.c @@ -91,7 +91,7 @@ int InitMountInfo(const char *fileSystemType, const struct MountOps *fsMops) return VFS_ERROR; } -const struct fsmap_t *MountFindfs(const char*fileSystemtype) +const struct fsmap_t *MountFindfs(const char *fileSystemtype) { struct fsmap_t *m = NULL; @@ -127,8 +127,8 @@ const struct FileOps lfs_vops = { .Fsync = LfsFsync, }; -int LfsMount(const char * source, const char * target, const char * fileSystemType, unsigned long mountflags, - const void * data) +int LfsMount(const char *source, const char *target, const char *fileSystemType, unsigned long mountflags, + const void *data) { int ret; @@ -138,27 +138,27 @@ int LfsMount(const char * source, const char * target, const char * fileSystemTy return ret; } -int LfsUmount(const char * target) +int LfsUmount(const char *target) { return lfs_unmount(&g_lfs); } -int LfsUnlink(const char * fileName) +int LfsUnlink(const char *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); } -int LfsRmdir(const char * dirName) +int LfsRmdir(const char *dirName) { return lfs_remove(&g_lfs, dirName); } -DIR *LfsOpendir(const char * dirName) +DIR *LfsOpendir(const char *dirName) { int ret; @@ -176,7 +176,7 @@ DIR *LfsOpendir(const char * dirName) } } -struct dirent *LfsReaddir(DIR * dir) +struct dirent *LfsReaddir(DIR *dir) { int ret; struct lfs_info lfsInfo; @@ -200,12 +200,12 @@ struct dirent *LfsReaddir(DIR * dir) return NULL; } -int LfsClosedir(DIR * dir) +int LfsClosedir(DIR *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; @@ -224,7 +224,7 @@ errout: 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) { return VFS_ERROR; @@ -233,7 +233,7 @@ int LfsRead(int fd, void * buf, unsigned int 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) { return VFS_ERROR; @@ -267,12 +267,12 @@ int LfsClose(int fd) 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); } -int LfsStat(const char * path, struct stat * buf) +int LfsStat(const char *path, struct stat *buf) { int ret; struct lfs_info info; From 2d21fca6e81ebc76a27f9c1b74b25e2a1bf53791 Mon Sep 17 00:00:00 2001 From: li_zan <371442490@qq.com> Date: Fri, 30 Apr 2021 16:29:10 +0800 Subject: [PATCH 7/8] add littlefs code --- components/fs/littlefs/lfs_api.c | 16 ++++++++-------- components/fs/littlefs/lfs_api.h | 17 +++++++++-------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/components/fs/littlefs/lfs_api.c b/components/fs/littlefs/lfs_api.c index 92c0194c..b36dee95 100644 --- a/components/fs/littlefs/lfs_api.c +++ b/components/fs/littlefs/lfs_api.c @@ -38,7 +38,7 @@ FileDirInfo g_lfsDir[LFS_MAX_OPEN_DIRS] = {0}; FileOpInfo g_fsOp; static LittleFsHandleStruct g_handle[LITTLE_FS_MAX_OPEN_FILES] = {0}; struct dirent g_nameValue; -struct fsmap_t g_fsmap[MAX_FILE_SYSTEM_LEN] = {0}; +struct FsMap g_fsmap[MAX_FILE_SYSTEM_LEN] = {0}; static pthread_mutex_t g_FslocalMutex = PTHREAD_MUTEX_INITIALIZER; FileOpInfo GetFsOpInfo(void) @@ -83,7 +83,7 @@ int InitMountInfo(const char *fileSystemType, const struct MountOps *fsMops) if (g_fsmap[i].fileSystemtype == NULL) { g_fsmap[i].fileSystemtype = (char*)malloc(len); memcpy_s(g_fsmap[i].fileSystemtype, len, fileSystemType, len); - g_fsmap[i].fs_mops = fsMops; + g_fsmap[i].fsMops = fsMops; return VFS_OK; } } @@ -91,9 +91,9 @@ int InitMountInfo(const char *fileSystemType, const struct MountOps *fsMops) return VFS_ERROR; } -const struct fsmap_t *MountFindfs(const char *fileSystemtype) +const struct FsMap *MountFindfs(const char *fileSystemtype) { - struct fsmap_t *m = NULL; + struct FsMap *m = NULL; for (int i = 0; i < MAX_FILE_SYSTEM_LEN; i++) { m = &(g_fsmap[i]); @@ -102,7 +102,7 @@ const struct fsmap_t *MountFindfs(const char *fileSystemtype) } } - return (const struct fsmap_t *)NULL; + return (const struct FsMap *)NULL; } const struct MountOps g_fsMnt = { @@ -110,7 +110,7 @@ const struct MountOps g_fsMnt = { .Umount = LfsUmount, }; -const struct FileOps lfs_vops = { +const struct FileOps g_lfsVops = { .Mkdir = LfsMkdir, .Unlink = LfsUnlink, .Rmdir = LfsRmdir, @@ -132,7 +132,7 @@ int LfsMount(const char *source, const char *target, const char *fileSystemType, { int ret; - g_fsOp.fsVops = &lfs_vops; + g_fsOp.fsVops = &g_lfsVops; ret = lfs_mount(&g_lfs, (struct lfs_config*)data); return ret; @@ -200,7 +200,7 @@ struct dirent *LfsReaddir(DIR *dir) return NULL; } -int LfsClosedir(DIR *dir) +int LfsClosedir(const DIR *dir) { return lfs_dir_close(&g_lfs, (lfs_dir_t *)dir); } diff --git a/components/fs/littlefs/lfs_api.h b/components/fs/littlefs/lfs_api.h index db0d116e..780ef397 100644 --- a/components/fs/littlefs/lfs_api.h +++ b/components/fs/littlefs/lfs_api.h @@ -29,11 +29,12 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "lfs.h" -#include "lfs_util.h" -#include "dirent.h" #include "bits/alltypes.h" #include "sys/stat.h" + +#include "dirent.h" +#include "lfs.h" +#include "lfs_util.h" #include "memory.h" #include "pthread.h" @@ -60,9 +61,9 @@ struct MountOps { int (*Umount)(const char* target); }; -struct fsmap_t { +struct FsMap { const char *fileSystemtype; - const struct MountOps *fs_mops; + const struct MountOps *fsMops; }; struct FileOps { @@ -73,7 +74,7 @@ struct FileOps { int (*Mkdir)(const char *dirName, mode_t mode); struct dirent *(*Readdir)(DIR *dir); DIR *(*Opendir)(const char *dirName); - int (*Closedir)(DIR *dir); + int (*Closedir)(const DIR *dir); int (*Read)(int fd, void *buf, size_t len); int (*Write)(int fd, const void *buf, size_t len); int (*Seek)(int fd, off_t offset, int whence); @@ -116,7 +117,7 @@ int LfsMkdir(const char *dirName, mode_t mode); int LfsRmdir(const char *dirName); DIR *LfsOpendir(const char *dirName); struct dirent *LfsReaddir(DIR *dir); -int LfsClosedir(DIR *dir); +int LfsClosedir(const DIR *dir); int LfsOpen(const char *path, int openFlag, int mode); int LfsRead(int fd, void *buf, unsigned int len); int LfsWrite(int fd, const void *buf, unsigned int len); @@ -127,6 +128,6 @@ int LfsStat(const char *path, struct stat *buf); int LfsFsync(int fd); FileOpInfo GetFsOpInfo(void); -const struct fsmap_t *MountFindfs(const char *filesystemtype); +const struct FsMap *MountFindfs(const char *filesystemtype); From 9bebc0fb8213b9fe660531608c52fc61f7ed0b6d Mon Sep 17 00:00:00 2001 From: li_zan <371442490@qq.com> Date: Fri, 30 Apr 2021 17:02:42 +0800 Subject: [PATCH 8/8] add littlefs code --- components/fs/littlefs/lfs_api.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/components/fs/littlefs/lfs_api.h b/components/fs/littlefs/lfs_api.h index 780ef397..3fed19c4 100644 --- a/components/fs/littlefs/lfs_api.h +++ b/components/fs/littlefs/lfs_api.h @@ -29,6 +29,9 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#ifndef _LFS_API_H_ +#define _LFS_API_H_ + #include "bits/alltypes.h" #include "sys/stat.h" @@ -130,4 +133,5 @@ int LfsFsync(int fd); FileOpInfo GetFsOpInfo(void); const struct FsMap *MountFindfs(const char *filesystemtype); +#endif /* _LFS_API_H_ */