Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/viport-loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
76 changes: 76 additions & 0 deletions crates/viport-loader/src/publish.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}
9 changes: 2 additions & 7 deletions crates/viport-loader/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down