use std::fs::File;
use std::io::Result;
use std::path::Path;
#[inline]
pub(crate) fn protocol_process_id() -> u32 {
#[cfg(target_os = "wasi")]
{
0
}
#[cfg(not(target_os = "wasi"))]
{
std::process::id()
}
}
#[cfg(windows)]
mod publish {
use std::path::{Path, PathBuf};
use std::sync::Mutex;
static PENDING: Mutex<Vec<(PathBuf, PathBuf)>> = Mutex::new(Vec::new());
pub(super) fn temp_name_for(path: &Path) -> PathBuf {
use std::sync::atomic::{AtomicU64, Ordering};
static N: AtomicU64 = AtomicU64::new(0);
let n = N.fetch_add(1, Ordering::Relaxed);
let mut name = path.as_os_str().to_os_string();
name.push(format!(".publish-{}-{n}", std::process::id()));
PathBuf::from(name)
}
pub(super) fn register(path: &Path, temp: PathBuf) {
let mut pending = PENDING.lock().unwrap();
if !pending.iter().any(|(p, _)| p == path) {
pending.push((path.to_path_buf(), temp));
}
}
pub(super) fn pending_temp(path: &Path) -> Option<PathBuf> {
PENDING.lock().unwrap().iter().find(|(p, _)| p == path).map(|(_, t)| t.clone())
}
pub(super) fn resolve(path: &Path) -> PathBuf {
pending_temp(path).unwrap_or_else(|| path.to_path_buf())
}
pub(super) fn forget(path: &Path) -> Option<PathBuf> {
let mut pending = PENDING.lock().unwrap();
let at = pending.iter().position(|(p, _)| p == path)?;
Some(pending.remove(at).1)
}
pub(super) fn publish_dir(dir: &Path) -> std::io::Result<()> {
let due: Vec<(PathBuf, PathBuf)> = PENDING
.lock()
.unwrap()
.iter()
.filter(|(p, _)| p.parent().map(|d| same_dir(d, dir)).unwrap_or(false))
.cloned()
.collect();
for (path, temp) in due {
crate::sys::rename_noreplace(&temp, &path)?;
forget(&path);
}
Ok(())
}
fn same_dir(a: &Path, b: &Path) -> bool {
let norm =
|p: &Path| if p.as_os_str().is_empty() { PathBuf::from(".") } else { p.to_path_buf() };
norm(a) == norm(b)
}
}
#[cfg(feature = "dst")]
pub mod record {
use std::cell::{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,
},
RenameLagged {
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);
}
});
}
thread_local! {
static SYNC_FAULT: Cell<Option<usize>> = const { Cell::new(None) };
static SYNC_FIRED: Cell<Option<usize>> = const { Cell::new(None) };
static SYNC_ATTEMPTS: Cell<usize> = const { Cell::new(0) };
static WRITE_FAULT: Cell<Option<usize>> = const { Cell::new(None) };
static WRITE_FIRED: Cell<Option<usize>> = const { Cell::new(None) };
static WRITE_ATTEMPTS: Cell<usize> = const { Cell::new(0) };
}
#[derive(Debug)]
pub struct InjectedSyncFailure {
pub attempt: usize,
}
impl std::fmt::Display for InjectedSyncFailure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "injected sync failure at attempted sync {}", self.attempt)
}
}
impl std::error::Error for InjectedSyncFailure {}
#[derive(Debug)]
pub struct InjectedWriteFailure {
pub attempt: usize,
}
impl std::fmt::Display for InjectedWriteFailure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "injected write failure at attempted write {}", self.attempt)
}
}
impl std::error::Error for InjectedWriteFailure {}
pub struct SyncFault {
_not_send: std::marker::PhantomData<std::rc::Rc<()>>,
}
pub struct WriteFault {
_not_send: std::marker::PhantomData<std::rc::Rc<()>>,
}
impl WriteFault {
pub fn fired_at(&self) -> Option<usize> {
WRITE_FIRED.with(|f| f.get())
}
}
impl Drop for WriteFault {
fn drop(&mut self) {
WRITE_FAULT.with(|f| f.set(None));
}
}
impl SyncFault {
pub fn fired_at(&self) -> Option<usize> {
SYNC_FIRED.with(|f| f.get())
}
pub fn attempts(&self) -> usize {
SYNC_ATTEMPTS.with(|a| a.get())
}
}
impl Drop for SyncFault {
fn drop(&mut self) {
SYNC_FAULT.with(|f| f.set(None));
}
}
pub fn fail_sync_after(n: usize) -> SyncFault {
SYNC_FAULT.with(|f| f.set(Some(n)));
SYNC_FIRED.with(|f| f.set(None));
SYNC_ATTEMPTS.with(|a| a.set(0));
SyncFault { _not_send: std::marker::PhantomData }
}
pub fn fail_write_after(n: usize) -> WriteFault {
WRITE_FAULT.with(|f| f.set(Some(n)));
WRITE_FIRED.with(|f| f.set(None));
WRITE_ATTEMPTS.with(|a| a.set(0));
WriteFault { _not_send: std::marker::PhantomData }
}
pub(super) fn write_fault() -> std::io::Result<()> {
let attempt = WRITE_ATTEMPTS.with(|a| {
let n = a.get();
a.set(n + 1);
n
});
WRITE_FAULT.with(|f| match f.get() {
None => Ok(()),
Some(0) => {
f.set(None);
WRITE_FIRED.with(|x| x.set(Some(attempt)));
Err(std::io::Error::other(InjectedWriteFailure { attempt }))
}
Some(left) => {
f.set(Some(left - 1));
Ok(())
}
})
}
pub(super) fn sync_fault() -> std::io::Result<()> {
let attempt = SYNC_ATTEMPTS.with(|a| {
let n = a.get();
a.set(n + 1);
n
});
SYNC_FAULT.with(|f| match f.get() {
None => Ok(()),
Some(0) => {
f.set(None);
SYNC_FIRED.with(|x| x.set(Some(attempt)));
Err(std::io::Error::other(InjectedSyncFailure { attempt }))
}
Some(left) => {
f.set(Some(left - 1));
Ok(())
}
})
}
pub fn len() -> usize {
LOG.with(|l| l.borrow().as_ref().map_or(0, |v| v.len()))
}
}
#[cfg(feature = "dst")]
use record::{push, Op};
pub(crate) trait ArtifactSink {
fn write_all_at(&mut self, data: &[u8], off: u64) -> std::io::Result<()>;
fn sync(&mut self) -> std::io::Result<()>;
fn describe(&self) -> String;
}
#[inline]
pub(crate) fn open_rw(path: &Path) -> Result<File> {
#[cfg(windows)]
let path = &publish::resolve(path);
std::fs::OpenOptions::new().read(true).write(true).truncate(false).open(path)
}
#[inline]
pub(crate) fn open_read(path: &Path) -> Result<File> {
#[cfg(windows)]
let path = &publish::resolve(path);
File::open(path)
}
#[inline]
pub(crate) fn exists(path: &Path) -> bool {
#[cfg(windows)]
let path = &publish::resolve(path);
path.exists()
}
#[inline]
pub(crate) fn create(path: &Path) -> Result<File> {
#[cfg(not(windows))]
let f = File::create(path)?;
#[cfg(windows)]
let f = if let Some(temp) = publish::pending_temp(path) {
File::create(&temp)?
} else if path.exists() {
File::create(path)?
} else {
let temp = publish::temp_name_for(path);
let f = File::create(&temp)?;
publish::register(path, temp);
f
};
#[cfg(feature = "dst")]
push(Op::Create { path: path.to_path_buf() });
Ok(f)
}
#[inline]
pub(crate) fn read_file(path: &Path) -> Result<Vec<u8>> {
#[cfg(windows)]
let path = &publish::resolve(path);
std::fs::read(path)
}
#[inline]
pub(crate) fn open_or_create(path: &Path) -> Result<(File, bool)> {
let existed = exists(path);
let open = |p: &Path| {
std::fs::OpenOptions::new().create(true).truncate(false).read(true).write(true).open(p)
};
#[cfg(not(windows))]
let f = open(path)?;
#[cfg(windows)]
let f = if existed {
open(&publish::resolve(path))?
} else {
let temp = publish::temp_name_for(path);
let f = open(&temp)?;
publish::register(path, temp);
f
};
#[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 open =
|p: &Path| std::fs::OpenOptions::new().create_new(true).write(true).read(true).open(p);
#[cfg(not(windows))]
let f = open(path)?;
#[cfg(windows)]
let f = {
if exists(path) {
return Err(std::io::Error::from(std::io::ErrorKind::AlreadyExists));
}
let temp = publish::temp_name_for(path);
let f = open(&temp)?;
publish::register(path, temp);
f
};
#[cfg(feature = "dst")]
push(Op::Create { path: path.to_path_buf() });
Ok(f)
}
pub(crate) fn create_new_staging(path: &Path) -> Result<File> {
let file = 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(file)
}
pub(crate) fn create_numbered_staging(
final_path: &Path,
operation: &str,
) -> Result<(std::path::PathBuf, File)> {
use std::sync::atomic::{AtomicU64, Ordering};
static NEXT: AtomicU64 = AtomicU64::new(0);
loop {
let serial = NEXT.fetch_add(1, Ordering::Relaxed);
let mut name = final_path.as_os_str().to_os_string();
name.push(format!(".{operation}-{}-{serial}", protocol_process_id()));
let path = std::path::PathBuf::from(name);
match create_new_staging(&path) {
Ok(file) => {
return Ok((path, file));
}
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => continue,
Err(error) => return Err(error),
}
}
}
#[inline]
pub(crate) fn write_all_at(f: &File, path: &Path, buf: &[u8], off: u64) -> Result<()> {
#[cfg(feature = "dst")]
record::write_fault()?;
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<()> {
#[cfg(not(windows))]
std::fs::write(path, data)?;
#[cfg(windows)]
{
if let Some(temp) = publish::pending_temp(path) {
std::fs::write(&temp, data)?;
} else if path.exists() {
std::fs::write(path, data)?;
} else {
let temp = publish::temp_name_for(path);
std::fs::write(&temp, data)?;
publish::register(path, temp);
}
}
#[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<()> {
#[cfg(feature = "dst")]
record::sync_fault()?;
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<()> {
let dir = if dir.as_os_str().is_empty() { Path::new(".") } else { dir };
#[cfg(feature = "dst")]
record::sync_fault()?;
#[cfg(windows)]
publish::publish_dir(dir)?;
crate::sys::sync_dir(dir)?;
#[cfg(feature = "dst")]
push(Op::SyncDir { path: dir.to_path_buf() });
Ok(())
}
#[inline]
pub(crate) fn rename(from: &Path, to: &Path) -> Result<()> {
#[cfg(windows)]
let physical = publish::resolve(from);
#[cfg(windows)]
let from_physical: &Path = &physical;
#[cfg(not(windows))]
let from_physical: &Path = from;
crate::sys::rename(from_physical, to)?;
#[cfg(windows)]
publish::forget(from);
#[cfg(feature = "dst")]
push(Op::Rename { from: from.to_path_buf(), to: to.to_path_buf() });
Ok(())
}
#[inline]
pub(crate) fn rename_replace_open(from: &Path, to: &Path) -> Result<()> {
#[cfg(windows)]
let physical = publish::resolve(from);
#[cfg(windows)]
let from_physical: &Path = &physical;
#[cfg(not(windows))]
let from_physical: &Path = from;
crate::sys::rename(from_physical, to)?;
#[cfg(windows)]
publish::forget(from);
#[cfg(feature = "dst")]
push(Op::RenameLagged { from: from.to_path_buf(), to: to.to_path_buf() });
Ok(())
}
#[inline]
pub(crate) fn rename_noreplace(from: &Path, to: &Path) -> Result<()> {
#[cfg(windows)]
let physical = publish::resolve(from);
#[cfg(windows)]
let from_physical: &Path = &physical;
#[cfg(not(windows))]
let from_physical: &Path = from;
crate::sys::rename_noreplace(from_physical, to)?;
#[cfg(windows)]
publish::forget(from);
#[cfg(feature = "dst")]
push(Op::Rename { from: from.to_path_buf(), to: to.to_path_buf() });
Ok(())
}
#[inline]
pub(crate) fn unlink(path: &Path) -> Result<()> {
#[cfg(windows)]
let physical = publish::resolve(path);
#[cfg(windows)]
let path_physical: &Path = &physical;
#[cfg(not(windows))]
let path_physical: &Path = path;
std::fs::remove_file(path_physical)?;
#[cfg(windows)]
publish::forget(path);
#[cfg(feature = "dst")]
push(Op::Unlink { path: path.to_path_buf() });
Ok(())
}
#[inline]
pub(crate) fn unlink_if_exists(path: &Path) -> Result<()> {
match unlink(path) {
Ok(()) => Ok(()),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(error) => Err(error),
}
}
#[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 remove_tree(path: &Path) -> Result<()> {
std::fs::remove_dir_all(path)?;
#[cfg(feature = "dst")]
push(Op::RemoveTree { path: path.to_path_buf() });
Ok(())
}
#[cfg(all(test, windows))]
mod windows_publish_tests {
use super::*;
fn scratch(tag: &str) -> std::path::PathBuf {
let d = std::env::temp_dir().join(format!("turndb-publish-{tag}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&d);
std::fs::create_dir_all(&d).unwrap();
d
}
fn temps_beside(final_name: &Path) -> Vec<std::path::PathBuf> {
let stem = final_name.file_name().unwrap().to_string_lossy().to_string();
let mut v: Vec<_> = std::fs::read_dir(final_name.parent().unwrap())
.unwrap()
.flatten()
.map(|e| e.path())
.filter(|p| {
let n = p.file_name().unwrap().to_string_lossy();
n.starts_with(&format!("{stem}.publish-"))
})
.collect();
v.sort();
v
}
#[test]
fn an_unpublished_name_is_invisible_to_a_fresh_process_and_its_temp_is_garbage() {
let d = scratch("unpublished");
let name = d.join("part-00000001.part");
let f = create(&name).unwrap();
write_all_at(&f, &name, b"payload", 0).unwrap();
sync_file(&f, &name).unwrap();
assert!(exists(&name));
assert_eq!(std::io::Read::bytes(open_read(&name).unwrap()).count(), 7);
assert_eq!(temps_beside(&name).len(), 1, "the bytes live under one temp name");
publish::forget(&name);
assert!(!name.exists(), "the final name was never published");
assert!(!exists(&name), "and nothing resolves to the temp any more");
assert!(open_read(&name).is_err(), "a fresh process cannot read it by name");
assert_eq!(temps_beside(&name).len(), 1, "the temp is debris, still there, never promoted");
drop(f);
let _ = std::fs::remove_dir_all(&d);
}
#[test]
fn a_stale_temp_beside_a_valid_final_is_never_consulted_and_a_publish_removes_nothing_else() {
let d = scratch("stale");
let name = d.join("part-00000001.part");
std::fs::write(d.join("part-00000001.part.publish-1-0"), b"stale one").unwrap();
std::fs::write(d.join("part-00000001.part.publish-1-1"), b"st").unwrap();
let f = create(&name).unwrap();
write_all_at(&f, &name, b"the real bytes", 0).unwrap();
sync_file(&f, &name).unwrap();
sync_dir(&d).unwrap();
assert_eq!(std::fs::read(&name).unwrap(), b"the real bytes");
assert_eq!(
temps_beside(&name).len(),
2,
"stale temps are untouched by an unrelated publish"
);
assert_eq!(std::io::Read::bytes(open_read(&name).unwrap()).count(), 14);
assert!(publish::forget(&name).is_none(), "nothing pending after a publish");
assert!(name.exists());
drop(f);
let _ = std::fs::remove_dir_all(&d);
}
#[test]
fn publishing_follows_registration_order_not_lexical_order() {
let d = scratch("order");
let b = d.join("b.part");
let a = d.join("a.part");
let fb = create(&b).unwrap();
let fa = create(&a).unwrap();
sync_file(&fb, &b).unwrap();
sync_file(&fa, &a).unwrap();
std::fs::create_dir(&a).unwrap(); let err = sync_dir(&d).expect_err("a's publish is blocked");
assert!(err.raw_os_error().is_some(), "{err}");
assert!(b.is_file(), "b, registered first, was published before a failed");
assert_eq!(temps_beside(&b).len(), 0);
assert_eq!(temps_beside(&a).len(), 1, "a is still pending");
drop((fa, fb));
publish::forget(&a);
let _ = std::fs::remove_dir_all(&d);
}
#[test]
fn create_truncates_an_existing_final_in_place_and_a_pending_name_at_its_one_temp() {
let d = scratch("semantics");
let existing = d.join("existing.part");
std::fs::write(&existing, b"old bytes here").unwrap();
let f = create(&existing).unwrap();
drop(f);
assert_eq!(std::fs::read(&existing).unwrap(), b"", "truncated in place");
assert!(temps_beside(&existing).is_empty(), "no temp for a name that already exists");
assert!(create_new(&existing).is_err());
let fresh = d.join("fresh.part");
let f1 = create(&fresh).unwrap();
write_all_at(&f1, &fresh, b"first", 0).unwrap();
drop(f1);
let f2 = create(&fresh).unwrap();
drop(f2);
assert_eq!(temps_beside(&fresh).len(), 1, "a repeated create truncates the same temp");
assert_eq!(read_file(&fresh).unwrap(), b"", "and it is truncated");
assert!(create_new(&fresh).is_err(), "pending counts as existing for create_new");
write_file(&fresh, b"via write_file").unwrap();
assert_eq!(read_file(&fresh).unwrap(), b"via write_file");
assert_eq!(temps_beside(&fresh).len(), 1);
write_file(&existing, b"rewritten").unwrap();
assert_eq!(std::fs::read(&existing).unwrap(), b"rewritten");
assert!(temps_beside(&existing).is_empty());
sync_dir(&d).unwrap();
assert_eq!(std::fs::read(&fresh).unwrap(), b"via write_file");
assert!(temps_beside(&fresh).is_empty());
let _ = std::fs::remove_dir_all(&d);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn syncing_the_empty_parent_of_a_bare_name_syncs_the_current_directory() {
let parent = Path::new("mystore.turndb").parent().unwrap();
assert!(parent.as_os_str().is_empty());
sync_dir(parent).expect("an empty parent is the current directory, not ENOENT");
}
}