Skip to content

Commit

Permalink
Ensure memmap source is not empty
Browse files Browse the repository at this point in the history
resolves #74
  • Loading branch information
chmln committed Apr 22, 2020
1 parent 4a57005 commit 39c29b2
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/input.rs
Expand Up @@ -84,6 +84,13 @@ impl Replacer {
self.regex.is_match(content)
}

fn check_not_empty(path: &Path) -> Result<()> {
let mut buf: [u8; 1] = Default::default();
let mut file = std::fs::File::open(path)?;
file.read_exact(&mut buf)?;
Ok(())
}

fn replace<'a>(&'a self, content: &'a [u8]) -> std::borrow::Cow<'a, [u8]> {
if self.is_literal {
self.regex.replace_all(
Expand All @@ -99,7 +106,10 @@ impl Replacer {
use memmap::{Mmap, MmapMut};
use std::ops::DerefMut;

let path = std::path::Path::new(path);
if let Err(_) = Self::check_not_empty(path) {
return Ok(());
}

let source = File::open(path)?;
let meta = source.metadata()?;
let mmap_source = unsafe { Mmap::map(&source)? };
Expand Down

0 comments on commit 39c29b2

Please sign in to comment.