1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

Merge pull request #1197 from c-edw/master

mkdir: Silently fail in recursive mode if unable to create directories.
This commit is contained in:
Alex Lyon 2018-05-03 02:12:24 -07:00 committed by GitHub
commit 4d89c2d796
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 25 deletions

View file

@ -92,32 +92,19 @@ fn exec(dirs: Vec<String>, recursive: bool, mode: u16, verbose: bool) -> i32 {
let empty = Path::new(""); let empty = Path::new("");
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(); if let Some(parent) = path.parent() {
for component in path.components() { if parent != empty && !parent.exists() {
pathbuf.push(component.as_os_str()); show_info!(
if !path.is_dir() { "cannot create directory '{}': No such file or directory",
status |= mkdir(pathbuf.as_path(), mode, verbose); path.display()
} );
} status = 1;
} else { continue;
match path.parent() {
Some(parent) => {
if parent != empty && !parent.exists() {
show_info!(
"cannot create directory '{}': No such file or directory",
path.display()
);
status = 1;
} else {
status |= mkdir(path, mode, verbose);
}
}
None => {
status |= mkdir(path, mode, verbose);
} }
} }
} }
status |= mkdir(path, recursive, mode, verbose);
} }
status status
} }
@ -125,8 +112,9 @@ 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, mode: u16, verbose: bool) -> i32 { fn mkdir(path: &Path, recursive: bool, mode: u16, verbose: bool) -> i32 {
if let Err(e) = fs::create_dir(path) { let create_dir = if recursive { fs::create_dir_all } else { fs::create_dir };
if let Err(e) = create_dir(path) {
show_info!("{}: {}", path.display(), e.to_string()); show_info!("{}: {}", path.display(), e.to_string());
return 1; return 1;
} }

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();
} }