Skip to content

Commit 9710b31

Browse files
committed
Fix race condition in fs::create_dir_all
The code would crash if the directory was created after create_dir_all checked whether the directory already existed. This was contrary to the documentation which claimed to create the directory if it doesn't exist, implying (but not stating) that there would not be a failure due to the directory existing.
1 parent dfe88bf commit 9710b31

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/libstd/fs.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1312,11 +1312,20 @@ impl DirBuilder {
13121312
}
13131313

13141314
fn create_dir_all(&self, path: &Path) -> io::Result<()> {
1315+
use io::{ErrorKind};
13151316
if path == Path::new("") || path.is_dir() { return Ok(()) }
13161317
if let Some(p) = path.parent() {
13171318
try!(self.create_dir_all(p))
13181319
}
1319-
self.inner.mkdir(path)
1320+
match self.inner.mkdir(path) {
1321+
Err(ref e) if e.kind() == ErrorKind::AlreadyExists
1322+
&& path.is_dir() => {
1323+
// It looks like the directory was created after
1324+
// we checked, so this is not an error after all.
1325+
Ok(())
1326+
},
1327+
r => r,
1328+
}
13201329
}
13211330
}
13221331

0 commit comments

Comments
 (0)