Skip to content

Commit

Permalink
Fix crash on unknown file name, improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
duckysmacky committed Aug 22, 2024
1 parent 048f16d commit 2433b83
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/cli/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ fn search_for_original(dir_path: &Path, target_name: OsString) -> io::Result<Pat

if !path.is_file() || path.extension().unwrap() != "box" { continue; }

let original_name = parser::parse_file(path.as_path())?.header.original_filename;
let original_name = parser::parse_file(path.as_path())
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err.to_string()))?
.header.original_filename;

if target_name == original_name {
log_info!("Found an encrypted (.box) file with the same original name: {}", path.display());
Expand Down
11 changes: 6 additions & 5 deletions src/file/parser.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
use std::fs::File;
use std::{fs, io};
use std::fs;
use std::io::{Read, Write};
use std::path::Path;
use crate::file::{BoxFile, BoxHeader};
use crate::{Result, Error};

pub fn parse_file(path: &Path) -> io::Result<BoxFile> {
pub fn parse_file(path: &Path) -> Result<BoxFile> {
let mut file = File::open(path)?;
let metadata = fs::metadata(path)?;

let mut buffer = vec![0; metadata.len() as usize];
file.read(&mut buffer)?;

let box_file: BoxFile = bincode::deserialize(&buffer)
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, format!("Unable to deserialize file data: {}", err)))?;
.map_err(|err| Error::InvalidFile(format!("Unable to deserialize \"{}\" file data for: {}", path.display(), err)))?;
Ok(box_file)
}

pub fn write_file(path: &Path, header: BoxHeader, body: Vec<u8>) -> io::Result<()> {
pub fn write_file(path: &Path, header: BoxHeader, body: Vec<u8>) -> Result<()> {
let mut file = File::create(path)?;

let box_file = BoxFile {header, body};
let box_data = bincode::serialize(&box_file)
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, format!("Unable to serialize file data: {}", err)))?;
.map_err(|err| Error::InvalidFile(format!("Unable to serialize \"{}\" file data for: {}", path.display(), err)))?;

file.write_all(&box_data)?;

Expand Down
8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use crate::encryption::{checksum, cipher};
Expand Down Expand Up @@ -36,7 +37,8 @@ pub fn encrypt(input_path: &Path, password: &str, opts: &mut options::Encryption
return Err(Error::InvalidFile(format!("\"{}\" is already encrypted", input_path.display())))
}

log_success!("Encrypting file {:?}", input_path.file_name().unwrap().to_os_string());
let file_name = input_path.file_name().unwrap_or(OsStr::new("unknown")).to_os_string();
log_success!("Encrypting file {:?}", file_name);

let mut path_buffer = PathBuf::from(input_path);
let file_path = path_buffer.as_path();
Expand Down Expand Up @@ -84,8 +86,8 @@ pub fn decrypt(input_path: &Path, password: &str, opts: &mut options::Decryption
return Err(Error::InvalidFile(format!("\"{}\" cannot be decrypted", input_path.display())))
}

let file_name = input_path.file_name().unwrap().to_os_string();
log_success!("Decrypting file: {:?}", file_name);
let file_name = input_path.file_name().unwrap_or(OsStr::new("unknown")).to_os_string();
log_success!("Decrypting file {:?}", file_name);

let mut path_buffer = PathBuf::from(input_path);
let file_path = path_buffer.as_path();
Expand Down

0 comments on commit 2433b83

Please sign in to comment.