kerneldevice,generic: use regex to implement string matching
Instead of the custom simple implementation which only supported the '*' modifier in the pattern. This allows us to support e.g. attribute value matches like e.g. 'DATA[0-9]*_CNTL'.
This commit is contained in:
@@ -666,39 +666,30 @@ kernel_device_cmp (MMKernelDevice *a,
|
|||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
string_match (const gchar *str,
|
string_match (MMKernelDeviceGeneric *self,
|
||||||
const gchar *original_pattern)
|
const gchar *str,
|
||||||
|
const gchar *pattern)
|
||||||
{
|
{
|
||||||
gchar *pattern;
|
g_autoptr(GError) inner_error = NULL;
|
||||||
gchar *start;
|
g_autoptr(GRegex) regex = NULL;
|
||||||
gboolean open_prefix = FALSE;
|
g_autoptr(GMatchInfo) match_info = NULL;
|
||||||
gboolean open_suffix = FALSE;
|
|
||||||
gboolean match;
|
|
||||||
|
|
||||||
pattern = g_strdup (original_pattern);
|
regex = g_regex_new (pattern, 0, 0, &inner_error);
|
||||||
start = pattern;
|
if (!regex) {
|
||||||
|
mm_obj_warn (self, "invalid pattern in rule '%s': %s", pattern, inner_error->message);
|
||||||
if (start[0] == '*') {
|
return FALSE;
|
||||||
open_prefix = TRUE;
|
}
|
||||||
start++;
|
g_regex_match_full (regex, str, -1, 0, 0, &match_info, &inner_error);
|
||||||
|
if (inner_error) {
|
||||||
|
mm_obj_warn (self, "couldn't apply pattern match in rule '%s': %s", pattern, inner_error->message);
|
||||||
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (start[strlen (start) - 1] == '*') {
|
if (!g_match_info_matches (match_info))
|
||||||
open_suffix = TRUE;
|
return FALSE;
|
||||||
start[strlen (start) - 1] = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (open_suffix && !open_prefix)
|
mm_obj_dbg (self, "pattern '%s' matched: '%s'", pattern, str);
|
||||||
match = g_str_has_prefix (str, start);
|
return TRUE;
|
||||||
else if (!open_suffix && open_prefix)
|
|
||||||
match = g_str_has_suffix (str, start);
|
|
||||||
else if (open_suffix && open_prefix)
|
|
||||||
match = !!strstr (str, start);
|
|
||||||
else
|
|
||||||
match = g_str_equal (str, start);
|
|
||||||
|
|
||||||
g_free (pattern);
|
|
||||||
return match;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
@@ -731,7 +722,7 @@ check_condition (MMKernelDeviceGeneric *self,
|
|||||||
|
|
||||||
/* Device name checks */
|
/* Device name checks */
|
||||||
if (g_str_equal (match->parameter, "KERNEL"))
|
if (g_str_equal (match->parameter, "KERNEL"))
|
||||||
return (string_match (mm_kernel_device_get_name (MM_KERNEL_DEVICE (self)), match->value) == condition_equal);
|
return (string_match (self, mm_kernel_device_get_name (MM_KERNEL_DEVICE (self)), match->value) == condition_equal);
|
||||||
|
|
||||||
/* Device sysfs path checks; we allow both a direct match and a prefix patch */
|
/* Device sysfs path checks; we allow both a direct match and a prefix patch */
|
||||||
if (g_str_equal (match->parameter, "DEVPATH")) {
|
if (g_str_equal (match->parameter, "DEVPATH")) {
|
||||||
@@ -748,22 +739,22 @@ check_condition (MMKernelDeviceGeneric *self,
|
|||||||
if (match->value[0] && match->value[strlen (match->value) - 1] != '*')
|
if (match->value[0] && match->value[strlen (match->value) - 1] != '*')
|
||||||
prefix_match = g_strdup_printf ("%s/*", match->value);
|
prefix_match = g_strdup_printf ("%s/*", match->value);
|
||||||
|
|
||||||
if (string_match (self->priv->sysfs_path, match->value) == condition_equal) {
|
if (string_match (self, self->priv->sysfs_path, match->value) == condition_equal) {
|
||||||
result = TRUE;
|
result = TRUE;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prefix_match && string_match (self->priv->sysfs_path, prefix_match) == condition_equal) {
|
if (prefix_match && string_match (self, self->priv->sysfs_path, prefix_match) == condition_equal) {
|
||||||
result = TRUE;
|
result = TRUE;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (g_str_has_prefix (self->priv->sysfs_path, "/sys")) {
|
if (g_str_has_prefix (self->priv->sysfs_path, "/sys")) {
|
||||||
if (string_match (&self->priv->sysfs_path[4], match->value) == condition_equal) {
|
if (string_match (self, &self->priv->sysfs_path[4], match->value) == condition_equal) {
|
||||||
result = TRUE;
|
result = TRUE;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
if (prefix_match && string_match (&self->priv->sysfs_path[4], prefix_match) == condition_equal) {
|
if (prefix_match && string_match (self, &self->priv->sysfs_path[4], prefix_match) == condition_equal) {
|
||||||
result = TRUE;
|
result = TRUE;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user