Refactor and combine all FileType structs in yanix
This commit does a bit of everything: refactors bits here and there, fixes a bug discovered in another #701, and combines all structs that we used in `yanix` and `wasi-common` crates to represent file types on *nix into one struct, `yanix::file::FileType`. Up until now, in `yanix`, we've had two separate structs used to represent file types on the host: `yanix::dir::FileType` and `yanix::file::SFlags` (well, not quite, but that was its main use). They both were used in different context (the former when parsing `dirent` struct, and the latter when parsing `stat` struct), they were C-compatible (as far as their representation goes), and as it turns out, they shared possible enumeration values. This commit combines them both into an idiomatic Rust enum with the caveat that it is now *not* C-compatible, however, I couldn't find a single use where that would actually matter, and even if it does in the future, we can simply add appropriate impl methods. The combine `yanix::file::FileType` struct can be constructed in two ways: 1) either from `stat.st_mode` value (and while we're here, now it's done correctly according to POSIX which fixes the bug mentioned in VFS impl PR #701), or 2) from `dirent.d_type` value. Also, since we now have one struct for representing both contexts, this cleans up nicely a lot of duplicated code in `host` module.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use crate::{
|
||||
file::FileType,
|
||||
sys::dir::{iter_impl, EntryImpl},
|
||||
Errno, Result,
|
||||
};
|
||||
@@ -84,7 +85,7 @@ impl Entry {
|
||||
|
||||
/// Returns the type of this directory entry.
|
||||
pub fn file_type(&self) -> FileType {
|
||||
FileType::from_raw(self.0.d_type)
|
||||
FileType::from_dirent_d_type(self.0.d_type)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,47 +100,6 @@ impl SeekLoc {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
#[repr(u8)]
|
||||
pub enum FileType {
|
||||
CharacterDevice = libc::DT_CHR,
|
||||
Directory = libc::DT_DIR,
|
||||
BlockDevice = libc::DT_BLK,
|
||||
RegularFile = libc::DT_REG,
|
||||
Symlink = libc::DT_LNK,
|
||||
Fifo = libc::DT_FIFO,
|
||||
Socket = libc::DT_SOCK,
|
||||
Unknown = libc::DT_UNKNOWN,
|
||||
}
|
||||
|
||||
impl FileType {
|
||||
pub fn from_raw(file_type: u8) -> Self {
|
||||
match file_type {
|
||||
libc::DT_CHR => Self::CharacterDevice,
|
||||
libc::DT_DIR => Self::Directory,
|
||||
libc::DT_BLK => Self::BlockDevice,
|
||||
libc::DT_REG => Self::RegularFile,
|
||||
libc::DT_LNK => Self::Symlink,
|
||||
libc::DT_SOCK => Self::Socket,
|
||||
libc::DT_FIFO => Self::Fifo,
|
||||
/* libc::DT_UNKNOWN */ _ => Self::Unknown,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_raw(&self) -> u8 {
|
||||
match self {
|
||||
Self::CharacterDevice => libc::DT_CHR,
|
||||
Self::Directory => libc::DT_DIR,
|
||||
Self::BlockDevice => libc::DT_BLK,
|
||||
Self::RegularFile => libc::DT_REG,
|
||||
Self::Symlink => libc::DT_LNK,
|
||||
Self::Socket => libc::DT_SOCK,
|
||||
Self::Fifo => libc::DT_FIFO,
|
||||
Self::Unknown => libc::DT_UNKNOWN,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DirIter<T: Deref<Target = Dir>>(T);
|
||||
|
||||
|
||||
@@ -90,16 +90,43 @@ bitflags! {
|
||||
}
|
||||
}
|
||||
|
||||
bitflags! {
|
||||
pub struct SFlag: libc::mode_t {
|
||||
const IFIFO = libc::S_IFIFO;
|
||||
const IFCHR = libc::S_IFCHR;
|
||||
const IFDIR = libc::S_IFDIR;
|
||||
const IFBLK = libc::S_IFBLK;
|
||||
const IFREG = libc::S_IFREG;
|
||||
const IFLNK = libc::S_IFLNK;
|
||||
const IFSOCK = libc::S_IFSOCK;
|
||||
const IFMT = libc::S_IFMT;
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum FileType {
|
||||
CharacterDevice,
|
||||
Directory,
|
||||
BlockDevice,
|
||||
RegularFile,
|
||||
Symlink,
|
||||
Fifo,
|
||||
Socket,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl FileType {
|
||||
pub fn from_stat_st_mode(st_mode: libc::mode_t) -> Self {
|
||||
match st_mode & libc::S_IFMT {
|
||||
libc::S_IFIFO => Self::Fifo,
|
||||
libc::S_IFCHR => Self::CharacterDevice,
|
||||
libc::S_IFDIR => Self::Directory,
|
||||
libc::S_IFBLK => Self::BlockDevice,
|
||||
libc::S_IFREG => Self::RegularFile,
|
||||
libc::S_IFLNK => Self::Symlink,
|
||||
libc::S_IFSOCK => Self::Socket,
|
||||
_ => Self::Unknown, // Should we actually panic here since this one *should* never happen?
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_dirent_d_type(d_type: u8) -> Self {
|
||||
match d_type {
|
||||
libc::DT_CHR => Self::CharacterDevice,
|
||||
libc::DT_DIR => Self::Directory,
|
||||
libc::DT_BLK => Self::BlockDevice,
|
||||
libc::DT_REG => Self::RegularFile,
|
||||
libc::DT_LNK => Self::Symlink,
|
||||
libc::DT_SOCK => Self::Socket,
|
||||
libc::DT_FIFO => Self::Fifo,
|
||||
/* libc::DT_UNKNOWN */ _ => Self::Unknown,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user