Skip to content

Commit 8688b1b

Browse files
committed
core: Add os::walk_dir
1 parent 7235f3c commit 8688b1b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/libcore/os.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export homedir, list_dir, list_dir_path, path_is_dir, path_exists,
3434
copy_file;
3535
export last_os_error;
3636
export set_exit_status;
37+
export walk_dir;
3738

3839
// FIXME: move these to str perhaps?
3940
export as_c_charp, fill_charp_buf;
@@ -387,7 +388,34 @@ fn homedir() -> option<path> {
387388
}
388389
}
389390

391+
#[doc = "Recursively walk a directory structure"]
392+
fn walk_dir(p: path, f: fn(path) -> bool) {
390393

394+
walk_dir_(p, f);
395+
396+
fn walk_dir_(p: path, f: fn(path) -> bool) -> bool {
397+
let mut keepgoing = true;
398+
list_dir(p).each {|q|
399+
let path = path::connect(p, q);
400+
if !f(path) {
401+
keepgoing = false;
402+
false
403+
} else {
404+
if path_is_dir(path) {
405+
if !walk_dir_(path, f) {
406+
keepgoing = false;
407+
false
408+
} else {
409+
true
410+
}
411+
} else {
412+
true
413+
}
414+
}
415+
}
416+
ret keepgoing;
417+
}
418+
}
391419

392420
#[doc = "Indicates whether a path represents a directory"]
393421
fn path_is_dir(p: path) -> bool {

0 commit comments

Comments
 (0)