use std::fs::File;
use std::io::Result;
use std::path::Path;
#[cfg(feature = "dst")]
pub mod record {
use std::cell::RefCell;
use std::path::PathBuf;
#[derive(Clone, Debug)]
pub enum Op {
Create {
path: PathBuf,
},
WriteAt {
path: PathBuf,
off: u64,
data: Vec<u8>,
},
SetLen {
path: PathBuf,
len: u64,
},
WriteFile {
path: PathBuf,
data: Vec<u8>,
},
SyncFile {
path: PathBuf,
},
SyncDir {
path: PathBuf,
},
Rename {
from: PathBuf,
to: PathBuf,
},
Link {
from: PathBuf,
to: PathBuf,
},
Unlink {
path: PathBuf,
},
PunchHole {
path: PathBuf,
off: u64,
len: u64,
},
Mkdir {
path: PathBuf,
},
RemoveTree {
path: PathBuf,
},
}
thread_local! {
static LOG: RefCell<Option<Vec<Op>>> = const { RefCell::new(None) };
}
pub fn arm() {
LOG.with(|l| *l.borrow_mut() = Some(Vec::new()));
}
pub fn disarm() -> Vec<Op> {
LOG.with(|l| l.borrow_mut().take().unwrap_or_default())
}
pub(super) fn push(op: Op) {
LOG.with(|l| {
if let Some(v) = l.borrow_mut().as_mut() {
v.push(op);
}
});
}
pub fn len() -> usize {
LOG.with(|l| l.borrow().as_ref().map_or(0, |v| v.len()))
}
}
#[cfg(feature = "dst")]
use record::{push, Op};
#[inline]
pub(crate) fn create(path: &Path) -> Result<File> {
let f = File::create(path)?;
#[cfg(feature = "dst")]
push(Op::Create { path: path.to_path_buf() });
Ok(f)
}
#[inline]
pub(crate) fn open_or_create(path: &Path) -> Result<(File, bool)> {
let existed = path.exists();
let f = std::fs::OpenOptions::new()
.create(true)
.truncate(false)
.read(true)
.write(true)
.open(path)?;
#[cfg(feature = "dst")]
if !existed {
push(Op::Create { path: path.to_path_buf() });
}
Ok((f, !existed))
}
#[inline]
pub(crate) fn create_new(path: &Path) -> Result<File> {
let f = std::fs::OpenOptions::new().create_new(true).write(true).read(true).open(path)?;
#[cfg(feature = "dst")]
push(Op::Create { path: path.to_path_buf() });
Ok(f)
}
#[inline]
pub(crate) fn write_all_at(f: &File, path: &Path, buf: &[u8], off: u64) -> Result<()> {
crate::sys::write_all_at(f, buf, off)?;
#[cfg(feature = "dst")]
push(Op::WriteAt { path: path.to_path_buf(), off, data: buf.to_vec() });
#[cfg(not(feature = "dst"))]
let _ = path;
Ok(())
}
#[inline]
pub(crate) fn set_len(f: &File, path: &Path, len: u64) -> Result<()> {
f.set_len(len)?;
#[cfg(feature = "dst")]
push(Op::SetLen { path: path.to_path_buf(), len });
#[cfg(not(feature = "dst"))]
let _ = path;
Ok(())
}
#[inline]
pub(crate) fn write_file(path: &Path, data: &[u8]) -> Result<()> {
std::fs::write(path, data)?;
#[cfg(feature = "dst")]
push(Op::WriteFile { path: path.to_path_buf(), data: data.to_vec() });
Ok(())
}
#[inline]
pub(crate) fn sync_file(f: &File, path: &Path) -> Result<()> {
f.sync_all()?;
#[cfg(feature = "dst")]
push(Op::SyncFile { path: path.to_path_buf() });
#[cfg(not(feature = "dst"))]
let _ = path;
Ok(())
}
#[inline]
pub(crate) fn sync_dir(dir: &Path) -> Result<()> {
File::open(dir)?.sync_all()?;
#[cfg(feature = "dst")]
push(Op::SyncDir { path: dir.to_path_buf() });
Ok(())
}
#[inline]
pub(crate) fn rename(from: &Path, to: &Path) -> Result<()> {
std::fs::rename(from, to)?;
#[cfg(feature = "dst")]
push(Op::Rename { from: from.to_path_buf(), to: to.to_path_buf() });
Ok(())
}
#[inline]
pub(crate) fn rename_noreplace(from: &Path, to: &Path) -> Result<()> {
crate::sys::rename_noreplace(from, to)?;
#[cfg(feature = "dst")]
push(Op::Rename { from: from.to_path_buf(), to: to.to_path_buf() });
Ok(())
}
#[inline]
pub(crate) fn link(from: &Path, to: &Path) -> Result<()> {
std::fs::hard_link(from, to)?;
#[cfg(feature = "dst")]
push(Op::Link { from: from.to_path_buf(), to: to.to_path_buf() });
Ok(())
}
#[inline]
pub(crate) fn unlink(path: &Path) -> Result<()> {
std::fs::remove_file(path)?;
#[cfg(feature = "dst")]
push(Op::Unlink { path: path.to_path_buf() });
Ok(())
}
#[inline]
pub(crate) fn punch_hole(f: &File, path: &Path, off: u64, len: u64) -> Result<()> {
crate::sys::punch_hole(f, off, len)?;
#[cfg(feature = "dst")]
push(Op::PunchHole { path: path.to_path_buf(), off, len });
#[cfg(not(feature = "dst"))]
let _ = path;
Ok(())
}
#[inline]
pub(crate) fn mkdir_all(path: &Path) -> Result<()> {
std::fs::create_dir_all(path)?;
#[cfg(feature = "dst")]
push(Op::Mkdir { path: path.to_path_buf() });
Ok(())
}
#[inline]
pub(crate) fn mkdir_new(path: &Path) -> Result<()> {
std::fs::create_dir(path)?;
#[cfg(feature = "dst")]
push(Op::Mkdir { path: path.to_path_buf() });
Ok(())
}
#[inline]
pub(crate) fn remove_tree(path: &Path) -> Result<()> {
std::fs::remove_dir_all(path)?;
#[cfg(feature = "dst")]
push(Op::RemoveTree { path: path.to_path_buf() });
Ok(())
}