Skip to content

Commit 79bf783

Browse files
committed
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
1 parent f0f7ca2 commit 79bf783

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/libstd/fs.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,14 @@ pub fn create_dir_all<P: AsPath + ?Sized>(path: &P) -> io::Result<()> {
540540
Some(p) if p != path => try!(create_dir_all(p)),
541541
_ => {}
542542
}
543-
create_dir(path)
543+
// If the file name of the given `path` is blank then the creation of the
544+
// parent directory will have taken care of the whole path for us, so we're
545+
// good to go.
546+
if path.file_name().is_none() {
547+
Ok(())
548+
} else {
549+
create_dir(path)
550+
}
544551
}
545552

546553
/// Remove an existing, empty directory
@@ -1500,4 +1507,11 @@ mod tests {
15001507
check!(fs::set_permissions(&path, perm));
15011508
check!(fs::remove_file(&path));
15021509
}
1510+
1511+
#[test]
1512+
fn mkdir_trailing_slash() {
1513+
let tmpdir = tmpdir();
1514+
let path = tmpdir.join("file");
1515+
check!(fs::create_dir_all(&path.join("a/")));
1516+
}
15031517
}

0 commit comments

Comments
 (0)