Skip to content

Commit edd28b9

Browse files
committed
fs: Added a recursive create_dir function
1 parent 0ebae3f commit edd28b9

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/fs/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,19 @@ pub fn create_dir(path: &str, mode: AccessPermission) -> io::Result<()> {
402402
})
403403
}
404404

405+
/// Creates a directory and creates all missing parent directories as well.
406+
fn create_dir_recursive(path: &str, mode: AccessPermission) -> io::Result<()> {
407+
trace!("create_dir_recursive: {path}");
408+
create_dir(path, mode).or_else(|errno| {
409+
if errno != Errno::Badf {
410+
return Err(errno);
411+
}
412+
let parent_path = &path[0..path.rfind('/').unwrap()];
413+
create_dir_recursive(parent_path, mode)?;
414+
create_dir(path, mode)
415+
})
416+
}
417+
405418
/// Returns an vector with all the entries within a directory.
406419
pub fn readdir(name: &str) -> io::Result<Vec<DirectoryEntry>> {
407420
debug!("Read directory {name}");

0 commit comments

Comments
 (0)