From a7a65d25103cbd69c14e6a65442cc988b4a8b2a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Marcos=20P=2E=20Bezerra?= Date: Sat, 25 Nov 2023 20:58:27 -0300 Subject: [PATCH] Report errors for non-UTF-8 entries in Zip and 7z --- src/archive/sevenz.rs | 9 +++++++-- src/archive/zip.rs | 16 +++++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/archive/sevenz.rs b/src/archive/sevenz.rs index aa234c3..0847f4c 100644 --- a/src/archive/sevenz.rs +++ b/src/archive/sevenz.rs @@ -10,6 +10,7 @@ use fs_err as fs; use same_file::Handle; use crate::{ + error::FinalError, info, utils::{self, cd_into_same_dir_as, Bytes, EscapedPathDisplay, FileVisibilityPolicy}, warning, @@ -70,8 +71,12 @@ where } }; - let entry_name = path.to_str().unwrap().to_owned(); - let entry = sevenz_rust::SevenZArchiveEntry::from_path(path, entry_name); + let entry_name = path.to_str().ok_or_else(|| { + FinalError::with_title("7z requires that all entry names are valid UTF-8") + .detail(format!("File at '{path:?}' has a non-UTF-8 name")) + })?; + + let entry = sevenz_rust::SevenZArchiveEntry::from_path(path, entry_name.to_owned()); let entry_data = if metadata.is_dir() { None } else { diff --git a/src/archive/zip.rs b/src/archive/zip.rs index 3ba462c..e9bde15 100644 --- a/src/archive/zip.rs +++ b/src/archive/zip.rs @@ -209,8 +209,13 @@ where #[cfg(unix)] let options = options.unix_permissions(metadata.permissions().mode()); + let entry_name = path.to_str().ok_or_else(|| { + FinalError::with_title("Zip requires that all directories names are valid UTF-8") + .detail(format!("File at '{path:?}' has a non-UTF-8 name")) + })?; + if metadata.is_dir() { - writer.add_directory(path.to_str().unwrap().to_owned(), options)?; + writer.add_directory(entry_name, options)?; } else { #[cfg(not(unix))] let options = if is_executable::is_executable(path) { @@ -220,10 +225,11 @@ where }; let mut file = fs::File::open(path)?; - writer.start_file( - path.to_str().unwrap(), - options.last_modified_time(get_last_modified_time(&file)), - )?; + + // Updated last modified time + let last_modified_time = options.last_modified_time(get_last_modified_time(&file)); + + writer.start_file(entry_name, last_modified_time)?; io::copy(&mut file, &mut writer)?; } }