mirror of
https://github.com/systemd/systemd.git
synced 2025-03-22 06:50:18 +03:00
device-util: introduce device_in_subsystems()
This commit is contained in:
parent
cf0f4527b3
commit
2b18708ee6
src/libsystemd/sd-device
@ -131,13 +131,14 @@ char** device_make_log_fields(sd_device *device) {
|
||||
return TAKE_PTR(strv);
|
||||
}
|
||||
|
||||
bool device_in_subsystem(sd_device *device, const char *subsystem) {
|
||||
const char *s = NULL;
|
||||
|
||||
bool device_in_subsystems(sd_device *device, char * const *subsystems) {
|
||||
assert(device);
|
||||
|
||||
(void) sd_device_get_subsystem(device, &s);
|
||||
return streq_ptr(s, subsystem);
|
||||
const char *s;
|
||||
if (sd_device_get_subsystem(device, &s) < 0)
|
||||
return strv_isempty(subsystems);
|
||||
|
||||
return strv_contains(subsystems, s);
|
||||
}
|
||||
|
||||
bool device_is_devtype(sd_device *device, const char *devtype) {
|
||||
|
@ -104,7 +104,10 @@ int device_open_from_devnum(mode_t mode, dev_t devnum, int flags, char **ret_dev
|
||||
|
||||
char** device_make_log_fields(sd_device *device);
|
||||
|
||||
bool device_in_subsystem(sd_device *device, const char *subsystem);
|
||||
bool device_in_subsystems(sd_device *device, char * const *subsystems);
|
||||
static inline bool device_in_subsystem(sd_device *device, const char *subsystem) {
|
||||
return device_in_subsystems(device, STRV_MAKE(subsystem));
|
||||
}
|
||||
bool device_is_devtype(sd_device *device, const char *devtype);
|
||||
|
||||
static inline bool device_property_can_set(const char *property) {
|
||||
|
@ -28,6 +28,15 @@ TEST(device_in_subsystem) {
|
||||
ASSERT_FALSE(device_in_subsystem(dev, ""));
|
||||
ASSERT_FALSE(device_in_subsystem(dev, NULL));
|
||||
|
||||
ASSERT_TRUE(device_in_subsystems(dev, STRV_MAKE("net")));
|
||||
ASSERT_TRUE(device_in_subsystems(dev, STRV_MAKE("", "net")));
|
||||
ASSERT_TRUE(device_in_subsystems(dev, STRV_MAKE("net", "disk")));
|
||||
ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("disk", "subsystem")));
|
||||
ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("disk", "")));
|
||||
ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("")));
|
||||
ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE(NULL)));
|
||||
ASSERT_FALSE(device_in_subsystems(dev, NULL));
|
||||
|
||||
dev = sd_device_unref(dev);
|
||||
}
|
||||
|
||||
@ -38,6 +47,15 @@ TEST(device_in_subsystem) {
|
||||
ASSERT_FALSE(device_in_subsystem(dev, ""));
|
||||
ASSERT_FALSE(device_in_subsystem(dev, NULL));
|
||||
|
||||
ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("net")));
|
||||
ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("", "net")));
|
||||
ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("net", "disk")));
|
||||
ASSERT_TRUE(device_in_subsystems(dev, STRV_MAKE("disk", "subsystem")));
|
||||
ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("disk", "")));
|
||||
ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("")));
|
||||
ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE(NULL)));
|
||||
ASSERT_FALSE(device_in_subsystems(dev, NULL));
|
||||
|
||||
dev = sd_device_unref(dev);
|
||||
|
||||
ASSERT_OK(sd_device_new_from_syspath(&dev, "/sys/class"));
|
||||
@ -46,6 +64,15 @@ TEST(device_in_subsystem) {
|
||||
ASSERT_FALSE(device_in_subsystem(dev, "subsystem"));
|
||||
ASSERT_FALSE(device_in_subsystem(dev, ""));
|
||||
ASSERT_TRUE(device_in_subsystem(dev, NULL));
|
||||
|
||||
ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("net")));
|
||||
ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("", "net")));
|
||||
ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("net", "disk")));
|
||||
ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("disk", "subsystem")));
|
||||
ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("disk", "")));
|
||||
ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("")));
|
||||
ASSERT_TRUE(device_in_subsystems(dev, STRV_MAKE(NULL)));
|
||||
ASSERT_TRUE(device_in_subsystems(dev, NULL));
|
||||
}
|
||||
|
||||
TEST(device_is_devtype) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user