]> git.lizzy.rs Git - rust.git/blob - src/librustc_incremental/persist/util.rs
Auto merge of #33541 - eddyb:promote-only-temps, r=arielb1
[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::ty::TyCtxt;
12
13 use std::fs;
14 use std::io;
15 use std::path::{PathBuf, Path};
16
17 pub fn dep_graph_path(tcx: TyCtxt) -> Option<PathBuf> {
18     // For now, just save/load dep-graph from
19     // directory/dep_graph.rbml
20     tcx.sess.opts.incremental.as_ref().and_then(|incr_dir| {
21         match create_dir_racy(&incr_dir) {
22             Ok(()) => {}
23             Err(err) => {
24                 tcx.sess.err(
25                     &format!("could not create the directory `{}`: {}",
26                              incr_dir.display(), err));
27                 return None;
28             }
29         }
30
31         Some(incr_dir.join("dep_graph.rbml"))
32     })
33 }
34
35 // Like std::fs::create_dir_all, except handles concurrent calls among multiple
36 // threads or processes.
37 fn create_dir_racy(path: &Path) -> io::Result<()> {
38     match fs::create_dir(path) {
39         Ok(()) => return Ok(()),
40         Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => return Ok(()),
41         Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
42         Err(e) => return Err(e),
43     }
44     match path.parent() {
45         Some(p) => try!(create_dir_racy(p)),
46         None => return Err(io::Error::new(io::ErrorKind::Other,
47                                           "failed to create whole tree")),
48     }
49     match fs::create_dir(path) {
50         Ok(()) => Ok(()),
51         Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => Ok(()),
52         Err(e) => Err(e),
53     }
54 }