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:
commit
4d89c2d796
2 changed files with 14 additions and 25 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue