Use untar in extract_compressed_from_readable

So that we can specify the files we want to extract to avoid io and save
disk usage.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-06-09 13:26:47 +10:00
parent be5e8616a2
commit 72983e4113
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
3 changed files with 41 additions and 19 deletions

View file

@ -12,6 +12,8 @@ use zstd::stream::Decoder as ZstdDecoder;
use crate::{BinstallError, PkgFmt};
/// * `desired_outputs - If Some(_), then it will filter the tar
/// and only extract files specified in it.
fn untar(
dat: impl Read,
path: &Path,
@ -40,37 +42,34 @@ fn untar(
/// Extract files from the specified source onto the specified path.
///
/// * `fmt` - must not be `PkgFmt::Bin` or `PkgFmt::Zip`.
/// * `desired_outputs - If Some(_), then it will filter the tar
/// and only extract files specified in it.
pub(crate) fn extract_compressed_from_readable(
dat: impl Read,
fmt: PkgFmt,
path: &Path,
desired_outputs: Option<&[Cow<'_, Path>]>,
) -> Result<(), BinstallError> {
match fmt {
PkgFmt::Tar => {
// Extract to install dir
debug!("Extracting from tar archive to `{path:?}`");
let mut tar = Archive::new(dat);
tar.unpack(path)?;
untar(dat, path, desired_outputs)?
}
PkgFmt::Tgz => {
// Extract to install dir
debug!("Decompressing from tgz archive to `{path:?}`");
let tar = GzDecoder::new(dat);
let mut tgz = Archive::new(tar);
tgz.unpack(path)?;
untar(tar, path, desired_outputs)?;
}
PkgFmt::Txz => {
// Extract to install dir
debug!("Decompressing from txz archive to `{path:?}`");
let tar = XzDecoder::new(dat);
let mut txz = Archive::new(tar);
txz.unpack(path)?;
untar(tar, path, desired_outputs)?;
}
PkgFmt::Tzstd => {
// Extract to install dir
@ -81,9 +80,7 @@ pub(crate) fn extract_compressed_from_readable(
// 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)?;
untar(tar, path, desired_outputs)?;
}
PkgFmt::Zip => panic!("Unexpected PkgFmt::Zip!"),
PkgFmt::Bin => panic!("Unexpected PkgFmt::Bin!"),