use filecache::*;
use std::{fmt::Debug, fs, marker::PhantomData, path::Path};
use crate::{ContentExtractor, ExtractionPool};
pub trait ContentType: Copy + Eq + PartialEq + Debug {
const COUNT: u16;
fn as_u16(&self) -> u16;
fn from_u16(value: u16) -> Option<Self>;
}
pub trait Content<T: ContentType> {
fn content_type(&self) -> Option<T> {
None
}
fn path(&self) -> &str;
fn size(&self) -> u64;
fn read(&mut self, offset: u64, count: u32) -> Option<&[u8]>;
}
impl ContentType for bool {
const COUNT: u16 = 2;
fn as_u16(&self) -> u16 {
*self as u16
}
fn from_u16(value: u16) -> Option<Self> {
if value == 0 {
Some(false)
} else if value == 1 {
Some(true)
} else {
None
}
}
}
pub struct BufferContent<T: ContentType> {
buffer: Vec<u8>,
path: String,
content_type: Option<T>,
}
impl<T: ContentType> BufferContent<T> {
pub fn new(buffer: &[u8], path: &str) -> Self {
Self {
buffer: buffer.to_vec(),
path: path.to_string(),
content_type: None,
}
}
pub fn with_content_type(buffer: &[u8], path: &str, content_type: T) -> Self {
Self {
buffer: buffer.to_vec(),
path: path.to_string(),
content_type: Some(content_type),
}
}
pub fn from_parts(buffer: Vec<u8>, path: String, content_type: Option<T>) -> Self {
Self { buffer, path, content_type }
}
}
impl<T: ContentType> Content<T> for BufferContent<T> {
#[inline(always)]
fn content_type(&self) -> Option<T> {
self.content_type
}
#[inline(always)]
fn path(&self) -> &str {
&self.path
}
#[inline(always)]
fn size(&self) -> u64 {
self.buffer.len() as u64
}
fn read(&mut self, offset: u64, count: u32) -> Option<&[u8]> {
if offset > self.buffer.len() as u64 {
return None;
}
if offset == self.buffer.len() as u64 {
return Some(&[]);
}
let len = (self.buffer.len() as u64 - offset).min(count as u64) as usize;
Some(&self.buffer.as_slice()[offset as usize..offset as usize + len])
}
}
enum FileContentStatus {
NotOpened,
Opened(FileCache<filecache::RandomAccessFile>),
Error,
}
pub struct FileContent<T: ContentType> {
path: String,
content_type: Option<T>,
status: FileContentStatus,
size: u64,
}
impl<T: ContentType> FileContent<T> {
pub fn new(path: &str) -> Self {
Self {
path: path.to_string(),
content_type: None,
status: FileContentStatus::NotOpened,
size: fs::metadata(path).map(|m| m.len()).unwrap_or(0),
}
}
pub fn with_content_type(path: &str, content_type: T) -> Self {
Self {
path: path.to_string(),
content_type: Some(content_type),
status: FileContentStatus::NotOpened,
size: fs::metadata(path).map(|m| m.len()).unwrap_or(0),
}
}
pub fn with_size(path: &str, size: u64) -> Self {
Self {
path: path.to_string(),
content_type: None,
status: FileContentStatus::NotOpened,
size,
}
}
fn open(&mut self) {
match RandomAccessFile::open(Path::new(&self.path), RandomAccessFlags::Exclusive) {
Ok(reader) => match FileCache::new(CacheType::MemoryMap, reader) {
Ok(file) => {
self.status = FileContentStatus::Opened(file);
}
Err(_) => {
self.status = FileContentStatus::Error;
}
},
Err(_) => {
self.status = FileContentStatus::Error;
}
}
}
}
impl<T: ContentType> Content<T> for FileContent<T> {
#[inline(always)]
fn content_type(&self) -> Option<T> {
self.content_type
}
#[inline(always)]
fn path(&self) -> &str {
&self.path
}
#[inline(always)]
fn size(&self) -> u64 {
match &self.status {
FileContentStatus::Opened(file) => file.len(),
FileContentStatus::NotOpened => self.size,
FileContentStatus::Error => 0,
}
}
#[inline(always)]
fn read(&mut self, offset: u64, count: u32) -> Option<&[u8]> {
if matches!(self.status, FileContentStatus::NotOpened) {
self.open();
}
match &mut self.status {
FileContentStatus::Opened(file) => file.read(offset, count as usize).ok(),
FileContentStatus::NotOpened | FileContentStatus::Error => None,
}
}
}
pub struct FolderContent<T: ContentType> {
path: String,
content_type: T,
}
impl<T: ContentType> FolderContent<T> {
pub fn with_content_type(path: &str, content_type: T) -> Self {
Self {
path: path.to_string(),
content_type,
}
}
}
impl<T: ContentType> Content<T> for FolderContent<T> {
fn path(&self) -> &str {
&self.path
}
fn size(&self) -> u64 {
0
}
fn read(&mut self, _: u64, _: u32) -> Option<&[u8]> {
None
}
fn content_type(&self) -> Option<T> {
Some(self.content_type)
}
}
pub struct FolderExtractor<T: ContentType> {
_marker: PhantomData<T>,
pool: ExtractionPool<fs::ReadDir>,
entry: crate::Entry,
current_is_folder: bool,
recursive: bool,
}
impl<T: ContentType> FolderExtractor<T> {
pub fn new(recursive: bool) -> Self {
Self {
_marker: PhantomData,
pool: ExtractionPool::new(4),
entry: crate::Entry::default(),
current_is_folder: false,
recursive,
}
}
}
impl<T: ContentType + 'static> ContentExtractor<T> for FolderExtractor<T> {
fn acquire(&mut self, content: &mut dyn Content<T>, _: &mut varmap::VarMap) -> Option<crate::ExtractionHandle> {
let obj = fs::read_dir(content.path()).ok()?;
Some(self.pool.acquire_slot(obj))
}
fn advance(&mut self, handle: crate::ExtractionHandle, _: &mut dyn Content<T>) -> Option<&crate::Entry> {
let Some(rd) = self.pool.get_mut(handle) else {
return None;
};
loop {
let folder_ent = rd.next()?.ok()?;
let ft = folder_ent.file_type().ok()?;
self.current_is_folder = ft.is_dir();
let symlink = ft.is_symlink();
if self.current_is_folder && symlink {
continue;
} if !self.recursive && self.current_is_folder {
continue;
} self.entry.path.clear();
self.entry.path.push_str(folder_ent.path().to_str().unwrap_or_default());
self.entry.size = if self.current_is_folder { 0 } else { folder_ent.metadata().map(|m| m.len()).unwrap_or(0) }; self.entry.skip_from_filtering = self.current_is_folder;
return Some(&self.entry);
}
}
fn extract(&mut self, _: crate::ExtractionHandle, content: &mut dyn Content<T>) -> Option<Box<dyn Content<T>>> {
if self.current_is_folder {
Some(Box::new(FolderContent::with_content_type(&self.entry.path, content.content_type()?)))
} else {
Some(Box::new(FileContent::with_size(&self.entry.path, self.entry.size)))
}
}
fn release(&mut self, handle: crate::ExtractionHandle) {
self.pool.release_slot(handle);
}
}