Skip to content

Commit

Permalink
Report errors for non-UTF-8 entries in Zip and 7z
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospb19 committed Nov 26, 2023
1 parent 4374c9b commit f9fba4a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/archive/sevenz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 11 additions & 5 deletions src/archive/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)?;
}
}
Expand Down

0 comments on commit f9fba4a

Please sign in to comment.