具体的代码实现,如下:(为了保持篇幅简洁,只列了open_file和read_file 两个方法)
- //fs_interface.h
- #ifndef FS_INTERFACE_H
- #define FS_INTERFACE_H
- // 接口函数指针类型
- typedef int (*open_file_fn)(void* pfs, const char* path, int flags);
- typedef int (*read_file_fn)(void* pfs, int fd, char* buf, int len);
- // 接口方法集
- typedef struct fs_methods_t {
- open_file_fn open_file;
- read_file_fn read_file;
- } fs_methods_t;
- // 接口的实现体
- typedef struct file_system_interface {
- void* pfs; // 文件系统的具体实现struct
- fs_methods_t* pmethods; // 这个文件系统的具体接口方式实现
- } file_system_interface;
- // 各个文件系统,通过 register_file_system 将自己注册进内核
- // const char* pname; // 文件系统的名称,如ext2, xfs...
- int register_file_system(const char* pname, file_system_interface fsi);
上面的代码中,有几个地方需要注意: 1)每个接口函数类型声明中,都比interface中的函数多了一个参数:void* pfs, 这个参数指向具体的文件系统的struct。 这样,内核才能真正对这个struct对象发起调用。 2)file_system_interface 是interface的具体实现体,里面包括2个指针:一个是指向文件系统实现体struct的指针pfs, 另一个指针指向文件系统实现的接口函数的集合。
|