1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge pull request #903 from knight42/chmod-patch

chmod: check umask if ugoa is not present
This commit is contained in:
Heather 2016-06-19 21:37:44 +04:00 committed by GitHub
commit 96d4cd8da1
2 changed files with 35 additions and 1 deletions

View file

@ -248,11 +248,18 @@ fn parse_symbolic(mut fperm: libc::mode_t, mut mode: &str, file: &Path) -> Resul
if pos == mode.len() { if pos == mode.len() {
return Err(format!("invalid mode ({})", mode)); return Err(format!("invalid mode ({})", mode));
} }
let respect_umask = pos == 0;
let last_umask = unsafe {
libc::umask(0)
};
mode = &mode[pos..]; mode = &mode[pos..];
while mode.len() > 0 { while mode.len() > 0 {
let (op, pos) = try!(parse_op(mode, None)); let (op, pos) = try!(parse_op(mode, None));
mode = &mode[pos..]; mode = &mode[pos..];
let (srwx, pos) = parse_change(mode, fperm, file); let (mut srwx, pos) = parse_change(mode, fperm, file);
if respect_umask {
srwx &= !last_umask;
}
mode = &mode[pos..]; mode = &mode[pos..];
match op { match op {
'+' => fperm |= srwx & mask, '+' => fperm |= srwx & mask,
@ -261,6 +268,9 @@ fn parse_symbolic(mut fperm: libc::mode_t, mut mode: &str, file: &Path) -> Resul
_ => unreachable!() _ => unreachable!()
} }
} }
unsafe {
libc::umask(last_umask);
}
Ok(fperm) Ok(fperm)
} }

View file

@ -2,6 +2,9 @@ use common::util::*;
use std::fs::{metadata, OpenOptions, set_permissions}; use std::fs::{metadata, OpenOptions, set_permissions};
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
extern crate libc;
use self::libc::umask;
static UTIL_NAME: &'static str = "chmod"; static UTIL_NAME: &'static str = "chmod";
static TEST_FILE: &'static str = "file"; static TEST_FILE: &'static str = "file";
static REFERENCE_FILE: &'static str = "reference"; static REFERENCE_FILE: &'static str = "reference";
@ -67,6 +70,9 @@ fn test_chmod_octal() {
#[test] #[test]
fn test_chmod_ugoa() { fn test_chmod_ugoa() {
let last = unsafe {
umask(0)
};
let tests = vec!{ let tests = vec!{
TestCase{args: vec!{"u=rwx", TEST_FILE}, before: 0o000, after: 0o700}, TestCase{args: vec!{"u=rwx", TEST_FILE}, before: 0o000, after: 0o700},
TestCase{args: vec!{"g=rwx", TEST_FILE}, before: 0o000, after: 0o070}, TestCase{args: vec!{"g=rwx", TEST_FILE}, before: 0o000, after: 0o070},
@ -77,6 +83,24 @@ fn test_chmod_ugoa() {
TestCase{args: vec!{"-x", TEST_FILE}, before: 0o777, after: 0o666}, TestCase{args: vec!{"-x", TEST_FILE}, before: 0o777, after: 0o666},
}; };
run_tests(tests); run_tests(tests);
unsafe {
umask(0o022);
}
let tests = vec!{
TestCase{args: vec!{"u=rwx", TEST_FILE}, before: 0o000, after: 0o700},
TestCase{args: vec!{"g=rwx", TEST_FILE}, before: 0o000, after: 0o070},
TestCase{args: vec!{"o=rwx", TEST_FILE}, before: 0o000, after: 0o007},
TestCase{args: vec!{"a=rwx", TEST_FILE}, before: 0o000, after: 0o777},
TestCase{args: vec!{"+rw", TEST_FILE}, before: 0o000, after: 0o644},
TestCase{args: vec!{"=rwx", TEST_FILE}, before: 0o000, after: 0o755},
TestCase{args: vec!{"-w", TEST_FILE}, before: 0o777, after: 0o577},
TestCase{args: vec!{"-x", TEST_FILE}, before: 0o777, after: 0o666},
};
run_tests(tests);
unsafe {
umask(last);
}
} }
#[test] #[test]