From ab465fa55ae822738dea87b789cecec552df9208 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8C=AB=E7=A7=91=E9=BE=99?= Date: Wed, 9 Aug 2023 08:45:27 +0000 Subject: [PATCH] =?UTF-8?q?rust=20tar=20=E6=96=B0=E5=A2=9E=E6=94=AF?= =?UTF-8?q?=E6=8C=81gz=E5=8E=8B=E7=BC=A9=E6=96=B9=E5=BC=8F=E6=89=93?= =?UTF-8?q?=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 猫科龙 --- utils/rust/tar/Cargo.toml | 2 ++ utils/rust/tar/src/backup.rs | 26 +++++++++++++++++++++++++- utils/rust/tar/src/main.rs | 6 +++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/utils/rust/tar/Cargo.toml b/utils/rust/tar/Cargo.toml index 744c7f161..422fab5d1 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 93e25d46d..79266987c 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 de414226f..7825306f1 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) -- Gitee