Merge pull request #224 from sigmaSd/sym

Ignore broken symlinks when compressing
This commit is contained in:
João Marcos Bezerra 2021-12-09 01:06:34 -03:00 committed by GitHub
commit 9c3446924c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 5 deletions

View File

@ -90,7 +90,17 @@ where
if path.is_dir() {
builder.append_dir(path, path)?;
} else {
let mut file = fs::File::open(path)?;
let mut file = match fs::File::open(path) {
Ok(f) => f,
Err(e) => {
if e.kind() == std::io::ErrorKind::NotFound && utils::is_symlink(path) {
// This path is for a broken symlink
// We just ignore it
continue;
}
return Err(e.into());
}
};
builder.append_file(path, file.file_mut()).map_err(|err| {
FinalError::with_title("Could not create archive")
.detail("Unexpected error while trying to read file")

View File

@ -15,8 +15,8 @@ use crate::{
info,
list::FileInArchive,
utils::{
cd_into_same_dir_as, concatenate_os_str_list, dir_is_empty, get_invalid_utf8_paths, strip_cur_dir, to_utf,
Bytes,
self, cd_into_same_dir_as, concatenate_os_str_list, dir_is_empty, get_invalid_utf8_paths, strip_cur_dir,
to_utf, Bytes,
},
};
@ -142,7 +142,17 @@ where
// If a dir has files, the files are responsible for creating them.
} else {
writer.start_file(path.to_str().unwrap().to_owned(), options)?;
let file_bytes = fs::read(entry.path())?;
let file_bytes = match fs::read(entry.path()) {
Ok(b) => b,
Err(e) => {
if e.kind() == std::io::ErrorKind::NotFound && utils::is_symlink(path) {
// This path is for a broken symlink
// We just ignore it
continue;
}
return Err(e.into());
}
};
writer.write_all(&*file_bytes)?;
}
}

View File

@ -124,3 +124,10 @@ pub fn try_infer_extension(path: &Path) -> Option<Extension> {
None
}
}
/// Returns true if a path is a symlink.
/// This is the same as the nightly https://doc.rust-lang.org/std/path/struct.Path.html#method.is_symlink
// Useful to detect broken symlinks when compressing. (So we can safely ignore them)
pub fn is_symlink(path: &Path) -> bool {
fs::symlink_metadata(path).map(|m| m.file_type().is_symlink()).unwrap_or(false)
}

View File

@ -9,7 +9,9 @@ mod fs;
mod question;
pub use formatting::{concatenate_os_str_list, nice_directory_display, strip_cur_dir, to_utf, Bytes};
pub use fs::{cd_into_same_dir_as, clear_path, create_dir_if_non_existent, dir_is_empty, try_infer_extension};
pub use fs::{
cd_into_same_dir_as, clear_path, create_dir_if_non_existent, dir_is_empty, is_symlink, try_infer_extension,
};
pub use question::{
create_or_ask_overwrite, user_wants_to_continue_compressing, user_wants_to_continue_decompressing,
user_wants_to_overwrite, QuestionPolicy,