feat: optimize code
This commit is contained in:
@@ -4,7 +4,6 @@ use std::path::PathBuf;
|
||||
use std::time::{Instant, SystemTime};
|
||||
|
||||
use clap::Args;
|
||||
use fs_set_times::SystemTimeSpec;
|
||||
use openpgp_card::crypto_data::Cryptogram;
|
||||
use rust_util::{
|
||||
debugging, failure, iff, information, opt_result, simple_error, success,
|
||||
@@ -17,10 +16,13 @@ use yubikey::piv::{AlgorithmId, decrypt_data};
|
||||
use yubikey::YubiKey;
|
||||
use zeroize::Zeroize;
|
||||
|
||||
use crate::{file, util, util_pgp, util_piv};
|
||||
use crate::{util, util_enc_file, util_file, util_pgp, util_piv};
|
||||
use crate::compress::GzStreamDecoder;
|
||||
use crate::config::TinyEncryptConfig;
|
||||
use crate::consts::{DATE_TIME_FORMAT, ENC_AES256_GCM_P256, ENC_AES256_GCM_P384, ENC_AES256_GCM_X25519, SALT_COMMENT, TINY_ENC_CONFIG_FILE, TINY_ENC_FILE_EXT};
|
||||
use crate::consts::{
|
||||
DATE_TIME_FORMAT, ENC_AES256_GCM_P256, ENC_AES256_GCM_P384, ENC_AES256_GCM_X25519,
|
||||
SALT_COMMENT, TINY_ENC_CONFIG_FILE, TINY_ENC_FILE_EXT,
|
||||
};
|
||||
use crate::crypto_aes::{aes_gcm_decrypt, try_aes_gcm_decrypt_with_salt};
|
||||
use crate::spec::{EncEncryptedMeta, TinyEncryptEnvelop, TinyEncryptEnvelopType, TinyEncryptMeta};
|
||||
use crate::wrap_key::WrapKey;
|
||||
@@ -91,7 +93,7 @@ pub fn decrypt_single(config: &Option<TinyEncryptConfig>,
|
||||
util::require_tiny_enc_file_and_exists(path)?;
|
||||
|
||||
let mut file_in = opt_result!(File::open(path), "Open file: {} failed: {}", &path_display);
|
||||
let meta = opt_result!(file::read_tiny_encrypt_meta_and_normalize(&mut file_in), "Read file: {}, failed: {}", &path_display);
|
||||
let meta = opt_result!(util_enc_file::read_tiny_encrypt_meta_and_normalize(&mut file_in), "Read file: {}, failed: {}", &path_display);
|
||||
debugging!("Found meta: {}", serde_json::to_string_pretty(&meta).unwrap());
|
||||
|
||||
let path_out = &path_display[0..path_display.len() - TINY_ENC_FILE_EXT.len()];
|
||||
@@ -122,7 +124,7 @@ pub fn decrypt_single(config: &Option<TinyEncryptConfig>,
|
||||
drop(file_out);
|
||||
util_msg::clear_lastline();
|
||||
|
||||
update_out_file_time(enc_meta, path_out);
|
||||
util_file::update_out_file_time(enc_meta, path_out);
|
||||
let encrypt_duration = start.elapsed();
|
||||
debugging!("Inner decrypt file{}: {} elapsed: {} ms", compressed_desc, path_display, encrypt_duration.as_millis());
|
||||
}
|
||||
@@ -171,24 +173,6 @@ fn decrypt_file(file_in: &mut File, file_out: &mut File, key: &[u8], nonce: &[u8
|
||||
Ok(total_len)
|
||||
}
|
||||
|
||||
fn update_out_file_time(enc_meta: Option<EncEncryptedMeta>, path_out: &str) {
|
||||
if let Some(enc_meta) = &enc_meta {
|
||||
let create_time = enc_meta.c_time.map(SystemTime::from_millis);
|
||||
let modify_time = enc_meta.m_time.map(SystemTime::from_millis);
|
||||
if create_time.is_some() || modify_time.is_some() {
|
||||
let set_times_result = fs_set_times::set_times(
|
||||
path_out,
|
||||
create_time.map(SystemTimeSpec::Absolute),
|
||||
modify_time.map(SystemTimeSpec::Absolute),
|
||||
);
|
||||
match set_times_result {
|
||||
Ok(_) => information!("Set file time succeed."),
|
||||
Err(e) => warning!("Set file time failed: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_encrypted_comment(meta: &TinyEncryptMeta, key: &[u8], nonce: &[u8]) -> XResult<()> {
|
||||
if let Some(encrypted_comment) = &meta.encrypted_comment {
|
||||
match util::decode_base64(encrypted_comment) {
|
||||
|
||||
@@ -11,7 +11,7 @@ use rust_util::{debugging, failure, iff, information, opt_result, simple_error,
|
||||
use rust_util::util_time::UnixEpochTime;
|
||||
use zeroize::Zeroize;
|
||||
|
||||
use crate::{file, util, util_ecdh, util_p384, util_x25519};
|
||||
use crate::{util_enc_file, util, util_ecdh, util_p384, util_x25519};
|
||||
use crate::compress::GzStreamEncoder;
|
||||
use crate::config::{TinyEncryptConfig, TinyEncryptConfigEnvelop};
|
||||
use crate::consts::{ENC_AES256_GCM_P256, ENC_AES256_GCM_P384, ENC_AES256_GCM_X25519, SALT_COMMENT, TINY_ENC_CONFIG_FILE, TINY_ENC_FILE_EXT};
|
||||
@@ -146,7 +146,7 @@ fn encrypt_single(path: &PathBuf, envelops: &[&TinyEncryptConfigEnvelop], cmd_en
|
||||
|
||||
let mut file_out = File::create(&path_out)?;
|
||||
let compress_meta = !cmd_encrypt.disable_compress_meta;
|
||||
let _ = file::write_tiny_encrypt_meta(&mut file_out, &encrypt_meta, compress_meta)?;
|
||||
let _ = util_enc_file::write_tiny_encrypt_meta(&mut file_out, &encrypt_meta, compress_meta)?;
|
||||
|
||||
let compress_desc = iff!(cmd_encrypt.compress, " [with compress]", "");
|
||||
let start = Instant::now();
|
||||
|
||||
@@ -9,7 +9,7 @@ use rust_util::util_time::UnixEpochTime;
|
||||
use simpledateformat::format_human2;
|
||||
|
||||
use crate::consts::{DATE_TIME_FORMAT, TINY_ENC_AES_GCM, TINY_ENC_FILE_EXT};
|
||||
use crate::file;
|
||||
use crate::util_enc_file;
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct CmdInfo {
|
||||
@@ -39,7 +39,7 @@ pub fn info_single(path: &PathBuf, cmd_info: &CmdInfo) -> XResult<()> {
|
||||
|
||||
let mut file_in = opt_result!(File::open(path), "Open file: {} failed: {}", &path_display);
|
||||
let meta = opt_result!(
|
||||
file::read_tiny_encrypt_meta_and_normalize(&mut file_in), "Read file: {}, failed: {}", &path_display
|
||||
util_enc_file::read_tiny_encrypt_meta_and_normalize(&mut file_in), "Read file: {}, failed: {}", &path_display
|
||||
);
|
||||
|
||||
if cmd_info.raw_meta {
|
||||
|
||||
@@ -22,7 +22,8 @@ mod spec;
|
||||
mod crypto_aes;
|
||||
mod crypto_rsa;
|
||||
mod wrap_key;
|
||||
mod file;
|
||||
mod util_file;
|
||||
mod util_enc_file;
|
||||
mod cmd_version;
|
||||
mod cmd_config;
|
||||
mod cmd_info;
|
||||
|
||||
25
src/util_file.rs
Normal file
25
src/util_file.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
use std::time::SystemTime;
|
||||
|
||||
use fs_set_times::SystemTimeSpec;
|
||||
use rust_util::{information, warning};
|
||||
use rust_util::util_time::UnixEpochTime;
|
||||
|
||||
use crate::spec::EncEncryptedMeta;
|
||||
|
||||
pub fn update_out_file_time(enc_meta: Option<EncEncryptedMeta>, path_out: &str) {
|
||||
if let Some(enc_meta) = &enc_meta {
|
||||
let create_time = enc_meta.c_time.map(SystemTime::from_millis);
|
||||
let modify_time = enc_meta.m_time.map(SystemTime::from_millis);
|
||||
if create_time.is_some() || modify_time.is_some() {
|
||||
let set_times_result = fs_set_times::set_times(
|
||||
path_out,
|
||||
create_time.map(SystemTimeSpec::Absolute),
|
||||
modify_time.map(SystemTimeSpec::Absolute),
|
||||
);
|
||||
match set_times_result {
|
||||
Ok(_) => information!("Set file time succeed."),
|
||||
Err(e) => warning!("Set file time failed: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user