当前位置: 首页 > 文档资料 > YoC 编程基础 >

虚拟文件系统

优质
小牛编辑
111浏览
2023-12-01

概述

VFS用于为各类文件(包括设备和文件系统的文件)提供了一个统一的用户操作接口

设备驱动

文件系统

用户可以将具体的文件系统注册到 VFS 中,如 FatFS、SPIFFS 等

接口定义

相关数据结构

file_t

typedef struct {
    inode_t *node;   /**< node for file */
    void    *f_arg;  /**< f_arg for file */
    size_t   offset; /**< offset for file */
} file_t;

inode_t

typedef struct {
    union inode_ops_t  ops;     /**< inode operations */
    void              *i_arg;   /**< per inode private data */
    char              *i_name;  /**< name of inode */
    int                i_flags; /**< flags for inode */
    uint8_t            type;    /**< type for inode */
    uint8_t            refs;    /**< refs for inode */
} inode_t;

aos_dir_t

typedef struct {
    int32_t dd_vfs_fd;  /**< file index in vfs */
    int32_t dd_rsv;     /**< Reserved */
} aos_dir_t;

aos_dirent_t

typedef struct {
    int32_t d_ino;    /**< file number */
    uint8_t d_type;   /**< type of file */
    char    d_name[]; /**< file name */
} aos_dirent_t;

初始化虚拟文件系统

int aos_vfs_init(void);
  • 参数
  • 返回值
    • 文件描述符:成功
    • <0:失败

通过路径打开文件

int aos_open(const char *path, int flags);
  • 参数
    • path:文件或者设备的路径
    • flags:打开操作模式
  • 返回值

    • 文件描述符:成功
    • < 0:失败
  • flags:

参数描述
O_RDONLY只读方式打开文件
O_WRONLY只写方式打开文件
O_RDWR以读写方式打开文件
O_CREAT如果要打开的文件不存在,则建立该文件
O_APPEND当读写文件时会从文件尾开始移动,也就是所写入的数据会以附加的方式添加到文件的尾部
O_TRUNC如果文件已经存在,则清空文件中的内容

关闭文件

int aos_close(int fd);
  • 参数
    • fd:文件描述符
  • 返回值
    • 0:成功
    • < 0:失败

读取文件内容

ssize_t aos_read(int fd, void *buf, size_t nbytes);
  • 参数
    • fd:文件描述符
    • nbytes:需要读取的字节数
    • buf:读取到缓冲区的指针
  • 返回值
    • 实际读取到的字节数
    • 0:读取数据到达文件结尾
    • < 0:失败

向文件写入内容

ssize_t aos_write(int fd, const void *buf, size_t nbytes);
  • 参数
    • fd:文件描述符
    • nbytes:需要写入的字节数
    • buf:数据缓冲区的指针
  • 返回值
    • 实际写入的字节数:成功
    • < 0:失败

发送特定命令控制接口

int aos_ioctl(int fd, int cmd, unsigned long arg);
  • 参数
    • fd:文件描述符
    • cmd:特定控制命令
    • arg:命令的参数
  • 返回值
    • 通常成功时返回0,出现错误时返回错误代码及指示原因

执行特定轮询时间

int aos_do_pollfd(int fd, int flag, poll_notify_t notify, void *fds, void *arg);
  • 参数
    • fd:文件描述符
    • flag:轮询的标志
    • notify:轮询通知回调
    • fds:指向pollfdde数组的指针
    • arg:命令的参数
  • 返回值
    • 0:成功
    • < 0:失败

在打开的文件描述符上执行下面描述的操作,操作由 cmd 确定

int aos_fcntl(int fd, int cmd, int val);
  • 参数
    • fd:文件描述符
    • cmd:文件操作命令
    • val:依赖cmd的参数
  • 返回值
    • 0:成功
    • < 0:失败

设置下次读取文件的位置

off_t aos_lseek(int fd, off_t offset, int whence);
  • 参数
    • fd:文件描述符
    • offset:根据参数whence来移动读写位置的位移数
    • whence:SEEK_SET 参数offset 即为新的读写位置
    • SEEK_CUR 以目前的读写位置往后增加offset 个位移量
    • SEEK_END 将读写位置指向文件尾后再增加offset个位移量. 当whence 值为SEEK_CUR 或SEEK_END 时, 参数offet允许负值的出现.
  • 返回值
    • 返回新的读写位置:成功
    • 错误代码:失败

将文件分割为指定大小

int aos_ftruncate(int fd, off_t size);
int aos_truncate(const char *path, off_t size);
  • 参数
    • fd:文件描述符
    • off_t size:指定文件大小
    • path:文件路径
  • 返回值
    • 0:成功
    • < 0:失败

同步文件

int aos_sync(int fd);
  • 参数
    • fd:文件描述符
  • 返回值
    • 0:成功
    • < 0:失败

同步所有文件

void aos_allsync(void);
  • 参数
  • 返回值

获取文件状态

int aos_stat(const char *path, struct stat *st);
int aos_fstat(int fd, struct aos_stat *st);

相关数据结构:

