Skip to content

Commit 5c9208f

Browse files
committed
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.
2 parents 758f374 + e412af2 commit 5c9208f

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/build_helper/lib.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212

1313
extern crate filetime;
1414

15-
use std::{fs, env};
1615
use std::fs::File;
17-
use std::process::{Command, Stdio};
16+
use std::io;
1817
use std::path::{Path, PathBuf};
18+
use std::process::{Command, Stdio};
19+
use std::{fs, env};
1920

2021
use filetime::FileTime;
2122

@@ -196,7 +197,7 @@ pub fn native_lib_boilerplate(src_name: &str,
196197

197198
let out_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap());
198199
let out_dir = PathBuf::from(out_dir).join(out_name);
199-
let _ = fs::create_dir_all(&out_dir);
200+
t!(create_dir_racy(&out_dir));
200201
println!("cargo:rustc-link-lib=static={}", link_name);
201202
println!("cargo:rustc-link-search=native={}", out_dir.join(search_subdir).display());
202203

@@ -223,3 +224,21 @@ fn fail(s: &str) -> ! {
223224
println!("\n\n{}\n\n", s);
224225
std::process::exit(1);
225226
}
227+
228+
fn create_dir_racy(path: &Path) -> io::Result<()> {
229+
match fs::create_dir(path) {
230+
Ok(()) => return Ok(()),
231+
Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => return Ok(()),
232+
Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
233+
Err(e) => return Err(e),
234+
}
235+
match path.parent() {
236+
Some(p) => try!(create_dir_racy(p)),
237+
None => return Err(io::Error::new(io::ErrorKind::Other, "failed to create whole tree")),
238+
}
239+
match fs::create_dir(path) {
240+
Ok(()) => Ok(()),
241+
Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => Ok(()),
242+
Err(e) => Err(e),
243+
}
244+
}

0 commit comments

Comments
 (0)