Skip to content

Commit 9c906da

Browse files
committed
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.
1 parent 66853af commit 9c906da

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/libstd/fs/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ pub fn create_dir<P: AsPath + ?Sized>(path: &P) -> io::Result<()> {
570570
#[stable(feature = "rust1", since = "1.0.0")]
571571
pub fn create_dir_all<P: AsPath + ?Sized>(path: &P) -> io::Result<()> {
572572
let path = path.as_path();
573-
if path.is_dir() { return Ok(()) }
573+
if path == Path::new("") || path.is_dir() { return Ok(()) }
574574
if let Some(p) = path.parent() { try!(create_dir_all(p)) }
575575
create_dir(path)
576576
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::env;
12+
use std::fs::{self, TempDir};
13+
14+
fn main() {
15+
let td = TempDir::new("create-dir-all-bare").unwrap();
16+
env::set_current_dir(td.path()).unwrap();
17+
fs::create_dir_all("create-dir-all-bare").unwrap();
18+
}

0 commit comments

Comments
 (0)