From 80b63931a4bdc4334c0ed64eb85361355b710b19 Mon Sep 17 00:00:00 2001 From: Andrew Liebenow Date: Sun, 20 Oct 2024 10:45:46 -0500 Subject: [PATCH] mkdir: emit error when path is empty --- src/uu/mkdir/src/mkdir.rs | 7 +++++++ tests/by-util/test_mkdir.rs | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/uu/mkdir/src/mkdir.rs b/src/uu/mkdir/src/mkdir.rs index c65fa11674..46084ba9ee 100644 --- a/src/uu/mkdir/src/mkdir.rs +++ b/src/uu/mkdir/src/mkdir.rs @@ -160,6 +160,13 @@ fn exec(dirs: ValuesRef, recursive: bool, mode: u32, verbose: bool) -> /// To match the GNU behavior, a path with the last directory being a single dot /// (like `some/path/to/.`) is created (with the dot stripped). pub fn mkdir(path: &Path, recursive: bool, mode: u32, verbose: bool) -> UResult<()> { + if path.as_os_str().is_empty() { + return Err(USimpleError::new( + 1, + "cannot create directory '': No such file or directory".to_owned(), + )); + } + // Special case to match GNU's behavior: // mkdir -p foo/. should work and just create foo/ // std::fs::create_dir("foo/."); fails in pure Rust diff --git a/tests/by-util/test_mkdir.rs b/tests/by-util/test_mkdir.rs index 9978cbca95..b9d5275f49 100644 --- a/tests/by-util/test_mkdir.rs +++ b/tests/by-util/test_mkdir.rs @@ -2,6 +2,9 @@ // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. + +// spell-checker:ignore bindgen + #![allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] use crate::common::util::TestScenario; @@ -332,3 +335,11 @@ fn test_umask_compliance() { test_single_case(i as mode_t); } } + +#[test] +fn test_empty_argument() { + new_ucmd!() + .arg("") + .fails() + .stderr_only("mkdir: cannot create directory '': No such file or directory\n"); +}