Skip to content

Commit 9ee162e

Browse files
Navaneeth Yadamreddycodebuff-team
authored andcommitted
mkdir: fix EEXIST race condition in non-recursive mode
When mkdir(2) returns EEXIST, the previous code checked path.is_dir() and returned Ok(()) if true — even for plain `mkdir dir` (non-recursive). This meant two concurrent processes racing to create the same directory could both exit 0, breaking the classic mkdir-as-mutex pattern. Fix by only treating EEXIST + is_dir() as success when creating parent directories (is_parent) or when -p was given (recursive). In the plain `mkdir dir` case, EEXIST now always returns an error, matching GNU behavior. Fixes uutils#13970 🤖 Generated with Codebuff Co-Authored-By: Codebuff <[email protected]>
1 parent 0d8310c commit 9ee162e

2 files changed

Lines changed: 74 additions & 2 deletions

File tree

‎src/uu/mkdir/src/mkdir.rs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,18 @@ fn create_single_dir(path: &Path, is_parent: bool, config: &Config) -> UResult<(
354354
}
355355

356356
Err(_) if path.is_dir() => {
357-
// Directory already exists - check if this is a logical directory creation
358-
// (i.e., not just a parent reference like "test_dir/..")
357+
// Directory already exists. Only treat this as success when we
358+
// are creating parent directories (is_parent) or when -p was
359+
// given (recursive). In the plain `mkdir dir` case, EEXIST must
360+
// be an error — even if the directory was created by a concurrent
361+
// process — to preserve the mkdir-as-mutex pattern.
362+
if !is_parent && !config.recursive {
363+
return Err(USimpleError::new(
364+
1,
365+
translate!("mkdir-error-file-exists", "path" => path.maybe_quote()),
366+
));
367+
}
368+
359369
let ends_with_parent_dir = matches!(
360370
path.components().next_back(),
361371
Some(std::path::Component::ParentDir)

‎tests/by-util/test_mkdir.rs‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,3 +1134,65 @@ mod diagnostics {
11341134
assert!(!stderr.contains(":1:"), "{stderr}");
11351135
}
11361136
}
1137+
1138+
/// Regression test: plain `mkdir` on an existing directory must fail with
1139+
/// exit 1, not 0. Previously the EEXIST path checked `path.is_dir()` and
1140+
/// returned Ok(()), which broke the mkdir-as-mutex pattern under
1141+
/// concurrency (issue #13970).
1142+
#[test]
1143+
fn test_mkdir_eexist_returns_error() {
1144+
let scene = TestScenario::new(util_name!());
1145+
let at = &scene.fixtures;
1146+
let dir = at.plus("existing_dir");
1147+
1148+
let dir_str = dir.to_str().unwrap();
1149+
1150+
// Create the directory first
1151+
new_ucmd!().args(&[dir_str]).succeeds();
1152+
1153+
// Trying again without -p must fail
1154+
new_ucmd!()
1155+
.args(&[dir_str])
1156+
.fails_with_code(1)
1157+
.stderr_contains("File exists");
1158+
}
1159+
1160+
/// Regression test: concurrent `mkdir` (no -p) on the same path must
1161+
/// produce exactly one winner (issue #13970).
1162+
#[test]
1163+
fn test_mkdir_concurrent_eexist() {
1164+
use std::thread;
1165+
1166+
let scene = TestScenario::new(util_name!());
1167+
let at = &scene.fixtures;
1168+
let dir = at.plus("race_dir");
1169+
let path_str = dir.to_string_lossy().to_string();
1170+
let bin_path = scene.bin_path.clone();
1171+
1172+
for _ in 0..10 {
1173+
// Remove directory before each round
1174+
let _ = std::fs::remove_dir(&path_str);
1175+
1176+
let mut handles = vec![];
1177+
for _ in 0..20 {
1178+
let path = path_str.clone();
1179+
let bin = bin_path.clone();
1180+
handles.push(thread::spawn(move || {
1181+
std::process::Command::new(&bin)
1182+
.arg("mkdir")
1183+
.arg(&path)
1184+
.stdout(std::process::Stdio::null())
1185+
.stderr(std::process::Stdio::null())
1186+
.status()
1187+
.map(|s| s.success())
1188+
.unwrap_or(false)
1189+
}));
1190+
}
1191+
1192+
let winners: usize = handles
1193+
.into_iter()
1194+
.map(|h| h.join().unwrap() as usize)
1195+
.sum();
1196+
assert_eq!(winners, 1, "Expected exactly 1 winner, got {winners}");
1197+
}
1198+
}

0 commit comments

Comments
 (0)