10000 mkdir: fixed not respecting set umask by pyoky · Pull Request #3150 · uutils/coreutils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

mkdir: fixed not respecting set umask #3150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Apr 13, 2022
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
7 changes: 5 additions & 2 deletions src/uu/mkdir/src/mkdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ fn get_mode(matches: &ArgMatches, mode_had_minus_prefix: bool) -> Result<u32, St
}
Ok(new_mode)
}
None => Ok(DEFAULT_PERM),
None => {
// If no mode argument is specified return the mode derived from umask
Ok(!mode::get_umask() & 0o0777)
}
}
}

Expand Down Expand Up @@ -115,7 +118,7 @@ pub fn uu_app<'a>() -> Command<'a> {
.short('m')
.long(options::MODE)
.help("set file mode (not implemented on windows)")
.default_value("755"),
.takes_value(true),
)
.arg(
Arg::new(options::PARENTS)
Expand Down
29 changes: 29 additions & 0 deletions tests/by-util/test_mkdir.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::common::util::*;
#[cfg(not(windows))]
use std::os::unix::fs::PermissionsExt;
#[cfg(not(windows))]
extern crate libc;
#[cfg(not(windows))]
use self::libc::{mode_t, umask};

static TEST_DIR1: &str = "mkdir_test1";
static TEST_DIR2: &str = "mkdir_test2";
Expand All @@ -13,6 +17,8 @@ static TEST_DIR8: &str = "mkdir_test8/mkdir_test8_1/mkdir_test8_2";
static TEST_DIR9: &str = "mkdir_test9/../mkdir_test9_1/../mkdir_test9_2";
static TEST_DIR10: &str = "mkdir_test10/.";
static TEST_DIR11: &str = "mkdir_test11/..";
#[cfg(not(windows))]
static TEST_DIR12: &str = "mkdir_test12";

#[test]
fn test_mkdir_mkdir() {
Expand Down Expand Up @@ -151,3 +157,26 @@ fn test_mkdir_trailing_dot() {
let result = scene2.cmd("ls").arg("-al").run();
println!("ls dest {}", result.stdout_str());
}

#[test]
#[cfg(not(windows))]
fn test_umask_compliance() {
fn test_single_case(umask_set: mode_t) {
let (at, mut ucmd) = at_and_ucmd!();

let original_umask = unsafe { umask(umask_set) };

ucmd.arg(TEST_DIR12).succeeds();
let perms = at.metadata(TEST_DIR12).permissions().mode() as mode_t;

assert_eq!(perms, (!umask_set & 0o0777) + 0o40000); // before compare, add the set GUID, UID bits
unsafe {
umask(original_umask);
} // set umask back to original
}

for i in 0o0..0o027 {
// tests all permission combinations
Comment on lines +178 to +179
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not all the possible permission settings. That's fine, let's just make sure the comment matches the code.

test_single_case(i as mode_t);
}
}
2 changes: 1 addition & 1 deletion tests/by-util/test_pwd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn test_deleted_dir() {
let output = Command::new("sh")
.arg("-c")
.arg(format!(
"cd '{}'; mkdir foo; cd foo; rmdir ../foo; exec {} {}",
"cd '{}'; mkdir foo; cd foo; rmdir ../foo; exec '{}' {}",
at.root_dir_resolved(),
ts.bin_path.to_str().unwrap(),
ts.util_name,
Expand Down
0