Skip to content

Commit

Permalink
Merge pull request #6802 from andrewliebenow/mkdir-empty-argument-fix
Browse files Browse the repository at this point in the history
mkdir: emit error when path is empty
  • Loading branch information
sylvestre authored Oct 20, 2024
2 parents 5b881a0 + 80b6393 commit 3f694fa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/uu/mkdir/src/mkdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ fn exec(dirs: ValuesRef<OsString>, 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
Expand Down
11 changes: 11 additions & 0 deletions tests/by-util/test_mkdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}

0 comments on commit 3f694fa

Please sign in to comment.