From: krw Date: Mon, 14 Mar 2022 17:11:44 +0000 (+0000) Subject: Abstract duplicated code scanning gpt_types[] into a helper X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=f2ec1a1396ab80e38332c311eb55fa4fa7267f24;p=openbsd Abstract duplicated code scanning gpt_types[] into a helper function find_gpt_type(). Use find_gpt_type() to simplify the functions obtaining information from gpt_types[]. Add not yet used PRT_uuid_to_protection() to allow simplification of GPT partition protection code.. No intentional functional change. --- diff --git a/sbin/fdisk/part.c b/sbin/fdisk/part.c index 8ed801fa484..65bad6b40e6 100644 --- a/sbin/fdisk/part.c +++ b/sbin/fdisk/part.c @@ -1,4 +1,4 @@ -/* $OpenBSD: part.c,v 1.119 2022/03/14 14:28:58 krw Exp $ */ +/* $OpenBSD: part.c,v 1.120 2022/03/14 17:11:44 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -180,6 +180,8 @@ const struct gpt_type gpt_types[] = { { 0xEF, 0, "EFI Sys ", "c12a7328-f81f-11d2-ba4b-00a0c93ec93b" }, }; +const struct gpt_type *find_gpt_type(const struct uuid *); + int PRT_protected_guid(const struct uuid *uuid) { @@ -419,62 +421,75 @@ PRT_lba_to_chs(const struct prt *prt, struct chs *start, struct chs *end) return 0; } -const char * -PRT_uuid_to_typename(const struct uuid *uuid) +const struct gpt_type * +find_gpt_type(const struct uuid *uuid) { - static char partition_type[UUID_STR_LEN + 1]; char *uuidstr = NULL; - int i, entries, status; - - memset(partition_type, 0, sizeof(partition_type)); + unsigned int i; + uint32_t status; uuid_to_string(uuid, &uuidstr, &status); - if (status != uuid_s_ok) - goto done; - - entries = nitems(gpt_types); - - for (i = 0; i < entries; i++) { - if (memcmp(gpt_types[i].gt_guid, uuidstr, - sizeof(gpt_types[i].gt_guid)) == 0) - break; - } + if (status == uuid_s_ok) { + for (i = 0; i < nitems(gpt_types); i++) { + if (memcmp(gpt_types[i].gt_guid, uuidstr, + sizeof(gpt_types[i].gt_guid)) == 0) + break; + } + } else + i = nitems(gpt_types); + free(uuidstr); - if (i < entries) - strlcpy(partition_type, gpt_types[i].gt_sname, - sizeof(partition_type)); + if (i < nitems(gpt_types)) + return &gpt_types[i]; else - strlcpy(partition_type, uuidstr, sizeof(partition_type)); + return NULL; +} -done: - free(uuidstr); +int +PRT_uuid_to_protection(const struct uuid *uuid) +{ + const struct gpt_type *gt; - return partition_type; + gt = find_gpt_type(uuid); + if (gt == NULL) + return 0; + else + return gt->gt_protected; } -int -PRT_uuid_to_type(const struct uuid *uuid) +const char * +PRT_uuid_to_typename(const struct uuid *uuid) { + static char typename[UUID_STR_LEN + 1]; + const struct gpt_type *gt; char *uuidstr; - int i, status, type; + int status; - type = 0; + memset(typename, 0, sizeof(typename)); - uuid_to_string(uuid, &uuidstr, &status); - if (status != uuid_s_ok) - goto done; - - for (i = 0; i < nitems(gpt_types); i++) { - if (memcmp(gpt_types[i].gt_guid, uuidstr, - sizeof(gpt_types[i].gt_guid)) == 0) { - type = gpt_types[i].gt_type; - break; - } + gt = find_gpt_type(uuid); + if (gt == NULL) { + uuid_to_string(uuid, &uuidstr, &status); + if (status == uuid_s_ok) + strlcpy(typename, uuidstr, sizeof(typename)); + free(uuidstr); + } else { + strlcpy(typename, gt->gt_sname, sizeof(typename)); } -done: - free(uuidstr); - return type; + return typename; +} + +int +PRT_uuid_to_type(const struct uuid *uuid) +{ + const struct gpt_type *gt; + + gt = find_gpt_type(uuid); + if (gt == NULL) + return 0; + else + return gt->gt_type; } struct uuid * diff --git a/sbin/fdisk/part.h b/sbin/fdisk/part.h index 883ab3340ae..3f4a6dc557a 100644 --- a/sbin/fdisk/part.h +++ b/sbin/fdisk/part.h @@ -1,4 +1,4 @@ -/* $OpenBSD: part.h,v 1.35 2022/03/14 14:31:23 krw Exp $ */ +/* $OpenBSD: part.h,v 1.36 2022/03/14 17:11:44 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -37,6 +37,7 @@ void PRT_make(const struct prt *,const uint64_t, const uint64_t, struct dos_partition *); void PRT_print_part(const int, const struct prt *, const char *); void PRT_print_parthdr(void); +int PRT_uuid_to_protection(const struct uuid *); const char *PRT_uuid_to_typename(const struct uuid *); int PRT_uuid_to_type(const struct uuid *); struct uuid *PRT_type_to_uuid(const int);