about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-03-09 06:06:34 +0000
committerbors <bors@rust-lang.org>2017-03-09 06:06:34 +0000
commit5c9208faf1f180cd15cf93f74f1e57b24856d11e (patch)
tree0d6cd4274d9abf0a976baea94459942a34155b8e
parent758f37480be34e657abd3870c863e6e7edb1cd30 (diff)
parente412af2a887da25c987d4ca367b7b471b6c5fb55 (diff)
downloadrust-5c9208faf1f180cd15cf93f74f1e57b24856d11e.tar.gz
rust-5c9208faf1f180cd15cf93f74f1e57b24856d11e.zip
Auto merge of #40337 - alexcrichton:racy-dirs, r=brson
rustbuild: Assert directory creation succeeds

I've been seeing failures on the bots when building jemalloc and my assumption
is that it's because cwd isn't created. That may be possible if this
`create_dir_all` call change in this commit fails, in which case we ignore the
error.

This commit updates the location to call `create_dir_racy` which handles
concurrent invocations, as multiple build scripts may be trying to create the
`native` dir.
-rw-r--r--src/build_helper/lib.rs25
1 files changed, 22 insertions, 3 deletions
diff --git a/src/build_helper/lib.rs b/src/build_helper/lib.rs
index dffaebbd929..cb58a916fb7 100644
--- a/src/build_helper/lib.rs
+++ b/src/build_helper/lib.rs
@@ -12,10 +12,11 @@
 
 extern crate filetime;
 
-use std::{fs, env};
 use std::fs::File;
-use std::process::{Command, Stdio};
+use std::io;
 use std::path::{Path, PathBuf};
+use std::process::{Command, Stdio};
+use std::{fs, env};
 
 use filetime::FileTime;
 
@@ -196,7 +197,7 @@ pub fn native_lib_boilerplate(src_name: &str,
 
     let out_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap());
     let out_dir = PathBuf::from(out_dir).join(out_name);
-    let _ = fs::create_dir_all(&out_dir);
+    t!(create_dir_racy(&out_dir));
     println!("cargo:rustc-link-lib=static={}", link_name);
     println!("cargo:rustc-link-search=native={}", out_dir.join(search_subdir).display());
 
@@ -223,3 +224,21 @@ fn fail(s: &str) -> ! {
     println!("\n\n{}\n\n", s);
     std::process::exit(1);
 }
+
+fn create_dir_racy(path: &Path) -> io::Result<()> {
+    match fs::create_dir(path) {
+        Ok(()) => return Ok(()),
+        Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => return Ok(()),
+        Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
+        Err(e) => return Err(e),
+    }
+    match path.parent() {
+        Some(p) => try!(create_dir_racy(p)),
+        None => return Err(io::Error::new(io::ErrorKind::Other, "failed to create whole tree")),
+    }
+    match fs::create_dir(path) {
+        Ok(()) => Ok(()),
+        Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => Ok(()),
+        Err(e) => Err(e),
+    }
+}