1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +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 {
let path = Path::new(dir);
if recursive {
let mut pathbuf = PathBuf::new();
if !path.is_dir() {
for component in path.components() {
pathbuf.push(component.as_os_str());
mkdir(pathbuf.as_path(), recursive, mode, verbose);
}
}
status |= mkdir(path, recursive, mode, verbose);
} else {
match path.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
*/
fn mkdir(path: &Path, recursive: bool, mode: u16, verbose: bool) -> i32 {
if let Err(e) = fs::create_dir(path) {
if !recursive {
show_info!("{}: {}", path.display(), e.to_string());
if recursive {
if let Err(e) = fs::create_dir_all(path) {
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 {

View file

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