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 rcore_fs::vfs::FsInfo;
use std::convert::TryFrom;
use std::ffi::CString;
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 statfs = {
let fs_info = file_ref.fs()?.info();
do_statfs_inner(fs_info)?
Statfs::try_from(fs_info)?
};
trace!("fstatfs result: {:?}", statfs);
Ok(statfs)
@ -24,25 +25,12 @@ pub fn do_statfs(path: &str) -> Result<Statfs> {
};
let statfs = {
let fs_info = inode.fs().info();
do_statfs_inner(fs_info)?
Statfs::try_from(fs_info)?
};
trace!("statfs result: {:?}", 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)]
#[repr(C)]
pub struct Statfs {
@ -87,30 +75,41 @@ impl Statfs {
}
}
impl From<FsInfo> for Statfs {
fn from(info: FsInfo) -> Self {
Self {
f_type: match info.magic {
// The "/dev" and "/dev/shm" are tmpfs on Linux, so we transform the
// magic number to TMPFS_MAGIC.
rcore_fs_ramfs::RAMFS_MAGIC | rcore_fs_devfs::DEVFS_MAGIC => {
const TMPFS_MAGIC: usize = 0x0102_1994;
TMPFS_MAGIC
}
val => val,
},
f_bsize: info.bsize,
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],
}
impl TryFrom<FsInfo> for Statfs {
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 {
f_type: match info.magic {
// The "/dev" and "/dev/shm" are tmpfs on Linux, so we transform the
// magic number to TMPFS_MAGIC.
rcore_fs_ramfs::RAMFS_MAGIC | rcore_fs_devfs::DEVFS_MAGIC => {
const TMPFS_MAGIC: usize = 0x0102_1994;
TMPFS_MAGIC
}
val => val,
},
f_bsize: info.bsize,
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)
}
}