From 883ed0918b1ae39d57e04191ed44f7a74313234a Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 3 Dec 2023 15:20:18 +0800 Subject: [PATCH] feat: v1.0.2, support --create to create empty encrypted file --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/cmd_encrypt.rs | 20 ++++++++++++++++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f05f19d..1a27b07 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1691,7 +1691,7 @@ dependencies = [ [[package]] name = "tiny-encrypt" -version = "1.0.0" +version = "1.0.2" dependencies = [ "aes-gcm-stream", "base64", diff --git a/Cargo.toml b/Cargo.toml index 95d515c..c466f26 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tiny-encrypt" -version = "1.0.1" +version = "1.0.2" edition = "2021" license = "MIT" description = "A simple and tiny file encrypt tool" diff --git a/src/cmd_encrypt.rs b/src/cmd_encrypt.rs index bb2c562..216aece 100644 --- a/src/cmd_encrypt.rs +++ b/src/cmd_encrypt.rs @@ -52,6 +52,9 @@ pub struct CmdEncrypt { /// Remove source file #[arg(long, short = 'R')] pub remove_file: bool, + /// Create file + #[arg(long)] + pub create: bool, /// Disable compress meta #[arg(long)] pub disable_compress_meta: bool, @@ -112,7 +115,15 @@ pub fn encrypt(cmd_encrypt: CmdEncrypt) -> XResult<()> { pub fn encrypt_single(path: &PathBuf, envelops: &[&TinyEncryptConfigEnvelop], cmd_encrypt: &CmdEncrypt) -> XResult { let path_display = format!("{}", path.display()); let path_out = format!("{}{}", path_display, TINY_ENC_FILE_EXT); - encrypt_single_file_out(path, &path_out, envelops, cmd_encrypt) + let encrypt_single_result = encrypt_single_file_out(path, &path_out, envelops, cmd_encrypt); + if cmd_encrypt.create { + if let Ok(content) = fs::read_to_string(path) { + if content == "\n" { + let _ = fs::remove_file(path); + } + } + } + encrypt_single_result } pub fn encrypt_single_file_out(path: &PathBuf, path_out: &str, envelops: &[&TinyEncryptConfigEnvelop], cmd_encrypt: &CmdEncrypt) -> XResult { @@ -125,7 +136,12 @@ pub fn encrypt_single_file_out(path: &PathBuf, path_out: &str, envelops: &[&Tiny let cryptor = crypto_cryptor::get_cryptor_by_encryption_algorithm(&cmd_encrypt.encryption_algorithm)?; information!("Using encryption algorithm: {}", cryptor.get_name()); - util::require_file_exists(path)?; + if cmd_encrypt.create { + util::require_file_not_exists(path)?; + opt_result!(fs::write(path, "\n"), "Write empty file failed: {}"); + } else { + util::require_file_exists(path)?; + } let mut file_in = opt_result!(File::open(path), "Open file: {} failed: {}", &path_display);