about summary refs log tree commit diff
path: root/src/librustpkg/workcache_support.rs
blob: d49a581e07047b6a6448ffb013854ba5d3ed0825 (plain)
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
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use extra::sha1::Sha1;
use extra::digest::Digest;
use extra::workcache;
use std::io;

/// Hashes the file contents along with the last-modified time
pub fn digest_file_with_date(path: &Path) -> ~str {
    use conditions::bad_path::cond;
    use cond1 = conditions::bad_stat::cond;

    let s = io::read_whole_file_str(path);
    match s {
        Ok(s) => {
            let mut sha = Sha1::new();
            sha.input_str(s);
            let st = match path.stat() {
                Some(st) => st,
                None => cond1.raise((path.clone(), format!("Couldn't get file access time")))
            };
            sha.input_str(st.modified.to_str());
            sha.result_str()
        }
        Err(e) => {
            let path = cond.raise((path.clone(), format!("Couldn't read file: {}", e)));
            // FIXME (#9639): This needs to handle non-utf8 paths
            // XXX: I'm pretty sure this is the wrong return value
            path.as_str().unwrap().to_owned()
        }
    }
}

/// Hashes only the last-modified time
pub fn digest_only_date(path: &Path) -> ~str {
    use cond = conditions::bad_stat::cond;

    let mut sha = Sha1::new();
    let st = match path.stat() {
                Some(st) => st,
                None => cond.raise((path.clone(), format!("Couldn't get file access time")))
    };
    sha.input_str(st.modified.to_str());
    sha.result_str()
}

/// Adds multiple discovered outputs
pub fn discover_outputs(e: &mut workcache::Exec, outputs: ~[Path]) {
    debug!("Discovering {:?} outputs", outputs.len());
    for p in outputs.iter() {
        debug!("Discovering output! {}", p.display());
        // For now, assume that all discovered outputs are binaries
        // FIXME (#9639): This needs to handle non-utf8 paths
        e.discover_output("binary", p.as_str().unwrap(), digest_only_date(p));
    }
}

/// Returns the function name for building a crate
pub fn crate_tag(p: &Path) -> ~str {
    // FIXME (#9639): This needs to handle non-utf8 paths
    p.as_str().unwrap().to_owned() // implicitly, it's "build(p)"...
}