Use `Bitflags` trait and `Bit` enum in `which` applet;

Move `Bit` and `Who` enums out of `mode` module into submodules
This commit is contained in:
Nathan Fisher 2023-01-23 09:39:02 -05:00
parent 6c0b7cb787
commit 3ef3a8b3aa
5 changed files with 156 additions and 137 deletions

View File

@ -1,3 +1,4 @@
use crate::{bitflags::BitFlags, mode::Bit};
use super::Cmd;
use clap::{Arg, Command};
use std::{env, fs::File, io, os::unix::prelude::MetadataExt, path::PathBuf, process};
@ -60,10 +61,10 @@ fn which(command: &str, path: &[&str]) -> Option<String> {
let myuid = unsafe { libc::geteuid() };
let mygroups = crate::pw::get_gids();
// we own the file and it has u+x
if myuid == meta.uid() && mode & 0o100 != 0 {
if myuid == meta.uid() && mode.contains(Bit::UExec) {
return Some(format!("{}", file.display()));
// file has ug+x
} else if mode & 0o110 != 0 {
} else if mode.contains(Bit::UExec | Bit::GExec) {
if let Ok(groups) = mygroups {
// one of our groups owns the file
if groups.contains(&meta.gid()) {

99
src/mode/bit.rs Normal file
View File

@ -0,0 +1,99 @@
use crate::bitflags::BitFlags;
use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign};
/// Unix permission bit flags
#[derive(Clone, Copy, PartialEq)]
pub enum Bit {
Suid = 0o4000,
Sgid = 0o2000,
Sticky = 0o1000,
URead = 0o400,
UWrite = 0o200,
UExec = 0o100,
GRead = 0o40,
GWrite = 0o20,
GExec = 0o10,
ORead = 0o4,
OWrite = 0o2,
OExec = 0o1,
}
impl BitAnd<u32> for Bit {
type Output = u32;
fn bitand(self, rhs: u32) -> Self::Output {
self as u32 & rhs
}
}
impl BitAnd<Bit> for u32 {
type Output = u32;
fn bitand(self, rhs: Bit) -> Self::Output {
self & rhs as u32
}
}
impl BitAnd for Bit {
type Output = u32;
fn bitand(self, rhs: Self) -> Self::Output {
self as u32 & rhs as u32
}
}
impl BitAndAssign<Bit> for u32 {
fn bitand_assign(&mut self, rhs: Bit) {
*self = *self & rhs;
}
}
impl BitOr<u32> for Bit {
type Output = u32;
fn bitor(self, rhs: u32) -> Self::Output {
self as u32 | rhs
}
}
impl BitOr<Bit> for u32 {
type Output = u32;
fn bitor(self, rhs: Bit) -> Self::Output {
self | rhs as u32
}
}
impl BitOr for Bit {
type Output = u32;
fn bitor(self, rhs: Self) -> Self::Output {
self as u32 | rhs as u32
}
}
impl BitOrAssign<Bit> for u32 {
fn bitor_assign(&mut self, rhs: Bit) {
*self = *self | rhs;
}
}
impl Bit {
pub fn as_char(&self, mode: u32) -> char {
if mode & *self != 0 {
match self {
Self::Suid | Self::Sgid => 's',
Self::Sticky => 't',
Self::URead | Self::GRead | Self::ORead => 'r',
Self::UWrite | Self::GWrite | Self::OWrite => 'w',
Self::UExec if mode.contains(Self::Suid) => 's',
Self::GExec if mode.contains(Self::Sgid) => 's',
Self::OExec if mode.contains(Self::Sticky) => 't',
Self::UExec | Self::GExec | Self::OExec => 'x',
}
} else {
'-'
}
}
}

View File

@ -1,11 +1,10 @@
//! Functions for parsing and managing permissions
mod bit;
mod parser;
use std::{
fmt::{self, Write},
ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign},
};
mod who;
use std::fmt::{self, Write};
pub use parser::{ParseError, Parser};
pub use {bit::Bit, parser::{ParseError, Parser}, who::Who};
/// Gets the umask for the current user
#[must_use]
@ -15,42 +14,6 @@ pub fn get_umask() -> u32 {
mask
}
/// Unix permission bit flags
#[derive(Clone, Copy, PartialEq)]
pub enum Bit {
Suid = 0o4000,
Sgid = 0o2000,
Sticky = 0o1000,
URead = 0o400,
UWrite = 0o200,
UExec = 0o100,
GRead = 0o40,
GWrite = 0o20,
GExec = 0o10,
ORead = 0o4,
OWrite = 0o2,
OExec = 0o1,
}
impl Bit {
fn as_char(&self, mode: u32) -> char {
if mode & *self != 0 {
match self {
Self::Suid | Self::Sgid => 's',
Self::Sticky => 't',
Self::URead | Self::GRead | Self::ORead => 'r',
Self::UWrite | Self::GWrite | Self::OWrite => 'w',
Self::UExec if mode & Self::Suid != 0 => 's',
Self::GExec if mode & Self::Sgid != 0 => 's',
Self::OExec if mode & Self::Sticky != 0 => 't',
Self::UExec | Self::GExec | Self::OExec => 'x',
}
} else {
'-'
}
}
}
/// Functions for extracting information about Unix modes
pub trait Mode {
/// Returns a string representing permissions in symbolic format
@ -110,50 +73,6 @@ impl Mode for u32 {
}
}
impl BitAnd<u32> for Bit {
type Output = u32;
fn bitand(self, rhs: u32) -> Self::Output {
self as u32 & rhs
}
}
impl BitAnd<Bit> for u32 {
type Output = u32;
fn bitand(self, rhs: Bit) -> Self::Output {
self & rhs as u32
}
}
impl BitAndAssign<Bit> for u32 {
fn bitand_assign(&mut self, rhs: Bit) {
*self = *self & rhs;
}
}
impl BitOr<u32> for Bit {
type Output = u32;
fn bitor(self, rhs: u32) -> Self::Output {
self as u32 | rhs
}
}
impl BitOr<Bit> for u32 {
type Output = u32;
fn bitor(self, rhs: Bit) -> Self::Output {
self | rhs as u32
}
}
impl BitOrAssign<Bit> for u32 {
fn bitor_assign(&mut self, rhs: Bit) {
*self = *self | rhs;
}
}
#[cfg(test)]
mod test {
use super::*;

View File

@ -1,11 +1,9 @@
use crate::bitflags::BitFlags;
use super::{get_umask, Bit};
use super::{get_umask, Bit, Who};
use std::{
error,
fmt::Display,
num::ParseIntError,
ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign},
};
/// Errors which might occur when parsing Unix permissions from a string
@ -48,53 +46,6 @@ enum Op {
Equals,
}
#[derive(PartialEq)]
/// The granularity of the given permissions
enum Who {
/// applies for the current user
User = 0o100,
/// applies for the current group
Group = 0o10,
/// applies for everyone else
Other = 0o1,
}
impl BitAnd<Who> for u32 {
type Output = u32;
fn bitand(self, rhs: Who) -> Self::Output {
self & rhs as 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;
}
}
impl BitOr<Who> for u32 {
type Output = u32;
fn bitor(self, rhs: Who) -> Self::Output {
self | rhs as u32
}
}
impl BitOrAssign<Who> for u32 {
fn bitor_assign(&mut self, rhs: Who) {
*self = *self | rhs;
}
}
/// A parser for octal and symbolic permissions. `Parser::default` creates an
/// instance which applies the given operations to the default setting for the
/// current user's umask. `Parser::new` creates a parser which applies the given

49
src/mode/who.rs Normal file
View File

@ -0,0 +1,49 @@
use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign};
#[derive(PartialEq)]
/// The granularity of the given permissions
pub enum Who {
/// applies for the current user
User = 0o100,
/// applies for the current group
Group = 0o10,
/// applies for everyone else
Other = 0o1,
}
impl BitAnd<Who> for u32 {
type Output = u32;
fn bitand(self, rhs: Who) -> Self::Output {
self & rhs as 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;
}
}
impl BitOr<Who> for u32 {
type Output = u32;
fn bitor(self, rhs: Who) -> Self::Output {
self | rhs as u32
}
}
impl BitOrAssign<Who> for u32 {
fn bitor_assign(&mut self, rhs: Who) {
*self = *self | rhs;
}
}