From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id EED3FB52F for ; Wed, 9 Aug 2023 12:19:49 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id D1F71136E2 for ; Wed, 9 Aug 2023 12:19:19 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Wed, 9 Aug 2023 12:19:19 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id E3DD743391 for ; Wed, 9 Aug 2023 12:19:18 +0200 (CEST) From: Gabriel Goller To: pbs-devel@lists.proxmox.com Date: Wed, 9 Aug 2023 12:19:12 +0200 Message-Id: <20230809101913.81818-1-g.goller@proxmox.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.001 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH pathpatterns] match_list: added `matches_path()` function, which matches only the path X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 09 Aug 2023 10:19:50 -0000 Added `matches_path()` function, which only matches against the path and returns an error if a file_mode pattern is found/needed in the matching list. This is useful when we want to check if a file is excluded before running `stat()` on the file to get the file_mode (which could fail). Signed-off-by: Gabriel Goller --- src/match_list.rs | 159 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 158 insertions(+), 1 deletion(-) diff --git a/src/match_list.rs b/src/match_list.rs index c5b14e0..acad328 100644 --- a/src/match_list.rs +++ b/src/match_list.rs @@ -1,6 +1,6 @@ //! Helpers for include/exclude lists. - use bitflags::bitflags; +use std::fmt; use crate::PatternFlag; @@ -39,6 +39,17 @@ impl Default for MatchFlag { } } +#[derive(Debug, PartialEq)] +pub struct FileModeRequiredForMatching; + +impl fmt::Display for FileModeRequiredForMatching { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "File mode is required for matching") + } +} + +impl std::error::Error for FileModeRequiredForMatching {} + /// A pattern entry. (Glob patterns or literal patterns.) // Note: // For regex we'd likely use the POSIX extended REs via `regexec(3)`, since we're targetting @@ -304,12 +315,32 @@ impl MatchEntry { self.matches_path_exact(path) } + + /// Check whether the path contains a matching suffix. Returns an error if a file mode is required. + pub fn matches_path>( + &self, + path: T, + ) -> Result { + self.matches_path_do(path.as_ref()) + } + + fn matches_path_do(&self, path: &[u8]) -> Result { + if !self.flags.contains(MatchFlag::ANY_FILE_TYPE) { + return Err(FileModeRequiredForMatching); + } + + Ok(self.matches_path_suffix_do(path)) + } } #[doc(hidden)] pub trait MatchListEntry { fn entry_matches(&self, path: &[u8], file_mode: Option) -> Option; fn entry_matches_exact(&self, path: &[u8], file_mode: Option) -> Option; + fn entry_matches_path( + &self, + path: &[u8], + ) -> Result, FileModeRequiredForMatching>; } impl MatchListEntry for &'_ MatchEntry { @@ -328,6 +359,21 @@ impl MatchListEntry for &'_ MatchEntry { None } } + + fn entry_matches_path( + &self, + path: &[u8], + ) -> Result, FileModeRequiredForMatching> { + if let Ok(b) = self.matches_path(path) { + if b { + Ok(Some(self.match_type())) + } else { + Ok(None) + } + } else { + Err(FileModeRequiredForMatching) + } + } } impl MatchListEntry for &'_ &'_ MatchEntry { @@ -346,6 +392,21 @@ impl MatchListEntry for &'_ &'_ MatchEntry { None } } + + fn entry_matches_path( + &self, + path: &[u8], + ) -> Result, FileModeRequiredForMatching> { + if let Ok(b) = self.matches_path(path) { + if b { + Ok(Some(self.match_type())) + } else { + Ok(None) + } + } else { + Err(FileModeRequiredForMatching) + } + } } /// This provides [`matches`](MatchList::matches) and [`matches_exact`](MatchList::matches_exact) @@ -374,6 +435,20 @@ pub trait MatchList { } fn matches_exact_do(&self, path: &[u8], file_mode: Option) -> Option; + + /// Check whether this list contains anything exactly matching the path, returns error if + /// `file_mode` is required for exact matching. + fn matches_path>( + &self, + path: T, + ) -> Result, FileModeRequiredForMatching> { + self.matches_path_do(path.as_ref()) + } + + fn matches_path_do( + &self, + path: &[u8], + ) -> Result, FileModeRequiredForMatching>; } impl<'a, T> MatchList for T @@ -408,6 +483,24 @@ where None } + + fn matches_path_do( + &self, + path: &[u8], + ) -> Result, FileModeRequiredForMatching> { + // This is an &self method on a `T where T: 'a`. + let this: &'a Self = unsafe { std::mem::transmute(self) }; + + for m in this.into_iter().rev() { + if let Ok(mt) = m.entry_matches_path(path) { + if mt.is_some() { + return Ok(mt); + } + } + } + + Err(FileModeRequiredForMatching) + } } #[test] @@ -530,3 +623,67 @@ fn test_path_relativity() { assert_eq!(matchlist.matches("foo/slash", None), None); assert_eq!(matchlist.matches("foo/slash-a", None), None); } + +#[test] +fn test_matches_path() { + let vec = vec![ + MatchEntry::include(crate::Pattern::path("as*").unwrap()), + MatchEntry::include(crate::Pattern::path("a*").unwrap()), + ]; + assert_eq!(vec.matches_path("asdf"), Ok(Some(MatchType::Include))); + + let list: &[MatchEntry] = &vec[..]; + assert_eq!(list.matches_path("asdf"), Ok(Some(MatchType::Include))); + + let list: Vec<&MatchEntry> = vec.iter().collect(); + assert_eq!(list.matches_path("asdf"), Ok(Some(MatchType::Include))); + + let list: &[&MatchEntry] = &list[..]; + assert_eq!(list.matches_path("asdf"), Ok(Some(MatchType::Include))); + + let vec = vec![ + MatchEntry::include(crate::Pattern::path("a*").unwrap()), + MatchEntry::include(crate::Pattern::path("a*").unwrap()) + .flags(MatchFlag::MATCH_REGULAR_FILES), + ]; + assert_eq!(vec.matches_path("asdf"), Ok(Some(MatchType::Include))); + + let vec = vec![ + MatchEntry::include(crate::Pattern::path("a*").unwrap()), + MatchEntry::include(crate::Pattern::path("asdf").unwrap()), + ]; + assert_eq!(vec.matches_path("asdf"), Ok(Some(MatchType::Include))); + + let list: &[MatchEntry] = &vec[..]; + assert_eq!(list.matches_path("azdf"), Ok(Some(MatchType::Include))); + + let vec = vec![ + MatchEntry::include(crate::Pattern::path("a*").unwrap()).flags(MatchFlag::ANY_FILE_TYPE), + MatchEntry::include(crate::Pattern::path("asdf").unwrap()).flags(MatchFlag::ANY_FILE_TYPE), + ]; + assert_eq!(vec.matches_path("asdf"), Ok(Some(MatchType::Include))); + assert_eq!(vec.matches_path("adbb"), Ok(Some(MatchType::Include))); +} + +#[test] +fn test_matches_path_error() { + let vec = vec![ + MatchEntry::include(crate::Pattern::path("a*").unwrap()) + .flags(MatchFlag::MATCH_REGULAR_FILES), + MatchEntry::include(crate::Pattern::path("asdf").unwrap()), + ]; + assert_eq!(vec.matches_path("asdf"), Ok(Some(MatchType::Include))); + + let list: &[MatchEntry] = &vec[..]; + assert_eq!(list.matches_path("azdf"), Err(FileModeRequiredForMatching)); + + let list: Vec<&MatchEntry> = vec.iter().collect(); + assert_eq!(list.matches_path("abdf"), Err(FileModeRequiredForMatching)); + + let list: &[&MatchEntry] = &list[..]; + assert_eq!(list.matches_path("acdf"), Err(FileModeRequiredForMatching)); + + let vec = vec![MatchEntry::include(crate::Pattern::path("a*").unwrap()) + .flags(MatchFlag::MATCH_DIRECTORIES)]; + assert_eq!(vec.matches_path("asdf"), Err(FileModeRequiredForMatching)); +} -- 2.39.2