From 8008fe4b169b8e65d80a0f2ddf2be41568c2be02 Mon Sep 17 00:00:00 2001 From: Mark Owen Date: Mon, 5 Dec 2022 12:26:58 -0700 Subject: [PATCH] Build even when broken symlinks are present Better resolution to #1396 --- src/utils/fs.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/utils/fs.rs b/src/utils/fs.rs index 0d6f3837..4c42c312 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -1,5 +1,5 @@ use crate::errors::*; -use log::{debug, trace}; +use log::{debug, info, trace}; use std::convert::Into; use std::fs::{self, File}; use std::io::Write; @@ -113,7 +113,13 @@ pub fn copy_files_except_ext( let entry = entry?; let metadata = entry .path() + // Attempt to read metadata of symlink target .metadata() + // Handle broken symlinks by reading the symlink metadata directly (not the symlink target metadata) + .or_else(|_| { + info!("Failed to read file metadata of {:?}. Attempting to read metadata without traversing symlinks.", entry.path()); + entry.path().symlink_metadata() + }) .with_context(|| format!("Failed to read {:?}", entry.path()))?; // If the entry is a dir and the recursive option is enabled, call itself @@ -238,6 +244,12 @@ mod tests { ) { panic!("Could not symlink file.png: {}", err); } + if let Err(err) = symlink( + &tmp.path().join("missing_file.txt"), + &tmp.path().join("missing_symlink.txt"), + ) { + panic!("Could not symlink missing_file.txt: {}", err); + } // Create output dir if let Err(err) = fs::create_dir(&tmp.path().join("output")) {