mode::Parser - made self.who into u32 and test using bitflags

This commit is contained in:
Nathan Fisher 2023-01-16 16:22:29 -05:00
parent bee6cdf08e
commit 528d2478cd

View File

@ -33,15 +33,15 @@ enum Op {
#[derive(Clone, Debug, PartialEq)]
enum Who {
User,
Group,
Other,
User = 0o100,
Group = 0o10,
Other = 0o1,
}
pub struct Parser {
mode: u32,
op: Option<Op>,
who: Vec<Who>,
who: u32,
bits: Vec<Bit>,
}
@ -53,7 +53,7 @@ impl Default for Parser {
Self {
mode,
op: None,
who: vec![],
who: 0,
bits: vec![],
}
}
@ -64,7 +64,7 @@ impl Parser {
Self {
mode,
op: None,
who: vec![],
who: 0,
bits: vec![],
}
}
@ -79,10 +79,10 @@ impl Parser {
}
fn add_who(&mut self, who: Who) -> Result<(), ParseError> {
if self.op.is_some() || self.who.contains(&who) || !self.bits.is_empty() {
if self.op.is_some() || !self.bits.is_empty() {
Err(ParseError::InvalidDigit)
} else {
self.who.push(who);
self.who |= who as u32;
Ok(())
}
}
@ -97,61 +97,63 @@ impl Parser {
}
fn push_read_bits(&mut self) -> Result<(), ParseError> {
for w in &self.who {
match w {
Who::User => self.bits.push(Bit::URead),
Who::Group => self.bits.push(Bit::GRead),
Who::Other => self.bits.push(Bit::ORead),
if self.who & 0o100 != 0 {
self.bits.push(Bit::URead);
}
if self.who & 0o10 != 0 {
self.bits.push(Bit::GRead);
}
if self.who & 0o1 != 0 {
self.bits.push(Bit::ORead);
}
Ok(())
}
fn push_write_bits(&mut self) -> Result<(), ParseError> {
for w in &self.who {
match w {
Who::User => self.bits.push(Bit::UWrite),
Who::Group => self.bits.push(Bit::GWrite),
Who::Other => self.bits.push(Bit::OWrite),
if self.who & 0o100 != 0 {
self.bits.push(Bit::UWrite);
}
if self.who & 0o10 != 0 {
self.bits.push(Bit::GWrite);
}
if self.who & 0o1 != 0 {
self.bits.push(Bit::OWrite);
}
Ok(())
}
fn push_exec_bits(&mut self) -> Result<(), ParseError> {
for w in &self.who {
match w {
Who::User => self.bits.push(Bit::UExec),
Who::Group => self.bits.push(Bit::GExec),
Who::Other => self.bits.push(Bit::OExec),
if self.who & 0o100 != 0 {
self.bits.push(Bit::UExec);
}
if self.who & 0o10 != 0 {
self.bits.push(Bit::GExec);
}
if self.who & 0o1 != 0 {
self.bits.push(Bit::OExec);
}
Ok(())
}
fn push_suid_sgid(&mut self) -> Result<(), ParseError> {
if self.who.is_empty() {
if self.who == 0 || self.who & 0o1 != 0{
return Err(ParseError::InvalidBit);
}
for w in &self.who {
match w {
&Who::Other => return Err(ParseError::InvalidBit),
&Who::User => self.bits.push(Bit::Suid),
&Who::Group => self.bits.push(Bit::Sgid),
if self.who & 0o100 != 0 {
self.bits.push(Bit::Suid);
}
if self.who &0o10 != 0 {
self.bits.push(Bit::Sgid);
}
Ok(())
}
fn push_sticky(&mut self) -> Result<(), ParseError> {
if self.who.is_empty() {
if self.who == 0 || self.who & 0o100 != 0 || self.who & 0o10 != 0 {
return Err(ParseError::InvalidBit);
}
for w in &self.who {
match w {
&Who::User | &Who::Group => return Err(ParseError::InvalidBit),
&Who::Other => self.bits.push(Bit::Sticky),
}
if self.who & 0o1 != 0 {
self.bits.push(Bit::Sticky);
}
Ok(())
}
@ -173,12 +175,14 @@ impl Parser {
Some(Op::Add) => self.add_bits(),
Some(Op::Remove) => self.remove_bits(),
Some(Op::Equals) => {
for w in &self.who {
match w {
Who::User => self.mode &= !0o4444,
Who::Group => self.mode &= !0o2222,
Who::Other => self.mode &= !0o1111,
if self.who & 0o100 != 0 {
self.mode &= 0o4444;
}
if self.who & 0o10 != 0 {
self.mode &= 0o2222;
}
if self.who &0o1 != 0 {
self.mode &= 0o1111;
}
self.add_bits();
},
@ -188,7 +192,7 @@ impl Parser {
}
fn reset(&mut self) {
self.who.clear();
self.who = 0;
self.op = None;
self.bits.clear();
}
@ -228,10 +232,8 @@ impl Parser {
_ => return Err(ParseError::InvalidDigit),
}
}
if self.who.is_empty() {
self.who.push(Who::User);
self.who.push(Who::Group);
self.who.push(Who::Other);
if self.who == 0 {
self.who |= 0o111;
}
self.set_bits()?;
self.reset();
@ -240,7 +242,7 @@ impl Parser {
pub fn parse_all(&mut self, value: &str) -> Result<u32, ParseError> {
for seg in value.split(',') {
self.parse(seg);
let _mode = self.parse(seg);
}
Ok(self.mode())
}