use std::fs;
use std::fs::File;
use std::io;
use std::path::Path;
use crate::config::runtime::IoMode;
use crate::io::file::CacheFile;
use crate::io::file::StorageFile;
pub trait FileSystem {
type File: StorageFile;
fn open(&self, path: &Path, create: bool, mode: IoMode) -> io::Result<Self::File>;
fn create_new(&self, path: &Path) -> io::Result<Self::File>;
fn remove_file(&self, path: &Path) -> io::Result<()>;
fn rename(&self, source: &Path, destination: &Path) -> io::Result<()>;
fn sync_parent(&self, path: &Path) -> io::Result<()>;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct OsFileSystem;
impl FileSystem for OsFileSystem {
type File = CacheFile;
fn open(&self, path: &Path, create: bool, mode: IoMode) -> io::Result<Self::File> {
CacheFile::open_with_mode(path, mode, create)
}
fn create_new(&self, path: &Path) -> io::Result<Self::File> {
CacheFile::create_new_buffered(path)
}
fn remove_file(&self, path: &Path) -> io::Result<()> {
match fs::remove_file(path) {
Ok(()) => Ok(()),
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()),
Err(error) => Err(error),
}
}
fn rename(&self, source: &Path, destination: &Path) -> io::Result<()> {
fs::rename(source, destination)
}
fn sync_parent(&self, path: &Path) -> io::Result<()> {
File::open(parent_directory(path))?.sync_all()
}
}
pub fn parent_directory(path: &Path) -> &Path {
path.parent()
.filter(|parent| !parent.as_os_str().is_empty())
.unwrap_or_else(|| Path::new("."))
}