Skip to content

Commit

Permalink
fix(bootstrap): use rename instead of persist for atomic write
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed Dec 5, 2024
1 parent d8f3ac7 commit f2dc37f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion ant-bootstrap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ clap = { version = "4.2.1", features = ["derive", "env"] }
dirs-next = "~2.0.0"
futures = "0.3.30"
libp2p = { version = "0.54.1", features = ["serde"] }
rand = { version = "~0.8.5", features = ["small_rng"] }
reqwest = { version = "0.12.2", default-features = false, features = [
"rustls-tls-manual-roots",
] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tempfile = "3.8.1"
thiserror = "1.0"
tokio = { version = "1.0", features = ["time"] }
tracing = "0.1"
Expand All @@ -39,6 +39,7 @@ fs2 = "0.4.3"
wiremock = "0.5"
tokio = { version = "1.0", features = ["full", "test-util"] }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tempfile = "3.8.1"

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasmtimer = "0.2.0"
27 changes: 21 additions & 6 deletions ant-bootstrap/src/cache_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ use crate::{
use fs2::FileExt;
use libp2p::multiaddr::Protocol;
use libp2p::{Multiaddr, PeerId};
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use serde::{Deserialize, Serialize};
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fs::{self, File, OpenOptions};
use std::io::Read;
use std::path::PathBuf;
use std::time::{Duration, SystemTime};
use tempfile::NamedTempFile;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheData {
Expand Down Expand Up @@ -440,13 +441,27 @@ impl BootstrapCacheStore {
fs::create_dir_all(parent)?;
}

// Create a temporary file in the same directory as the cache file
let temp_dir = std::env::temp_dir();
let temp_file = NamedTempFile::new_in(&temp_dir)?;
// create a random char string for the temp file name

let mut rng = StdRng::from_entropy();
let random_string: String = (0..7)
.map(|_| rng.sample(rand::distributions::Alphanumeric) as char)
.collect();
let temp_file_path = self
.cache_path
.parent()
.ok_or(Error::CouldNotObtainDataDir)?
.join(format!(".cache-{random_string}.tmp"));

let temp_file = File::create(&temp_file_path)?;

// Write data to temporary file
serde_json::to_writer_pretty(&temp_file, &self.data)?;

// Ensure the temporary file is closed before renaming
temp_file.sync_all()?;
drop(temp_file); // Explicitly close the file

// Open the target file with proper permissions
let file = OpenOptions::new()
.write(true)
Expand All @@ -458,8 +473,8 @@ impl BootstrapCacheStore {
Self::acquire_exclusive_lock(&file).await?;

// Perform atomic rename
temp_file.persist(&self.cache_path).inspect_err(|err| {
error!("Failed to persist file with err: {err:?}");
fs::rename(&temp_file_path, &self.cache_path).inspect_err(|err| {
error!("Failed to rename file with err: {err:?}");
})?;

info!("Cache written to disk: {:?}", self.cache_path);

Check notice

Code scanning / devskim

Accessing localhost could indicate debug code, or could hinder scaling. Note

Do not leave debug code in production
Expand Down
2 changes: 0 additions & 2 deletions ant-bootstrap/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ pub enum Error {
Json(#[from] serde_json::Error),
#[error("HTTP error: {0}")]
Http(#[from] reqwest::Error),
#[error("Persist error: {0}")]
Persist(#[from] tempfile::PersistError),
#[error("Lock error")]
LockError,
}
Expand Down

0 comments on commit f2dc37f

Please sign in to comment.