Use Bitflags trait in mode::Parser

This commit is contained in:
Nathan Fisher 2023-01-23 09:10:08 -05:00
parent 71915916a0
commit 6c0b7cb787

View File

@ -1,3 +1,5 @@
use crate::bitflags::BitFlags;
use super::{get_umask, Bit}; use super::{get_umask, Bit};
use std::{ use std::{
error, error,
@ -65,6 +67,14 @@ impl BitAnd<Who> for u32 {
} }
} }
impl BitAnd<u32> for Who {
type Output = u32;
fn bitand(self, rhs: u32) -> Self::Output {
self as u32 & rhs
}
}
impl BitAndAssign<Who> for u32 { impl BitAndAssign<Who> for u32 {
fn bitand_assign(&mut self, rhs: Who) { fn bitand_assign(&mut self, rhs: Who) {
*self = *self & rhs; *self = *self & rhs;
@ -157,13 +167,13 @@ impl Parser {
if self.op.is_none() { if self.op.is_none() {
Err(ParseError::NoOpSet) Err(ParseError::NoOpSet)
} else { } else {
if self.who & 0o100 != 0 { if self.who.contains(Who::User) {
self.bits |= Bit::URead; self.bits |= Bit::URead;
} }
if self.who & 0o10 != 0 { if self.who.contains(Who::Group) {
self.bits |= Bit::GRead; self.bits |= Bit::GRead;
} }
if self.who & 0o1 != 0 { if self.who.contains(Who::Other) {
self.bits |= Bit::ORead; self.bits |= Bit::ORead;
} }
Ok(()) Ok(())
@ -174,13 +184,13 @@ impl Parser {
if self.op.is_none() { if self.op.is_none() {
Err(ParseError::NoOpSet) Err(ParseError::NoOpSet)
} else { } else {
if self.who & 0o100 != 0 { if self.who.contains(Who::User) {
self.bits |= Bit::UWrite; self.bits |= Bit::UWrite;
} }
if self.who & 0o10 != 0 { if self.who.contains(Who::Group) {
self.bits |= Bit::GWrite; self.bits |= Bit::GWrite;
} }
if self.who & 0o1 != 0 { if self.who.contains(Who::Other) {
self.bits |= Bit::OWrite; self.bits |= Bit::OWrite;
} }
Ok(()) Ok(())
@ -191,13 +201,13 @@ impl Parser {
if self.op.is_none() { if self.op.is_none() {
Err(ParseError::NoOpSet) Err(ParseError::NoOpSet)
} else { } else {
if self.who & 0o100 != 0 { if self.who.contains(Who::User) {
self.bits |= Bit::UExec; self.bits |= Bit::UExec;
} }
if self.who & 0o10 != 0 { if self.who.contains(Who::Group) {
self.bits |= Bit::GExec; self.bits |= Bit::GExec;
} }
if self.who & 0o1 != 0 { if self.who.contains(Who::Other) {
self.bits |= Bit::OExec; self.bits |= Bit::OExec;
} }
Ok(()) Ok(())
@ -205,27 +215,27 @@ impl Parser {
} }
fn push_suid_sgid(&mut self) -> Result<(), ParseError> { fn push_suid_sgid(&mut self) -> Result<(), ParseError> {
if self.who == 0 || self.who & 0o1 != 0 { if self.who == 0 || self.who.contains(Who::Other) {
return Err(ParseError::InvalidBit); return Err(ParseError::InvalidBit);
} else if self.op.is_none() { } else if self.op.is_none() {
return Err(ParseError::NoOpSet); return Err(ParseError::NoOpSet);
} }
if self.who & 0o100 != 0 { if self.who.contains(Who::User) {
self.bits |= Bit::Suid; self.bits |= Bit::Suid;
} }
if self.who & 0o10 != 0 { if self.who.contains(Who::Group) {
self.bits |= Bit::Sgid; self.bits |= Bit::Sgid;
} }
Ok(()) Ok(())
} }
fn push_sticky(&mut self) -> Result<(), ParseError> { fn push_sticky(&mut self) -> Result<(), ParseError> {
if self.who == 0 || self.who & 0o100 != 0 || self.who & 0o10 != 0 { if self.who == 0 || self.who.contains(Who::User) || self.who.contains(Who::Group) {
return Err(ParseError::InvalidBit); return Err(ParseError::InvalidBit);
} else if self.op.is_none() { } else if self.op.is_none() {
return Err(ParseError::NoOpSet); return Err(ParseError::NoOpSet);
} }
if self.who & 0o1 != 0 { if self.who.contains(Who::Other) {
self.bits |= Bit::Sticky; self.bits |= Bit::Sticky;
} }
Ok(()) Ok(())
@ -244,13 +254,13 @@ impl Parser {
Some(Op::Add) => self.add_bits(), Some(Op::Add) => self.add_bits(),
Some(Op::Remove) => self.remove_bits(), Some(Op::Remove) => self.remove_bits(),
Some(Op::Equals) => { Some(Op::Equals) => {
if self.who & 0o100 != 0 { if self.who.contains(Who::User) {
self.mode &= !(0o4700); self.mode &= !(0o4700);
} }
if self.who & 0o10 != 0 { if self.who.contains(Who::Group) {
self.mode &= !(0o2070); self.mode &= !(0o2070);
} }
if self.who & 0o1 != 0 { if self.who.contains(Who::Other) {
self.mode &= !(0o1007); self.mode &= !(0o1007);
} }
self.add_bits(); self.add_bits();