diff options
author | Canop <cano.petrole@gmail.com> | 2021-06-22 16:18:45 +0200 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2021-06-22 16:18:45 +0200 |
commit | d5b5840a786da77faa575941450310be2f27791e (patch) | |
tree | 258dfc80cdaa242b6fef4bc4c74e9bd73821c864 /src | |
parent | f7202f52646302591cc6516c91a583203e904769 (diff) |
fix preview of linux pseudo-files
For some reason, fs::metadata doesn't report a length of
zero anymore, so the old test didn't work.
Diffstat (limited to 'src')
-rw-r--r-- | src/errors.rs | 1 | ||||
-rw-r--r-- | src/preview/preview.rs | 7 | ||||
-rw-r--r-- | src/syntactic/syntactic_view.rs | 8 |
3 files changed, 13 insertions, 3 deletions
diff --git a/src/errors.rs b/src/errors.rs index 388b30d..7b2b01f 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -28,6 +28,7 @@ custom_error! {pub ProgramError ImageError {source: ImageError } = "{}", Lfs {details: String} = "Failed to fetch mounts: {}", ZeroLenFile = "File seems empty", + UnmappableFile = "File can't be mapped", } custom_error! {pub TreeBuildError diff --git a/src/preview/preview.rs b/src/preview/preview.rs index 63e4079..47a6812 100644 --- a/src/preview/preview.rs +++ b/src/preview/preview.rs @@ -82,7 +82,8 @@ impl Preview { } /// build a text preview (maybe with syntaxic coloring) if possible, - /// a hex (binary) view if content isnt't UTF8, or a IOError when + /// a hex (binary) view if content isnt't UTF8, a ZeroLen file if there's + /// no length (it's probably a linux pseudofile) or a IOError when /// there's a IO problem pub fn unfiltered_text( path: &Path, @@ -90,8 +91,8 @@ impl Preview { ) -> Self { match SyntacticView::new(path, InputPattern::none(), &mut Dam::unlimited(), con) { Ok(Some(sv)) => Self::Syntactic(sv), - Err(ProgramError::ZeroLenFile) => { - debug!("zero len file - check if system file"); + Err(ProgramError::ZeroLenFile | ProgramError::UnmappableFile) => { + debug!("zero len or unmappable file - check if system file"); Self::ZeroLen(ZeroLenFileView::new(path.to_path_buf())) } // not previewable as UTF8 text diff --git a/src/syntactic/syntactic_view.rs b/src/syntactic/syntactic_view.rs index d23d88e..b632171 100644 --- a/src/syntactic/syntactic_view.rs +++ b/src/syntactic/syntactic_view.rs @@ -104,6 +104,14 @@ impl SyntacticView { con: &AppContext, ) -> Result<bool, ProgramError> { let f = File::open(&self.path)?; + { + // if we detect the file isn't mappable, we'll + // let the ZeroLenFilePreview try to read it + let mmap = unsafe { Mmap::map(&f) }; + if mmap.is_err() { + return Err(ProgramError::UnmappableFile); + } + } let md = f.metadata()?; if md.len() == 0 { return Err(ProgramError::ZeroLenFile); |