Remove the unecessary file lock, attempt to rename before copying.

This commit is contained in:
Nicolas Patry
2023-06-28 16:42:26 +02:00
parent 9c86e4afa8
commit cfdfc04d5c
2 changed files with 6 additions and 10 deletions

View File

@ -7,7 +7,6 @@ edition = "2021"
[dependencies] [dependencies]
dirs = "5.0.1" dirs = "5.0.1"
fs2 = "0.4.3"
rand = "0.8.5" rand = "0.8.5"
thiserror = "1.0.40" thiserror = "1.0.40"
futures = { version = "0.3.28", optional = true } futures = { version = "0.3.28", optional = true }

View File

@ -1,5 +1,4 @@
use crate::{Cache, Repo}; use crate::{Cache, Repo};
use fs2::FileExt;
use indicatif::{ProgressBar, ProgressStyle}; use indicatif::{ProgressBar, ProgressStyle};
use rand::{distributions::Alphanumeric, thread_rng, Rng}; use rand::{distributions::Alphanumeric, thread_rng, Rng};
use reqwest::{ use reqwest::{
@ -492,13 +491,6 @@ impl Api {
let blob_path = self.cache.blob_path(repo, &metadata.etag); let blob_path = self.cache.blob_path(repo, &metadata.etag);
std::fs::create_dir_all(blob_path.parent().unwrap())?; std::fs::create_dir_all(blob_path.parent().unwrap())?;
let file1 = std::fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&blob_path)?;
file1.lock_exclusive()?;
let progressbar = if self.progress { let progressbar = if self.progress {
let progress = ProgressBar::new(metadata.size as u64); let progress = ProgressBar::new(metadata.size as u64);
progress.set_style( progress.set_style(
@ -522,7 +514,12 @@ impl Api {
let tmp_filename = self let tmp_filename = self
.download_tempfile(&url, metadata.size, progressbar) .download_tempfile(&url, metadata.size, progressbar)
.await?; .await?;
tokio::fs::copy(tmp_filename, &blob_path).await?;
if tokio::fs::rename(&tmp_filename, &blob_path).await.is_err() {
// Renaming may fail if locations are different mount points
std::fs::File::create(&blob_path)?;
tokio::fs::copy(tmp_filename, &blob_path).await?;
}
let mut pointer_path = self.cache.pointer_path(repo, &metadata.commit_hash); let mut pointer_path = self.cache.pointer_path(repo, &metadata.commit_hash);
pointer_path.push(filename); pointer_path.push(filename);