From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 152951FF348 for ; Wed, 17 Apr 2024 14:32:16 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 197E55A89; Wed, 17 Apr 2024 14:31:45 +0200 (CEST) From: Aaron Lauterer To: pve-devel@lists.proxmox.com Date: Wed, 17 Apr 2024 14:30:49 +0200 Message-Id: <20240417123108.212720-18-a.lauterer@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240417123108.212720-1-a.lauterer@proxmox.com> References: <20240417123108.212720-1-a.lauterer@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.047 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: [pve-devel] [PATCH installer v6 17/36] auto-installer: use glob crate for pattern matching X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox VE development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" Tested-by: Christoph Heiss Reviewed-by: Christoph Heiss Signed-off-by: Aaron Lauterer --- proxmox-auto-installer/Cargo.toml | 1 + proxmox-auto-installer/src/utils.rs | 46 +++++++++++------------------ 2 files changed, 18 insertions(+), 29 deletions(-) diff --git a/proxmox-auto-installer/Cargo.toml b/proxmox-auto-installer/Cargo.toml index b1d3e7a..741794a 100644 --- a/proxmox-auto-installer/Cargo.toml +++ b/proxmox-auto-installer/Cargo.toml @@ -9,6 +9,7 @@ homepage = "https://www.proxmox.com" [dependencies] anyhow = "1.0" +glob = "0.3" proxmox-installer-common = { path = "../proxmox-installer-common" } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/proxmox-auto-installer/src/utils.rs b/proxmox-auto-installer/src/utils.rs index ae28b1e..6101d66 100644 --- a/proxmox-auto-installer/src/utils.rs +++ b/proxmox-auto-installer/src/utils.rs @@ -1,4 +1,5 @@ use anyhow::{bail, Result}; +use glob::Pattern; use log::info; use std::{ collections::BTreeMap, @@ -15,29 +16,11 @@ use proxmox_installer_common::{ }; use serde::Deserialize; -/// Supports the globbing character '*' at the beginning, end or both of the pattern. -/// Globbing within the pattern is not supported -fn find_with_glob(pattern: &str, value: &str) -> bool { - let globbing_symbol = '*'; - let mut start_glob = false; - let mut end_glob = false; - let mut pattern = pattern; - - if pattern.starts_with(globbing_symbol) { - start_glob = true; - pattern = &pattern[1..]; - } - - if pattern.ends_with(globbing_symbol) { - end_glob = true; - pattern = &pattern[..pattern.len() - 1] - } - - match (start_glob, end_glob) { - (true, true) => value.contains(pattern), - (true, false) => value.ends_with(pattern), - (false, true) => value.starts_with(pattern), - _ => value == pattern, +fn find_with_glob(pattern: &str, value: &str) -> Result { + let p = Pattern::new(pattern)?; + match p.matches(value) { + true => Ok(true), + false => Ok(false), } } @@ -75,7 +58,7 @@ fn get_single_udev_index( 'outer: for (dev, dev_values) in udev_list { for (filter_key, filter_value) in &filter { for (udev_key, udev_value) in dev_values { - if udev_key == filter_key && find_with_glob(filter_value, udev_value) { + if udev_key == filter_key && find_with_glob(filter_value, udev_value)? { dev_index = Some(dev.clone()); break 'outer; // take first match } @@ -100,7 +83,7 @@ fn get_matched_udev_indexes( let mut did_match_all = true; for (filter_key, filter_value) in &filter { for (udev_key, udev_value) in dev_values { - if udev_key == filter_key && find_with_glob(filter_value, udev_value) { + if udev_key == filter_key && find_with_glob(filter_value, udev_value)? { did_match_once = true; } else if udev_key == filter_key { did_match_all = false; @@ -416,9 +399,14 @@ mod tests { #[test] fn test_glob_patterns() { let test_value = "foobar"; - assert_eq!(find_with_glob("*bar", test_value), true); - assert_eq!(find_with_glob("foo*", test_value), true); - assert_eq!(find_with_glob("foobar", test_value), true); - assert_eq!(find_with_glob("oobar", test_value), false); + assert_eq!(find_with_glob("*bar", test_value).unwrap(), true); + assert_eq!(find_with_glob("foo*", test_value).unwrap(), true); + assert_eq!(find_with_glob("foobar", test_value).unwrap(), true); + assert_eq!(find_with_glob("oobar", test_value).unwrap(), false); + assert_eq!(find_with_glob("f*bar", test_value).unwrap(), true); + assert_eq!(find_with_glob("f?bar", test_value).unwrap(), false); + assert_eq!(find_with_glob("fo?bar", test_value).unwrap(), true); + assert_eq!(find_with_glob("f[!a]obar", test_value).unwrap(), true); + assert_eq!(find_with_glob("f[oa]obar", test_value).unwrap(), true); } } -- 2.39.2 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel