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 1040D93E8A for ; Wed, 21 Feb 2024 12:08:47 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id DE19415787 for ; Wed, 21 Feb 2024 12:08:14 +0100 (CET) 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, 21 Feb 2024 12:08:10 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 3DB8144458 for ; Wed, 21 Feb 2024 12:08:10 +0100 (CET) From: Aaron Lauterer To: pve-devel@lists.proxmox.com Date: Wed, 21 Feb 2024 12:07:58 +0100 Message-Id: <20240221110805.931925-16-a.lauterer@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240221110805.931925-1-a.lauterer@proxmox.com> References: <20240221110805.931925-1-a.lauterer@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.063 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 T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pve-devel] [PATCH v2 15/22] 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: , X-List-Received-Date: Wed, 21 Feb 2024 11:08:47 -0000 Signed-off-by: Aaron Lauterer --- proxmox-auto-installer/Cargo.toml | 1 + proxmox-auto-installer/src/utils.rs | 48 +++++++++++------------------ 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/proxmox-auto-installer/Cargo.toml b/proxmox-auto-installer/Cargo.toml index 3dcb5fa..c00d25a 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 23bedd9..38aab91 100644 --- a/proxmox-auto-installer/src/utils.rs +++ b/proxmox-auto-installer/src/utils.rs @@ -1,4 +1,5 @@ use anyhow::{anyhow, bail, Context, Result}; +use glob::Pattern; use log::info; use std::{ collections::BTreeMap, @@ -21,30 +22,12 @@ 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), + } } pub fn get_network_settings( @@ -107,7 +90,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 } @@ -132,7 +115,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; @@ -463,9 +446,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