]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fdisk-util: add fdisk_partition_get_uuid_as_id128() helper
authorLennart Poettering <lennart@poettering.net>
Tue, 29 Nov 2022 10:54:22 +0000 (11:54 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 29 Nov 2022 11:07:15 +0000 (12:07 +0100)
Inspired by: #25534

src/partition/repart.c
src/shared/fdisk-util.c
src/shared/fdisk-util.h
src/sysupdate/sysupdate-partition.c

index 7857f7b3d199d5c06aea420680d9b21d1cca57f0..375df04ad7f7720f13743045f964bc5aa431e198 100644 (file)
@@ -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)) {
index 1cdf09b18dc70e43d313adfd5fda54aa0af279e2..9aba3230430b968a300539a3e05dfc174d951630 100644 (file)
@@ -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
index 49cb840c330c48dcfc2fdf247742c2f4789101d7..91c3ce46bd35fe5fa0965cb55e816dd06e28d32a 100644 (file)
@@ -5,6 +5,8 @@
 
 #include <libfdisk.h>
 
+#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
index 33d0e584ba0706c1f8bf3d1e3daa20a427adf726..486f9f19ddc5d3781c5c681836c1d3fa469c6011 100644 (file)
@@ -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)