mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Merge pull request #1031 from shinh/mkdir-dup
mkdir: Fix the behavior for existing files
This commit is contained in:
commit
a2de3997b9
2 changed files with 25 additions and 8 deletions
|
@ -94,7 +94,9 @@ fn exec(dirs: Vec<String>, recursive: bool, mode: u16, verbose: bool) -> i32 {
|
||||||
let mut pathbuf = PathBuf::new();
|
let mut pathbuf = PathBuf::new();
|
||||||
for component in path.components() {
|
for component in path.components() {
|
||||||
pathbuf.push(component.as_os_str());
|
pathbuf.push(component.as_os_str());
|
||||||
status |= mkdir(pathbuf.as_path(), mode, verbose);
|
if !path.is_dir() {
|
||||||
|
status |= mkdir(pathbuf.as_path(), mode, verbose);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
match path.parent() {
|
match path.parent() {
|
||||||
|
@ -119,11 +121,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, mode: u16, verbose: bool) -> i32 {
|
||||||
if !path.exists() {
|
if let Err(e) = fs::create_dir(path) {
|
||||||
if let Err(e) = fs::create_dir(path) {
|
show_info!("{}: {}", path.display(), e.to_string());
|
||||||
show_info!("{}: {}", path.display(), e.to_string());
|
return 1;
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if verbose {
|
if verbose {
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
use common::util::*;
|
use common::util::*;
|
||||||
|
|
||||||
|
|
||||||
static TEST_DIR1: &'static str = "mkdir_test1";
|
static TEST_DIR1: &'static str = "mkdir_test1";
|
||||||
static TEST_DIR2: &'static str = "mkdir_test2";
|
static TEST_DIR2: &'static str = "mkdir_test2";
|
||||||
static TEST_DIR3: &'static str = "mkdir_test3";
|
static TEST_DIR3: &'static str = "mkdir_test3";
|
||||||
static TEST_DIR4: &'static str = "mkdir_test4/mkdir_test4_1";
|
static TEST_DIR4: &'static str = "mkdir_test4/mkdir_test4_1";
|
||||||
static TEST_DIR5: &'static str = "mkdir_test5/mkdir_test5_1";
|
static TEST_DIR5: &'static str = "mkdir_test5/mkdir_test5_1";
|
||||||
|
static TEST_DIR6: &'static str = "mkdir_test6";
|
||||||
|
static TEST_FILE7: &'static str = "mkdir_test7";
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_mkdir_mkdir() {
|
fn test_mkdir_mkdir() {
|
||||||
|
@ -16,7 +17,7 @@ fn test_mkdir_mkdir() {
|
||||||
fn test_mkdir_dup_dir() {
|
fn test_mkdir_dup_dir() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let scene = TestScenario::new(util_name!());
|
||||||
scene.ucmd().arg(TEST_DIR2).succeeds();
|
scene.ucmd().arg(TEST_DIR2).succeeds();
|
||||||
scene.ucmd().arg(TEST_DIR2).succeeds();
|
scene.ucmd().arg(TEST_DIR2).fails();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -39,3 +40,19 @@ fn test_mkdir_parent() {
|
||||||
fn test_mkdir_no_parent() {
|
fn test_mkdir_no_parent() {
|
||||||
new_ucmd!().arg(TEST_DIR5).fails();
|
new_ucmd!().arg(TEST_DIR5).fails();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_mkdir_dup_dir_parent() {
|
||||||
|
let scene = TestScenario::new(util_name!());
|
||||||
|
scene.ucmd().arg(TEST_DIR6).succeeds();
|
||||||
|
scene.ucmd().arg("-p").arg(TEST_DIR6).succeeds();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
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