From f3cf20a59257e8f7ed33bb6e6e3eaef4460bfa55 Mon Sep 17 00:00:00 2001 From: krw Date: Sun, 9 Apr 2023 17:19:59 +0000 Subject: [PATCH] Refactor and simplify GPT menu selection logic. No intentional functional change. --- sbin/fdisk/cmd.c | 42 ++++++++++++++---------------------------- sbin/fdisk/part.c | 45 ++++++++++++++++++++++----------------------- sbin/fdisk/part.h | 6 +++--- 3 files changed, 39 insertions(+), 54 deletions(-) diff --git a/sbin/fdisk/cmd.c b/sbin/fdisk/cmd.c index e6593dcf671..fdc7cdb24bc 100644 --- a/sbin/fdisk/cmd.c +++ b/sbin/fdisk/cmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd.c,v 1.174 2023/04/07 16:34:41 krw Exp $ */ +/* $OpenBSD: cmd.c,v 1.175 2023/04/09 17:19:59 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -590,22 +590,18 @@ ask_pid(const int dflt) const struct uuid * ask_uuid(const struct uuid *olduuid) { - static struct uuid uuid; char lbuf[LINEBUFSZ]; - char *dflt = NULL; + static struct uuid uuid; + const char *guid; + char *dflt; uint32_t status; - int menuid; - menuid = PRT_uuid_to_menuid(olduuid); - if (menuid == -1) { - uuid_to_string(olduuid, &dflt, &status); - if (status != uuid_s_ok) { - printf("uuid_to_string() failed\n"); + dflt = PRT_uuid_to_menudflt(olduuid); + if (dflt == NULL) { + if (asprintf(&dflt, "0") == -1) { + warn("asprintf()"); goto done; } - } else if (asprintf(&dflt, "%X", menuid) == -1) { - warn("asprintf()"); - goto done; } for (;;) { @@ -623,25 +619,15 @@ ask_uuid(const struct uuid *olduuid) goto done; } - uuid_from_string(lbuf, &uuid, &status); + guid = PRT_menuid_to_guid(hex_octet(lbuf)); + if (guid == NULL) + guid = lbuf; + + uuid_from_string(guid, &uuid, &status); if (status == uuid_s_ok) goto done; - menuid = hex_octet(lbuf); - switch (menuid) { - case -1: - printf("'%s' is not a valid partition id\n", lbuf); - break; - case 0: - uuid_create_nil(&uuid, NULL); - goto done; - default: - uuid = *PRT_menuid_to_uuid(menuid); - if (uuid_is_nil(&uuid, NULL) == 0) - goto done; - printf("'%s' has no associated UUID\n", lbuf); - break; - } + printf("'%s' has no associated UUID\n", lbuf); } done: diff --git a/sbin/fdisk/part.c b/sbin/fdisk/part.c index d3d4cae324d..e67f9be399a 100644 --- a/sbin/fdisk/part.c +++ b/sbin/fdisk/part.c @@ -1,4 +1,4 @@ -/* $OpenBSD: part.c,v 1.153 2023/04/08 15:25:47 krw Exp $ */ +/* $OpenBSD: part.c,v 1.154 2023/04/09 17:19:59 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -1091,39 +1091,38 @@ PRT_uuid_to_desc(const struct uuid *uuid) return guid; } -int -PRT_uuid_to_menuid(const struct uuid *uuid) +char * +PRT_uuid_to_menudflt(const struct uuid *uuid) { - const struct gpt_type *gt; + char *dflt; unsigned int i; + uint32_t status; - gt = find_gpt_type(uuid); - if (gt != NULL) { - for (i = 0; i < nitems(menu_items); i++) { - if (gpt_item(i) == 0 && - strcasecmp(menu_items[i].mi_guid, gt->gt_guid) == 0) - return menu_items[i].mi_menuid; - } + uuid_to_string(uuid, &dflt, &status); + if (status != uuid_s_ok) + return NULL; + + for (i = 0; i < nitems(menu_items); i++) { + if (gpt_item(i) || strcasecmp(menu_items[i].mi_guid, dflt)) + continue; + free(dflt); + dflt = NULL; + if (asprintf(&dflt, "%X", menu_items[i].mi_menuid) == -1) + return NULL; } - return -1; + return dflt; } -const struct uuid * -PRT_menuid_to_uuid(const int menuid) +const char * +PRT_menuid_to_guid(const int menuid) { - static struct uuid uuid; unsigned int i; - uint32_t status = uuid_s_ok; for (i = 0; i < nitems(menu_items); i++) { - if (gpt_item(i) == 0 && menu_items[i].mi_menuid == menuid) { - uuid_from_string(menu_items[i].mi_guid, &uuid, &status); - break; - } + if (gpt_item(i) == 0 && menu_items[i].mi_menuid == menuid) + return menu_items[i].mi_guid; } - if (i == nitems(menu_items) || status != uuid_s_ok) - uuid_create_nil(&uuid, NULL); - return &uuid; + return NULL; } diff --git a/sbin/fdisk/part.h b/sbin/fdisk/part.h index 4427114ce9a..a0c5e618b14 100644 --- a/sbin/fdisk/part.h +++ b/sbin/fdisk/part.h @@ -1,4 +1,4 @@ -/* $OpenBSD: part.h,v 1.44 2023/04/07 14:42:51 krw Exp $ */ +/* $OpenBSD: part.h,v 1.45 2023/04/09 17:19:59 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -38,7 +38,7 @@ void PRT_make(const struct prt *,const uint64_t, const uint64_t, void PRT_print_part(const int, const struct prt *, const char *); void PRT_print_parthdr(void); const char *PRT_uuid_to_desc(const struct uuid *); -int PRT_uuid_to_menuid(const struct uuid *); -const struct uuid *PRT_menuid_to_uuid(const int); +char *PRT_uuid_to_menudflt(const struct uuid *); +const char *PRT_menuid_to_guid(const int); int PRT_protected_uuid(const struct uuid *); int PRT_lba_to_chs(const struct prt*, struct chs *, struct chs*); -- 2.20.1