From 589b585f382a6a7eab986b0259912d3fa75c8427 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 12 Apr 2020 23:03:51 +0800 Subject: [PATCH] add swtch for sequoia_openpgp --- Cargo.toml | 7 ++++++- src/main.rs | 49 +++++++++++++++++++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 61f074c..9cdcc1c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,11 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +# cargo b --features sequoia_openpgp +default = [] # "sequoia_openpgp" +sequoia_openpgp = ["sequoia-openpgp"] + [dependencies] dirs = "2.0.1" argparse = "0.2.2" @@ -16,7 +21,7 @@ indicatif = "0.13.0" urlencoding = "1.0.0" base64 = "0.11.0" reqwest = "0.9.22" -sequoia-openpgp = "0.16.0" +sequoia-openpgp = { version = "0.16.0", optional = true } chrono = "0.4.10" zip = "0.5.3" rust_util = "0.2.1" diff --git a/src/main.rs b/src/main.rs index 289eef8..bff1582 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,9 @@ // #[macro_use] // extern crate lazy_static; +#[cfg(feature = "sequoia_openpgp")] extern crate sequoia_openpgp as openpgp; mod oss_util; +#[cfg(feature = "sequoia_openpgp")] mod pgp_util; mod config_util; mod zip_util; @@ -19,6 +21,7 @@ use rust_util::{ }; use oss_util::*; use config_util::*; +#[cfg(feature = "sequoia_openpgp")] use pgp_util::OpenPGPTool; use opt::Options; @@ -106,13 +109,6 @@ fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem, print_message(MessageType::DEBUG, &format!("New backup file: {}", new_file)); } - let open_pgp_tool = match OpenPGPTool::from_file(encrypt_pubkey_file) { - Ok(t) => t, Err(e) => { - print_message(MessageType::ERROR, &format!("Error in load pgp file: {}, at item index: {}", e, item_index)); - return Ok(()); - }, - }; - let secs = get_current_secs(); let temp_zip_file = &format!("temp_file_{}.zip", secs); let temp_pgp_file = &format!("temp_file_{}.gpg", secs); @@ -136,15 +132,40 @@ fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem, return Ok(()); }; - if options.verbose { - print_message(MessageType::DEBUG, &format!("Encrypt file: {} -> {}", temp_zip_file, temp_pgp_file)); - } - if let Err(e) = open_pgp_tool.encrypt_file(temp_zip_file, temp_pgp_file, false) { - print_message(MessageType::ERROR, &format!("Error in encrypt file: {}, at item index: {}", e, item_index)); - remove_temp_files(); - return Ok(()); + #[cfg(feature = "sequoia_openpgp")] + let enc_file_by_pgp = || { + let open_pgp_tool = match OpenPGPTool::from_file(encrypt_pubkey_file) { + Ok(t) => t, Err(e) => { + print_message(MessageType::ERROR, &format!("Error in load pgp file: {}, at item index: {}", e, item_index)); + return false; + }, + }; + if options.verbose { + print_message(MessageType::DEBUG, &format!("Encrypt file: {} -> {}", temp_zip_file, temp_pgp_file)); + } + if let Err(e) = open_pgp_tool.encrypt_file(temp_zip_file, temp_pgp_file, false) { + print_message(MessageType::ERROR, &format!("Error in encrypt file: {}, at item index: {}", e, item_index)); + remove_temp_files(); + return false; + }; + return true; }; + #[cfg(not(feature = "sequoia_openpgp"))] + let enc_file_by_pgp = || { + let mut cmd = std::process::Command::new("gpg"); + cmd.args(&["-e", "-r", encrypt_pubkey_file, "-o", temp_pgp_file, temp_zip_file]); + if let Err(e) = rust_util::util_cmd::run_command_and_wait(&mut cmd) { + print_message(MessageType::ERROR, &format!("Error in encrypt file: {}, at item index: {}", e, item_index)); + return false; + } + return true; + }; + + if !enc_file_by_pgp() { + return Ok(()); + } + let file_temp_pgp_file = match File::open(temp_pgp_file) { Ok(f) => f, Err(e) => { print_message(MessageType::ERROR, &format!("Error in open file: {}, at item index: {}", e, item_index));