use std::{path::{Path, PathBuf}, fmt::Display};
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>;
}
pub struct VMetadata {
pub created : usize,
pub accessed : usize,
pub modified : 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
}
pub fn accessed(&self) -> usize{
self.accessed
}
pub fn modified(&self) -> usize{
self.modified
}
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)
}
}