summaryrefslogtreecommitdiff
path: root/candle-hub/src/api.rs
diff options
context:
space:
mode:
authorNicolas Patry <patry.nicolas@protonmail.com>2023-06-28 16:42:26 +0200
committerNicolas Patry <patry.nicolas@protonmail.com>2023-06-28 16:42:26 +0200
commitcfdfc04d5cd2203060691cc787d060fb5a1380c6 (patch)
treefb677fd675230394b27e1a874a66f7d58f4740f5 /candle-hub/src/api.rs
parent9c86e4afa8c90ceb342f1062d143a9fe68c56d1d (diff)
downloadcandle-cfdfc04d5cd2203060691cc787d060fb5a1380c6.tar.gz
candle-cfdfc04d5cd2203060691cc787d060fb5a1380c6.tar.bz2
candle-cfdfc04d5cd2203060691cc787d060fb5a1380c6.zip
Remove the unecessary file lock, attempt to rename before copying.
Diffstat (limited to 'candle-hub/src/api.rs')
-rw-r--r--candle-hub/src/api.rs15
1 files changed, 6 insertions, 9 deletions
diff --git a/candle-hub/src/api.rs b/candle-hub/src/api.rs
index 9ea014ec..f4d9c1ec 100644
--- a/candle-hub/src/api.rs
+++ b/candle-hub/src/api.rs
@@ -1,5 +1,4 @@
use crate::{Cache, Repo};
-use fs2::FileExt;
use indicatif::{ProgressBar, ProgressStyle};
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use reqwest::{
@@ -492,13 +491,6 @@ impl Api {
let blob_path = self.cache.blob_path(repo, &metadata.etag);
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 progress = ProgressBar::new(metadata.size as u64);
progress.set_style(
@@ -522,7 +514,12 @@ impl Api {
let tmp_filename = self
.download_tempfile(&url, metadata.size, progressbar)
.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);
pointer_path.push(filename);