diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-09-18 15:41:49 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-18 15:41:49 +0200 |
commit | 8b6a70f69997243f425688d548e09994e09d6cdf (patch) | |
tree | 157d7b91d3200a8a56e3269d4d2f4d36db8f3611 | |
parent | sd-device: refuse block device without subsystem (diff) | |
parent | dissect-image: introduce DISSECTED_PARTITION_NULL (diff) | |
download | systemd-8b6a70f69997243f425688d548e09994e09d6cdf.tar.xz systemd-8b6a70f69997243f425688d548e09994e09d6cdf.zip |
Merge pull request #24719 from yuwata/dissect-image-dissected-image-new
dissect-image: introduce dissected_image_new()
-rw-r--r-- | src/shared/dissect-image.c | 74 | ||||
-rw-r--r-- | src/shared/dissect-image.h | 6 |
2 files changed, 52 insertions, 28 deletions
diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c index ec7b5e9516..a0eac0f24c 100644 --- a/src/shared/dissect-image.c +++ b/src/shared/dissect-image.c @@ -150,6 +150,48 @@ 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), + }; + + for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) + m->partitions[i] = DISSECTED_PARTITION_NULL; + + *ret = TAKE_PTR(m); + return 0; +} +#endif + static void dissected_partition_done(DissectedPartition *p) { assert(p); @@ -160,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 @@ -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)) || 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 */ |