feat: encrypt, decrypt statistics

This commit is contained in:
2023-10-01 11:31:42 +08:00
parent 30acd253c9
commit 3562e0f93c
2 changed files with 62 additions and 10 deletions

View File

@@ -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<String>, slot: &Option<String>, cmd_decrypt: &CmdDecrypt) -> XResult<()> {
pub fn decrypt_single(path: &PathBuf, pin: &Option<String>, slot: &Option<String>, cmd_decrypt: &CmdDecrypt) -> XResult<u64> {
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<String>, slot: &Option<String
Ok(_) => 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<usize> {
@@ -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;

View File

@@ -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<u64> {
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;