Skip to content

Instantly share code, notes, and snippets.

@BurntSushi
Created August 19, 2018 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BurntSushi/bc44576642c4fe9feedfb9f839245d92 to your computer and use it in GitHub Desktop.
Save BurntSushi/bc44576642c4fe9feedfb9f839245d92 to your computer and use it in GitHub Desktop.
extern crate walkdir;
use std::error::Error;
use std::fs::File;
use std::io::{self, Read, Write};
use std::process;
use walkdir::WalkDir;
fn main() {
if let Err(err) = try_main() {
eprintln!("{}", err);
process::exit(1);
}
}
fn try_main() -> Result<(), Box<Error>> {
let nul = 0;
let stdout = io::stdout();
let mut stdout = stdout.lock();
for entry in WalkDir::new("./").into_iter().filter_map(|e| e.ok()) {
if !entry.file_type().is_file() {
continue;
}
let mut bytes_count = 0;
let mut file = File::open(entry.path())
.map_err(|err| format!("{}: {}", entry.path().display(), err))?;
let mut s = Vec::with_capacity(file.metadata()?.len() as usize);
file.read_to_end(&mut s)?;
for b in s.into_iter() {
if b == nul {
writeln!(
stdout,
"{} bytes={} binary file",
entry.path().display(),
bytes_count
)?;
break;
}
bytes_count += 1;
}
writeln!(stdout, "{} bytes={}", entry.path().display(), bytes_count)?;
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment