use std::io;
use std::path::Path;
pub(crate) struct DirEntry {
pub(crate) name: String,
pub(crate) is_dir: bool,
}
pub(crate) trait FolderIo: Send + Sync {
fn exists(&self, path: &Path) -> io::Result<bool>;
fn read(&self, path: &Path) -> io::Result<Option<Vec<u8>>>;
fn read_tail(&self, path: &Path, max: u64) -> io::Result<Option<Vec<u8>>>;
fn list_dir(&self, path: &Path) -> io::Result<Vec<DirEntry>>;
fn create_dir(&self, path: &Path) -> io::Result<()>;
fn write(&self, path: &Path, bytes: &[u8]) -> io::Result<()>;
fn append(&self, path: &Path, bytes: &[u8]) -> io::Result<()>;
fn sync_file(&self, path: &Path) -> io::Result<()>;
fn sync_dir(&self, path: &Path) -> io::Result<()>;
fn rename(&self, from: &Path, to: &Path) -> io::Result<()>;
fn remove_file(&self, path: &Path) -> io::Result<()>;
fn remove_dir(&self, path: &Path) -> io::Result<()>;
}
pub(crate) struct RealIo;
impl FolderIo for RealIo {
fn exists(&self, path: &Path) -> io::Result<bool> {
match std::fs::symlink_metadata(path) {
Ok(_) => Ok(true),
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(false),
Err(e) => Err(e),
}
}
fn read(&self, path: &Path) -> io::Result<Option<Vec<u8>>> {
match std::fs::read(path) {
Ok(bytes) => Ok(Some(bytes)),
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e),
}
}
fn read_tail(&self, path: &Path, max: u64) -> io::Result<Option<Vec<u8>>> {
use std::io::{Read as _, Seek as _, SeekFrom};
let mut file = match std::fs::File::open(path) {
Ok(f) => f,
Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(None),
Err(e) => return Err(e),
};
let len = file.metadata()?.len();
file.seek(SeekFrom::Start(len.saturating_sub(max)))?;
let mut buf = Vec::new();
file.read_to_end(&mut buf)?;
Ok(Some(buf))
}
fn list_dir(&self, path: &Path) -> io::Result<Vec<DirEntry>> {
let entries = match std::fs::read_dir(path) {
Ok(e) => e,
Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(Vec::new()),
Err(e) => return Err(e),
};
let mut out = Vec::new();
for entry in entries {
let entry = entry?;
out.push(DirEntry {
name: entry.file_name().to_string_lossy().into_owned(),
is_dir: entry.file_type()?.is_dir(),
});
}
Ok(out)
}
fn create_dir(&self, path: &Path) -> io::Result<()> {
std::fs::create_dir(path)
}
fn write(&self, path: &Path, bytes: &[u8]) -> io::Result<()> {
std::fs::write(path, bytes)
}
fn append(&self, path: &Path, bytes: &[u8]) -> io::Result<()> {
use std::io::Write as _;
let mut file = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(path)?;
file.write_all(bytes)
}
fn sync_file(&self, path: &Path) -> io::Result<()> {
#[cfg(windows)]
{
std::fs::OpenOptions::new()
.write(true)
.open(path)?
.sync_all()
}
#[cfg(not(windows))]
{
std::fs::File::open(path)?.sync_all()
}
}
fn sync_dir(&self, path: &Path) -> io::Result<()> {
#[cfg(unix)]
{
std::fs::File::open(path)?.sync_all()
}
#[cfg(not(unix))]
{
let _ = path;
Ok(())
}
}
fn rename(&self, from: &Path, to: &Path) -> io::Result<()> {
std::fs::rename(from, to)
}
fn remove_file(&self, path: &Path) -> io::Result<()> {
std::fs::remove_file(path)
}
fn remove_dir(&self, path: &Path) -> io::Result<()> {
std::fs::remove_dir(path)
}
}