1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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 // idk if this is correct on bsd/linux
    } else {
        m.len()
    }
}

/// Gather the information from `.gitignore`, `.ignore`, and darcs `boring` files in a given
/// directory, and assemble a `RegexSet` from it.
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.",
        ); // ok because we check that the file exists
        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.",
        ); // ok because we check that the file exists
        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.",
        ); // ok because we check that the file exists
        Some(darcs_contents_to_regex(&contents, &darcs_path))
    } else {
        None
    }
}

/// Helper function to get the number of CPUs. We subtract 1, because the main thread that's doing
/// the spawning counts as one OS thread.
pub fn get_processors() -> usize {
    let n = get();
    if n > 1 { n - 1 } else { n }
}