Fix panic when using --owner
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Unfortunately, clap_derive can't combine a value_parser of Option<T> with an optional argument to get a merged Option<T> so we need to do the check for the nop outside of the value parser. Also adds some tests for --owner
authored by Thayne McCombs · committed by Thayne McCombs · parent 567ce26 · Browse files · 3 files·+70 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Fixes: #1163
Diff stats
3 files+70 added−16 removed
- tests:+44 added−0 removed
- other:+26 added−16 removed
Provenance
- Commit
c159ea2- Tree
097bf92- Author
Thayne McCombs <astrothayne@gmail.com>- Committer
Thayne McCombs <astrothayne@gmail.com>- Parent
567ce26
3 files changed+70 added−16 removed
modified:src/filter/owner.rs+25 added-15 removed
1 use anyhow::{anyhow, Result};2 use std::fs;3 4-#[derive(Clone, Copy, Debug, PartialEq)]4+#[derive(Clone, Copy, Debug, PartialEq, Eq)]5 pub struct OwnerFilter {6 uid: Check<u32>,7 gid: Check<u32>,8 }9 10-#[derive(Clone, Copy, Debug, PartialEq)]10+#[derive(Clone, Copy, Debug, PartialEq, Eq)]11 enum Check<T> {12 Equal(T),13 NotEq(T),14 Ignore,15 }16 17 impl OwnerFilter {18+ const IGNORE: Self = OwnerFilter {19+ uid: Check::Ignore,20+ gid: Check::Ignore,21+ };22+23 /// Parses an owner constraint24 /// Returns an error if the string is invalid25 /// Returns Ok(None) when string is acceptable but a noop (such as "" or ":")21- pub fn from_string(input: &str) -> Result<Option<Self>> {26+ pub fn from_string(input: &str) -> Result<Self> {27 let mut it = input.split(':');28 let (fst, snd) = (it.next(), it.next());29 30 if it.next().is_some() {15 unmodified lines46 .or_else(|| users::get_group_by_name(s).map(|group| group.gid()))47 .ok_or_else(|| anyhow!("'{}' is not a recognized group name", s))48 })?;49 45- if let (Check::Ignore, Check::Ignore) = (uid, gid) {46- Ok(None)50+ Ok(OwnerFilter { uid, gid })51+ }52+53+ /// If self is a no-op (ignore both uid and gid) then return `None`, otherwise wrap in a `Some`54+ pub fn filter_ignore(self) -> Option<Self> {55+ if self == Self::IGNORE {56+ None57 } else {48- Ok(Some(OwnerFilter { uid, gid }))58+ Some(self)59 }60 }61 62 pub fn matches(&self, md: &fs::Metadata) -> bool {52 unmodified lines115 }116 117 use super::Check::*;118 owner_tests! {109- empty: "" => Ok(None),110- uid_only: "5" => Ok(Some(OwnerFilter { uid: Equal(5), gid: Ignore })),111- uid_gid: "9:3" => Ok(Some(OwnerFilter { uid: Equal(9), gid: Equal(3) })),112- gid_only: ":8" => Ok(Some(OwnerFilter { uid: Ignore, gid: Equal(8) })),113- colon_only: ":" => Ok(None),114- trailing: "5:" => Ok(Some(OwnerFilter { uid: Equal(5), gid: Ignore })),119+ empty: "" => Ok(OwnerFilter::IGNORE),120+ uid_only: "5" => Ok(OwnerFilter { uid: Equal(5), gid: Ignore }),121+ uid_gid: "9:3" => Ok(OwnerFilter { uid: Equal(9), gid: Equal(3) }),122+ gid_only: ":8" => Ok(OwnerFilter { uid: Ignore, gid: Equal(8) }),123+ colon_only: ":" => Ok(OwnerFilter::IGNORE),124+ trailing: "5:" => Ok(OwnerFilter { uid: Equal(5), gid: Ignore }),125 116- uid_negate: "!5" => Ok(Some(OwnerFilter { uid: NotEq(5), gid: Ignore })),117- both_negate:"!4:!3" => Ok(Some(OwnerFilter { uid: NotEq(4), gid: NotEq(3) })),118- uid_not_gid:"6:!8" => Ok(Some(OwnerFilter { uid: Equal(6), gid: NotEq(8) })),126+ uid_negate: "!5" => Ok(OwnerFilter { uid: NotEq(5), gid: Ignore }),127+ both_negate:"!4:!3" => Ok(OwnerFilter { uid: NotEq(4), gid: NotEq(3) }),128+ uid_not_gid:"6:!8" => Ok(OwnerFilter { uid: Equal(6), gid: NotEq(8) }),129 130 more_colons:"3:5:" => Err(_),131 only_colons:"::" => Err(_),132 }1 unmodified linemodified:src/main.rs+1 added-1 removed
182 unmodified lines183 184 let size_limits = std::mem::take(&mut opts.size);185 let time_constraints = extract_time_constraints(&opts)?;186 #[cfg(unix)]187- let owner_constraint: Option<OwnerFilter> = opts.owner;187+ let owner_constraint: Option<OwnerFilter> = opts.owner.and_then(OwnerFilter::filter_ignore);188 189 #[cfg(windows)]190 let ansi_colors_support =191 ansi_term::enable_ansi_support().is_ok() || std::env::var_os("TERM").is_some();248 unmodified linesmodified:tests/tests.rs+44 added
1917 unmodified lines1918 "30dec2017",1919 );1920 }1921 1922+#[cfg(unix)]1923 #[test]1924+fn test_owner_ignore_all() {1925+ let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);1926+ te.assert_output(&["--owner", ":", "a.foo"], "a.foo");1927+ te.assert_output(&["--owner", "", "a.foo"], "a.foo");1928+}1929+1930+#[cfg(unix)]1931+#[test]1932+fn test_owner_current_user() {1933+ let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);1934+ let uid = users::get_current_uid();1935+ te.assert_output(&["--owner", &uid.to_string(), "a.foo"], "a.foo");1936+ if let Some(username) = users::get_current_username().map(|u| u.into_string().unwrap()) {1937+ te.assert_output(&["--owner", &username, "a.foo"], "a.foo");1938+ }1939+}1940+1941+#[cfg(unix)]1942+#[test]1943+fn test_owner_current_group() {1944+ let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);1945+ let gid = users::get_current_gid();1946+ te.assert_output(&["--owner", &format!(":{}", gid), "a.foo"], "a.foo");1947+ if let Some(groupname) = users::get_current_groupname().map(|u| u.into_string().unwrap()) {1948+ te.assert_output(&["--owner", &format!(":{}", groupname), "a.foo"], "a.foo");1949+ }1950+}1951+1952+#[cfg(unix)]1953+#[test]1954+fn test_owner_root() {1955+ // This test assumes the current user isn't root1956+ if users::get_current_uid() == 0 || users::get_current_gid() == 0 {1957+ return;1958+ }1959+ let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);1960+ te.assert_output(&["--owner", "root", "a.foo"], "");1961+ te.assert_output(&["--owner", "0", "a.foo"], "");1962+ te.assert_output(&["--owner", ":root", "a.foo"], "");1963+ te.assert_output(&["--owner", ":0", "a.foo"], "");1964+}1965+1966+#[test]1967 fn test_custom_path_separator() {1968 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);1969 1970 te.assert_output(250 unmodified lines