diff --git a/components/fs/vfs/los_fs.h b/components/fs/vfs/los_fs.h index eee83240..650ee0cb 100644 --- a/components/fs/vfs/los_fs.h +++ b/components/fs/vfs/los_fs.h @@ -46,6 +46,7 @@ #include "sys/uio.h" #include "unistd.h" #include +#include "vfs_maps.h" #ifdef __cplusplus #if __cplusplus @@ -152,6 +153,28 @@ int LOS_DiskPartition(const char *dev, const char *fsType, int *lengthArray, int */ int LOS_PartitionFormat(const char *partName, char *fsType, void *data); +/* + * @brief new file system callbacks register. + * These callback functions are the adaptation layer implemented by the developer, + * used to interconnect the vfs with the new file system. + * + * LOS_FsRegister must be called after kernel initialization is complete. + * + * @param fsType file system type, don't register the same type fs more than once. + * @param fsMops mount operation of the fs. + * @param fsFops file operation of the fs. + * @param fsMgt management operation of the fs. + * + * @return Return LOS_OK if success. + * Return LOS_NOK if error. + * errno EINVAL: input errors, such as null pointers. + * errno ENOMEM: memory may malloc failed. + * + */ +int LOS_FsRegister(const char *fsType, const struct MountOps *fsMops, + const struct FileOps *fsFops, const struct FsManagement *fsMgt); + + #ifdef __cplusplus #if __cplusplus extern "C" { diff --git a/components/fs/vfs/vfs_maps.c b/components/fs/vfs/vfs_maps.c index 520c2854..bec4c1a1 100644 --- a/components/fs/vfs/vfs_maps.c +++ b/components/fs/vfs/vfs_maps.c @@ -55,17 +55,19 @@ struct FsMap *VfsFsMapGet(const char *fsType) return NULL; } -int OsFsRegister(const char *fsType, struct MountOps *fsMops, - struct FileOps *fsFops, struct FsManagement *fsMgt) +int OsFsRegister(const char *fsType, const struct MountOps *fsMops, + const struct FileOps *fsFops, const struct FsManagement *fsMgt) { size_t len; if ((fsMops == NULL) || (fsFops == NULL)) { + VFS_ERRNO_SET(EINVAL); return (int)LOS_NOK; } struct FsMap *newfs = (struct FsMap *)LOSCFG_FS_MALLOC_HOOK(sizeof(struct FsMap)); if (newfs == NULL) { PRINT_ERR("Fs register malloc failed, fsType %s.\n", fsType); + VFS_ERRNO_SET(ENOMEM); return (int)LOS_NOK; } (void)memset_s(newfs, sizeof(struct FsMap), 0, sizeof(struct FsMap)); @@ -74,6 +76,7 @@ int OsFsRegister(const char *fsType, struct MountOps *fsMops, newfs->fsType = LOSCFG_FS_MALLOC_HOOK(len); if (newfs->fsType == NULL) { LOSCFG_FS_FREE_HOOK(newfs); + VFS_ERRNO_SET(ENOMEM); return (int)LOS_NOK; } (void)strcpy_s((char *)newfs->fsType, len, fsType); @@ -90,3 +93,15 @@ int OsFsRegister(const char *fsType, struct MountOps *fsMops, VfsUnlock(); return LOS_OK; } + +int LOS_FsRegister(const char *fsType, const struct MountOps *fsMops, + const struct FileOps *fsFops, const struct FsManagement *fsMgt) +{ + if (VfsFsMapGet(fsType) != NULL) { + PRINT_ERR("fsType has been registered or fsType error\n"); + VFS_ERRNO_SET(EINVAL); + return (int)LOS_NOK; + } + + return OsFsRegister(fsType, fsMops, fsFops, fsMgt); +} diff --git a/components/fs/vfs/vfs_maps.h b/components/fs/vfs/vfs_maps.h index 027d6623..d8fa55d9 100644 --- a/components/fs/vfs/vfs_maps.h +++ b/components/fs/vfs/vfs_maps.h @@ -56,8 +56,8 @@ struct FsMap { struct FsMap *next; }; -int OsFsRegister(const char *fsType, struct MountOps *fsMops, - struct FileOps *fsFops, struct FsManagement *fsMgt); +int OsFsRegister(const char *fsType, const struct MountOps *fsMops, + const struct FileOps *fsFops, const struct FsManagement *fsMgt); struct FsMap *VfsFsMapGet(const char *fsType); #ifdef __cplusplus diff --git a/testsuites/unittest/posix/BUILD.gn b/testsuites/unittest/posix/BUILD.gn index 0c2f8f4b..26ac5a97 100644 --- a/testsuites/unittest/posix/BUILD.gn +++ b/testsuites/unittest/posix/BUILD.gn @@ -39,7 +39,6 @@ static_library("posix_test") { "src/ctype/tolower_test.c", "src/ctype/toupper_test.c", "src/errno/strerror_test.c", - "src/fs/posix_fs_func_test.c", "src/math/math_func_test.c", "src/mqueue/mqueue_func_test.c", "src/posix_test.c", @@ -65,7 +64,9 @@ static_library("posix_test") { "//test/xts/tools/hctest/include", "src", ] - + if (defined(LOSCFG_SUPPORT_FATFS) || defined(LOSCFG_SUPPORT_LITTLEFS)) { + sources += [ "src/fs/posix_fs_func_test.c" ] + } if (!defined(LOSCFG_COMPILER_ICCARM)) { cflags = [ "-Wno-error" ] } else {