Merge pull request #24719 from yuwata/dissect-image-dissected-image-new

dissect-image: introduce dissected_image_new()
This commit is contained in:
Yu Watanabe
2022-09-18 22:41:49 +09:00
committed by GitHub
2 changed files with 52 additions and 28 deletions
+46 -28
View File
@@ -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)) ||
+6
View File
@@ -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 */