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

Commit

Permalink
Extract tmp_file_name function + fix file serve as well
Browse files Browse the repository at this point in the history
  • Loading branch information
tuommaki committed May 22, 2024
1 parent 4563c63 commit 9287c72
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions crates/node/src/mempool/txvalidation/download_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,7 @@ 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();
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"),
};
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 @@ -208,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 @@ -238,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 9287c72

Please sign in to comment.