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,8 +75,17 @@ impl Statfs {
} }
} }
impl From<FsInfo> for Statfs { impl TryFrom<FsInfo> for Statfs {
fn from(info: FsInfo) -> Self { type Error = error::Error;
fn try_from(info: FsInfo) -> Result<Self> {
let statfs = if info.magic == rcore_fs_unionfs::UNIONFS_MAGIC
|| info.magic == rcore_fs_sefs::SEFS_MAGIC as usize
{
let mut host_statfs = host_statfs()?;
host_statfs.f_type = info.magic;
host_statfs
} else {
Self { Self {
f_type: match info.magic { f_type: match info.magic {
// The "/dev" and "/dev/shm" are tmpfs on Linux, so we transform the // The "/dev" and "/dev/shm" are tmpfs on Linux, so we transform the
@ -111,6 +108,8 @@ impl From<FsInfo> for Statfs {
f_flags: 0, f_flags: 0,
f_spare: [0usize; 4], f_spare: [0usize; 4],
} }
};
Ok(statfs)
} }
} }