]> git.lizzy.rs Git - rust.git/commitdiff
fs: use an iterative algorithm for 'mkdir_recursive'
authorLaurent Bonnans <bonnans.l@gmail.com>
Wed, 26 Feb 2014 16:50:02 +0000 (17:50 +0100)
committerLaurent Bonnans <bonnans.l@gmail.com>
Mon, 10 Mar 2014 18:24:28 +0000 (19:24 +0100)
as requested in #6109

src/libstd/io/fs.rs

index 79e191a9ec90d93a0e278da78212f9f195bb1c8a..3ad35e3124bb87eb2799d97652e5a36a6cbdabe6 100644 (file)
@@ -528,10 +528,25 @@ pub fn mkdir_recursive(path: &Path, mode: FilePermission) -> IoResult<()> {
     if path.is_dir() {
         return Ok(())
     }
-    if path.filename().is_some() {
-        try!(mkdir_recursive(&path.dir_path(), mode));
+
+    let mut comps = path.components();
+    let mut curpath = path.root_path().unwrap_or(Path::new("."));
+
+    for c in comps {
+        curpath.push(c);
+
+        match mkdir(&curpath, mode) {
+            Err(mkdir_err) => {
+                // already exists ?
+                if try!(stat(&curpath)).kind != io::TypeDirectory {
+                    return Err(mkdir_err);
+                }
+            }
+            Ok(()) => ()
+        }
     }
-    mkdir(path, mode)
+
+    Ok(())
 }
 
 /// Removes a directory at this path, after removing all its contents. Use