Skip to content

Commit d97469b

Browse files
Navaneeth YadamreddyNavaneeth Yadamreddy
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
1 parent 76774a6 commit d97469b

2 files changed

Lines changed: 75 additions & 1 deletion

File tree

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,19 @@ fn create_single_dir(path: &Path, is_parent: bool, config: &Config) -> UResult<(
353353
Ok(())
354354
}
355355

356-
Err(_) if config.recursive && path.is_dir() => {
356+
Err(_) if path.is_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+
357369
// Directory already exists - check if this is a logical directory creation
358370
// (i.e., not just a parent reference like "test_dir/..")
359371
let ends_with_parent_dir = matches!(

‎tests/by-util/test_mkdir.rs‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,3 +1177,65 @@ fn test_mkdir_concurrent_non_recursive() {
11771177
);
11781178
}
11791179
}
1180+
1181+
/// Regression test: plain `mkdir` on an existing directory must fail with
1182+
/// exit 1, not 0. Previously the EEXIST path checked `path.is_dir()` and
1183+
/// returned Ok(()), which broke the mkdir-as-mutex pattern under
1184+
/// concurrency (issue #13970).
1185+
#[test]
1186+
fn test_mkdir_eexist_returns_error() {
1187+
let scene = TestScenario::new(util_name!());
1188+
let at = &scene.fixtures;
1189+
let dir = at.plus("existing_dir");
1190+
1191+
let dir_str = dir.to_str().unwrap();
1192+
1193+
// Create the directory first
1194+
new_ucmd!().args(&[dir_str]).succeeds();
1195+
1196+
// Trying again without -p must fail
1197+
new_ucmd!()
1198+
.args(&[dir_str])
1199+
.fails_with_code(1)
1200+
.stderr_contains("File exists");
1201+
}
1202+
1203+
/// Regression test: concurrent `mkdir` (no -p) on the same path must
1204+
/// produce exactly one winner (issue #13970).
1205+
#[test]
1206+
fn test_mkdir_concurrent_eexist() {
1207+
use std::thread;
1208+
1209+
let scene = TestScenario::new(util_name!());
1210+
let at = &scene.fixtures;
1211+
let dir = at.plus("race_dir");
1212+
let path_str = dir.to_string_lossy().to_string();
1213+
let bin_path = scene.bin_path.clone();
1214+
1215+
for _ in 0..10 {
1216+
// Remove directory before each round
1217+
let _ = std::fs::remove_dir(&path_str);
1218+
1219+
let mut handles = vec![];
1220+
for _ in 0..20 {
1221+
let path = path_str.clone();
1222+
let bin = bin_path.clone();
1223+
handles.push(thread::spawn(move || {
1224+
std::process::Command::new(&bin)
1225+
.arg("mkdir")
1226+
.arg(&path)
1227+
.stdout(std::process::Stdio::null())
1228+
.stderr(std::process::Stdio::null())
1229+
.status()
1230+
.map(|s| s.success())
1231+
.unwrap_or(false)
1232+
}));
1233+
}
1234+
1235+
let winners: usize = handles
1236+
.into_iter()
1237+
.map(|h| h.join().unwrap() as usize)
1238+
.sum();
1239+
assert_eq!(winners, 1, "Expected exactly 1 winner, got {winners}");
1240+
}
1241+
}

0 commit comments

Comments
 (0)