From 9c906da7ade767925aca1da06f139152835b661b Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 14 Mar 2015 23:40:12 -0700 Subject: [PATCH] std: Fix create_dir_all for empty paths Recent changes in path semantics meant that if none of the components in a relative path existed as a part of a call to `create_dir_all` then the call would fail as `create_dir("")` would be attempted and would fail with an OS error. --- src/libstd/fs/mod.rs | 2 +- src/test/run-pass/create-dir-all-bare.rs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 src/test/run-pass/create-dir-all-bare.rs diff --git a/src/libstd/fs/mod.rs b/src/libstd/fs/mod.rs index ac1f5993aa145..813283ed16465 100644 --- a/src/libstd/fs/mod.rs +++ b/src/libstd/fs/mod.rs @@ -570,7 +570,7 @@ pub fn create_dir(path: &P) -> io::Result<()> { #[stable(feature = "rust1", since = "1.0.0")] pub fn create_dir_all(path: &P) -> io::Result<()> { let path = path.as_path(); - if path.is_dir() { return Ok(()) } + if path == Path::new("") || path.is_dir() { return Ok(()) } if let Some(p) = path.parent() { try!(create_dir_all(p)) } create_dir(path) } diff --git a/src/test/run-pass/create-dir-all-bare.rs b/src/test/run-pass/create-dir-all-bare.rs new file mode 100644 index 0000000000000..3a4286c292709 --- /dev/null +++ b/src/test/run-pass/create-dir-all-bare.rs @@ -0,0 +1,18 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::env; +use std::fs::{self, TempDir}; + +fn main() { + let td = TempDir::new("create-dir-all-bare").unwrap(); + env::set_current_dir(td.path()).unwrap(); + fs::create_dir_all("create-dir-all-bare").unwrap(); +}