struct aos_stat {
    uint16_t st_mode;    /**< mode of file */
    uint32_t st_size;    /**< bytes of file */
    time_t   st_actime;  /**< time of last access */
    time_t   st_modtime; /**< time of last modification */
};
  • 参数
    • path:文件路径
    • st:结构指针,指向一个存放文件状态信息的结构体
    • fd:文件描述符
  • 返回值
    • 0:成功
    • < 0:失败

删除指定目录下的文件

int aos_unlink(const char *path);
int aos_remove(const char *path);
  • 参数
    • path:要删除文件的路径
  • 返回值
    • 0:成功
    • < 0:失败

创建指定目录下的文件

int aos_link(const char *oldpath, const char *newpath);
  • 参数
    • oldpath:现有文件路径
    • newpath:新文件路径
  • 返回值
    • 0:成功
    • < 0:失败

重命名文件

int aos_rename(const char *oldpath, const char *newpath);
  • 参数
    • oldpath:旧文件名
    • newpath:新文件名
  • 返回值
    • 0:成功
    • < 0:失败

打开目录

aos_dir_t *aos_opendir(const char *path);
  • 参数
    • path:目录名
  • 返回值
    • 目录流指针:成功
    • NULL:失败

关闭目录

int aos_closedir(aos_dir_t *dir);
  • 参数
    • dir:目录流指针
  • 返回值
    • 0:成功
    • < 0:失败

读取下个目录

aos_dirent_t *aos_readdir(aos_dir_t *dir);
  • 参数
    • dir:目录流指针
  • 返回值
    • 目录流指针:成功
    • NULL:已读到目录尾部

创建目录

int aos_mkdir(const char *path);
  • 参数
    • path:目录名
  • 返回值
    • 0:成功
    • < 0:失败

删除目录

int aos_rmdir(const char *path);
  • 参数
    • path:目录名
  • 返回值
    • 0:成功
    • < 0:失败

重置目录

void aos_rewinddir(aos_dir_t *dir);
  • 参数
    • dir:目标流指针
  • 返回值

返回指定目录

long aos_telldir(aos_dir_t *dir);
  • 参数
    • dir:目标流指针
  • 返回值
    • 目标流指针:成功
    • 错误代码:失败

设置目录位置,下一个aos_ readdir()调用从该处开始

void aos_seekdir(aos_dir_t *dir, long loc);
  • 参数
    • dir:目标流指针
    • loc:先前调用aos_telldir()返回的值
  • 返回值

获取已安装文件系统信息

int aos_statfs(const char *path, struct aos_statfs *buf);

相关数据结构:

struct aos_statfs {
    long f_type;    /**< fs type */
    long f_bsize;   /**< optimized transport block size */
    long f_blocks;  /**< total blocks */
    long f_bfree;   /**< available blocks */
    long f_bavail;  /**< number of blocks that non-super users can acquire */
    long f_files;   /**< total number of file nodes */
    long f_ffree;   /**< available file nodes */
    long f_fsid;    /**< fs id */
    long f_namelen; /**< max file name length */
};
  • 参数
    • path:文件系统中任意的文件路径
    • buf:数据缓冲区的指针
  • 返回值
    • 0:成功
    • <0:失败

检查调用进程的可访问性

int aos_access(const char *path, int amode);
  • 参数
    • path:文件路径
    • mode:指定要执行的可访问性检查
  • 返回值
    • 0:成功
    • <0:失败

修改目录

int aos_chdir(const char *path);
  • 参数
    • path:变更后的文件路径
  • 返回值
    • 0:成功
    • <0:失败

复制目录绝对路径名到指定数组

char *aos_getcwd(char *buf, size_t size);
  • 参数
    • buf:数据缓冲区的指针
    • size:数组长度
  • 返回值
    • 字符串指针:成功
    • NULL:失败

获取文件配置项

long aos_pathconf(const char *path, int name);
  • 参数
    • path:路径名
    • name:配置项
  • 返回值
    • 正值:成功
    • 错误代码:失败

获取文件协同配置项

long aos_fpathconf(int fd, int name);
  • 参数
    • fd:文件描述符
    • name:配置项
  • 返回值
    • 正值:成功
    • 错误代码:失败

修改索引节点访问和修改时间

int aos_utime(const char *path, const struct aos_utimbuf *times);

相关数据结构:

struct aos_utimbuf {
    time_t actime;  /**< 上次访问时间 */
    time_t modtime; /**< 上次修改时间 */
};
  • 参数
    • path:索引节点的路径名
    • times:给索引节点指定访问和修改时间
  • 返回值
    • 0:成功
    • <0:失败

获取VFS_fd偏移量

int aos_vfs_fd_offset_get(void);
  • 参数
  • 返回值
    • VFS_fd偏移量

注册/注销虚拟设备驱动

int aos_register_driver(const char *path, file_ops_t *ops, void *arg);
int aos_unregister_driver(const char *path);
  • 参数
    • path:文件或设备路径
    • ops:需要绑定的虚拟设备驱动
    • arg:命令的参数
  • 返回值
    • 0:成功
    • <0:失败

注册/注销虚拟文件系统

int aos_register_fs(const char *path, fs_ops_t* ops, void *arg);
int aos_unregister_fs(const char *path);
  • 参数
    • path:安装路径
    • ops:文件系统操作
    • arg:命令的参数
  • 返回值
    • 0:成功
    • <0:失败