use super::fs;
use std::fs::{File, OpenOptions};
use std::io;
use std::path::{Path, PathBuf};
const SMALL_PREALLOC_MAX: u64 = 2 * 1024 * 1024;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
static STAGED_COUNTER: AtomicU64 = AtomicU64::new(0);
#[derive(Debug)]
pub struct StagedFile {
final_path: PathBuf,
staged_path: PathBuf,
committed: AtomicBool,
}
impl StagedFile {
pub fn new(final_path: &Path) -> io::Result<Self> {
let staged_path = unique_sibling_path(final_path, None)?;
Ok(Self {
final_path: final_path.to_path_buf(),
staged_path,
committed: AtomicBool::new(false),
})
}
#[must_use]
pub fn path(&self) -> &Path {
&self.staged_path
}
pub fn prepare(&self, size: u64, sparse: bool) -> io::Result<()> {
if let Some(parent) = self.staged_path.parent() {
make_dir_chain(parent)?;
}
let staged = OpenOptions::new()
.read(true)
.write(true)
.create_new(true)
.open(&self.staged_path)?;
let result = if sparse {
staged.set_len(size)
} else if size <= SMALL_PREALLOC_MAX {
Ok(())
} else {
fs::preallocate(&staged, size)
};
drop(staged);
result
}
pub fn open(&self) -> io::Result<File> {
OpenOptions::new()
.read(true)
.write(true)
.open(&self.staged_path)
}
pub fn commit(&self, fsync: bool, backup: bool) -> io::Result<()> {
if self.committed.load(Ordering::SeqCst) {
return Ok(());
}
if fsync {
if let Ok(file) = self.open() {
file.sync_all()?;
}
}
install_prepared_path(&self.staged_path, &self.final_path, backup)?;
self.committed.store(true, Ordering::SeqCst);
Ok(())
}
pub fn cleanup(&self) -> io::Result<()> {
match std::fs::remove_file(&self.staged_path) {
Ok(()) => Ok(()),
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()),
Err(error) => Err(error),
}
}
}
impl Drop for StagedFile {
fn drop(&mut self) {
if self.committed.load(Ordering::SeqCst) {
return;
}
let _ = self.cleanup();
}
}
pub(crate) fn make_dir_chain(dir: &Path) -> io::Result<()> {
if std::fs::symlink_metadata(dir).is_ok_and(|meta| meta.is_dir()) {
return Ok(());
}
let mut current = dir;
loop {
match std::fs::symlink_metadata(current) {
Ok(meta) if meta.is_dir() => break,
Ok(_) => {
super::fs::remove_file_any(current)?;
}
Err(error) if error.kind() == io::ErrorKind::NotFound => match current.parent() {
Some(parent) => current = parent,
None => break,
},
Err(error) => return Err(error),
}
}
std::fs::create_dir_all(dir)
}
fn install_prepared_path(prepared_path: &Path, final_path: &Path, backup: bool) -> io::Result<()> {
let mut replaced = match std::fs::symlink_metadata(final_path) {
Ok(meta) if meta.is_dir() => Some(move_aside(final_path)?),
Ok(_) => {
#[cfg(windows)]
clear_readonly_attribute(final_path)?;
if backup {
backup_existing(final_path)?;
}
None
}
Err(error) if error.kind() == io::ErrorKind::NotFound => None,
Err(error) => return Err(error),
};
match std::fs::rename(prepared_path, final_path) {
Ok(()) => {
if let Some((original, backup)) = replaced.take() {
let entry_count = std::fs::read_dir(&backup).map_or(0, std::iter::Iterator::count);
if entry_count > 0 {
tracing::warn!(
"replacing non-empty directory {original:?} with a file — {} entry(ies) removed",
entry_count
);
}
fs::remove_path_if_exists(&backup)?;
}
Ok(())
}
Err(error) => {
let _ = fs::remove_path_if_exists(prepared_path);
if let Some((original, backup)) = replaced {
let _ = restore_moved(&original, &backup);
}
Err(error)
}
}
}
#[cfg(windows)]
#[allow(clippy::permissions_set_readonly_false)]
fn clear_readonly_attribute(path: &Path) -> io::Result<()> {
let mut perms = std::fs::metadata(path)?.permissions();
if perms.readonly() {
perms.set_readonly(false);
std::fs::set_permissions(path, perms)?;
}
Ok(())
}
fn move_aside(path: &Path) -> io::Result<(PathBuf, PathBuf)> {
let backup = unique_sibling_path(path, Some("backup"))?;
std::fs::rename(path, &backup)?;
Ok((path.to_path_buf(), backup))
}
pub(crate) fn backup_existing(path: &Path) -> io::Result<()> {
let mut backup = path.as_os_str().to_os_string();
backup.push("~");
let backup = PathBuf::from(backup);
match std::fs::symlink_metadata(&backup) {
Ok(_) => std::fs::remove_file(&backup)?,
Err(error) if error.kind() == io::ErrorKind::NotFound => {}
Err(error) => return Err(error),
}
std::fs::rename(path, &backup)
}
fn restore_moved(original: &Path, backup: &Path) -> io::Result<()> {
let _ = fs::remove_path_if_exists(original);
std::fs::rename(backup, original)
}
fn unique_sibling_path(path: &Path, tag: Option<&str>) -> io::Result<PathBuf> {
let parent = path.parent().ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidInput, "path has no parent directory")
})?;
let file_name = path
.file_name()
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "path has no file name"))?;
let file_name = file_name.to_string_lossy();
loop {
let counter = STAGED_COUNTER.fetch_add(1, Ordering::Relaxed);
let name = match tag {
Some(tag) => {
format!(".{file_name}.cp2.{tag}.{pid}.{counter}.tmp", pid = std::process::id())
}
None => format!(".{file_name}.cp2.{pid}.{counter}.tmp", pid = std::process::id()),
};
let candidate = parent.join(name);
match std::fs::symlink_metadata(&candidate) {
Ok(_) => {}
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(candidate),
Err(error) => return Err(error),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn staged_commit_roundtrip() {
use std::io::Write;
let dir = tempfile::tempdir().unwrap();
let final_path = dir.path().join("out.bin");
let staged = StagedFile::new(&final_path).unwrap();
staged.prepare(8, false).unwrap();
{
let mut file = staged.open().unwrap();
file.write_all(b"abcdefgh").unwrap();
}
staged.commit(false, false).unwrap();
assert_eq!(std::fs::read(&final_path).unwrap(), b"abcdefgh");
}
#[test]
fn staged_commit_backup_keeps_original() {
use std::io::Write;
let dir = tempfile::tempdir().unwrap();
let final_path = dir.path().join("out.bin");
std::fs::write(&final_path, b"old").unwrap();
let staged = StagedFile::new(&final_path).unwrap();
staged.prepare(8, false).unwrap();
{
let mut file = staged.open().unwrap();
file.write_all(b"abcdefgh").unwrap();
}
staged.commit(false, true).unwrap();
assert_eq!(std::fs::read(&final_path).unwrap(), b"abcdefgh");
assert_eq!(std::fs::read(dir.path().join("out.bin~")).unwrap(), b"old");
}
#[test]
fn staged_commit_no_backup_replaces() {
use std::io::Write;
let dir = tempfile::tempdir().unwrap();
let final_path = dir.path().join("out.bin");
std::fs::write(&final_path, b"old").unwrap();
let staged = StagedFile::new(&final_path).unwrap();
staged.prepare(3, false).unwrap();
{
let mut file = staged.open().unwrap();
file.write_all(b"new").unwrap();
}
staged.commit(false, false).unwrap();
assert_eq!(std::fs::read(&final_path).unwrap(), b"new");
assert!(!dir.path().join("out.bin~").exists());
}
#[test]
fn staged_commit_backup_overwrites_stale_backup() {
use std::io::Write;
let dir = tempfile::tempdir().unwrap();
let final_path = dir.path().join("out.bin");
std::fs::write(&final_path, b"old").unwrap();
std::fs::write(dir.path().join("out.bin~"), b"stale").unwrap();
let staged = StagedFile::new(&final_path).unwrap();
staged.prepare(3, false).unwrap();
{
let mut file = staged.open().unwrap();
file.write_all(b"new").unwrap();
}
staged.commit(false, true).unwrap();
assert_eq!(std::fs::read(&final_path).unwrap(), b"new");
assert_eq!(std::fs::read(dir.path().join("out.bin~")).unwrap(), b"old");
}
#[test]
fn staged_drop_cleans_up() {
let dir = tempfile::tempdir().unwrap();
let final_path = dir.path().join("out.bin");
let staged = StagedFile::new(&final_path).unwrap();
staged.prepare(16, false).unwrap();
let tmp = staged.path().to_path_buf();
assert!(tmp.exists());
drop(staged);
assert!(!tmp.exists());
}
#[test]
fn staged_rejects_no_parent() {
assert!(StagedFile::new(Path::new("/")).is_err());
}
}