Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Marek <mail@marek.onl>
  • Loading branch information
arya2 and upbqdn authored Aug 28, 2024
1 parent 483ffd3 commit 4641734
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
9 changes: 3 additions & 6 deletions zebra-chain/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn default_cache_dir() -> PathBuf {
/// # Panics
///
/// If the provided `file_path` is a directory path.
pub fn atomic_write_to_tmp_file(
pub fn atomic_write(
file_path: PathBuf,
data: &[u8],
) -> io::Result<Result<PathBuf, PersistError<fs::File>>> {
Expand Down Expand Up @@ -60,15 +60,12 @@ pub fn atomic_write_to_tmp_file(
.prefix(&tmp_file_prefix)
.tempfile_in(file_dir)?;

// Write data to the file asynchronously, by extracting the inner file, using it,
// then combining it back into a type that will correctly drop the file on error.
tmp_file.write_all(data)?;

// Atomically replace the current file with the temporary file.
// Do blocking filesystem operations on a dedicated thread.
// Atomically write the temp file to `file_path`.
let persist_result = tmp_file
.persist(&file_path)
// Drops the temp file and returns the file path if needed.
// Drops the temp file and returns the file path.
.map(|_| file_path);
Ok(persist_result)
}
10 changes: 5 additions & 5 deletions zebra-network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tokio::fs;

use tracing::Span;
use zebra_chain::{
common::atomic_write_to_tmp_file,
common::atomic_write,
parameters::{
testnet::{self, ConfiguredActivationHeights, ConfiguredFundingStreams},
Magic, Network, NetworkKind,
Expand Down Expand Up @@ -502,14 +502,14 @@ impl Config {
// Make a newline-separated list
let peer_data = peer_list.join("\n");

// Write to a temporary file, so the cache is not corrupted if Zebra shuts down or crashes
// at the same time.
// Write the peer cache file atomically so the cache is not corrupted if Zebra shuts down
// or crashes.
let span = Span::current();
let write_result = tokio::task::spawn_blocking(move || {
span.in_scope(move || atomic_write_to_tmp_file(peer_cache_file, peer_data.as_bytes()))
span.in_scope(move || atomic_write(peer_cache_file, peer_data.as_bytes()))
})
.await
.expect("unexpected panic creating temporary file")?;
.expect("could not write the peer cache file")?;

match write_result {
Ok(peer_cache_file) => {
Expand Down
8 changes: 4 additions & 4 deletions zebra-state/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ pub(crate) use hidden::{
pub(crate) mod hidden {
#![allow(dead_code)]

use zebra_chain::common::atomic_write_to_tmp_file;
use zebra_chain::common::atomic_write;

use super::*;

Expand Down Expand Up @@ -510,9 +510,9 @@ pub(crate) mod hidden {

let version = format!("{}.{}", changed_version.minor, changed_version.patch);

// Write to a temporary file, so the cache is not corrupted if Zebra shuts down or crashes
// at the same time.
atomic_write_to_tmp_file(version_path, version.as_bytes())??;
// Write the version file atomically so the cache is not corrupted if Zebra shuts down or
// crashes.
atomic_write(version_path, version.as_bytes())??;

Ok(())
}
Expand Down

0 comments on commit 4641734

Please sign in to comment.