Rm helpers::extract

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-06-09 14:52:14 +10:00
parent 728d1fd6dd
commit 5ba8b07bcb
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -1,22 +1,16 @@
use std::{
borrow::Cow,
fs,
io::{stderr, stdin, Write},
path::{Path, PathBuf},
};
use cargo_toml::Manifest;
use flate2::read::GzDecoder;
use futures_util::stream::StreamExt;
use log::{debug, info};
use reqwest::Method;
use serde::Serialize;
use tar::Archive;
use tinytemplate::TinyTemplate;
use url::Url;
use xz2::read::XzDecoder;
use zip::read::ZipArchive;
use zstd::stream::Decoder as ZstdDecoder;
use crate::{BinstallError, Meta, PkgFmt};
@ -90,79 +84,6 @@ pub async fn download_and_extract<P: AsRef<Path>, const N: usize>(
Ok(())
}
/// Extract files from the specified source onto the specified path
pub fn extract<S: AsRef<Path>, P: AsRef<Path>>(
source: S,
fmt: PkgFmt,
path: P,
) -> Result<(), BinstallError> {
let source = source.as_ref();
let path = path.as_ref();
match fmt {
PkgFmt::Tar => {
// Extract to install dir
debug!("Extracting from tar archive '{source:?}' to `{path:?}`");
let dat = fs::File::open(source)?;
let mut tar = Archive::new(dat);
tar.unpack(path)?;
}
PkgFmt::Tgz => {
// Extract to install dir
debug!("Decompressing from tgz archive '{source:?}' to `{path:?}`");
let dat = fs::File::open(source)?;
let tar = GzDecoder::new(dat);
let mut tgz = Archive::new(tar);
tgz.unpack(path)?;
}
PkgFmt::Txz => {
// Extract to install dir
debug!("Decompressing from txz archive '{source:?}' to `{path:?}`");
let dat = fs::File::open(source)?;
let tar = XzDecoder::new(dat);
let mut txz = Archive::new(tar);
txz.unpack(path)?;
}
PkgFmt::Tzstd => {
// Extract to install dir
debug!("Decompressing from tzstd archive '{source:?}' to `{path:?}`");
let dat = std::fs::File::open(source)?;
// The error can only come from raw::Decoder::with_dictionary
// as of zstd 0.10.2 and 0.11.2, which is specified
// as &[] by ZstdDecoder::new, thus ZstdDecoder::new
// should not return any error.
let tar = ZstdDecoder::new(dat)?;
let mut txz = Archive::new(tar);
txz.unpack(path)?;
}
PkgFmt::Zip => {
// Extract to install dir
debug!("Decompressing from zip archive '{source:?}' to `{path:?}`");
let dat = fs::File::open(source)?;
let mut zip = ZipArchive::new(dat)?;
zip.extract(path)?;
}
PkgFmt::Bin => {
debug!("Copying binary '{source:?}' to `{path:?}`");
// Copy to install dir
fs::copy(source, path)?;
}
};
Ok(())
}
/// 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> {