diff --git a/Cargo.lock b/Cargo.lock index 1e45b78..f845619 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -190,6 +190,27 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" +[[package]] +name = "bzip2" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdb116a6ef3f6c3698828873ad02c3014b3c85cadb88496095628e3ef1e347f8" +dependencies = [ + "bzip2-sys", + "libc", +] + +[[package]] +name = "bzip2-sys" +version = "0.1.11+1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "736a955f3fa7875102d57c82b8cac37ec45224a07fd32d58f9f7a186b6cd4cdc" +dependencies = [ + "cc", + "libc", + "pkg-config", +] + [[package]] name = "camino" version = "1.1.6" @@ -1726,6 +1747,7 @@ dependencies = [ name = "obs-service-cargo" version = "0.8.15" dependencies = [ + "bzip2", "clap", "flate2", "glob", diff --git a/cargo/src/cli.rs b/cargo/src/cli.rs index d61868c..c53573f 100644 --- a/cargo/src/cli.rs +++ b/cargo/src/cli.rs @@ -82,6 +82,7 @@ pub enum Compression { Xz, #[default] Zst, + Bz2, } #[derive(Debug)] @@ -121,6 +122,7 @@ impl Display for Compression { Compression::Gz => "gz", Compression::Xz => "xz", Compression::Zst => "zst", + Compression::Bz2 => "bz2", }; write!(f, "{}", msg) } @@ -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), } } diff --git a/cargo/src/utils/compress.rs b/cargo/src/utils/compress.rs index 4938d51..67cc3e5 100644 --- a/cargo/src/utils/compress.rs +++ b/cargo/src/utils/compress.rs @@ -114,10 +114,15 @@ pub fn tarxz( tar_builder(&mut builder, prjdir, archive_files) } -pub fn tarbz( +pub fn tarbz2( outpath: impl AsRef, prjdir: impl AsRef, archive_files: &[impl AsRef], ) -> 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) } diff --git a/cargo/src/utils/decompress.rs b/cargo/src/utils/decompress.rs index e81e017..6bfcc7d 100644 --- a/cargo/src/utils/decompress.rs +++ b/cargo/src/utils/decompress.rs @@ -15,7 +15,7 @@ use tar; #[allow(unused_imports)] use tracing::{debug, error, info, warn}; -pub fn targz(outdir: impl AsRef, srcpath: impl AsRef) -> Result<(), io::Error> { +pub fn targz(outdir: impl AsRef, srcpath: impl AsRef) -> io::Result<()> { use flate2::bufread::GzDecoder; let mut src = io::BufReader::new(fs::File::open(srcpath.as_ref())?); src.seek(io::SeekFrom::Start(0))?; @@ -31,7 +31,7 @@ pub fn targz(outdir: impl AsRef, srcpath: impl AsRef) -> Result<(), }) } -pub fn tarzst(outdir: impl AsRef, srcpath: impl AsRef) -> Result<(), io::Error> { +pub fn tarzst(outdir: impl AsRef, srcpath: impl AsRef) -> io::Result<()> { use zstd::Decoder; let mut src = io::BufReader::new(fs::File::open(srcpath.as_ref())?); src.seek(io::SeekFrom::Start(0))?; @@ -47,7 +47,7 @@ pub fn tarzst(outdir: impl AsRef, srcpath: impl AsRef) -> Result<(), }) } -pub fn tarxz(outdir: impl AsRef, srcpath: impl AsRef) -> Result<(), io::Error> { +pub fn tarxz(outdir: impl AsRef, srcpath: impl AsRef) -> io::Result<()> { use xz2::read::XzDecoder; let mut src = io::BufReader::new(fs::File::open(srcpath.as_ref())?); src.seek(io::SeekFrom::Start(0))?; @@ -62,3 +62,20 @@ pub fn tarxz(outdir: impl AsRef, srcpath: impl AsRef) -> Result<(), ); }) } + +pub fn tarbz2(outdir: impl AsRef, srcpath: impl AsRef) -> 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(), + ); + }) +} diff --git a/cargo/src/vendor.rs b/cargo/src/vendor.rs index 3915bd6..6d71cff 100644 --- a/cargo/src/vendor.rs +++ b/cargo/src/vendor.rs @@ -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); })