From cc97a3a5e826a2281953b001436f9168bcdd22f6 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 4 Apr 2022 12:55:12 +0200 Subject: gpt: introduce common implementation of type uuid search loop --- src/shared/gpt.c | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) (limited to 'src/shared') diff --git a/src/shared/gpt.c b/src/shared/gpt.c index cd57447b8a..88b32ac102 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 }; } -- cgit v1.2.1