1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-22 13:33:56 +03:00

Merge pull request #25558 from poettering/fdisk-id128

add generic uuid/id128 helpers for libfdisk too
This commit is contained in:
Lennart Poettering 2022-11-29 16:06:10 +01:00 committed by GitHub
commit e6eb581cb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 36 deletions

View File

@ -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, *ids, *label;
const char *label;
uint64_t sz, start;
bool found = false;
sd_id128_t ptid, id;
@ -2111,25 +2110,13 @@ 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");
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)) {

View File

@ -26,4 +26,35 @@ 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);
}
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

View 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,7 @@ 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

View File

@ -106,9 +106,8 @@ int read_partition_info(
PartitionInfo *ret) {
_cleanup_free_ char *label_copy = NULL, *device = NULL;
const char *pts, *ids, *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,25 +146,13 @@ 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");
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)