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:
parent
6c0b7cb787
commit
3ef3a8b3aa
@ -1,3 +1,4 @@
|
|||||||
|
use crate::{bitflags::BitFlags, mode::Bit};
|
||||||
use super::Cmd;
|
use super::Cmd;
|
||||||
use clap::{Arg, Command};
|
use clap::{Arg, Command};
|
||||||
use std::{env, fs::File, io, os::unix::prelude::MetadataExt, path::PathBuf, process};
|
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 myuid = unsafe { libc::geteuid() };
|
||||||
let mygroups = crate::pw::get_gids();
|
let mygroups = crate::pw::get_gids();
|
||||||
// we own the file and it has u+x
|
// 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()));
|
return Some(format!("{}", file.display()));
|
||||||
// file has ug+x
|
// file has ug+x
|
||||||
} else if mode & 0o110 != 0 {
|
} else if mode.contains(Bit::UExec | Bit::GExec) {
|
||||||
if let Ok(groups) = mygroups {
|
if let Ok(groups) = mygroups {
|
||||||
// one of our groups owns the file
|
// one of our groups owns the file
|
||||||
if groups.contains(&meta.gid()) {
|
if groups.contains(&meta.gid()) {
|
||||||
|
99
src/mode/bit.rs
Normal file
99
src/mode/bit.rs
Normal 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 {
|
||||||
|
'-'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,11 +1,10 @@
|
|||||||
//! Functions for parsing and managing permissions
|
//! Functions for parsing and managing permissions
|
||||||
|
mod bit;
|
||||||
mod parser;
|
mod parser;
|
||||||
use std::{
|
mod who;
|
||||||
fmt::{self, Write},
|
use std::fmt::{self, Write};
|
||||||
ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub use parser::{ParseError, Parser};
|
pub use {bit::Bit, parser::{ParseError, Parser}, who::Who};
|
||||||
|
|
||||||
/// Gets the umask for the current user
|
/// Gets the umask for the current user
|
||||||
#[must_use]
|
#[must_use]
|
||||||
@ -15,42 +14,6 @@ pub fn get_umask() -> u32 {
|
|||||||
mask
|
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
|
/// Functions for extracting information about Unix modes
|
||||||
pub trait Mode {
|
pub trait Mode {
|
||||||
/// Returns a string representing permissions in symbolic format
|
/// 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)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
use crate::bitflags::BitFlags;
|
use crate::bitflags::BitFlags;
|
||||||
|
use super::{get_umask, Bit, Who};
|
||||||
use super::{get_umask, Bit};
|
|
||||||
use std::{
|
use std::{
|
||||||
error,
|
error,
|
||||||
fmt::Display,
|
fmt::Display,
|
||||||
num::ParseIntError,
|
num::ParseIntError,
|
||||||
ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Errors which might occur when parsing Unix permissions from a string
|
/// Errors which might occur when parsing Unix permissions from a string
|
||||||
@ -48,53 +46,6 @@ enum Op {
|
|||||||
Equals,
|
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
|
/// A parser for octal and symbolic permissions. `Parser::default` creates an
|
||||||
/// instance which applies the given operations to the default setting for the
|
/// 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
|
/// current user's umask. `Parser::new` creates a parser which applies the given
|
||||||
|
49
src/mode/who.rs
Normal file
49
src/mode/who.rs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user