1extern crate num_cpus;
2
3use self::num_cpus::get;
4use gitignore::*;
5use regex::RegexSet;
6use std::fs::File;
7use std::fs::Metadata;
8use std::io::prelude::*;
9use std::path::PathBuf;
10
11#[cfg(target_os = "linux")]
12use std::os::linux::fs::MetadataExt;
13
14#[cfg(any(
15 target_os = "macos",
16 target_os = "freebsd",
17 target_os = "netbsd",
18 target_os = "dragonfly",
19 target_os = "solaris"
20))]
21use std::os::unix::fs::MetadataExt;
22
23#[cfg(target_os = "linux")]
24pub fn size(m: &Metadata, blocks: bool) -> u64 {
25 if blocks {
26 m.st_blocks() * 512
27 } else {
28 m.len()
29 }
30}
31
32#[cfg(any(target_os = "windows", target_os = "redox"))]
33pub fn size(m: &Metadata, _: bool) -> u64 {
34 m.len()
35}
36
37#[cfg(any(
38 target_os = "macos",
39 target_os = "freebsd",
40 target_os = "netbsd",
41 target_os = "dragonfly",
42 target_os = "solaris"
43))]
44pub fn size(m: &Metadata, blocks: bool) -> u64 {
45 if blocks {
46 m.blocks() * 512 } else {
48 m.len()
49 }
50}
51
52pub fn mk_ignores(in_paths: &PathBuf, maybe_ignore: &Option<RegexSet>) -> Option<RegexSet> {
55 if let Some(ref ignore) = *maybe_ignore {
56 Some(ignore.to_owned())
57 } else if let (ignore_path, Ok(mut file)) = {
58 let mut ignore_path = in_paths.clone();
59 ignore_path.push(".ignore");
60 (ignore_path.clone(), File::open(ignore_path))
61 } {
62 let mut contents = String::new();
63 file.read_to_string(&mut contents)
64 .expect("File read failed."); Some(file_contents_to_regex(&contents, &ignore_path))
66 } else if let (gitignore_path, Ok(mut file)) = {
67 let mut gitignore_path = in_paths.clone();
68 gitignore_path.push(".gitignore");
69 (gitignore_path.clone(), File::open(gitignore_path))
70 } {
71 let mut contents = String::new();
72 file.read_to_string(&mut contents)
73 .expect("File read failed."); Some(file_contents_to_regex(&contents, &gitignore_path))
75 } else if let (darcs_path, Ok(mut file)) = {
76 let mut darcs_path = in_paths.clone();
77 darcs_path.push("_darcs/prefs/boring");
78 (darcs_path.clone(), File::open(darcs_path))
79 } {
80 let mut contents = String::new();
81 file.read_to_string(&mut contents)
82 .expect("File read failed."); Some(darcs_contents_to_regex(&contents, &darcs_path))
84 } else {
85 None
86 }
87}
88
89pub fn get_processors() -> usize {
92 let n = get();
93 if n > 1 {
94 n - 1
95 } else {
96 n
97 }
98}