From: Lennart Poettering Date: Mon, 14 Feb 2022 13:54:24 +0000 (+0100) Subject: id128-util: add new helper id128_equal_string() X-Git-Tag: v251-rc1~295^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=75a505c6006f9ddf536b426941ace1f87b263399;p=thirdparty%2Fsystemd.git id128-util: add new helper id128_equal_string() 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. --- diff --git a/src/libsystemd/sd-id128/id128-util.c b/src/libsystemd/sd-id128/id128-util.c index 1068721dd47..e4a3bbd3ebe 100644 --- a/src/libsystemd/sd-id128/id128-util.c +++ b/src/libsystemd/sd-id128/id128-util.c @@ -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); +} diff --git a/src/libsystemd/sd-id128/id128-util.h b/src/libsystemd/sd-id128/id128-util.h index 17b180c10c1..65a278c8ee0 100644 --- a/src/libsystemd/sd-id128/id128-util.h +++ b/src/libsystemd/sd-id128/id128-util.h @@ -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);