Skip to content

Commit

Permalink
[backup] metadata cache: download to tmp file then rename
Browse files Browse the repository at this point in the history
so that if error happens we don't end up with a stale (probably empty) file cached.

Closes: diem#7807
  • Loading branch information
msmouse authored and bors-libra committed Mar 8, 2021
1 parent e568d43 commit 763c894
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion storage/backup/backup-cli/src/metadata/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ pub async fn sync_and_load(
async move {
let file_handle = fh_by_h_ref.get(*h).expect("In map.");
let local_file = cache_dir_ref.join(*h);
let local_tmp_file = cache_dir_ref.join(format!(".{}", *h));
// download to tmp file ".xxxxxx"
tokio::io::copy(
&mut storage_ref
.open_for_read(file_handle)
Expand All @@ -118,11 +120,14 @@ pub async fn sync_and_load(
&mut OpenOptions::new()
.write(true)
.create_new(true)
.open(&local_file)
.open(&local_tmp_file)
.await
.err_notes(&local_file)?,
)
.await?;
// rename to target file only if successful; stale tmp file caused by failure will be
// reclaimed on next run
tokio::fs::rename(local_tmp_file, local_file).await?;
NUM_META_DOWNLOAD.inc();
Ok(())
}
Expand Down

0 comments on commit 763c894

Please sign in to comment.