From 02e32aa62958eb49d97540985f95bd79b2f59ccc Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 29 Nov 2022 11:54:22 +0100 Subject: [PATCH 1/2] fdisk-util: add fdisk_partition_get_uuid_as_id128() helper Inspired by: #25534 --- src/partition/repart.c | 10 +++------- src/shared/fdisk-util.c | 13 +++++++++++++ src/shared/fdisk-util.h | 4 ++++ src/sysupdate/sysupdate-partition.c | 10 +++------- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/partition/repart.c b/src/partition/repart.c index 7857f7b3d1..375df04ad7 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -2093,7 +2093,7 @@ static int context_load_partition_table( Partition *last = NULL; struct fdisk_partition *p; struct fdisk_parttype *pt; - const char *pts, *ids, *label; + const char *pts, *label; uint64_t sz, start; bool found = false; sd_id128_t ptid, id; @@ -2123,13 +2123,9 @@ static int context_load_partition_table( if (r < 0) return log_error_errno(r, "Failed to parse partition type UUID %s: %m", pts); - ids = fdisk_partition_get_uuid(p); - if (!ids) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a UUID."); - - r = sd_id128_from_string(ids, &id); + r = fdisk_partition_get_uuid_as_id128(p, &id); if (r < 0) - return log_error_errno(r, "Failed to parse partition UUID %s: %m", ids); + return log_error_errno(r, "Failed to query partition UUID: %m"); label = fdisk_partition_get_name(p); if (!isempty(label)) { diff --git a/src/shared/fdisk-util.c b/src/shared/fdisk-util.c index 1cdf09b18d..9aba323043 100644 --- a/src/shared/fdisk-util.c +++ b/src/shared/fdisk-util.c @@ -26,4 +26,17 @@ int fdisk_new_context_fd(int fd, bool read_only, struct fdisk_context **ret) { return 0; } +int fdisk_partition_get_uuid_as_id128(struct fdisk_partition *p, sd_id128_t *ret) { + const char *ids; + + assert(p); + assert(ret); + + ids = fdisk_partition_get_uuid(p); + if (!ids) + return -ENXIO; + + return sd_id128_from_string(ids, ret); +} + #endif diff --git a/src/shared/fdisk-util.h b/src/shared/fdisk-util.h index 49cb840c33..91c3ce46bd 100644 --- a/src/shared/fdisk-util.h +++ b/src/shared/fdisk-util.h @@ -5,6 +5,8 @@ #include +#include "sd-id128.h" + #include "macro.h" DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_context*, fdisk_unref_context, NULL); @@ -14,4 +16,6 @@ DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_table*, fdisk_unref_table, NULL); int fdisk_new_context_fd(int fd, bool read_only, struct fdisk_context **ret); +int fdisk_partition_get_uuid_as_id128(struct fdisk_partition *p, sd_id128_t *ret); + #endif diff --git a/src/sysupdate/sysupdate-partition.c b/src/sysupdate/sysupdate-partition.c index 33d0e584ba..486f9f19dd 100644 --- a/src/sysupdate/sysupdate-partition.c +++ b/src/sysupdate/sysupdate-partition.c @@ -106,7 +106,7 @@ int read_partition_info( PartitionInfo *ret) { _cleanup_free_ char *label_copy = NULL, *device = NULL; - const char *pts, *ids, *label; + const char *pts, *label; struct fdisk_partition *p; struct fdisk_parttype *pt; uint64_t start, size, flags; @@ -159,13 +159,9 @@ int read_partition_info( if (r < 0) return log_error_errno(r, "Failed to parse partition type UUID %s: %m", pts); - ids = fdisk_partition_get_uuid(p); - if (!ids) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a UUID."); - - r = sd_id128_from_string(ids, &id); + r = fdisk_partition_get_uuid_as_id128(p, &id); if (r < 0) - return log_error_errno(r, "Failed to parse partition UUID %s: %m", ids); + return log_error_errno(r, "Failed to read partition UUID: %m"); r = fdisk_partition_get_attrs_as_uint64(p, &flags); if (r < 0) From 63b96eb9b3fde23d3d3dfbc44adc6bc3d36c2736 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 29 Nov 2022 12:06:35 +0100 Subject: [PATCH 2/2] fdisk-util: add fdisk_partition_get_type_as_id128() helper Let's also add an easy accessor for the other per-partition UUID. --- src/partition/repart.c | 15 +++------------ src/shared/fdisk-util.c | 18 ++++++++++++++++++ src/shared/fdisk-util.h | 1 + src/sysupdate/sysupdate-partition.c | 15 +++------------ 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/partition/repart.c b/src/partition/repart.c index 375df04ad7..95672f240d 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -2092,8 +2092,7 @@ static int context_load_partition_table( _cleanup_free_ char *label_copy = NULL; Partition *last = NULL; struct fdisk_partition *p; - struct fdisk_parttype *pt; - const char *pts, *label; + const char *label; uint64_t sz, start; bool found = false; sd_id128_t ptid, id; @@ -2111,17 +2110,9 @@ static int context_load_partition_table( fdisk_partition_has_partno(p) <= 0) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a position, size or number."); - pt = fdisk_partition_get_type(p); - if (!pt) - return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to acquire type of partition: %m"); - - pts = fdisk_parttype_get_string(pt); - if (!pts) - return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to acquire type of partition as string: %m"); - - r = sd_id128_from_string(pts, &ptid); + r = fdisk_partition_get_type_as_id128(p, &ptid); if (r < 0) - return log_error_errno(r, "Failed to parse partition type UUID %s: %m", pts); + return log_error_errno(r, "Failed to query partition type UUID: %m"); r = fdisk_partition_get_uuid_as_id128(p, &id); if (r < 0) diff --git a/src/shared/fdisk-util.c b/src/shared/fdisk-util.c index 9aba323043..eeed1840aa 100644 --- a/src/shared/fdisk-util.c +++ b/src/shared/fdisk-util.c @@ -39,4 +39,22 @@ int fdisk_partition_get_uuid_as_id128(struct fdisk_partition *p, sd_id128_t *ret return sd_id128_from_string(ids, ret); } +int fdisk_partition_get_type_as_id128(struct fdisk_partition *p, sd_id128_t *ret) { + struct fdisk_parttype *pt; + const char *pts; + + assert(p); + assert(ret); + + pt = fdisk_partition_get_type(p); + if (!pt) + return -ENXIO; + + pts = fdisk_parttype_get_string(pt); + if (!pts) + return -ENXIO; + + return sd_id128_from_string(pts, ret); +} + #endif diff --git a/src/shared/fdisk-util.h b/src/shared/fdisk-util.h index 91c3ce46bd..7f34a042ec 100644 --- a/src/shared/fdisk-util.h +++ b/src/shared/fdisk-util.h @@ -17,5 +17,6 @@ DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_table*, fdisk_unref_table, NULL); int fdisk_new_context_fd(int fd, bool read_only, struct fdisk_context **ret); int fdisk_partition_get_uuid_as_id128(struct fdisk_partition *p, sd_id128_t *ret); +int fdisk_partition_get_type_as_id128(struct fdisk_partition *p, sd_id128_t *ret); #endif diff --git a/src/sysupdate/sysupdate-partition.c b/src/sysupdate/sysupdate-partition.c index 486f9f19dd..bd0486d99e 100644 --- a/src/sysupdate/sysupdate-partition.c +++ b/src/sysupdate/sysupdate-partition.c @@ -106,9 +106,8 @@ int read_partition_info( PartitionInfo *ret) { _cleanup_free_ char *label_copy = NULL, *device = NULL; - const char *pts, *label; + const char *label; struct fdisk_partition *p; - struct fdisk_parttype *pt; uint64_t start, size, flags; sd_id128_t ptid, id; GptPartitionType type; @@ -147,17 +146,9 @@ int read_partition_info( if (!label) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a label."); - pt = fdisk_partition_get_type(p); - if (!pt) - return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to acquire type of partition: %m"); - - pts = fdisk_parttype_get_string(pt); - if (!pts) - return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to acquire type of partition as string: %m"); - - r = sd_id128_from_string(pts, &ptid); + r = fdisk_partition_get_type_as_id128(p, &ptid); if (r < 0) - return log_error_errno(r, "Failed to parse partition type UUID %s: %m", pts); + return log_error_errno(r, "Failed to read partition type UUID: %m"); r = fdisk_partition_get_uuid_as_id128(p, &id); if (r < 0)