1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-10-27 01:55:32 +03:00

sd-device-enumerator: move match_sysattr() to device-util.[ch]

It will be used by sd-device-monitor in later commits.
This commit is contained in:
Yu Watanabe 2021-02-21 09:32:39 +09:00
parent 112e6dd106
commit ac790e8bfc
4 changed files with 52 additions and 39 deletions

View File

@ -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

View File

@ -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);

View File

@ -0,0 +1,40 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <fnmatch.h>
#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;
}

View File

@ -1,8 +1,14 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include <stdbool.h>
#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);