Răsfoiți Sursa

Merge pull request #262 from cfxegbert/bad-symlinks-not-showing

fix: canonicalize errors when the destination of a symbolic link is bad
Preston Thorpe 2 ani în urmă
părinte
comite
0893ce1b81
3 a modificat fișierele cu 10 adăugiri și 9 ștergeri
  1. 1 1
      src/fs/feature/git.rs
  2. 8 7
      src/fs/file.rs
  3. 1 1
      src/output/file_name.rs

+ 1 - 1
src/fs/feature/git.rs

@@ -332,7 +332,7 @@ fn reorient(path: &Path) -> PathBuf {
 
 #[cfg(windows)]
 fn reorient(path: &Path) -> PathBuf {
-    let unc_path = path.canonicalize().unwrap();
+    let unc_path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
     // On Windows UNC path is returned. We need to strip the prefix for it to work.
     let normal_path = unc_path.as_os_str().to_str().unwrap().trim_start_matches("\\\\?\\");
     PathBuf::from(normal_path)

+ 8 - 7
src/fs/file.rs

@@ -83,7 +83,8 @@ pub struct File<'dir> {
     pub extended_attributes: Vec<Attribute>,
 
     /// The absolute value of this path, used to look up mount points.
-    pub absolute_path: PathBuf,}
+    pub absolute_path: Option<PathBuf>,
+}
 
 impl<'dir> File<'dir> {
     pub fn from_args<PD, FN>(path: PathBuf, parent_dir: PD, filename: FN, deref_links: bool) -> io::Result<File<'dir>>
@@ -98,7 +99,7 @@ impl<'dir> File<'dir> {
         let metadata   = std::fs::symlink_metadata(&path)?;
         let is_all_all = false;
         let extended_attributes = File::gather_extended_attributes(&path);
-        let absolute_path = std::fs::canonicalize(&path)?;
+        let absolute_path = std::fs::canonicalize(&path).ok();
 
         Ok(File { name, ext, path, metadata, parent_dir, is_all_all, deref_links, extended_attributes, absolute_path })
     }
@@ -112,7 +113,7 @@ impl<'dir> File<'dir> {
         let is_all_all = true;
         let parent_dir = Some(parent_dir);
         let extended_attributes = File::gather_extended_attributes(&path);
-        let absolute_path = std::fs::canonicalize(&path)?;
+        let absolute_path = std::fs::canonicalize(&path).ok();
 
         Ok(File { path, parent_dir, metadata, ext, name: ".".into(), is_all_all, deref_links: false, extended_attributes, absolute_path })
     }
@@ -125,7 +126,7 @@ impl<'dir> File<'dir> {
         let is_all_all = true;
         let parent_dir = Some(parent_dir);
         let extended_attributes = File::gather_extended_attributes(&path);
-        let absolute_path = std::fs::canonicalize(&path)?;
+        let absolute_path = std::fs::canonicalize(&path).ok();
 
         Ok(File { path, parent_dir, metadata, ext, name: "..".into(), is_all_all, deref_links: false, extended_attributes, absolute_path })
     }
@@ -253,7 +254,7 @@ impl<'dir> File<'dir> {
     /// Whether this file is a mount point
     pub fn is_mount_point(&self) -> bool {
         if cfg!(target_os = "linux") && self.is_directory() {
-            return ALL_MOUNTS.contains_key(&self.absolute_path);
+            return self.absolute_path.as_ref().is_some_and(|p|ALL_MOUNTS.contains_key(p));
         }
         false
     }
@@ -261,7 +262,7 @@ impl<'dir> File<'dir> {
     /// The filesystem device and type for a mount point
     pub fn mount_point_info(&self) -> Option<&MountedFs> {
         if cfg!(target_os = "linux") {
-            return ALL_MOUNTS.get(&self.absolute_path);
+            return self.absolute_path.as_ref().and_then(|p|ALL_MOUNTS.get(p));
         }
         None
     }
@@ -324,7 +325,7 @@ impl<'dir> File<'dir> {
                     is_all_all: false,
                     deref_links: self.deref_links,
                     extended_attributes,
-                    absolute_path
+                    absolute_path: Some(absolute_path)
                 };
                 FileTarget::Ok(Box::new(file))
             }

+ 1 - 1
src/output/file_name.rs

@@ -350,7 +350,7 @@ impl<'a, 'dir, C: Colours> FileName<'a, 'dir, C> {
 
         let mut display_hyperlink = false;
         if self.options.embed_hyperlinks == EmbedHyperlinks::On {
-            if let Some(abs_path) = self.file.path.canonicalize().unwrap().as_os_str().to_str() {
+            if let Some(abs_path) = self.file.absolute_path.as_ref().and_then(|p| p.as_os_str().to_str()) {
                 bits.insert(0, ANSIString::from(format!(
                     "{}file://{}{}{}",
                     HYPERLINK_START,