]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-device: also add device_get_sysattr_streq()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 12 Mar 2026 00:53:13 +0000 (09:53 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 17 May 2026 17:55:56 +0000 (02:55 +0900)
src/libsystemd/sd-device/device-private.h
src/libsystemd/sd-device/sd-device.c
src/libsystemd/sd-device/test-sd-device.c

index 33a2c7ebb6e03ac373172d0ca25e38cdebe0eaa2..0773ec909861d026e58e3a36159fb979dab4149f 100644 (file)
@@ -16,6 +16,7 @@ int device_get_property_bool(sd_device *device, const char *key);
 int device_get_property_int(sd_device *device, const char *key, int *ret);
 int device_get_property_uint(sd_device *device, const char *key, unsigned *ret);
 int device_get_ifname(sd_device *device, const char **ret);
+int device_get_sysattr_streq(sd_device *device, const char *sysattr, const char *expected);
 int device_get_sysattr_safe_string(sd_device *device, const char *sysattr, const char **ret);
 int device_get_sysattr_int(sd_device *device, const char *sysattr, int *ret);
 int device_get_sysattr_unsigned_full(sd_device *device, const char *sysattr, unsigned base, unsigned *ret);
index f157125c50bebecbdfeaf307a44ef639e505ad57..07c3dd60d2f8b72a562570b0270a64d28aa9ad35 100644 (file)
@@ -2619,6 +2619,19 @@ _public_ int sd_device_get_sysattr_value(sd_device *device, const char *sysattr,
         return sd_device_get_sysattr_value_with_size(device, sysattr, ret, NULL);
 }
 
+int device_get_sysattr_streq(sd_device *device, const char *sysattr, const char *expected) {
+        const char *value;
+        int r;
+
+        assert(expected);
+
+        r = sd_device_get_sysattr_value(device, sysattr, &value);
+        if (r < 0)
+                return r;
+
+        return streq(value, expected);
+}
+
 int device_get_sysattr_safe_string(sd_device *device, const char *sysattr, const char **ret) {
         const char *value;
         int r;
index efeae7a7155d0600d489d44d03df0dd07ea67e68..c53f0b0e88d6e7eba5e834c2ed3cd8ee95771ad5 100644 (file)
@@ -341,6 +341,11 @@ static void test_sd_device_one(sd_device *d) {
                         ASSERT_OK_POSITIVE(device_get_sysattr_u8(d, "ifindex", &u8));
                         ASSERT_EQ(u8, (uint8_t) ifindex);
                 }
+
+                const char *s;
+                ASSERT_OK(sd_device_get_sysattr_value(d, "ifindex", &s));
+                ASSERT_OK_POSITIVE(device_get_sysattr_streq(d, "ifindex", s));
+                ASSERT_OK_ZERO(device_get_sysattr_streq(d, "ifindex", "hoge"));
         }
 }