diff --git a/src/cmd_decrypt.rs b/src/cmd_decrypt.rs index 90d3669..1970562 100644 --- a/src/cmd_decrypt.rs +++ b/src/cmd_decrypt.rs @@ -39,16 +39,42 @@ pub struct CmdDecrypt { pub fn decrypt(cmd_decrypt: CmdDecrypt) -> XResult<()> { debugging!("Cmd decrypt: {:?}", cmd_decrypt); + let start = Instant::now(); + let mut succeed_count = 0; + let mut failed_count = 0; + let mut total_len = 0_u64; for path in &cmd_decrypt.paths { + let start_decrypt_single = Instant::now(); match decrypt_single(path, &cmd_decrypt.pin, &cmd_decrypt.slot, &cmd_decrypt) { - Ok(_) => success!("Decrypt {} succeed", path.to_str().unwrap_or("N/A")), - Err(e) => failure!("Decrypt {} failed: {}", path.to_str().unwrap_or("N/A"), e), + Ok(len) => { + succeed_count += 1; + total_len += len; + success!( + "Decrypt {} succeed, cost {} ms, file size {} byte(s)", + path.to_str().unwrap_or("N/A"), + start_decrypt_single.elapsed().as_millis(), + len + ); + } + Err(e) => { + failed_count += 1; + failure!("Decrypt {} failed: {}", path.to_str().unwrap_or("N/A"), e); + } } } + if (succeed_count + failed_count) > 1 { + success!( + "Decrypt succeed {} file(s) {} byte(s), failed {} file(s), total cost {} ms", + succeed_count, + total_len, + failed_count, + start.elapsed().as_millis(), + ); + } Ok(()) } -pub fn decrypt_single(path: &PathBuf, pin: &Option, slot: &Option, cmd_decrypt: &CmdDecrypt) -> XResult<()> { +pub fn decrypt_single(path: &PathBuf, pin: &Option, slot: &Option, cmd_decrypt: &CmdDecrypt) -> XResult { let path_display = format!("{}", path.display()); util::require_tiny_enc_file_and_exists(path)?; @@ -84,7 +110,7 @@ pub fn decrypt_single(path: &PathBuf, pin: &Option, slot: &Option information!("Remove file: {} succeed", path_display), } } - Ok(()) + Ok(meta.file_length) } fn decrypt_file(file_in: &mut File, file_out: &mut File, key: &[u8], nonce: &[u8], compress: bool) -> XResult { @@ -106,7 +132,7 @@ fn decrypt_file(file_in: &mut File, file_out: &mut File, key: &[u8], nonce: &[u8 last_block }; opt_result!(file_out.write_all(&last_block), "Write file failed: {}"); - success!("Decrypt finished, total bytes: {}", total_len); + debugging!("Decrypt finished, total bytes: {}", total_len); break; } else { total_len += len; diff --git a/src/cmd_encrypt.rs b/src/cmd_encrypt.rs index 4b4f8cb..02e1827 100644 --- a/src/cmd_encrypt.rs +++ b/src/cmd_encrypt.rs @@ -55,16 +55,42 @@ pub fn encrypt(cmd_encrypt: CmdEncrypt) -> XResult<()> { information!("Matched {} envelop(s): \n- {}", envelops.len(), envelop_tkids.join("\n- ")); debugging!("Cmd encrypt: {:?}", cmd_encrypt); + let start = Instant::now(); + let mut succeed_count = 0; + let mut failed_count = 0; + let mut total_len = 0_u64; for path in &cmd_encrypt.paths { + let start_encrypt_single = Instant::now(); match encrypt_single(path, &envelops, &cmd_encrypt) { - Ok(_) => success!("Encrypt {} succeed", path.to_str().unwrap_or("N/A")), - Err(e) => failure!("Encrypt {} failed: {}", path.to_str().unwrap_or("N/A"), e), + Ok(len) => { + succeed_count += 1; + total_len += len; + success!( + "Encrypt {} succeed, cost {} ms, file size {} byte(s)", + path.to_str().unwrap_or("N/A"), + start_encrypt_single.elapsed().as_millis(), + len + ); + } + Err(e) => { + failed_count += 1; + failure!("Encrypt {} failed: {}", path.to_str().unwrap_or("N/A"), e); + } } } + if (succeed_count + failed_count) > 1 { + success!( + "Encrypt succeed {} file(s) {} byte(s), failed {} file(s), total cost {} ms", + succeed_count, + total_len, + failed_count, + start.elapsed().as_millis(), + ); + } Ok(()) } -fn encrypt_single(path: &PathBuf, envelops: &[&TinyEncryptConfigEnvelop], cmd_encrypt: &CmdEncrypt) -> XResult<()> { +fn encrypt_single(path: &PathBuf, envelops: &[&TinyEncryptConfigEnvelop], cmd_encrypt: &CmdEncrypt) -> XResult { let path_display = format!("{}", path.display()); util::require_none_tiny_enc_file_and_exists(path)?; @@ -136,7 +162,7 @@ fn encrypt_single(path: &PathBuf, envelops: &[&TinyEncryptConfigEnvelop], cmd_en Ok(_) => information!("Remove file: {} succeed", path_display), } } - Ok(()) + Ok(file_metadata.len()) } @@ -170,7 +196,7 @@ fn encrypt_file(file_in: &mut File, file_out: &mut File, key: &[u8], nonce: &[u8 last_block }; opt_result!(file_out.write_all(&last_block), "Write file failed: {}"); - success!("Decrypt finished, total bytes: {}", total_len); + debugging!("Encrypt finished, total bytes: {}", total_len); break; } else { total_len += len;