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 std::{
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 {
fn bitand_assign(&mut self, rhs: Who) {
*self = *self & rhs;
@ -157,13 +167,13 @@ impl Parser {
if self.op.is_none() {
Err(ParseError::NoOpSet)
} else {
if self.who & 0o100 != 0 {
if self.who.contains(Who::User) {
self.bits |= Bit::URead;
}
if self.who & 0o10 != 0 {
if self.who.contains(Who::Group) {
self.bits |= Bit::GRead;
}
if self.who & 0o1 != 0 {
if self.who.contains(Who::Other) {
self.bits |= Bit::ORead;
}
Ok(())
@ -174,13 +184,13 @@ impl Parser {
if self.op.is_none() {
Err(ParseError::NoOpSet)
} else {
if self.who & 0o100 != 0 {
if self.who.contains(Who::User) {
self.bits |= Bit::UWrite;
}
if self.who & 0o10 != 0 {
if self.who.contains(Who::Group) {
self.bits |= Bit::GWrite;
}
if self.who & 0o1 != 0 {
if self.who.contains(Who::Other) {
self.bits |= Bit::OWrite;
}
Ok(())
@ -191,13 +201,13 @@ impl Parser {
if self.op.is_none() {
Err(ParseError::NoOpSet)
} else {
if self.who & 0o100 != 0 {
if self.who.contains(Who::User) {
self.bits |= Bit::UExec;
}
if self.who & 0o10 != 0 {
if self.who.contains(Who::Group) {
self.bits |= Bit::GExec;
}
if self.who & 0o1 != 0 {
if self.who.contains(Who::Other) {
self.bits |= Bit::OExec;
}
Ok(())
@ -205,27 +215,27 @@ impl Parser {
}
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);
} else if self.op.is_none() {
return Err(ParseError::NoOpSet);
}
if self.who & 0o100 != 0 {
if self.who.contains(Who::User) {
self.bits |= Bit::Suid;
}
if self.who & 0o10 != 0 {
if self.who.contains(Who::Group) {
self.bits |= Bit::Sgid;
}
Ok(())
}
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);
} else if self.op.is_none() {
return Err(ParseError::NoOpSet);
}
if self.who & 0o1 != 0 {
if self.who.contains(Who::Other) {
self.bits |= Bit::Sticky;
}
Ok(())
@ -244,13 +254,13 @@ impl Parser {
Some(Op::Add) => self.add_bits(),
Some(Op::Remove) => self.remove_bits(),
Some(Op::Equals) => {
if self.who & 0o100 != 0 {
if self.who.contains(Who::User) {
self.mode &= !(0o4700);
}
if self.who & 0o10 != 0 {
if self.who.contains(Who::Group) {
self.mode &= !(0o2070);
}
if self.who & 0o1 != 0 {
if self.who.contains(Who::Other) {
self.mode &= !(0o1007);
}
self.add_bits();