diff --git a/utils/rust/tar/Cargo.toml b/utils/rust/tar/Cargo.toml index 744c7f161b50cb87e95ace4183c295a1ac6c44ec..422fab5d13e44c5f6ad3cdac6995c32c9f0c09e1 100644 --- a/utils/rust/tar/Cargo.toml +++ b/utils/rust/tar/Cargo.toml @@ -28,6 +28,8 @@ tar = "0.4.39" walkdir = "2.3.3" ylong_runtime = { git = "https://gitee.com/openharmony-sig/commonlibrary_rust_ylong_runtime.git", features = ["full"]} libc = "0.2.101" +flate2 = { version = "1.0.26", optional = true } [features] compress_lz4_flex = ["dep:lz4_flex"] +compress_gz = ["dep:flate2"] \ No newline at end of file diff --git a/utils/rust/tar/src/backup.rs b/utils/rust/tar/src/backup.rs index 93e25d46d624c86c4ace6a749a660aebbdd835e5..79266987ccbdcc9c7d377a20b22aaa21b21f0c41 100644 --- a/utils/rust/tar/src/backup.rs +++ b/utils/rust/tar/src/backup.rs @@ -23,7 +23,7 @@ use std::path::{Path, PathBuf}; use tar::Builder; use ylong_runtime::sync::mpsc::bounded::{BoundedReceiver, BoundedSender}; -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Debug, clap::ValueEnum)] #[non_exhaustive] pub enum CompressAlgorithm { #[allow(dead_code)] @@ -32,6 +32,10 @@ pub enum CompressAlgorithm { #[cfg(feature = "compress_lz4_flex")] #[allow(dead_code)] Lz4Flex, + + #[cfg(feature = "compress_gz")] + #[allow(dead_code)] + GZ, } pub struct Options { @@ -90,6 +94,9 @@ enum Output { #[cfg(feature = "compress_lz4_flex")] CompressedLz4Flex(Builder>), + + #[cfg(feature = "compress_gz")] + CompressedGZ(Builder>), } pub async fn scan_files( @@ -220,6 +227,10 @@ impl Output { CompressAlgorithm::Lz4Flex => Ok(Self::CompressedLz4Flex(Builder::new( FrameEncoder::new(File::create(path)?), ))), + #[cfg(feature = "compress_gz")] + CompressAlgorithm::GZ => Ok(Self::CompressedGZ(Builder::new( + flate2::write::GzEncoder::new(File::create(path)?, flate2::Compression::default()), + ))), } } @@ -243,6 +254,11 @@ impl Output { Self::CompressedLz4Flex(builder) => { builder.append_path_with_name(&achievable_path, relative_path)? } + + #[cfg(feature = "compress_gz")] + Self::CompressedGZ(builder) => { + builder.append_path_with_name(&achievable_path, relative_path)? + } }; Ok(std::fs::metadata(achievable_path)?.len() as usize) } @@ -257,6 +273,11 @@ impl Output { let encoder = builder.into_inner().expect("failed to get encoder"); encoder.finish()?; } + #[cfg(feature = "compress_gz")] + Self::CompressedGZ(builder) => { + let encoder = builder.into_inner().expect("failed to get encoder"); + encoder.finish()?; + } } Ok(()) } @@ -329,6 +350,9 @@ impl BackupContext { #[cfg(feature = "compress_lz4_flex")] CompressAlgorithm::Lz4Flex => path.with_extension("tar.lz4"), + + #[cfg(feature = "compress_gz")] + CompressAlgorithm::GZ => path.with_extension("tar.gz"), } } diff --git a/utils/rust/tar/src/main.rs b/utils/rust/tar/src/main.rs index de414226f0eb58e98bb4bafb7be7faaa1a3405bf..7825306f1a68273286ac172a8b4f97d768a50662 100644 --- a/utils/rust/tar/src/main.rs +++ b/utils/rust/tar/src/main.rs @@ -12,7 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + +use backup::CompressAlgorithm; use clap::Parser; use std::path::PathBuf; use ylong_runtime::{block_on, sync::mpsc::bounded::bounded_channel}; @@ -33,6 +34,8 @@ struct Args { /// includes exceptions that do not need to be backed up #[clap(short, long)] excludes: Vec, + #[clap(short, long, value_enum, default_value = "none")] + compress_algorithm: CompressAlgorithm, } fn backup_main() { @@ -52,6 +55,7 @@ fn backup_main() { ylong_runtime::spawn(async move { let option = backup::Options { stash_dir: PathBuf::from(args.stash_dir), + compress_algorithm: args.compress_algorithm, ..Default::default() }; let _ = backup::backup_files(option, paths_to_backed_rx, outputs_tx)