1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

mkdir: Use std create_dir_all for recursive operations.

This commit is contained in:
Connor E 2018-05-01 12:42:11 +01:00
parent e03ab6b554
commit 9d5631228a
2 changed files with 11 additions and 11 deletions

View file

@ -93,13 +93,7 @@ fn exec(dirs: Vec<String>, recursive: bool, mode: u16, verbose: bool) -> i32 {
for dir in &dirs { for dir in &dirs {
let path = Path::new(dir); let path = Path::new(dir);
if recursive { if recursive {
let mut pathbuf = PathBuf::new(); status |= mkdir(path, recursive, mode, verbose);
if !path.is_dir() {
for component in path.components() {
pathbuf.push(component.as_os_str());
mkdir(pathbuf.as_path(), recursive, mode, verbose);
}
}
} else { } else {
match path.parent() { match path.parent() {
Some(parent) => { Some(parent) => {
@ -126,11 +120,16 @@ fn exec(dirs: Vec<String>, recursive: bool, mode: u16, verbose: bool) -> i32 {
* Wrapper to catch errors, return 1 if failed * Wrapper to catch errors, return 1 if failed
*/ */
fn mkdir(path: &Path, recursive: bool, mode: u16, verbose: bool) -> i32 { fn mkdir(path: &Path, recursive: bool, mode: u16, verbose: bool) -> i32 {
if let Err(e) = fs::create_dir(path) { if recursive {
if !recursive { if let Err(e) = fs::create_dir_all(path) {
show_info!("{}: {}", path.display(), e.to_string()); show_info!("cannot create directory '{}': {}", path.display(), e.to_string());
return 1;
}
} else {
if let Err(e) = fs::create_dir(path) {
show_info!("{}: {}", path.display(), e.to_string());
return 1;
} }
return 1;
} }
if verbose { if verbose {

View file

@ -53,6 +53,7 @@ fn test_mkdir_dup_file() {
let scene = TestScenario::new(util_name!()); let scene = TestScenario::new(util_name!());
scene.fixtures.touch(TEST_FILE7); scene.fixtures.touch(TEST_FILE7);
scene.ucmd().arg(TEST_FILE7).fails(); scene.ucmd().arg(TEST_FILE7).fails();
// mkdir should fail for a file even if -p is specified. // mkdir should fail for a file even if -p is specified.
scene.ucmd().arg("-p").arg(TEST_FILE7).fails(); scene.ucmd().arg("-p").arg(TEST_FILE7).fails();
} }