From ac790e8bfc953f6157043bb7fcc35e0833e068f4 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 21 Feb 2021 09:32:39 +0900 Subject: [PATCH] sd-device-enumerator: move match_sysattr() to device-util.[ch] It will be used by sd-device-monitor in later commits. --- src/libsystemd/meson.build | 1 + src/libsystemd/sd-device/device-enumerator.c | 42 ++------------------ src/libsystemd/sd-device/device-util.c | 40 +++++++++++++++++++ src/libsystemd/sd-device/device-util.h | 8 ++++ 4 files changed, 52 insertions(+), 39 deletions(-) create mode 100644 src/libsystemd/sd-device/device-util.c diff --git a/src/libsystemd/meson.build b/src/libsystemd/meson.build index 3def5655cc..f55bdcd1a5 100644 --- a/src/libsystemd/meson.build +++ b/src/libsystemd/meson.build @@ -127,6 +127,7 @@ libsystemd_sources = files(''' sd-device/device-monitor.c sd-device/device-private.c sd-device/device-private.h + sd-device/device-util.c sd-device/device-util.h sd-device/sd-device.c sd-hwdb/hwdb-internal.h diff --git a/src/libsystemd/sd-device/device-enumerator.c b/src/libsystemd/sd-device/device-enumerator.c index 2434fb625e..07c0d55065 100644 --- a/src/libsystemd/sd-device/device-enumerator.c +++ b/src/libsystemd/sd-device/device-enumerator.c @@ -287,42 +287,6 @@ int device_enumerator_add_device(sd_device_enumerator *enumerator, sd_device *de return 0; } -static bool match_sysattr_value(sd_device *device, const char *sysattr, const char *match_value) { - const char *value; - - assert(device); - assert(sysattr); - - if (sd_device_get_sysattr_value(device, sysattr, &value) < 0) - return false; - - if (!match_value) - return true; - - if (fnmatch(match_value, value, 0) == 0) - return true; - - return false; -} - -static bool match_sysattr(sd_device_enumerator *enumerator, sd_device *device) { - const char *sysattr; - const char *value; - - assert(enumerator); - assert(device); - - HASHMAP_FOREACH_KEY(value, sysattr, enumerator->nomatch_sysattr) - if (match_sysattr_value(device, sysattr, value)) - return false; - - HASHMAP_FOREACH_KEY(value, sysattr, enumerator->match_sysattr) - if (!match_sysattr_value(device, sysattr, value)) - return false; - - return true; -} - static bool match_property(sd_device_enumerator *enumerator, sd_device *device) { const char *property; const char *value; @@ -479,7 +443,7 @@ static int enumerator_scan_dir_and_add_devices(sd_device_enumerator *enumerator, if (!match_property(enumerator, device)) continue; - if (!match_sysattr(enumerator, device)) + if (!device_match_sysattr(device, enumerator->match_sysattr, enumerator->nomatch_sysattr)) continue; k = device_enumerator_add_device(enumerator, device); @@ -606,7 +570,7 @@ static int enumerator_scan_devices_tag(sd_device_enumerator *enumerator, const c if (!match_property(enumerator, device)) continue; - if (!match_sysattr(enumerator, device)) + if (!device_match_sysattr(device, enumerator->match_sysattr, enumerator->nomatch_sysattr)) continue; k = device_enumerator_add_device(enumerator, device); @@ -667,7 +631,7 @@ static int parent_add_child(sd_device_enumerator *enumerator, const char *path) if (!match_property(enumerator, device)) return 0; - if (!match_sysattr(enumerator, device)) + if (!device_match_sysattr(device, enumerator->match_sysattr, enumerator->nomatch_sysattr)) return 0; r = device_enumerator_add_device(enumerator, device); diff --git a/src/libsystemd/sd-device/device-util.c b/src/libsystemd/sd-device/device-util.c new file mode 100644 index 0000000000..fe495aecf7 --- /dev/null +++ b/src/libsystemd/sd-device/device-util.c @@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include + +#include "device-util.h" + +static bool device_match_sysattr_value(sd_device *device, const char *sysattr, const char *match_value) { + const char *value; + + assert(device); + assert(sysattr); + + if (sd_device_get_sysattr_value(device, sysattr, &value) < 0) + return false; + + if (!match_value) + return true; + + if (fnmatch(match_value, value, 0) == 0) + return true; + + return false; +} + +bool device_match_sysattr(sd_device *device, Hashmap *match_sysattr, Hashmap *nomatch_sysattr) { + const char *sysattr; + const char *value; + + assert(device); + + HASHMAP_FOREACH_KEY(value, sysattr, match_sysattr) + if (!device_match_sysattr_value(device, sysattr, value)) + return false; + + HASHMAP_FOREACH_KEY(value, sysattr, nomatch_sysattr) + if (device_match_sysattr_value(device, sysattr, value)) + return false; + + return true; +} diff --git a/src/libsystemd/sd-device/device-util.h b/src/libsystemd/sd-device/device-util.h index 88c9d9881e..4dfd8c1dd6 100644 --- a/src/libsystemd/sd-device/device-util.h +++ b/src/libsystemd/sd-device/device-util.h @@ -1,8 +1,14 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include + #include "sd-device.h" +#include "hashmap.h" +#include "log.h" +#include "macro.h" + #define FOREACH_DEVICE_PROPERTY(device, key, value) \ for (key = sd_device_get_property_first(device, &(value)); \ key; \ @@ -64,3 +70,5 @@ #define log_device_notice_errno(device, error, ...) log_device_full_errno(device, LOG_NOTICE, error, __VA_ARGS__) #define log_device_warning_errno(device, error, ...) log_device_full_errno(device, LOG_WARNING, error, __VA_ARGS__) #define log_device_error_errno(device, error, ...) log_device_full_errno(device, LOG_ERR, error, __VA_ARGS__) + +bool device_match_sysattr(sd_device *device, Hashmap *match_sysattr, Hashmap *nomatch_sysattr);