From 34c09d5d693bcf3fe27eec9c04e0144ef50db387 Mon Sep 17 00:00:00 2001 From: Nbiba Bedis Date: Wed, 8 Dec 2021 11:45:38 +0100 Subject: [PATCH] Ignore broken symlinks when compressing --- src/archive/tar.rs | 12 +++++++++++- src/archive/zip.rs | 12 +++++++++++- src/lib.rs | 2 ++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/archive/tar.rs b/src/archive/tar.rs index 6b7cd0c..4764caa 100644 --- a/src/archive/tar.rs +++ b/src/archive/tar.rs @@ -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 && path.is_symlink() { + // 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") diff --git a/src/archive/zip.rs b/src/archive/zip.rs index 3d294eb..b07d483 100644 --- a/src/archive/zip.rs +++ b/src/archive/zip.rs @@ -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 && path.is_symlink() { + // This path is for a broken symlink + // We just ignore it + continue; + } + return Err(e.into()); + } + }; writer.write_all(&*file_bytes)?; } } diff --git a/src/lib.rs b/src/lib.rs index e337061..8b35881 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,8 @@ // used as a binary. Since `clap` doesn't remove URL markup in it's help output, // we don't mark them as URLs. This suppresses the relevant rustdoc warning: #![allow(rustdoc::bare_urls)] +// Useful to detect broken symlinks when compressing. (So we can safely ignore them) +#![feature(is_symlink)] // Macros should be declared before pub mod macros;