From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <a.lauterer@proxmox.com>
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 2A8C691BC7
 for <pve-devel@lists.proxmox.com>; Thu,  4 Apr 2024 16:49:13 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 7FDEA4350
 for <pve-devel@lists.proxmox.com>; Thu,  4 Apr 2024 16:49:12 +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 <pve-devel@lists.proxmox.com>; Thu,  4 Apr 2024 16:49:09 +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 62F5745162
 for <pve-devel@lists.proxmox.com>; Thu,  4 Apr 2024 16:49:09 +0200 (CEST)
From: Aaron Lauterer <a.lauterer@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Thu,  4 Apr 2024 16:48:49 +0200
Message-Id: <20240404144902.273800-18-a.lauterer@proxmox.com>
X-Mailer: git-send-email 2.39.2
In-Reply-To: <20240404144902.273800-1-a.lauterer@proxmox.com>
References: <20240404144902.273800-1-a.lauterer@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.057 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 v4 17/30] 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 <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Thu, 04 Apr 2024 14:49:13 -0000

Signed-off-by: Aaron Lauterer <a.lauterer@proxmox.com>
---
 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<bool> {
+    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