1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +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("");
for dir in &dirs {
let path = Path::new(dir);
if recursive {
let mut pathbuf = PathBuf::new();
for component in path.components() {
pathbuf.push(component.as_os_str());
if !path.is_dir() {
status |= mkdir(pathbuf.as_path(), mode, verbose);
}
}
} else {
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);
if !recursive {
if let Some(parent) = path.parent() {
if parent != empty && !parent.exists() {
show_info!(
"cannot create directory '{}': No such file or directory",
path.display()
);
status = 1;
continue;
}
}
}
status |= mkdir(path, recursive, mode, verbose);
}
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
*/
fn mkdir(path: &Path, mode: u16, verbose: bool) -> i32 {
if let Err(e) = fs::create_dir(path) {
fn mkdir(path: &Path, recursive: bool, mode: u16, verbose: bool) -> i32 {
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());
return 1;
}

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