diff --git a/crates/viport-loader/src/lib.rs b/crates/viport-loader/src/lib.rs index 26f60b23..f2be0aaa 100644 --- a/crates/viport-loader/src/lib.rs +++ b/crates/viport-loader/src/lib.rs @@ -24,6 +24,7 @@ #[cfg(windows)] mod crash; pub mod prepare; +pub(crate) mod publish; pub mod resolve; #[cfg(all(windows, feature = "diagnostics"))] mod stacks; diff --git a/crates/viport-loader/src/publish.rs b/crates/viport-loader/src/publish.rs new file mode 100644 index 00000000..b1f7896c --- /dev/null +++ b/crates/viport-loader/src/publish.rs @@ -0,0 +1,76 @@ +//! Portable publication of derived files into a shared cache directory. +//! +//! Several copies of the runner can prepare the same title at once. Each +//! writer stages into its own file, so two of them cannot truncate each +//! other's bytes or rename a file the other already published. The final +//! rename is atomic within the directory, and the staging file is removed +//! if publication fails. + +use std::path::Path; +use std::sync::atomic::{AtomicU64, Ordering}; + +static NEXT_STAGING: AtomicU64 = AtomicU64::new(0); + +/// Writes `bytes` to `path` unless it is already there, using a staging file +/// unique to this process and call. +pub fn write_if_changed(path: &Path, bytes: &[u8]) -> std::io::Result<()> { + if std::fs::read(path).is_ok_and(|existing| existing == bytes) { + return Ok(()); + } + let name = path + .file_name() + .and_then(|name| name.to_str()) + .unwrap_or("image"); + let sequence = NEXT_STAGING.fetch_add(1, Ordering::Relaxed); + let staging = path.with_file_name(format!( + ".{name}.{}.{sequence}.tmp", + std::process::id() + )); + std::fs::write(&staging, bytes)?; + if let Err(error) = std::fs::rename(&staging, path) { + let _ = std::fs::remove_file(&staging); + return Err(error); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Two publishers staging the same destination at once both succeed, + /// and neither leaves a staging file behind. + #[test] + fn concurrent_publishers_do_not_share_a_staging_file() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("image.bin"); + let bytes = b"a prepared image".to_vec(); + let (first, second) = (path.clone(), path.clone()); + let (first_bytes, second_bytes) = (bytes.clone(), bytes.clone()); + let (one, two) = ( + std::thread::spawn(move || write_if_changed(&first, &first_bytes)), + std::thread::spawn(move || write_if_changed(&second, &second_bytes)), + ); + one.join().unwrap().unwrap(); + two.join().unwrap().unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), bytes); + let leftovers: Vec<_> = std::fs::read_dir(dir.path()) + .unwrap() + .filter_map(|entry| entry.ok()) + .filter(|entry| entry.path() != path) + .collect(); + assert!(leftovers.is_empty(), "staging files were removed: {leftovers:?}"); + } + + #[test] + fn a_failed_publication_removes_its_staging_file() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("missing-parent").join("image.bin"); + assert!(write_if_changed(&path, b"bytes").is_err()); + let leftovers: Vec<_> = std::fs::read_dir(dir.path()) + .unwrap() + .filter_map(|entry| entry.ok()) + .collect(); + assert!(leftovers.is_empty(), "no staging file survives an error"); + } +} diff --git a/crates/viport-loader/src/windows.rs b/crates/viport-loader/src/windows.rs index cba63bba..a62674e5 100644 --- a/crates/viport-loader/src/windows.rs +++ b/crates/viport-loader/src/windows.rs @@ -193,13 +193,8 @@ impl ImportResolver for WithTitleDlls<'_> { } fn write_if_changed(path: &Path, bytes: &[u8]) -> Result<()> { - if std::fs::read(path).is_ok_and(|existing| existing == bytes) { - return Ok(()); - } - let tmp = path.with_extension("tmp"); - std::fs::write(&tmp, bytes).with_context(|| format!("writing {}", tmp.display()))?; - std::fs::rename(&tmp, path).with_context(|| format!("renaming to {}", path.display()))?; - Ok(()) + crate::publish::write_if_changed(path, bytes) + .with_context(|| format!("publishing {}", path.display())) } impl LoadedImage {