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
19 changes: 12 additions & 7 deletions src/uu/mkdir/src/mkdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ pub fn mkdir(path: &Path, recursive: bool, mode: u32, verbose: bool) -> UResult<
let path_buf = dir_strip_dot_for_creation(path);
let path = path_buf.as_path();

create_dir(path, recursive, verbose, false)?;
chmod(path, mode)
if create_dir(path, recursive, verbose, false)? {
chmod(path, mode)?;
}
Ok(())
}

#[cfg(any(unix, target_os = "redox"))]
Expand All @@ -186,22 +188,25 @@ 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
#[allow(unused_variables)]
fn create_dir(path: &Path, recursive: bool, verbose: bool, is_parent: bool) -> UResult<()> {
fn create_dir(path: &Path, recursive: bool, verbose: bool, is_parent: bool) -> UResult<bool> {
if path.exists() && !recursive {
return Err(USimpleError::new(
1,
format!("{}: File exists", path.display()),
));
}
if path == Path::new("") {
return Ok(());
return Ok(false);
}

if recursive {
match path.parent() {
Some(p) => create_dir(p, recursive, verbose, true)?,
Some(p) => {
create_dir(p, recursive, verbose, true)?;
}
None => {
USimpleError::new(1, "failed to create whole tree");
}
Expand All @@ -222,9 +227,9 @@ fn create_dir(path: &Path, recursive: bool, verbose: bool, is_parent: bool) -> U
// which is umask modified by 'u+wx'
chmod(path, (!mode::get_umask() & 0o0777) | 0o0300)?;
}
Ok(())
Ok(true)
}
Err(_) if path.is_dir() => Ok(()),
Err(_) if path.is_dir() => Ok(false),
Err(e) => Err(e.into()),
}
}
21 changes: 21 additions & 0 deletions tests/by-util/test_mkdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,27 @@ fn test_mkdir_parent_mode_check_existing_parent() {
);
}

#[cfg(not(windows))]
#[test]
fn test_mkdir_parent_mode_skip_existing_last_component_chmod() {
let (at, mut ucmd) = at_and_ucmd!();

at.mkdir("a");
at.mkdir("a/b");
at.set_mode("a/b", 0);

let default_umask: mode_t = 0o160;

ucmd.arg("-p")
.arg("a/b")
.umask(default_umask)
.succeeds()
.no_stderr()
.no_stdout();

assert_eq!(at.metadata("a/b").permissions().mode() as mode_t, 0o40000);
}

#[test]
fn test_mkdir_dup_file() {
let scene = TestScenario::new(util_name!());
Expand Down
Loading