3、LiteOS-M FATFS的文件系统操作接口
快速记录下各个操作接口,对每个接口的用途用法不再描述。可以参考之前的系列文章,《鸿蒙轻内核M核源码分析系列十九 Musl LibC》中介绍了相关的接口,那些接口会调用VFS文件系统中操作接口,然后进一步调用FatFS文件操作接口。
3.1 挂载和卸载操作
先看下挂载操作,FatFS支持重新挂载操作。如果挂载选项包含MS_REMOUNT时,会调用函数Remount()重新挂载。函数Remount()中,⑴处调用FsPartitionMatch()获取卷索引,⑵处如果卷未被挂载,不允许重新挂载,返回错误码。⑶处设置对应的卷是否可读可写标记。由此看来,重新挂载,主要是更新卷的可读可写能力。
看下挂载函数fatfs_mount(),⑷处开始判断参数有效性,不能为空,文件系统类型必须为“fat”,⑸处调用FsPartitionMatch()获取卷索引,⑹处如果卷已经被挂载,则返回错误码。⑺处调用f_mount()实现挂载,第3个参数1表示立即挂载。⑻设置对应的卷是否可读可写标记。
static int Remount(const char *path, unsigned long mountflags)
{
INT32 index;
⑴ index = FsPartitionMatch(path, PART_NAME);
if (index == FS_FAILURE) {
PRINTK("Wrong volume path!\r\n");
errno = ENOENT;
return FS_FAILURE;
}
/* remount is not allowed when the device is not mounted. */
⑵ if (g_fatfs[index].fs_type == 0) {
errno = EINVAL;
return FS_FAILURE;
}
⑶ g_volWriteEnable[index] = (mountflags & MS_RDONLY) ? FALSE : TRUE;
return FS_SUCCESS;
}
......
int fatfs_mount(const char *source, const char *target,
const char *filesystemtype, unsigned long mountflags,
const void *data)
{
INT32 index;
FRESULT res;
INT32 ret;
⑷ if ((target == NULL) || (filesystemtype == NULL)) {
errno = EFAULT;
return FS_FAILURE;
}
ret = FsLock();
if (ret != 0) {
errno = ret;
return FS_FAILURE;
}
if (mountflags & MS_REMOUNT) {
ret = Remount(target, mountflags);
goto OUT;
}
if (strcmp(filesystemtype, "fat") != 0) {
errno = ENODEV;
ret = FS_FAILURE;
goto OUT;
}
⑸ index = FsPartitionMatch(target, VOLUME_NAME);
if (index == FS_FAILURE) {
errno = ENODEV;
ret = FS_FAILURE;
goto OUT;
}
/* If the volume has been mounted */
⑹ if (g_fatfs[index].fs_type != 0) {
errno = EBUSY;
ret