extern crate num_cpus;
use std::fs::File;
use std::io::prelude::*;
use regex::RegexSet;
use gitignore::*;
use std::path::PathBuf;
use self::num_cpus::get;
use std::fs::Metadata;
#[cfg(target_os = "linux")]
use std::os::linux::fs::MetadataExt;
#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "netbsd"))]
use std::os::unix::fs::MetadataExt;
#[cfg(target_os = "linux")]
pub fn size(m: &Metadata, blocks: bool) -> u64 {
if blocks { m.st_blocks() * 512 } else { m.len() }
}
#[cfg(target_os = "windows")]
pub fn size(m: &Metadata, _: bool) -> u64 {
m.len()
}
#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "netbsd"))]
pub fn size(m: &Metadata, blocks: bool) -> u64 {
if blocks {
m.blocks() * 512 } else {
m.len()
}
}
pub fn mk_ignores(in_paths: &PathBuf, maybe_ignore: &Option<RegexSet>) -> Option<RegexSet> {
if let Some(ref ignore) = *maybe_ignore {
Some(ignore.to_owned())
} else if let (ignore_path, Ok(mut file)) =
{
let mut ignore_path = in_paths.clone();
ignore_path.push(".ignore");
(ignore_path.clone(), File::open(ignore_path.clone()))
}
{
let mut contents = String::new();
file.read_to_string(&mut contents).expect(
"File read failed.",
); Some(file_contents_to_regex(&contents, &ignore_path))
} else if let (gitignore_path, Ok(mut file)) =
{
let mut gitignore_path = in_paths.clone();
gitignore_path.push(".gitignore");
(gitignore_path.clone(), File::open(gitignore_path))
}
{
let mut contents = String::new();
file.read_to_string(&mut contents).expect(
"File read failed.",
); Some(file_contents_to_regex(&contents, &gitignore_path))
} else if let (darcs_path, Ok(mut file)) =
{
let mut darcs_path = in_paths.clone();
darcs_path.push("_darcs/prefs/boring");
(darcs_path.clone(), File::open(darcs_path))
}
{
let mut contents = String::new();
file.read_to_string(&mut contents).expect(
"File read failed.",
); Some(darcs_contents_to_regex(&contents, &darcs_path))
} else {
None
}
}
pub fn get_processors() -> usize {
let n = get();
if n > 1 { n - 1 } else { n }
}