Skip to content

Commit 0fcd5d5

Browse files
committed
fs: use an iterative algorithm for 'mkdir_recursive'
as requested in #6109
1 parent 62f1d68 commit 0fcd5d5

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/libstd/io/fs.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,10 +528,25 @@ pub fn mkdir_recursive(path: &Path, mode: FilePermission) -> IoResult<()> {
528528
if path.is_dir() {
529529
return Ok(())
530530
}
531-
if path.filename().is_some() {
532-
try!(mkdir_recursive(&path.dir_path(), mode));
531+
532+
let mut comps = path.components();
533+
let mut curpath = path.root_path().unwrap_or(Path::new("."));
534+
535+
for c in comps {
536+
curpath.push(c);
537+
538+
match mkdir(&curpath, mode) {
539+
Err(mkdir_err) => {
540+
// already exists ?
541+
if try!(stat(&curpath)).kind != io::TypeDirectory {
542+
return Err(mkdir_err);
543+
}
544+
}
545+
Ok(()) => ()
546+
}
533547
}
534-
mkdir(path, mode)
548+
549+
Ok(())
535550
}
536551

537552
/// Removes a directory at this path, after removing all its contents. Use

0 commit comments

Comments
 (0)