add swtch for sequoia_openpgp

This commit is contained in:
2020-04-12 23:03:51 +08:00
parent 32aa27ab1a
commit 589b585f38
2 changed files with 41 additions and 15 deletions

View File

@@ -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"

View File

@@ -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));