1、VFS结构体定义
在文件components\fs\vfs\fs_operations.h中定义了VFS虚拟文件系统操作涉及的结构体。
⑴处的struct MountOps结构体封装了挂载相关的操作,包含挂载、卸载和文件系统统计操作。
⑵处的struct FsMap结构体映射文件系统类型及其对应的挂载操作和文件系统操作,支持的文件类型包含“fat”和“littlefs”两种,通过这个结构体可以获取对应文件类型的挂载操作及文件系统操作接口。
⑶处的struct FileOps封装文件系统的操作接口,包含文件操作、目录操作,统计等相应的接口。
⑴ 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 (*Umount2)(const char* target, int flag);
int (*Statfs)(const char *path, struct statfs *buf);
};
⑵ struct FsMap {
const char *fileSystemtype;
const struct MountOps *fsMops;
const struct FileOps *fsFops;
};
⑶ struct FileOps {
int (*Open)(const char *path, int openFlag, ...);
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);
off_t (*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 (*Fstat)(int fd, struct stat *buf);
int (*Stat)(const char *path, struct stat *buf);
int (*Ftruncate)(int fd, off_t length);
};
2、VFS重要的内部全局变量
在文件components\fs\vfs\los_fs.c中有2个全局变量比较重要,⑴处定义的数组g_fsmap维护文件系统类型映射信息,数组大小为2,支持"fat"和"littlefs"文件类型。⑵处的变量g_fs根据挂载的文件类型指向数组g_fsmap中的FsMap类型元素。⑶处的函数InitMountInfo()会给数组g_fsmap进行初始化赋值。第0个元素维护的"fat"文件类型的文件系统映射信息,第1个元素维护的"littlefs"文件类型的文件系统映射信息。涉及到的挂载操作、文件系统操作变量g_fatfsMnt、g_fatfsFops、g_lfsMnt、g_lfsFops在对应的文件系统文件中定义。⑷处的函数MountFindfs()用于根据文件类型从数组中获取文件映射信息。
⑴ static struct FsMap g_fsmap[MAX_FILESYSTEM_LEN] = {0};
⑵ static struct FsMap *g_fs = NULL;
⑶ static void InitMountInfo(void)
{
#if (LOSCFG_SUPPORT_FATFS == 1)
extern struct MountOps g_fatfsMnt;
extern struct FileOps g_fatfsFops;
g_fsmap[0].fileSystemtype = strdup("fat");
g_fsmap[0].fsMops = &g_fatfsMnt;
g_fsmap[0].fsFops = &g_fatfsFops;
#endif
#if (LOSCFG_SUPPORT_