use std::{
fmt::Display,
path::{Path, PathBuf},
};
use crate::err::ForensicResult;
pub struct VPath(PathBuf);
impl From<&str> for VPath {
fn from(v: &str) -> Self {
VPath(PathBuf::from(v))
}
}
impl From<String> for VPath {
fn from(v: String) -> Self {
VPath(PathBuf::from(v))
}
}
impl From<PathBuf> for VPath {
fn from(v: PathBuf) -> Self {
VPath(v)
}
}
impl From<&Path> for VPath {
fn from(v: &Path) -> Self {
VPath(v.to_path_buf())
}
}
pub trait VirtualFile: std::io::Seek + std::io::Read {
fn metadata(&self) -> ForensicResult<VMetadata>;
}
pub trait VirtualFileSystem {
fn from_file(&self, file: Box<dyn VirtualFile>) -> ForensicResult<Box<dyn VirtualFileSystem>>;
fn from_fs(&self, fs: Box<dyn VirtualFileSystem>)
-> ForensicResult<Box<dyn VirtualFileSystem>>;
fn read_to_string(&mut self, path: &Path) -> ForensicResult<String>;
fn read_all(&mut self, path: &Path) -> ForensicResult<Vec<u8>>;
fn read(&mut self, path: &Path, pos: u64, buf: &mut [u8]) -> ForensicResult<usize>;
fn metadata(&mut self, path: &Path) -> ForensicResult<VMetadata>;
fn read_dir(&mut self, path: &Path) -> ForensicResult<Vec<VDirEntry>>;
fn is_live(&self) -> bool;
fn open(&mut self, path: &Path) -> ForensicResult<Box<dyn VirtualFile>>;
fn duplicate(&self) -> Box<dyn VirtualFileSystem>;
#[allow(unused_variables)]
fn exists(&self, path: &Path) -> bool {
false
}
}
pub struct VMetadata {
pub created: Option<usize>,
pub accessed: Option<usize>,
pub modified: Option<usize>,
pub file_type: VFileType,
pub size: u64,
}
#[derive(PartialEq)]
pub enum VFileType {
File,
Directory,
Symlink,
}
impl VMetadata {
pub fn created(&self) -> usize {
self.created.unwrap_or_else(|| {
crate::warn!(
"this filesystem has no support for creation time, using UNIX_EPOCH instead"
);
0
})
}
pub fn accessed(&self) -> usize {
self.accessed.unwrap_or_else(|| {
crate::warn!("this filesystem has no support for access time, using UNIX_EPOCH instead");
0
})
}
pub fn modified(&self) -> usize {
self.modified.unwrap_or_else(|| {
crate::warn!(
"this filesystem has no support for modification time, using UNIX_EPOCH instead"
);
0
})
}
pub fn created_opt(&self) -> Option<&usize> {
self.created.as_ref()
}
pub fn accessed_opt(&self) -> Option<&usize> {
self.accessed.as_ref()
}
pub fn modified_opt(&self) -> Option<&usize> {
self.modified.as_ref()
}
pub fn is_file(&self) -> bool {
self.file_type == VFileType::File
}
pub fn is_dir(&self) -> bool {
self.file_type == VFileType::Directory
}
pub fn is_symlink(&self) -> bool {
self.file_type == VFileType::Symlink
}
pub fn len(&self) -> u64 {
self.size
}
}
pub enum VDirEntry {
Directory(String),
File(String),
Symlink(String),
}
impl Display for VDirEntry {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let content = match self {
VDirEntry::Directory(v) => v,
VDirEntry::File(v) => v,
VDirEntry::Symlink(v) => v,
};
write!(f, "{}", content)
}
}