Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Fix tempfile naming in download manager (#195)
Browse files Browse the repository at this point in the history
When multiple files with same name, but different extension are
submitted for download concurrently, there was a conflict with
the filename.

This change fixes by adding ".tmp" to filename instead of replacing
the whole extension.
  • Loading branch information
tuommaki authored May 22, 2024
1 parent 3ec31e9 commit 8134615
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions crates/node/src/mempool/txvalidation/download_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ pub async fn download_asset_file(
// Create a tmp file during download.
// This way the file won't be available for download from the other nodes
// until it is completely written.
let mut tmp_file_path = file_path.clone();
tmp_file_path.set_extension("tmp");
let tmp_file_path = tmp_file_name(&file_path);

let fd = tokio::fs::File::create(&tmp_file_path).await?;
let mut fd = tokio::io::BufWriter::new(fd);

Expand Down Expand Up @@ -202,13 +202,13 @@ async fn server_process_file(
) -> std::result::Result<Response<BoxBody<Bytes, std::io::Error>>, hyper::Error> {
let file_digest = &req.uri().path()[1..];

let mut file_path = data_directory.join(file_digest);
let file_path = data_directory.join(file_digest);

let file = match tokio::fs::File::open(&file_path).await {
Ok(file) => file,
Err(_) => {
// Try to see if the file is currently being updated.
file_path.set_extension("tmp");
let file_path = tmp_file_name(&file_path);
let (status_code, message) = if file_path.as_path().exists() {
(
StatusCode::PARTIAL_CONTENT,
Expand All @@ -232,3 +232,12 @@ async fn server_process_file(
.body(BodyExt::boxed(stream_body))
.unwrap())
}

fn tmp_file_name(file_path: &Path) -> PathBuf {
let mut tmp_file_path = file_path.to_path_buf();
match tmp_file_path.extension() {
Some(ext) => tmp_file_path.set_extension(format!("{}.{}", ext.to_string_lossy(), "tmp")),
None => tmp_file_path.set_extension("tmp"),
};
tmp_file_path
}

0 comments on commit 8134615

Please sign in to comment.