use crate::vfs::{FileStream, WritableFile, read_only};
use std::collections::HashMap;
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
#[cfg(windows)]
use crate::vfs::VfsDirEntry;
#[cfg(unix)]
use std::collections::HashSet;
pub(crate) const VIRTUAL_VOLUME_SERIAL: u32 = 0x484F_4B46;
pub(crate) enum OpenStream {
Read(Box<dyn FileStream>),
Write(Box<dyn WritableFile>),
}
impl OpenStream {
pub(crate) fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self {
Self::Read(s) => s.read(buf),
Self::Write(s) => s.read(buf),
}
}
pub(crate) fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
match self {
Self::Read(_) => Err(read_only()),
Self::Write(s) => s.write(buf),
}
}
pub(crate) fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
match self {
Self::Read(s) => s.seek(pos),
Self::Write(s) => s.seek(pos),
}
}
pub(crate) fn stream_position(&mut self) -> io::Result<u64> {
match self {
Self::Read(s) => s.stream_position(),
Self::Write(s) => s.stream_position(),
}
}
pub(crate) fn size(&mut self) -> io::Result<u64> {
match self {
Self::Read(s) => s.size(),
Self::Write(s) => s.size(),
}
}
pub(crate) fn set_len(&mut self, len: u64) -> io::Result<()> {
match self {
Self::Read(_) => Err(read_only()),
Self::Write(s) => s.set_len(len),
}
}
pub(crate) fn flush(&mut self) -> io::Result<()> {
match self {
Self::Read(_) => Ok(()),
Self::Write(s) => s.flush(),
}
}
}
pub(crate) struct OpenFile {
pub(crate) stream: OpenStream,
pub(crate) path: PathBuf,
pub(crate) writable: bool,
#[cfg(windows)]
pub(crate) overlapped: bool,
}
#[cfg(windows)]
pub(crate) struct FindState {
pub(crate) entries: Vec<VfsDirEntry>,
pub(crate) next: usize,
}
#[derive(Default)]
pub(crate) struct Registry {
files: Mutex<HashMap<usize, Arc<Mutex<OpenFile>>>>,
#[cfg(windows)]
finds: Mutex<HashMap<usize, Arc<Mutex<FindState>>>>,
#[cfg(unix)]
dirs: Mutex<HashSet<usize>>,
}
impl std::fmt::Debug for Registry {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let files = self.files.lock().map_or(0, |m| m.len());
let mut dbg = f.debug_struct("Registry");
dbg.field("files", &files);
#[cfg(windows)]
dbg.field("finds", &self.finds.lock().map_or(0, |m| m.len()));
#[cfg(unix)]
dbg.field("dirs", &self.dirs.lock().map_or(0, |m| m.len()));
dbg.finish_non_exhaustive()
}
}
impl Registry {
pub(crate) fn new() -> Self {
Self::default()
}
pub(crate) fn insert_file(&self, handle: usize, file: OpenFile) {
self.files
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.insert(handle, Arc::new(Mutex::new(file)));
}
pub(crate) fn get_file(&self, handle: usize) -> Option<Arc<Mutex<OpenFile>>> {
self.files
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.get(&handle)
.cloned()
}
pub(crate) fn remove_file(&self, handle: usize) -> Option<Arc<Mutex<OpenFile>>> {
self.files
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.remove(&handle)
}
#[cfg(windows)]
pub(crate) fn insert_find(&self, handle: usize, state: FindState) {
self.finds
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.insert(handle, Arc::new(Mutex::new(state)));
}
#[cfg(windows)]
pub(crate) fn get_find(&self, handle: usize) -> Option<Arc<Mutex<FindState>>> {
self.finds
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.get(&handle)
.cloned()
}
#[cfg(windows)]
pub(crate) fn remove_find(&self, handle: usize) -> Option<Arc<Mutex<FindState>>> {
self.finds
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.remove(&handle)
}
#[cfg(unix)]
pub(crate) fn insert_dir(&self, dir: usize) {
self.dirs
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.insert(dir);
}
#[cfg(unix)]
pub(crate) fn contains_dir(&self, dir: usize) -> bool {
self.dirs
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.contains(&dir)
}
#[cfg(unix)]
pub(crate) fn remove_dir(&self, dir: usize) -> bool {
self.dirs
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.remove(&dir)
}
}