Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 50 additions & 7 deletions src/uu/mkdir/src/mkdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ fn chmod(_path: &Path, _mode: u32) -> UResult<()> {
Ok(())
}

// Return true if the directory at `path` has been created by this call.
// `is_parent` argument is not used on windows
// Create a directory at the given path.
// Uses iterative approach instead of recursion to avoid stack overflow with deep nesting.
#[allow(unused_variables)]
fn create_dir(path: &Path, is_parent: bool, config: &Config) -> UResult<()> {
let path_exists = path.exists();
Expand All @@ -231,15 +231,41 @@ fn create_dir(path: &Path, is_parent: bool, config: &Config) -> UResult<()> {
return Ok(());
}

// Iterative implementation: collect all directories to create, then create them
// This avoids stack overflow with deeply nested directories
if config.recursive {
match path.parent() {
Some(p) => create_dir(p, true, config)?,
None => {
USimpleError::new(1, translate!("mkdir-error-failed-to-create-tree"));
// Pre-allocate approximate capacity to avoid reallocations
let mut dirs_to_create = Vec::with_capacity(16);
let mut current = path;

// First pass: collect all parent directories
while let Some(parent) = current.parent() {
if parent == Path::new("") {
break;
}
dirs_to_create.push(parent);
current = parent;
}

// Second pass: create directories from root to leaf
// Only create those that don't exist
for dir in dirs_to_create.iter().rev() {
if !dir.exists() {
create_single_dir(dir, true, config)?;
}
}
}

// Create the target directory
create_single_dir(path, is_parent, config)
}

// Helper function to create a single directory with appropriate permissions
// `is_parent` argument is not used on windows
#[allow(unused_variables)]
fn create_single_dir(path: &Path, is_parent: bool, config: &Config) -> UResult<()> {
let path_exists = path.exists();

match std::fs::create_dir(path) {
Ok(()) => {
if config.verbose {
Expand Down Expand Up @@ -287,7 +313,24 @@ fn create_dir(path: &Path, is_parent: bool, config: &Config) -> UResult<()> {
Ok(())
}

Err(_) if path.is_dir() => Ok(()),
Err(_) if path.is_dir() => {
// Directory already exists - check if this is a logical directory creation
// (i.e., not just a parent reference like "test_dir/..")
let ends_with_parent_dir = matches!(
path.components().next_back(),
Some(std::path::Component::ParentDir)
);

// Print verbose message for logical directories, even if they exist
// This matches GNU behavior for paths like "test_dir/../test_dir_a"
if config.verbose && is_parent && config.recursive && !ends_with_parent_dir {
println!(
"{}",
translate!("mkdir-verbose-created-directory", "util_name" => uucore::util_name(), "path" => path.quote())
);
}
Ok(())
}
Err(e) => Err(e.into()),
}
}
Loading
Loading