Fix the type conversion in statfs with TryFrom trait

This commit is contained in:
LI Qing 2021-09-22 14:25:49 +08:00 committed by Zongmin.Gu
parent c9083c787c
commit a8cd5eadba
2 changed files with 39 additions and 40 deletions

2
deps/sefs vendored

@ -1 +1 @@
Subproject commit 232823888c83c2ad3a29dee219d26252bcda37b1 Subproject commit 10424324bc67f56f0418604202767a4fd4367d7b

@ -1,5 +1,6 @@
use super::*; use super::*;
use rcore_fs::vfs::FsInfo; use rcore_fs::vfs::FsInfo;
use std::convert::TryFrom;
use std::ffi::CString; use std::ffi::CString;
pub fn do_fstatfs(fd: FileDesc) -> Result<Statfs> { pub fn do_fstatfs(fd: FileDesc) -> Result<Statfs> {
@ -8,7 +9,7 @@ pub fn do_fstatfs(fd: FileDesc) -> Result<Statfs> {
let file_ref = current!().file(fd)?; let file_ref = current!().file(fd)?;
let statfs = { let statfs = {
let fs_info = file_ref.fs()?.info(); let fs_info = file_ref.fs()?.info();
do_statfs_inner(fs_info)? Statfs::try_from(fs_info)?
}; };
trace!("fstatfs result: {:?}", statfs); trace!("fstatfs result: {:?}", statfs);
Ok(statfs) Ok(statfs)
@ -24,25 +25,12 @@ pub fn do_statfs(path: &str) -> Result<Statfs> {
}; };
let statfs = { let statfs = {
let fs_info = inode.fs().info(); let fs_info = inode.fs().info();
do_statfs_inner(fs_info)? Statfs::try_from(fs_info)?
}; };
trace!("statfs result: {:?}", statfs); trace!("statfs result: {:?}", statfs);
Ok(statfs) Ok(statfs)
} }
fn do_statfs_inner(fs_info: FsInfo) -> Result<Statfs> {
let statfs = if fs_info.magic == rcore_fs_unionfs::UNIONFS_MAGIC
|| fs_info.magic == rcore_fs_sefs::SEFS_MAGIC as usize
{
let mut host_statfs = host_statfs()?;
host_statfs.f_type = fs_info.magic;
host_statfs
} else {
Statfs::from(fs_info)
};
Ok(statfs)
}
#[derive(Default, Debug)] #[derive(Default, Debug)]
#[repr(C)] #[repr(C)]
pub struct Statfs { pub struct Statfs {
@ -87,30 +75,41 @@ impl Statfs {
} }
} }
impl From<FsInfo> for Statfs { impl TryFrom<FsInfo> for Statfs {
fn from(info: FsInfo) -> Self { type Error = error::Error;
Self {
f_type: match info.magic { fn try_from(info: FsInfo) -> Result<Self> {
// The "/dev" and "/dev/shm" are tmpfs on Linux, so we transform the let statfs = if info.magic == rcore_fs_unionfs::UNIONFS_MAGIC
// magic number to TMPFS_MAGIC. || info.magic == rcore_fs_sefs::SEFS_MAGIC as usize
rcore_fs_ramfs::RAMFS_MAGIC | rcore_fs_devfs::DEVFS_MAGIC => { {
const TMPFS_MAGIC: usize = 0x0102_1994; let mut host_statfs = host_statfs()?;
TMPFS_MAGIC host_statfs.f_type = info.magic;
} host_statfs
val => val, } else {
}, Self {
f_bsize: info.bsize, f_type: match info.magic {
f_blocks: info.blocks, // The "/dev" and "/dev/shm" are tmpfs on Linux, so we transform the
f_bfree: info.bfree, // magic number to TMPFS_MAGIC.
f_bavail: info.bavail, rcore_fs_ramfs::RAMFS_MAGIC | rcore_fs_devfs::DEVFS_MAGIC => {
f_files: info.files, const TMPFS_MAGIC: usize = 0x0102_1994;
f_ffree: info.ffree, TMPFS_MAGIC
f_fsid: [0i32; 2], }
f_namelen: info.namemax, val => val,
f_frsize: info.frsize, },
f_flags: 0, f_bsize: info.bsize,
f_spare: [0usize; 4], f_blocks: info.blocks,
} f_bfree: info.bfree,
f_bavail: info.bavail,
f_files: info.files,
f_ffree: info.ffree,
f_fsid: [0i32; 2],
f_namelen: info.namemax,
f_frsize: info.frsize,
f_flags: 0,
f_spare: [0usize; 4],
}
};
Ok(statfs)
} }
} }