]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
id128-util: add new helper id128_equal_string()
authorLennart Poettering <lennart@poettering.net>
Mon, 14 Feb 2022 13:54:24 +0000 (14:54 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 14 Feb 2022 14:14:03 +0000 (15:14 +0100)
Quite often we compare uuids/id128 formatted as strings with specific
values. So far we usually used streq() for that. let's add a new
explicit helper for this in id128_equal_string() that compares a string
with an sd_id128_t and is more robust than a simple string comparison.
Moreover, we can mroe easily reuse the various defines we have for
specific UUIDs, for example those from gpt.h.

src/libsystemd/sd-id128/id128-util.c
src/libsystemd/sd-id128/id128-util.h

index 1068721dd47bac027cf466f5389f1b2a1c30d676..e4a3bbd3ebe5b73085ea3c34b1f91da058a309da 100644 (file)
@@ -206,3 +206,19 @@ int id128_get_product(sd_id128_t *ret) {
         *ret = uuid;
         return 0;
 }
+
+int id128_equal_string(const char *s, sd_id128_t id) {
+        sd_id128_t parsed;
+        int r;
+
+        if (!s)
+                return false;
+
+        /* Checks if the specified string matches a valid string representation of the specified 128 bit ID/uuid */
+
+        r = sd_id128_from_string(s, &parsed);
+        if (r < 0)
+                return r;
+
+        return sd_id128_equal(parsed, id);
+}
index 17b180c10c14860c35dc477c51eea15a54b209ee..65a278c8ee02ffbb6f419e5a686ae253564f5573 100644 (file)
@@ -34,3 +34,5 @@ extern const struct hash_ops id128_hash_ops;
 sd_id128_t id128_make_v4_uuid(sd_id128_t id);
 
 int id128_get_product(sd_id128_t *ret);
+
+int id128_equal_string(const char *s, sd_id128_t id);