use crate::{ExtractError, ExtractResult};
use rattler_conda_types::package::CondaArchiveType;
use std::path::Path;
pub async fn extract_tar_bz2(
archive: &Path,
destination: &Path,
) -> Result<ExtractResult, ExtractError> {
let file = tokio::fs::File::open(archive)
.await
.map_err(ExtractError::IoError)?;
crate::tokio::async_read::extract_tar_bz2(file, destination).await
}
pub async fn extract_conda(
archive: &Path,
destination: &Path,
) -> Result<ExtractResult, ExtractError> {
let destination = destination.to_owned();
let archive = archive.to_owned();
match tokio::task::spawn_blocking(move || crate::fs::extract_conda(&archive, &destination))
.await
{
Ok(result) => result,
Err(err) => {
if let Ok(reason) = err.try_into_panic() {
std::panic::resume_unwind(reason);
}
Err(ExtractError::Cancelled)
}
}
}
pub async fn extract(archive: &Path, destination: &Path) -> Result<ExtractResult, ExtractError> {
match CondaArchiveType::try_from(archive).ok_or(ExtractError::UnsupportedArchiveType)? {
CondaArchiveType::TarBz2 => extract_tar_bz2(archive, destination).await,
CondaArchiveType::Conda => extract_conda(archive, destination).await,
}
}