From: Lennart Poettering Date: Mon, 4 Apr 2022 10:55:12 +0000 (+0200) Subject: gpt: introduce common implementation of type uuid search loop X-Git-Tag: v251-rc2~213 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cc97a3a5e826a2281953b001436f9168bcdd22f6;p=thirdparty%2Fsystemd.git gpt: introduce common implementation of type uuid search loop --- diff --git a/src/shared/gpt.c b/src/shared/gpt.c index cd57447b8af..88b32ac1024 100644 --- a/src/shared/gpt.c +++ b/src/shared/gpt.c @@ -65,14 +65,25 @@ const GptPartitionType gpt_partition_type_table[] = { {} }; -const char *gpt_partition_type_uuid_to_string(sd_id128_t id) { +static const GptPartitionType *gpt_partition_type_find_by_uuid(sd_id128_t id) { + for (size_t i = 0; i < ELEMENTSOF(gpt_partition_type_table) - 1; i++) if (sd_id128_equal(id, gpt_partition_type_table[i].uuid)) - return gpt_partition_type_table[i].name; + return gpt_partition_type_table + i; return NULL; } +const char *gpt_partition_type_uuid_to_string(sd_id128_t id) { + const GptPartitionType *pt; + + pt = gpt_partition_type_find_by_uuid(id); + if (!pt) + return NULL; + + return pt->name; +} + const char *gpt_partition_type_uuid_to_string_harder( sd_id128_t id, char buffer[static SD_ID128_UUID_STRING_MAX]) { @@ -102,11 +113,13 @@ int gpt_partition_type_uuid_from_string(const char *s, sd_id128_t *ret) { } Architecture gpt_partition_type_uuid_to_arch(sd_id128_t id) { - for (size_t i = 0; i < ELEMENTSOF(gpt_partition_type_table) - 1; i++) - if (sd_id128_equal(id, gpt_partition_type_table[i].uuid)) - return gpt_partition_type_table[i].arch; + const GptPartitionType *pt; + + pt = gpt_partition_type_find_by_uuid(id); + if (!pt) + return _ARCHITECTURE_INVALID; - return _ARCHITECTURE_INVALID; + return pt->arch; } int gpt_partition_label_valid(const char *s) { @@ -120,9 +133,11 @@ int gpt_partition_label_valid(const char *s) { } static GptPartitionType gpt_partition_type_from_uuid(sd_id128_t id) { - for (size_t i = 0; i < ELEMENTSOF(gpt_partition_type_table) - 1; i++) - if (sd_id128_equal(id, gpt_partition_type_table[i].uuid)) - return gpt_partition_type_table[i]; + const GptPartitionType *pt; + + pt = gpt_partition_type_find_by_uuid(id); + if (pt) + return *pt; return (GptPartitionType) { .uuid = id, .arch = _ARCHITECTURE_INVALID }; }