Skip to content

Commit

Permalink
write record atomically
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv committed Feb 13, 2025
1 parent 968e251 commit 9730f07
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/rattler_conda_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ serde-untagged = { workspace = true }
serde_yaml = { workspace = true }
smallvec = { workspace = true, features = ["serde", "const_new", "const_generics", "union"] }
strum = { workspace = true, features = ["derive"] }
tempfile = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }
typed-path = { workspace = true }
Expand All @@ -50,7 +51,6 @@ fs-err = { workspace = true }
rand = { workspace = true }
insta = { workspace = true, features = ["yaml", "redactions", "toml", "glob", "filters"] }
rattler_package_streaming = { path = "../rattler_package_streaming", default-features = false, features = ["rustls-tls"] }
tempfile = { workspace = true }
rstest = { workspace = true }
assert_matches = { workspace = true }
hex-literal = { workspace = true }
Expand Down
38 changes: 36 additions & 2 deletions crates/rattler_conda_types/src/prefix_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use serde_with::serde_as;
use std::io::{BufWriter, Read};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use tempfile::NamedTempFile;

#[cfg(feature = "rayon")]
use rayon::prelude::*;
Expand Down Expand Up @@ -234,8 +235,41 @@ impl PrefixRecord {
path: impl AsRef<Path>,
pretty: bool,
) -> Result<(), std::io::Error> {
let file = File::create(path.as_ref())?;
self.write_to(BufWriter::with_capacity(50 * 1024, file), pretty)
const BUFFER_SIZE: usize = 64 * 1024;

let path = path.as_ref();
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;

// Use a temporary file in the same directory for atomic writes
let temp_file =
NamedTempFile::with_prefix_in("prefix_record_", parent).map_err(|e| {
std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to create temporary file: {}", e),
)
})?;

// Write to temp file with buffered writer
let writer = BufWriter::with_capacity(BUFFER_SIZE, &temp_file);
self.write_to(writer, pretty)?;

// Ensure all data is written before persisting
temp_file.as_file().sync_all()?;

// Atomically rename the temp file to the target path
temp_file.persist(path).map_err(|e| {
std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to persist file: {}", e),
)
})?;
} else {
let file = File::create(path)?;
let writer = BufWriter::with_capacity(BUFFER_SIZE, file);
self.write_to(writer, pretty)?;
}
Ok(())
}

/// Writes the contents of this instance to the file at the specified location.
Expand Down

0 comments on commit 9730f07

Please sign in to comment.