diff --git a/src/libsystemd/sd-device/device-enumerator.c b/src/libsystemd/sd-device/device-enumerator.c index 07c0d550650..a290dbcc9ce 100644 --- a/src/libsystemd/sd-device/device-enumerator.c +++ b/src/libsystemd/sd-device/device-enumerator.c @@ -331,24 +331,6 @@ static bool match_tag(sd_device_enumerator *enumerator, sd_device *device) { return true; } -static bool match_parent(sd_device_enumerator *enumerator, sd_device *device) { - const char *syspath_parent, *syspath; - - assert(enumerator); - assert(device); - - if (set_isempty(enumerator->match_parent)) - return true; - - assert_se(sd_device_get_syspath(device, &syspath) >= 0); - - SET_FOREACH(syspath_parent, enumerator->match_parent) - if (path_startswith(syspath, syspath_parent)) - return true; - - return false; -} - static bool match_sysname(sd_device_enumerator *enumerator, const char *sysname) { const char *sysname_match; @@ -434,7 +416,7 @@ static int enumerator_scan_dir_and_add_devices(sd_device_enumerator *enumerator, sd_device_get_ifindex(device, NULL) >= 0)) continue; - if (!match_parent(enumerator, device)) + if (!device_match_parent(device, enumerator->match_parent, NULL)) continue; if (!match_tag(enumerator, device)) @@ -564,7 +546,7 @@ static int enumerator_scan_devices_tag(sd_device_enumerator *enumerator, const c if (!match_sysname(enumerator, sysname)) continue; - if (!match_parent(enumerator, device)) + if (!device_match_parent(device, enumerator->match_parent, NULL)) continue; if (!match_property(enumerator, device)) diff --git a/src/libsystemd/sd-device/device-util.c b/src/libsystemd/sd-device/device-util.c index fe495aecf71..616c16c1fc4 100644 --- a/src/libsystemd/sd-device/device-util.c +++ b/src/libsystemd/sd-device/device-util.c @@ -3,6 +3,7 @@ #include #include "device-util.h" +#include "path-util.h" static bool device_match_sysattr_value(sd_device *device, const char *sysattr, const char *match_value) { const char *value; @@ -38,3 +39,25 @@ bool device_match_sysattr(sd_device *device, Hashmap *match_sysattr, Hashmap *no return true; } + +bool device_match_parent(sd_device *device, Set *match_parent, Set *nomatch_parent) { + const char *syspath_parent, *syspath; + + assert(device); + + if (sd_device_get_syspath(device, &syspath) < 0) + return false; + + SET_FOREACH(syspath_parent, nomatch_parent) + if (path_startswith(syspath, syspath_parent)) + return false; + + if (set_isempty(match_parent)) + return true; + + SET_FOREACH(syspath_parent, match_parent) + if (path_startswith(syspath, syspath_parent)) + return true; + + return false; +} diff --git a/src/libsystemd/sd-device/device-util.h b/src/libsystemd/sd-device/device-util.h index 4dfd8c1dd69..026bdcd644c 100644 --- a/src/libsystemd/sd-device/device-util.h +++ b/src/libsystemd/sd-device/device-util.h @@ -8,6 +8,7 @@ #include "hashmap.h" #include "log.h" #include "macro.h" +#include "set.h" #define FOREACH_DEVICE_PROPERTY(device, key, value) \ for (key = sd_device_get_property_first(device, &(value)); \ @@ -72,3 +73,4 @@ #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); +bool device_match_parent(sd_device *device, Set *match_parent, Set *nomatch_parent);