Impl new fn helpers::download_tar_based_and_visit

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-06-11 20:38:11 +10:00
parent 4892d8bf3a
commit f8c8c66f57
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
2 changed files with 60 additions and 1 deletions

View file

@ -1,3 +1,4 @@
use std::fmt::Debug;
use std::path::{Path, PathBuf};
use cargo_toml::Manifest;
@ -19,6 +20,8 @@ mod ui_thread;
pub use ui_thread::UIThread;
mod extracter;
pub use extracter::TarEntriesVisitor;
mod readable_rx;
/// Load binstall metadata from the crate `Cargo.toml` at the provided path
@ -83,7 +86,7 @@ pub async fn download_and_extract<P: AsRef<Path>>(
/// Download a file from the provided URL and extract part of it to
/// the provided path.
///
/// * `filter` - If Some, then it will pass the path of the file to it
/// * `filter` - It will pass the path of the file to it
/// and only extract ones which filter returns `true`.
pub async fn download_and_extract_with_filter<
Filter: FnMut(&Path) -> bool + Send + 'static,
@ -110,6 +113,36 @@ pub async fn download_and_extract_with_filter<
Ok(())
}
/// Download a file from the provided URL and extract part of it to
/// the provided path.
///
/// * `filter` - If Some, then it will pass the path of the file to it
/// and only extract ones which filter returns `true`.
pub async fn download_tar_based_and_visit<
V: TarEntriesVisitor + Debug + Send + 'static,
P: AsRef<Path>,
>(
url: Url,
fmt: TarBasedFmt,
path: P,
visitor: V,
) -> Result<V, BinstallError> {
debug!("Downloading from: '{url}'");
let resp = create_request(url).await?;
let path = path.as_ref();
debug!("Downloading to file: '{}'", path.display());
let stream = resp.bytes_stream();
let visitor = extract_tar_based_stream_and_visit(stream, path, fmt, visitor).await?;
debug!("Download OK, written to file: '{}'", path.display());
Ok(visitor)
}
/// Fetch install path from environment
/// roughly follows <https://doc.rust-lang.org/cargo/commands/cargo-install.html#description>
pub fn get_install_path<P: AsRef<Path>>(install_path: Option<P>) -> Option<PathBuf> {

View file

@ -282,3 +282,29 @@ where
})
.await
}
pub async fn extract_tar_based_stream_and_visit<V: TarEntriesVisitor + Debug + Send + 'static, E>(
stream: impl Stream<Item = Result<Bytes, E>> + Unpin,
output: &Path,
fmt: TarBasedFmt,
mut visitor: V,
) -> Result<V, BinstallError>
where
BinstallError: From<E>,
{
let path = output.to_owned();
extract_impl(stream, move |mut rx| {
fs::create_dir_all(path.parent().unwrap())?;
extract_compressed_from_readable(
ReadableRx::new(&mut rx),
fmt,
&*path,
Some(&mut visitor),
)?;
Ok(visitor)
})
.await
}