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

Fix tempfile naming in download manager #195

Merged
merged 2 commits into from
May 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading