kerneldevice,generic: move string match method to helpers

So that we can propertly unit-test it.
This commit is contained in:
Aleksander Morgado
2021-11-08 23:22:48 +01:00
parent e5f08b7766
commit 73b44ea51b
3 changed files with 43 additions and 6 deletions

View File

@@ -796,7 +796,7 @@ check_condition (MMKernelDeviceGeneric *self,
/* Device name checks */
if (g_str_equal (match->parameter, "KERNEL"))
return (string_match (self, mm_kernel_device_get_name (MM_KERNEL_DEVICE (self)), match->value) == condition_equal);
return (mm_kernel_device_generic_string_match (mm_kernel_device_get_name (MM_KERNEL_DEVICE (self)), match->value, self) == condition_equal);
/* Device sysfs path checks; we allow both a direct match and a prefix patch */
if (g_str_equal (match->parameter, "DEVPATH")) {
@@ -813,22 +813,22 @@ check_condition (MMKernelDeviceGeneric *self,
if (match->value[0] && match->value[strlen (match->value) - 1] != '*')
prefix_match = g_strdup_printf ("%s/*", match->value);
if (string_match (self, self->priv->sysfs_path, match->value) == condition_equal) {
if (mm_kernel_device_generic_string_match (self->priv->sysfs_path, match->value, self) == condition_equal) {
result = TRUE;
goto out;
}
if (prefix_match && string_match (self, self->priv->sysfs_path, prefix_match) == condition_equal) {
if (prefix_match && mm_kernel_device_generic_string_match (self->priv->sysfs_path, prefix_match, self) == condition_equal) {
result = TRUE;
goto out;
}
if (g_str_has_prefix (self->priv->sysfs_path, "/sys")) {
if (string_match (self, &self->priv->sysfs_path[4], match->value) == condition_equal) {
if (mm_kernel_device_generic_string_match (&self->priv->sysfs_path[4], match->value, self) == condition_equal) {
result = TRUE;
goto out;
}
if (prefix_match && string_match (self, &self->priv->sysfs_path[4], prefix_match) == condition_equal) {
if (prefix_match && mm_kernel_device_generic_string_match (&self->priv->sysfs_path[4], prefix_match, self) == condition_equal) {
result = TRUE;
goto out;
}

View File

@@ -19,8 +19,11 @@
#include <glib-object.h>
#include <gio/gio.h>
#include "mm-log-object.h"
#include "mm-kernel-device-helpers.h"
/******************************************************************************/
gchar *
mm_kernel_device_get_lower_device_name (const gchar *sysfs_path)
{
@@ -59,3 +62,32 @@ mm_kernel_device_get_lower_device_name (const gchar *sysfs_path)
return NULL;
}
/******************************************************************************/
gboolean
mm_kernel_device_generic_string_match (const gchar *str,
const gchar *pattern,
gpointer log_object)
{
g_autoptr(GError) inner_error = NULL;
g_autoptr(GRegex) regex = NULL;
g_autoptr(GMatchInfo) match_info = NULL;
regex = g_regex_new (pattern, 0, 0, &inner_error);
if (!regex) {
mm_obj_warn (log_object, "invalid pattern in rule '%s': %s", pattern, inner_error->message);
return FALSE;
}
g_regex_match_full (regex, str, -1, 0, 0, &match_info, &inner_error);
if (inner_error) {
mm_obj_warn (log_object, "couldn't apply pattern match in rule '%s': %s", pattern, inner_error->message);
return FALSE;
}
if (!g_match_info_matches (match_info))
return FALSE;
mm_obj_dbg (log_object, "pattern '%s' matched: '%s'", pattern, str);
return TRUE;
}

View File

@@ -23,4 +23,9 @@
* (e.g. lower_device_name(qmimux0) == wwan0) */
gchar *mm_kernel_device_get_lower_device_name (const gchar *sysfs_path);
#endif /* MM_KERNEL_DEVICE_HERLPERS_H */
/* Generic string matching logic */
gboolean mm_kernel_device_generic_string_match (const gchar *str,
const gchar *pattern,
gpointer log_object);
#endif /* MM_KERNEL_DEVICE_HELPERS_H */