From 4630a91ef94671b2e03460966cc0d7b0738a7fef Mon Sep 17 00:00:00 2001 From: David Herberth Date: Tue, 13 Aug 2024 11:28:05 +0200 Subject: [PATCH] ref(relay): Move cabi glob matching into cabi --- Cargo.lock | 3 +- relay-cabi/Cargo.toml | 1 + {relay-common => relay-cabi}/src/glob.rs | 64 ++++++++++++++++++------ relay-cabi/src/lib.rs | 2 + relay-cabi/src/processing.rs | 43 +--------------- relay-common/Cargo.toml | 2 - relay-common/src/lib.rs | 1 - 7 files changed, 55 insertions(+), 61 deletions(-) rename {relay-common => relay-cabi}/src/glob.rs (72%) diff --git a/Cargo.lock b/Cargo.lock index 320b43cf55..5dd09847fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3620,6 +3620,7 @@ version = "0.9.1" dependencies = [ "anyhow", "chrono", + "globset", "json-forensics", "lru", "once_cell", @@ -3670,9 +3671,7 @@ version = "24.7.1" dependencies = [ "chrono", "globset", - "lru", "once_cell", - "parking_lot", "regex", "sentry-types", "serde", diff --git a/relay-cabi/Cargo.toml b/relay-cabi/Cargo.toml index 2135e61567..c347a76d17 100644 --- a/relay-cabi/Cargo.toml +++ b/relay-cabi/Cargo.toml @@ -18,6 +18,7 @@ workspace = true [dependencies] anyhow = { workspace = true, features = ["backtrace"] } chrono = { workspace = true } +globset = { workspace = true } json-forensics = { workspace = true } lru = { workspace = true } once_cell = { workspace = true } diff --git a/relay-common/src/glob.rs b/relay-cabi/src/glob.rs similarity index 72% rename from relay-common/src/glob.rs rename to relay-cabi/src/glob.rs index b39a4b3100..e662358de2 100644 --- a/relay-common/src/glob.rs +++ b/relay-cabi/src/glob.rs @@ -6,8 +6,50 @@ use std::num::NonZeroUsize; use globset::GlobBuilder; use lru::LruCache; use once_cell::sync::Lazy; -use parking_lot::Mutex; use regex::bytes::{Regex, RegexBuilder}; +use std::sync::{Mutex, PoisonError}; + +use crate::{RelayBuf, RelayStr}; + +/// Controls the globbing behaviors. +#[repr(u32)] +pub enum GlobFlags { + /// When enabled `**` matches over path separators and `*` does not. + DoubleStar = 1, + /// Enables case insensitive path matching. + CaseInsensitive = 2, + /// Enables path normalization. + PathNormalize = 4, + /// Allows newlines. + AllowNewline = 8, +} + +/// Performs a glob operation on bytes. +/// +/// Returns `true` if the glob matches, `false` otherwise. +#[no_mangle] +#[relay_ffi::catch_unwind] +pub unsafe extern "C" fn relay_is_glob_match( + value: *const RelayBuf, + pat: *const RelayStr, + flags: GlobFlags, +) -> bool { + let mut options = GlobOptions::default(); + let flags = flags as u32; + if (flags & GlobFlags::DoubleStar as u32) != 0 { + options.double_star = true; + } + if (flags & GlobFlags::CaseInsensitive as u32) != 0 { + options.case_insensitive = true; + } + if (flags & GlobFlags::PathNormalize as u32) != 0 { + options.path_normalize = true; + } + if (flags & GlobFlags::AllowNewline as u32) != 0 { + options.allow_newline = true; + } + glob_match_bytes((*value).as_bytes(), (*pat).as_str(), options) +} /// LRU cache for [`Regex`]s in relation to [`GlobOptions`] and the provided string pattern. static GLOB_CACHE: Lazy>> = @@ -15,7 +57,7 @@ static GLOB_CACHE: Lazy>> = /// Controls the options of the globber. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] -pub struct GlobOptions { +struct GlobOptions { /// When enabled `**` matches over path separators and `*` does not. pub double_star: bool, /// Enables case insensitive path matching. @@ -40,7 +82,7 @@ fn translate_pattern(pat: &str, options: GlobOptions) -> Option { /// Performs a glob operation on bytes. /// /// Returns `true` if the glob matches, `false` otherwise. -pub fn glob_match_bytes(value: &[u8], pat: &str, options: GlobOptions) -> bool { +fn glob_match_bytes(value: &[u8], pat: &str, options: GlobOptions) -> bool { let (value, pat) = if options.path_normalize { ( Cow::Owned( @@ -55,7 +97,7 @@ pub fn glob_match_bytes(value: &[u8], pat: &str, options: GlobOptions) -> bool { (Cow::Borrowed(value), pat.to_string()) }; let key = (options, pat); - let mut cache = GLOB_CACHE.lock(); + let mut cache = GLOB_CACHE.lock().unwrap_or_else(PoisonError::into_inner); if let Some(pattern) = cache.get(&key) { pattern.is_match(&value) @@ -68,20 +110,14 @@ pub fn glob_match_bytes(value: &[u8], pat: &str, options: GlobOptions) -> bool { } } -/// Performs a glob operation. -/// -/// Returns `true` if the glob matches. -/// -/// Note that even though this accepts strings, the case insensitivity here is only -/// applied on ASCII characters as the underlying globber matches on bytes exclusively. -pub fn glob_match(value: &str, pat: &str, options: GlobOptions) -> bool { - glob_match_bytes(value.as_bytes(), pat, options) -} - #[cfg(test)] mod tests { use super::*; + fn glob_match(value: &str, pat: &str, options: GlobOptions) -> bool { + glob_match_bytes(value.as_bytes(), pat, options) + } + #[test] fn test_globs() { macro_rules! test_glob { diff --git a/relay-cabi/src/lib.rs b/relay-cabi/src/lib.rs index 57784d83a2..95950a75eb 100644 --- a/relay-cabi/src/lib.rs +++ b/relay-cabi/src/lib.rs @@ -102,6 +102,7 @@ mod codeowners; mod constants; mod core; mod ffi; +mod glob; mod processing; pub use crate::auth::*; @@ -109,4 +110,5 @@ pub use crate::codeowners::*; pub use crate::constants::*; pub use crate::core::*; pub use crate::ffi::*; +pub use crate::glob::*; pub use crate::processing::*; diff --git a/relay-cabi/src/processing.rs b/relay-cabi/src/processing.rs index 4c9761e0bd..a5308872fa 100644 --- a/relay-cabi/src/processing.rs +++ b/relay-cabi/src/processing.rs @@ -11,7 +11,6 @@ use std::sync::OnceLock; use chrono::{DateTime, Utc}; use relay_cardinality::CardinalityLimit; -use relay_common::glob::{glob_match_bytes, GlobOptions}; use relay_dynamic_config::{normalize_json, GlobalConfig, ProjectConfig}; use relay_event_normalization::{ normalize_event, validate_event, BreakdownsConfig, ClientHints, EventValidationConfig, @@ -28,7 +27,7 @@ use relay_sampling::SamplingConfig; use serde::{Deserialize, Serialize}; use uuid::Uuid; -use crate::core::{RelayBuf, RelayStr}; +use crate::core::RelayStr; /// Configuration for the store step -- validation and normalization. #[derive(Serialize, Deserialize, Debug, Default)] @@ -369,46 +368,6 @@ pub unsafe extern "C" fn relay_test_panic() -> () { panic!("this is a test panic") } -/// Controls the globbing behaviors. -#[repr(u32)] -pub enum GlobFlags { - /// When enabled `**` matches over path separators and `*` does not. - DoubleStar = 1, - /// Enables case insensitive path matching. - CaseInsensitive = 2, - /// Enables path normalization. - PathNormalize = 4, - /// Allows newlines. - AllowNewline = 8, -} - -/// Performs a glob operation on bytes. -/// -/// Returns `true` if the glob matches, `false` otherwise. -#[no_mangle] -#[relay_ffi::catch_unwind] -pub unsafe extern "C" fn relay_is_glob_match( - value: *const RelayBuf, - pat: *const RelayStr, - flags: GlobFlags, -) -> bool { - let mut options = GlobOptions::default(); - let flags = flags as u32; - if (flags & GlobFlags::DoubleStar as u32) != 0 { - options.double_star = true; - } - if (flags & GlobFlags::CaseInsensitive as u32) != 0 { - options.case_insensitive = true; - } - if (flags & GlobFlags::PathNormalize as u32) != 0 { - options.path_normalize = true; - } - if (flags & GlobFlags::AllowNewline as u32) != 0 { - options.allow_newline = true; - } - glob_match_bytes((*value).as_bytes(), (*pat).as_str(), options) -} - /// Parse a sentry release structure from a string. #[no_mangle] #[relay_ffi::catch_unwind] diff --git a/relay-common/Cargo.toml b/relay-common/Cargo.toml index 3584b4ca96..4e073eedc1 100644 --- a/relay-common/Cargo.toml +++ b/relay-common/Cargo.toml @@ -15,9 +15,7 @@ workspace = true [dependencies] chrono = { workspace = true } globset = { workspace = true } -lru = { workspace = true } once_cell = { workspace = true } -parking_lot = { workspace = true } regex = { workspace = true } sentry-types = { workspace = true } serde = { workspace = true } diff --git a/relay-common/src/lib.rs b/relay-common/src/lib.rs index b9cdc7386f..b28401064e 100644 --- a/relay-common/src/lib.rs +++ b/relay-common/src/lib.rs @@ -8,7 +8,6 @@ mod macros; -pub mod glob; pub mod glob2; pub mod glob3; pub mod time;