Skip to content

Commit

Permalink
(de)compress: add Bz2 compression
Browse files Browse the repository at this point in the history
Signed-off-by: Soc Virnyl Estela <[email protected]>
  • Loading branch information
uncomfyhalomacro committed Dec 5, 2023
1 parent ef6963b commit f56cfdb
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions cargo/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub enum Compression {
Xz,
#[default]
Zst,
Bz2,
}

#[derive(Debug)]
Expand Down Expand Up @@ -121,6 +122,7 @@ impl Display for Compression {
Compression::Gz => "gz",
Compression::Xz => "xz",
Compression::Zst => "zst",
Compression::Bz2 => "bz2",
};
write!(f, "{}", msg)
}
Expand Down Expand Up @@ -152,6 +154,7 @@ pub fn decompress(comp_type: &Compression, outdir: &Path, src: &Path) -> io::Res
Compression::Gz => utils::decompress::targz(outdir, src),
Compression::Xz => utils::decompress::tarxz(outdir, src),
Compression::Zst => utils::decompress::tarzst(outdir, src),
Compression::Bz2 => utils::decompress::tarbz2(outdir, src),
}
}

Expand Down
9 changes: 7 additions & 2 deletions cargo/src/utils/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,15 @@ pub fn tarxz(
tar_builder(&mut builder, prjdir, archive_files)
}

pub fn tarbz(
pub fn tarbz2(
outpath: impl AsRef<Path>,
prjdir: impl AsRef<Path>,
archive_files: &[impl AsRef<Path>],
) -> io::Result<()> {
Ok(())
use bzip2::write::BzEncoder;
use bzip2::Compression;
let outtar = fs::File::create(outpath.as_ref())?;
let encoder = BzEncoder::new(outtar, Compression::best());
let mut builder = tar::Builder::new(encoder);
tar_builder(&mut builder, prjdir, archive_files)
}
23 changes: 20 additions & 3 deletions cargo/src/utils/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use tar;
#[allow(unused_imports)]
use tracing::{debug, error, info, warn};

pub fn targz(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> Result<(), io::Error> {
pub fn targz(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> io::Result<()> {
use flate2::bufread::GzDecoder;
let mut src = io::BufReader::new(fs::File::open(srcpath.as_ref())?);
src.seek(io::SeekFrom::Start(0))?;
Expand All @@ -31,7 +31,7 @@ pub fn targz(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> Result<(),
})
}

pub fn tarzst(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> Result<(), io::Error> {
pub fn tarzst(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> io::Result<()> {
use zstd::Decoder;
let mut src = io::BufReader::new(fs::File::open(srcpath.as_ref())?);
src.seek(io::SeekFrom::Start(0))?;
Expand All @@ -47,7 +47,7 @@ pub fn tarzst(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> Result<(),
})
}

pub fn tarxz(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> Result<(), io::Error> {
pub fn tarxz(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> io::Result<()> {
use xz2::read::XzDecoder;
let mut src = io::BufReader::new(fs::File::open(srcpath.as_ref())?);
src.seek(io::SeekFrom::Start(0))?;
Expand All @@ -62,3 +62,20 @@ pub fn tarxz(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> Result<(),
);
})
}

pub fn tarbz2(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> io::Result<()> {
use bzip2::bufread::MultiBzDecoder;

let mut src = io::BufReader::new(fs::File::open(srcpath.as_ref())?);
src.seek(io::SeekFrom::Start(0))?;
let enc = MultiBzDecoder::new(src);
let mut ar = tar::Archive::new(enc);
Ok({
ar.unpack(outdir.as_ref())?;
debug!(
"Successfully decompressed Bz2 archive from {} to {}",
srcpath.as_ref().to_string_lossy(),
outdir.as_ref().to_string_lossy(),
);
})
}
17 changes: 17 additions & 0 deletions cargo/src/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,23 @@ pub fn compress(
})?;
debug!("Compressed to {}", vendor_out.to_string_lossy());
}
Compression::Bz2 => {
vendor_out.set_extension("tar.bz2");
if vendor_out.exists() {
warn!(
replacing = ?vendor_out,
"🔦 Compressed tarball for vendor exists AND will be replaced."
);
}
compress::tarbz2(&vendor_out, &prjdir, paths_to_archive).map_err(|err| {
error!(?err, "bz2 compression failed");
OBSCargoError::new(
OBSCargoErrorKind::VendorCompressionFailed,
"bz2 compression failed".to_string(),
)
})?;
debug!("Compressed to {}", vendor_out.to_string_lossy());
}
}
debug!("Finished creating {} compressed tarball", compression);
})
Expand Down

0 comments on commit f56cfdb

Please sign in to comment.