feat: v1.0.2, support --create to create empty encrypted file

This commit is contained in:
2023-12-03 15:20:18 +08:00
parent fc4ec8d41c
commit 883ed0918b
3 changed files with 20 additions and 4 deletions

2
Cargo.lock generated
View File

@@ -1691,7 +1691,7 @@ dependencies = [
[[package]]
name = "tiny-encrypt"
version = "1.0.0"
version = "1.0.2"
dependencies = [
"aes-gcm-stream",
"base64",

View File

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

View File

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