Skip to content
Myr

[ownership] change uid/gid storage to tristate enum for negation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Alexandru Macovei · committed by David Peter · parent d7cc34e · Browse files · 1 file+35 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+35 added−10 removed

  • other:+35 added−10 removed

1 file+35 added−10 removed

modified:src/filter/owner.rs+35 added-10 removed
1 unmodified line
2use std::fs;34#[derive(Clone, Copy, Debug, PartialEq)]5pub struct OwnerFilter {6 uid: Option<u32>,7 gid: Option<u32>,6 uid: Check<u32>,7 gid: Check<u32>,8}910#[derive(Clone, Copy, Debug, PartialEq)]11enum Check<T> {12 Equal(T),13 NotEq(T),14 Ignore,15}1617impl OwnerFilter {18 pub fn from_string(input: &str) -> Result<Self> {19 let mut it = input.split(':');20 let (fst, snd) = (it.next(), it.next());
24 unmodified lines
45 }46 }47 };4842 if uid.is_none() && gid.is_none() {49 use self::Check::*;50 let uid = match uid {51 Some(u) => Equal(u),52 _ => Ignore,53 };54 let gid = match gid {55 Some(g) => Equal(g),56 _ => Ignore,57 };5859 if let (Ignore, Ignore) = (uid, gid) {60 Err(anyhow!(61 "'{}' is not a valid user/group specifier. See 'fd --help'.",62 input63 ))
4 unmodified lines
6869 pub fn matches(&self, md: &fs::Metadata) -> bool {70 use std::os::unix::fs::MetadataExt;7155 let uid_ok = self.uid.map(|u| u == md.uid()).unwrap_or(true);56 let gid_ok = self.gid.map(|g| g == md.gid()).unwrap_or(true);72 self.uid.check(md.uid()) && self.gid.check(md.gid())73 }74}7558 uid_ok && gid_ok76impl<T: PartialEq> Check<T> {77 fn check(&self, v: T) -> bool {78 match self {79 Check::Equal(x) => v == *x,80 Check::NotEq(x) => v != *x,81 Check::Ignore => true,82 }83 }84}8586#[cfg(test)]
14 unmodified lines
101 )*102 };103 }104105 use super::Check::*;106 owner_tests! {107 empty: "" => Err(_),83 uid_only: "5" => Ok(OwnerFilter { uid: Some(5), gid: None }),84 uid_gid: "9:3" => Ok(OwnerFilter { uid: Some(9), gid: Some(3)}),85 gid_only: ":8" => Ok(OwnerFilter { uid: None, gid: Some(8)}),108 uid_only: "5" => Ok(OwnerFilter { uid: Equal(5), gid: Ignore }),109 uid_gid: "9:3" => Ok(OwnerFilter { uid: Equal(9), gid: Equal(3) }),110 gid_only: ":8" => Ok(OwnerFilter { uid: Ignore, gid: Equal(8) }),111 colon_only: ":" => Err(_),87 trailing: "5:" => Ok(OwnerFilter { uid: Some(5), gid: None }),112 trailing: "5:" => Ok(OwnerFilter { uid: Equal(5), gid: Ignore }),113 }114}