From 5b417e251d80a5d5652fea05ffcfccb9992243cf Mon Sep 17 00:00:00 2001 From: Dean Li Date: Sun, 30 May 2021 10:45:54 +0800 Subject: [PATCH 1/2] rmdir: match GNU error output Related to #2258 --- src/uu/rmdir/src/rmdir.rs | 6 ++++++ tests/by-util/test_rmdir.rs | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/uu/rmdir/src/rmdir.rs b/src/uu/rmdir/src/rmdir.rs index 7a7e8fc9b..6f0a2ffa9 100644 --- a/src/uu/rmdir/src/rmdir.rs +++ b/src/uu/rmdir/src/rmdir.rs @@ -20,6 +20,8 @@ static OPT_VERBOSE: &str = "verbose"; static ARG_DIRS: &str = "dirs"; +static ENOTDIR: i32 = 20; + fn get_usage() -> String { format!("{0} [OPTION]... DIRECTORY...", executable!()) } @@ -105,6 +107,10 @@ fn remove(dirs: Vec, ignore: bool, parents: bool, verbose: bool) -> Resu fn remove_dir(path: &Path, ignore: bool, verbose: bool) -> Result<(), i32> { let mut read_dir = match fs::read_dir(path) { Ok(m) => m, + Err(e) if e.raw_os_error() == Some(ENOTDIR) => { + show_error!("failed to remove '{}': Not a directory", path.display()); + return Err(1); + } Err(e) => { show_error!("reading directory '{}': {}", path.display(), e); return Err(1); diff --git a/tests/by-util/test_rmdir.rs b/tests/by-util/test_rmdir.rs index eef2d50f5..4b74b2522 100644 --- a/tests/by-util/test_rmdir.rs +++ b/tests/by-util/test_rmdir.rs @@ -108,3 +108,19 @@ fn test_rmdir_ignore_nonempty_directory_with_parents() { assert!(at.dir_exists(dir)); } + +#[test] +fn test_rmdir_remove_symlink_match_gnu_error() { + let (at, mut ucmd) = at_and_ucmd!(); + + let file = "file"; + let fl = "fl"; + at.touch(file); + assert!(at.file_exists(file)); + at.symlink_file(file, fl); + assert!(at.file_exists(fl)); + + ucmd.arg("fl/") + .fails() + .stderr_is("rmdir: failed to remove 'fl/': Not a directory"); +} From 83bb8795bd75aaae907d5b339e50ef64279ed82e Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 30 May 2021 09:16:46 +0200 Subject: [PATCH 2/2] add a comment to explain the why --- src/uu/rmdir/src/rmdir.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/uu/rmdir/src/rmdir.rs b/src/uu/rmdir/src/rmdir.rs index 6f0a2ffa9..6b6db0b65 100644 --- a/src/uu/rmdir/src/rmdir.rs +++ b/src/uu/rmdir/src/rmdir.rs @@ -108,6 +108,7 @@ fn remove_dir(path: &Path, ignore: bool, verbose: bool) -> Result<(), i32> { let mut read_dir = match fs::read_dir(path) { Ok(m) => m, Err(e) if e.raw_os_error() == Some(ENOTDIR) => { + // To match the GNU output show_error!("failed to remove '{}': Not a directory", path.display()); return Err(1); }