From 6fbbc12f9b695d2c6af471d251594419bda93d52 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 02:53:26 +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 | 122 ++++++++++++++++++----------------- 3 files changed, 91 insertions(+), 59 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..12cbcaf93 100644 --- a/utils/rust/tar/src/main.rs +++ b/utils/rust/tar/src/main.rs @@ -13,62 +13,68 @@ * limitations under the License. */ -use clap::Parser; -use std::path::PathBuf; -use ylong_runtime::{block_on, sync::mpsc::bounded::bounded_channel}; - -mod backup; - -#[derive(Parser, Debug)] -#[command(author, version, about, long_about = None)] -struct Args { - /// Tar file saving path - #[clap(short, long)] - stash_dir: String, - - /// Files and directories that you want to back up - #[clap(short, long)] - includes: Vec, - - /// includes exceptions that do not need to be backed up - #[clap(short, long)] - excludes: Vec, -} - -fn backup_main() { - let mut args = Args::parse(); - args.excludes.push(args.stash_dir.clone()); - println!("{:#?}", args); - - let (paths_to_backed_tx, paths_to_backed_rx) = bounded_channel(100); - let (outputs_tx, mut outputs_rx) = bounded_channel(100); - - let handles = vec![ - ylong_runtime::spawn(async move { - let _ = backup::scan_files(args.includes, args.excludes, paths_to_backed_tx) - .await - .unwrap(); - }), - ylong_runtime::spawn(async move { - let option = backup::Options { - stash_dir: PathBuf::from(args.stash_dir), - ..Default::default() - }; - let _ = backup::backup_files(option, paths_to_backed_rx, outputs_tx) - .await - .unwrap(); - }), - ylong_runtime::spawn(async move { - while let Ok(archive) = outputs_rx.recv().await { - println!("output: {:?}", archive); - } - }), - ]; - for handle in handles { - block_on(handle).unwrap(); - } -} + use backup::CompressAlgorithm; + use clap::Parser; + use std::path::PathBuf; + use ylong_runtime::{block_on, sync::mpsc::bounded::bounded_channel}; + + mod backup; + + #[derive(Parser, Debug)] + #[command(author, version, about, long_about = None)] + struct Args { + /// Tar file saving path + #[clap(short, long)] + stash_dir: String, + + /// Files and directories that you want to back up + #[clap(short, long)] + includes: Vec, + + /// includes exceptions that do not need to be backed up + #[clap(short, long)] + excludes: Vec, -fn main() { - backup_main(); -} + #[clap(short, long, value_enum, default_value = "none")] + compress_algorithm: CompressAlgorithm, + } + + fn backup_main() { + let mut args = Args::parse(); + args.excludes.push(args.stash_dir.clone()); + println!("{:#?}", args); + + let (paths_to_backed_tx, paths_to_backed_rx) = bounded_channel(100); + let (outputs_tx, mut outputs_rx) = bounded_channel(100); + + let handles = vec![ + ylong_runtime::spawn(async move { + let _ = backup::scan_files(args.includes, args.excludes, paths_to_backed_tx) + .await + .unwrap(); + }), + 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) + .await + .unwrap(); + }), + ylong_runtime::spawn(async move { + while let Ok(archive) = outputs_rx.recv().await { + println!("output: {:?}", archive); + } + }), + ]; + for handle in handles { + block_on(handle).unwrap(); + } + } + + fn main() { + backup_main(); + } + \ No newline at end of file -- Gitee