Skip to content
Myr

Merge pull request #1164 from tmccombs/owner-without-panic

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

Fix panic when using --owner

authored by David Peter · committed by GitHub · parents fbef976, b04cae2 · 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.

Diff stats

3 files+70 added−16 removed

  • tests:+44 added−0 removed
  • other:+26 added−16 removed

3 files+70 added−16 removed

modified:src/filter/owner.rs+25 added-15 removed
1use anyhow::{anyhow, Result};2use std::fs;34#[derive(Clone, Copy, Debug, PartialEq)]4#[derive(Clone, Copy, Debug, PartialEq, Eq)]5pub struct OwnerFilter {6 uid: Check<u32>,7 gid: Check<u32>,8}910#[derive(Clone, Copy, Debug, PartialEq)]10#[derive(Clone, Copy, Debug, PartialEq, Eq)]11enum Check<T> {12 Equal(T),13 NotEq(T),14 Ignore,15}1617impl OwnerFilter {18 const IGNORE: Self = OwnerFilter {19 uid: Check::Ignore,20 gid: Check::Ignore,21 };2223 /// 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());2930 if it.next().is_some() {
15 unmodified lines
46 .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 })?;4945 if let (Check::Ignore, Check::Ignore) = (uid, gid) {46 Ok(None)50 Ok(OwnerFilter { uid, gid })51 }5253 /// 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 }6162 pub fn matches(&self, md: &fs::Metadata) -> bool {
52 unmodified lines
115 }116117 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 }),125116 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) }),129130 more_colons:"3:5:" => Err(_),131 only_colons:"::" => Err(_),132 }
1 unmodified line
modified:src/main.rs+1 added-1 removed
182 unmodified lines
183184 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);188189 #[cfg(windows)]190 let ansi_colors_support =191 ansi_term::enable_ansi_support().is_ok() || std::env::var_os("TERM").is_some();
248 unmodified lines
modified:tests/tests.rs+44 added
1917 unmodified lines
1918 "30dec2017",1919 );1920}19211922#[cfg(unix)]1923#[test]1924fn 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}19291930#[cfg(unix)]1931#[test]1932fn 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}19401941#[cfg(unix)]1942#[test]1943fn 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}19511952#[cfg(target_os = "linux")]1953#[test]1954fn 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}19651966#[test]1967fn test_custom_path_separator() {1968 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);19691970 te.assert_output(
250 unmodified lines