Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions src/librustc/util/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,3 @@ pub fn rename_or_copy_remove<P: AsRef<Path>, Q: AsRef<Path>>(p: P,
}
}
}

// Like std::fs::create_dir_all, except handles concurrent calls among multiple
// threads or processes.
pub 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),
}
}
2 changes: 1 addition & 1 deletion src/librustc_incremental/persist/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ fn generate_session_dir_path(crate_dir: &Path) -> PathBuf {
}

fn create_dir(sess: &Session, path: &Path, dir_tag: &str) -> Result<(),()> {
match fs_util::create_dir_racy(path) {
match std_fs::create_dir_all(path) {
Ok(()) => {
debug!("{} directory created successfully", dir_tag);
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_save_analysis/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ pub fn process_crate<'l, 'tcx>(tcx: TyCtxt<'l, 'tcx, 'tcx>,
},
};

if let Err(e) = rustc::util::fs::create_dir_racy(&root_path) {
if let Err(e) = std::fs::create_dir_all(&root_path) {
tcx.sess.err(&format!("Could not create directory {}: {}",
root_path.display(),
e));
Expand Down
59 changes: 55 additions & 4 deletions src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,12 @@ pub fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
/// error conditions for when a directory is being created (after it is
/// determined to not exist) are outlined by `fs::create_dir`.
///
/// Notable exception is made for situations where any of the directories
/// specified in the `path` could not be created as it was created concurrently.
/// Such cases are considered success. In other words: calling `create_dir_all`
/// concurrently from multiple threads or processes is guaranteed to not fail
/// due to race itself.
///
/// # Examples
///
/// ```
Expand Down Expand Up @@ -1769,11 +1775,25 @@ impl DirBuilder {
}

fn create_dir_all(&self, path: &Path) -> io::Result<()> {
if path == Path::new("") || path.is_dir() { return Ok(()) }
if let Some(p) = path.parent() {
self.create_dir_all(p)?
if path == Path::new("") {
return Ok(())
}

match self.inner.mkdir(path) {
Ok(()) => return Ok(()),
Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
Err(_) if path.is_dir() => return Ok(()),
Err(e) => return Err(e),
}
match path.parent() {
Some(p) => try!(self.create_dir_all(p)),
None => return Err(io::Error::new(io::ErrorKind::Other, "failed to create whole tree")),
}
match self.inner.mkdir(path) {
Ok(()) => Ok(()),
Err(_) if path.is_dir() => Ok(()),
Err(e) => Err(e),
}
self.inner.mkdir(path)
}
}

Expand All @@ -1793,6 +1813,7 @@ mod tests {
use rand::{StdRng, Rng};
use str;
use sys_common::io::test::{TempDir, tmpdir};
use thread;

#[cfg(windows)]
use os::windows::fs::{symlink_dir, symlink_file};
Expand Down Expand Up @@ -2260,11 +2281,41 @@ mod tests {
assert!(result.is_err());
}

#[test]
fn concurrent_recursive_mkdir() {
for _ in 0..50 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this test really need to be run 50 times? It fails the first time for me on Windows.

Copy link
Contributor

@retep998 retep998 Mar 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because it relies on there being a race condition between the threads, but race conditions don't occur very often. By doing the test multiple times, it is much more likely to hit the race condition.

let mut dir = tmpdir().join("a");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the main problem here is this use of tmpdir(). It deletes the path it returns when it goes out of scope and because it's not assigned to a variable that's immediately. See here. Changing this to the following should fix that:

let tmpdir = tmpdir();
let mut dir = tmpdir.join("a");

for _ in 0..100 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you reduced the number of times this test is run rather than the the length of the path 😉.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh dear. 🤦‍

dir = dir.join("a");
}
let mut join = vec!();
for _ in 0..8 {
let dir = dir.clone();
join.push(thread::spawn(move || {
check!(fs::create_dir_all(&dir));
}))
}

// No `Display` on result of `join()`
join.drain(..).map(|join| join.join().unwrap()).count();
}
}

#[test]
fn recursive_mkdir_slash() {
check!(fs::create_dir_all(&Path::new("/")));
}

#[test]
fn recursive_mkdir_dot() {
check!(fs::create_dir_all(&Path::new(".")));
}

#[test]
fn recursive_mkdir_empty() {
check!(fs::create_dir_all(&Path::new("")));
}

#[test]
fn recursive_rmdir() {
let tmpdir = tmpdir();
Expand Down
28 changes: 6 additions & 22 deletions src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use util::logv;
use std::collections::HashSet;
use std::env;
use std::fmt;
use std::fs::{self, File};
use std::fs::{self, File, create_dir_all};
use std::io::prelude::*;
use std::io::{self, BufReader};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -395,7 +395,7 @@ actual:\n\

let out_dir = self.output_base_name().with_extension("pretty-out");
let _ = fs::remove_dir_all(&out_dir);
self.create_dir_racy(&out_dir);
create_dir_all(&out_dir).unwrap();

// FIXME (#9639): This needs to handle non-utf8 paths
let mut args = vec!["-".to_owned(),
Expand Down Expand Up @@ -1269,7 +1269,7 @@ actual:\n\

fn compose_and_run_compiler(&self, args: ProcArgs, input: Option<String>) -> ProcRes {
if !self.props.aux_builds.is_empty() {
self.create_dir_racy(&self.aux_output_dir_name());
create_dir_all(&self.aux_output_dir_name()).unwrap();
}

let aux_dir = self.aux_output_dir_name();
Expand Down Expand Up @@ -1340,22 +1340,6 @@ actual:\n\
input)
}

// Like std::fs::create_dir_all, except handles concurrent calls among multiple
// threads or processes.
fn create_dir_racy(&self, path: &Path) {
match fs::create_dir(path) {
Ok(()) => return,
Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => return,
Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
Err(e) => panic!("failed to create dir {:?}: {}", path, e),
}
self.create_dir_racy(path.parent().unwrap());
match fs::create_dir(path) {
Ok(()) => {}
Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => {}
Err(e) => panic!("failed to create dir {:?}: {}", path, e),
}
}

fn compose_and_run(&self,
ProcArgs{ args, prog }: ProcArgs,
Expand Down Expand Up @@ -1435,7 +1419,7 @@ actual:\n\


let mir_dump_dir = self.get_mir_dump_dir();
self.create_dir_racy(mir_dump_dir.as_path());
create_dir_all(mir_dump_dir.as_path()).unwrap();
let mut dir_opt = "dump-mir-dir=".to_string();
dir_opt.push_str(mir_dump_dir.to_str().unwrap());
debug!("dir_opt: {:?}", dir_opt);
Expand Down Expand Up @@ -1923,7 +1907,7 @@ actual:\n\

let out_dir = self.output_base_name();
let _ = fs::remove_dir_all(&out_dir);
self.create_dir_racy(&out_dir);
create_dir_all(&out_dir).unwrap();

let proc_res = self.document(&out_dir);
if !proc_res.status.success() {
Expand Down Expand Up @@ -2299,7 +2283,7 @@ actual:\n\
if tmpdir.exists() {
self.aggressive_rm_rf(&tmpdir).unwrap();
}
self.create_dir_racy(&tmpdir);
create_dir_all(&tmpdir).unwrap();

let host = &self.config.host;
let make = if host.contains("bitrig") || host.contains("dragonfly") ||
Expand Down