use std::fs;
use std::os::unix::prelude::*;
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
#[non_exhaustive]
pub enum FileType {
File,
Directory,
Symlink,
Socket,
Block,
Character,
Fifo,
}
#[derive(Copy, Clone, Debug)]
pub struct Metadata {
stat: libc::stat,
}
#[allow(clippy::len_without_is_empty)]
impl Metadata {
#[inline]
pub(crate) fn new(stat: libc::stat) -> Self {
Self { stat }
}
pub fn file_type(&self) -> FileType {
match self.stat.st_mode & libc::S_IFMT {
libc::S_IFREG => FileType::File,
libc::S_IFDIR => FileType::Directory,
libc::S_IFLNK => FileType::Symlink,
libc::S_IFSOCK => FileType::Socket,
libc::S_IFBLK => FileType::Block,
libc::S_IFCHR => FileType::Character,
libc::S_IFIFO => FileType::Fifo,
_ => unreachable!(),
}
}
#[inline]
pub fn stat(&self) -> &libc::stat {
&self.stat
}
#[inline]
pub fn is_file(&self) -> bool {
self.stat.st_mode & libc::S_IFMT == libc::S_IFREG
}
#[inline]
pub fn is_dir(&self) -> bool {
self.stat.st_mode & libc::S_IFMT == libc::S_IFDIR
}
#[inline]
pub fn permissions(&self) -> fs::Permissions {
fs::Permissions::from_mode(self.stat.st_mode as u32)
}
#[inline]
pub fn len(&self) -> u64 {
self.stat.st_size as u64
}
#[inline]
pub fn dev(&self) -> u64 {
self.stat.st_dev as u64
}
#[inline]
pub fn ino(&self) -> u64 {
self.stat.st_ino as u64
}
}