Report errors for non-UTF-8 entries in Zip and 7z

This commit is contained in:
João Marcos P. Bezerra 2023-11-25 20:58:27 -03:00 committed by João Marcos
parent d07c65508a
commit a7a65d2510
2 changed files with 18 additions and 7 deletions

View File

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

View File

@ -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)?;
}
}