about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2015-02-23 15:07:10 -0800
committerAlex Crichton <alex@alexcrichton.com>2015-02-23 15:09:13 -0800
commit79bf783ebf41c756641e90f4295fbf98cd8a11ba (patch)
treeaaa7ea1af2756843220a9080d4de58552343103b /src
parentf0f7ca27de6b4e03f30012656dad270cda55a363 (diff)
downloadrust-79bf783ebf41c756641e90f4295fbf98cd8a11ba.tar.gz
rust-79bf783ebf41c756641e90f4295fbf98cd8a11ba.zip
std: Handle a trailing slash in create_dir_all
If the filename for a path is `None` then we know that the creation of the
parent directory created the whole path so there's no need to retry the call to
`create_dir`.

Closes #22737
Diffstat (limited to 'src')
-rw-r--r--src/libstd/fs.rs16
1 files changed, 15 insertions, 1 deletions
diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs
index 98c1b50a9bf..1f41d715c4e 100644
--- a/src/libstd/fs.rs
+++ b/src/libstd/fs.rs
@@ -540,7 +540,14 @@ pub fn create_dir_all<P: AsPath + ?Sized>(path: &P) -> io::Result<()> {
         Some(p) if p != path => try!(create_dir_all(p)),
         _ => {}
     }
-    create_dir(path)
+    // If the file name of the given `path` is blank then the creation of the
+    // parent directory will have taken care of the whole path for us, so we're
+    // good to go.
+    if path.file_name().is_none() {
+        Ok(())
+    } else {
+        create_dir(path)
+    }
 }
 
 /// Remove an existing, empty directory
@@ -1500,4 +1507,11 @@ mod tests {
         check!(fs::set_permissions(&path, perm));
         check!(fs::remove_file(&path));
     }
+
+    #[test]
+    fn mkdir_trailing_slash() {
+        let tmpdir = tmpdir();
+        let path = tmpdir.join("file");
+        check!(fs::create_dir_all(&path.join("a/")));
+    }
 }