From 00e295055be5a7e71ed34e3a2aa160349f25c6bc Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 10 Sep 2022 05:41:09 +0900 Subject: [PATCH 1/2] dissect-image: split out dissected_image_new() To make dissect_image() shorter. No functional changes, just refactoring. --- src/shared/dissect-image.c | 66 ++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c index 5e4883a7a7..fcc158665d 100644 --- a/src/shared/dissect-image.c +++ b/src/shared/dissect-image.c @@ -150,6 +150,45 @@ static void check_partition_flags( } #endif +#if HAVE_BLKID +static int dissected_image_new(const char *path, DissectedImage **ret) { + _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; + _cleanup_free_ char *name = NULL; + int r; + + assert(ret); + + if (path) { + _cleanup_free_ char *filename = NULL; + + r = path_extract_filename(path, &filename); + if (r < 0) + return r; + + r = raw_strip_suffixes(filename, &name); + if (r < 0) + return r; + + if (!image_name_is_valid(name)) { + log_debug("Image name %s is not valid, ignoring.", strna(name)); + name = mfree(name); + } + } + + m = new(DissectedImage, 1); + if (!m) + return -ENOMEM; + + *m = (DissectedImage) { + .has_init_system = -1, + .image_name = TAKE_PTR(name), + }; + + *ret = TAKE_PTR(m); + return 0; +} +#endif + static void dissected_partition_done(DissectedPartition *p) { assert(p); @@ -284,30 +323,9 @@ int dissect_image( if (r != 0) return errno_or_else(EIO); - m = new(DissectedImage, 1); - if (!m) - return -ENOMEM; - - *m = (DissectedImage) { - .has_init_system = -1, - }; - - if (image_path) { - _cleanup_free_ char *extracted_filename = NULL, *name_stripped = NULL; - - r = path_extract_filename(image_path, &extracted_filename); - if (r < 0) - return r; - - r = raw_strip_suffixes(extracted_filename, &name_stripped); - if (r < 0) - return r; - - if (!image_name_is_valid(name_stripped)) - log_debug("Image name %s is not valid, ignoring.", strna(name_stripped)); - else - m->image_name = TAKE_PTR(name_stripped); - } + r = dissected_image_new(image_path, &m); + if (r < 0) + return r; if ((!(flags & DISSECT_IMAGE_GPT_ONLY) && (flags & DISSECT_IMAGE_GENERIC_ROOT)) || From babd5b08b58a58ed0d6bf3d1efb87b87702bd182 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 10 Sep 2022 07:05:12 +0900 Subject: [PATCH 2/2] dissect-image: introduce DISSECTED_PARTITION_NULL Currently, it is not necessary to set partno or architecture in dissect_image_new(), but just for safety. Preparation for later commits. --- src/shared/dissect-image.c | 8 ++++---- src/shared/dissect-image.h | 6 ++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c index fcc158665d..360fc4a939 100644 --- a/src/shared/dissect-image.c +++ b/src/shared/dissect-image.c @@ -184,6 +184,9 @@ static int dissected_image_new(const char *path, DissectedImage **ret) { .image_name = TAKE_PTR(name), }; + for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) + m->partitions[i] = DISSECTED_PARTITION_NULL; + *ret = TAKE_PTR(m); return 0; } @@ -199,10 +202,7 @@ static void dissected_partition_done(DissectedPartition *p) { free(p->decrypted_node); free(p->mount_options); - *p = (DissectedPartition) { - .partno = -1, - .architecture = _ARCHITECTURE_INVALID, - }; + *p = DISSECTED_PARTITION_NULL; } #if HAVE_BLKID diff --git a/src/shared/dissect-image.h b/src/shared/dissect-image.h index fe478bde49..aee8f1c315 100644 --- a/src/shared/dissect-image.h +++ b/src/shared/dissect-image.h @@ -33,6 +33,12 @@ struct DissectedPartition { uint64_t offset; }; +#define DISSECTED_PARTITION_NULL \ + ((DissectedPartition) { \ + .partno = -1, \ + .architecture = _ARCHITECTURE_INVALID, \ + }) + typedef enum PartitionDesignator { PARTITION_ROOT, PARTITION_ROOT_SECONDARY, /* Secondary architecture */