unix: if setting permission fails, return an error instead of panicking

This commit is contained in:
Vinícius Rodrigues Miguel 2021-10-01 12:00:52 -03:00
parent 8fc2f06ef7
commit 17d8959a0f

View File

@ -51,7 +51,7 @@ where
} }
#[cfg(unix)] #[cfg(unix)]
__unix_set_permissions(&file_path, &file); __unix_set_permissions(&file_path, &file)?;
let file_path = fs::canonicalize(file_path.clone())?; let file_path = fs::canonicalize(file_path.clone())?;
unpacked_files.push(file_path); unpacked_files.push(file_path);
@ -76,6 +76,7 @@ where
.collect(); .collect();
if !invalid_unicode_filenames.is_empty() { if !invalid_unicode_filenames.is_empty() {
// TODO: make this an error variant
panic!("invalid unicode filenames found, cannot be supported by Zip:\n {:#?}", invalid_unicode_filenames); panic!("invalid unicode filenames found, cannot be supported by Zip:\n {:#?}", invalid_unicode_filenames);
} }
@ -87,15 +88,14 @@ where
for entry in WalkDir::new(filename) { for entry in WalkDir::new(filename) {
let entry = entry?; let entry = entry?;
let path = &entry.path(); let path = entry.path();
println!("Compressing '{}'.", utils::to_utf(path));
if path.is_dir() { if path.is_dir() {
continue; continue;
} }
writer.start_file(path.to_str().unwrap().to_owned(), options)?; writer.start_file(path.to_str().unwrap().to_owned(), options)?;
// TODO: check if isn't there a function that already does this for us......
// TODO: better error messages // TODO: better error messages
let file_bytes = fs::read(entry.path())?; let file_bytes = fs::read(entry.path())?;
writer.write_all(&*file_bytes)?; writer.write_all(&*file_bytes)?;
@ -116,10 +116,12 @@ fn check_for_comments(file: &ZipFile) {
} }
#[cfg(unix)] #[cfg(unix)]
fn __unix_set_permissions(file_path: &Path, file: &ZipFile) { fn __unix_set_permissions(file_path: &Path, file: &ZipFile) -> crate::Result<()> {
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
if let Some(mode) = file.unix_mode() { if let Some(mode) = file.unix_mode() {
fs::set_permissions(&file_path, fs::Permissions::from_mode(mode)).unwrap(); fs::set_permissions(file_path, fs::Permissions::from_mode(mode))?;
} }
Ok(())
} }