mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-05-05 03:30:03 +00:00
Use ScopeGuard
to auto remove file on failure
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
80706dc3c4
commit
c7965ceb4f
1 changed files with 7 additions and 0 deletions
|
@ -10,6 +10,7 @@ use flate2::read::GzDecoder;
|
||||||
use futures_util::stream::StreamExt;
|
use futures_util::stream::StreamExt;
|
||||||
use log::{debug, info};
|
use log::{debug, info};
|
||||||
use reqwest::Method;
|
use reqwest::Method;
|
||||||
|
use scopeguard::ScopeGuard;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use tar::Archive;
|
use tar::Archive;
|
||||||
use tinytemplate::TinyTemplate;
|
use tinytemplate::TinyTemplate;
|
||||||
|
@ -63,11 +64,17 @@ pub async fn download<P: AsRef<Path>>(url: &str, path: P) -> Result<(), Binstall
|
||||||
let mut bytes_stream = resp.bytes_stream();
|
let mut bytes_stream = resp.bytes_stream();
|
||||||
let writer = AsyncFileWriter::new(path)?;
|
let writer = AsyncFileWriter::new(path)?;
|
||||||
|
|
||||||
|
let guard = scopeguard::guard(path, |path| {
|
||||||
|
fs::remove_file(path).ok();
|
||||||
|
});
|
||||||
|
|
||||||
while let Some(res) = bytes_stream.next().await {
|
while let Some(res) = bytes_stream.next().await {
|
||||||
writer.write(res?).await;
|
writer.write(res?).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.done().await?;
|
writer.done().await?;
|
||||||
|
// Disarm as it is successfully downloaded and written to file.
|
||||||
|
ScopeGuard::into_inner(guard);
|
||||||
|
|
||||||
debug!("Download OK, written to file: '{}'", path.display());
|
debug!("Download OK, written to file: '{}'", path.display());
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue