]> git.lizzy.rs Git - rust.git/blob - src/librustc_incremental/persist/util.rs
rename the dep-graph file to include crate ident
[rust.git] / src / librustc_incremental / persist / util.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use rustc::middle::cstore::LOCAL_CRATE;
12 use rustc::ty::TyCtxt;
13
14 use std::fs;
15 use std::io;
16 use std::path::{PathBuf, Path};
17
18 pub fn dep_graph_path(tcx: TyCtxt) -> Option<PathBuf> {
19     // For now, just save/load dep-graph from
20     // directory/dep_graph.rbml
21     tcx.sess.opts.incremental.as_ref().and_then(|incr_dir| {
22         match create_dir_racy(&incr_dir) {
23             Ok(()) => {}
24             Err(err) => {
25                 tcx.sess.err(
26                     &format!("could not create the directory `{}`: {}",
27                              incr_dir.display(), err));
28                 return None;
29             }
30         }
31
32         let crate_name = tcx.crate_name(LOCAL_CRATE);
33         let crate_disambiguator = tcx.crate_disambiguator(LOCAL_CRATE);
34         let file_name = format!("dep-graph-{}-{}.bin",
35                                 crate_name,
36                                 crate_disambiguator);
37         Some(incr_dir.join(file_name))
38     })
39 }
40
41 // Like std::fs::create_dir_all, except handles concurrent calls among multiple
42 // threads or processes.
43 fn create_dir_racy(path: &Path) -> io::Result<()> {
44     match fs::create_dir(path) {
45         Ok(()) => return Ok(()),
46         Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => return Ok(()),
47         Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
48         Err(e) => return Err(e),
49     }
50     match path.parent() {
51         Some(p) => try!(create_dir_racy(p)),
52         None => return Err(io::Error::new(io::ErrorKind::Other,
53                                           "failed to create whole tree")),
54     }
55     match fs::create_dir(path) {
56         Ok(()) => Ok(()),
57         Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => Ok(()),
58         Err(e) => Err(e),
59     }
60 }