dissect: introduce PartitionDesignator as real type

This commit is contained in:
Lennart Poettering 2020-09-15 18:02:28 +02:00
parent f5215bc8d3
commit 569a0e42ec
5 changed files with 22 additions and 25 deletions

View file

@ -1460,7 +1460,7 @@ static int read_mount_options(sd_bus_message *message, sd_bus_error *error, Moun
while ((r = sd_bus_message_read(message, "(ss)", &partition, &mount_options)) > 0) { while ((r = sd_bus_message_read(message, "(ss)", &partition, &mount_options)) > 0) {
_cleanup_free_ char *previous = NULL, *escaped = NULL; _cleanup_free_ char *previous = NULL, *escaped = NULL;
_cleanup_free_ MountOptions *o = NULL; _cleanup_free_ MountOptions *o = NULL;
int partition_designator; PartitionDesignator partition_designator;
if (chars_intersect(mount_options, WHITESPACE)) if (chars_intersect(mount_options, WHITESPACE))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,

View file

@ -1483,7 +1483,7 @@ int config_parse_root_image_options(
MountOptions *o = NULL; MountOptions *o = NULL;
_cleanup_free_ char *mount_options_resolved = NULL; _cleanup_free_ char *mount_options_resolved = NULL;
const char *mount_options = NULL, *partition = "root"; const char *mount_options = NULL, *partition = "root";
int partition_designator; PartitionDesignator partition_designator;
/* Format is either 'root:foo' or 'foo' (root is implied) */ /* Format is either 'root:foo' or 'foo' (root is implied) */
if (!isempty(*second)) { if (!isempty(*second)) {
@ -4866,7 +4866,7 @@ int config_parse_mount_images(
for (;;) { for (;;) {
_cleanup_free_ char *partition = NULL, *mount_options = NULL, *mount_options_resolved = NULL; _cleanup_free_ char *partition = NULL, *mount_options = NULL, *mount_options_resolved = NULL;
MountOptions *o = NULL; MountOptions *o = NULL;
int partition_designator; PartitionDesignator partition_designator;
r = extract_many_words(&q, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS, &partition, &mount_options, NULL); r = extract_many_words(&q, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS, &partition, &mount_options, NULL);
if (r == -ENOMEM) if (r == -ENOMEM)

View file

@ -456,7 +456,7 @@ static int action_dissect(DissectedImage *m, LoopDevice *d) {
(void) table_set_empty_string(t, "-"); (void) table_set_empty_string(t, "-");
(void) table_set_align_percent(t, table_get_cell(t, 0, 7), 100); (void) table_set_align_percent(t, table_get_cell(t, 0, 7), 100);
for (unsigned i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
DissectedPartition *p = m->partitions + i; DissectedPartition *p = m->partitions + i;
if (!p->found) if (!p->found)

View file

@ -325,7 +325,6 @@ int dissect_image(
int r, generic_nr; int r, generic_nr;
struct stat st; struct stat st;
sd_device *q; sd_device *q;
unsigned i;
assert(fd >= 0); assert(fd >= 0);
assert(ret); assert(ret);
@ -504,7 +503,8 @@ int dissect_image(
continue; continue;
if (is_gpt) { if (is_gpt) {
int designator = _PARTITION_DESIGNATOR_INVALID, architecture = _ARCHITECTURE_INVALID; PartitionDesignator designator = _PARTITION_DESIGNATOR_INVALID;
int architecture = _ARCHITECTURE_INVALID;
const char *stype, *sid, *fstype = NULL; const char *stype, *sid, *fstype = NULL;
sd_id128_t type_id, id; sd_id128_t type_id, id;
bool rw = true; bool rw = true;
@ -866,7 +866,7 @@ int dissect_image(
b = NULL; b = NULL;
/* Fill in file system types if we don't know them yet. */ /* Fill in file system types if we don't know them yet. */
for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
DissectedPartition *p = m->partitions + i; DissectedPartition *p = m->partitions + i;
if (!p->found) if (!p->found)
@ -894,12 +894,10 @@ int dissect_image(
} }
DissectedImage* dissected_image_unref(DissectedImage *m) { DissectedImage* dissected_image_unref(DissectedImage *m) {
unsigned i;
if (!m) if (!m)
return NULL; return NULL;
for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
free(m->partitions[i].fstype); free(m->partitions[i].fstype);
free(m->partitions[i].node); free(m->partitions[i].node);
free(m->partitions[i].decrypted_fstype); free(m->partitions[i].decrypted_fstype);
@ -1557,7 +1555,6 @@ int dissected_image_decrypt(
#if HAVE_LIBCRYPTSETUP #if HAVE_LIBCRYPTSETUP
_cleanup_(decrypted_image_unrefp) DecryptedImage *d = NULL; _cleanup_(decrypted_image_unrefp) DecryptedImage *d = NULL;
unsigned i;
int r; int r;
#endif #endif
@ -1585,7 +1582,7 @@ int dissected_image_decrypt(
if (!d) if (!d)
return -ENOMEM; return -ENOMEM;
for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
DissectedPartition *p = m->partitions + i; DissectedPartition *p = m->partitions + i;
int k; int k;
@ -2039,14 +2036,14 @@ int dissect_image_and_warn(
} }
} }
bool dissected_image_can_do_verity(const DissectedImage *image, unsigned partition_designator) { bool dissected_image_can_do_verity(const DissectedImage *image, PartitionDesignator partition_designator) {
if (image->single_file_system) if (image->single_file_system)
return partition_designator == PARTITION_ROOT && image->can_verity; return partition_designator == PARTITION_ROOT && image->can_verity;
return PARTITION_VERITY_OF(partition_designator) >= 0; return PARTITION_VERITY_OF(partition_designator) >= 0;
} }
bool dissected_image_has_verity(const DissectedImage *image, unsigned partition_designator) { bool dissected_image_has_verity(const DissectedImage *image, PartitionDesignator partition_designator) {
int k; int k;
if (image->single_file_system) if (image->single_file_system)
@ -2068,7 +2065,7 @@ MountOptions* mount_options_free_all(MountOptions *options) {
return NULL; return NULL;
} }
const char* mount_options_from_designator(const MountOptions *options, int designator) { const char* mount_options_from_designator(const MountOptions *options, PartitionDesignator designator) {
const MountOptions *m; const MountOptions *m;
LIST_FOREACH(mount_options, m, options) LIST_FOREACH(mount_options, m, options)
@ -2164,4 +2161,4 @@ static const char *const partition_designator_table[] = {
[PARTITION_VAR] = "var", [PARTITION_VAR] = "var",
}; };
DEFINE_STRING_TABLE_LOOKUP(partition_designator, int); DEFINE_STRING_TABLE_LOOKUP(partition_designator, PartitionDesignator);

View file

@ -27,7 +27,7 @@ struct DissectedPartition {
char *mount_options; char *mount_options;
}; };
enum { typedef enum PartitionDesignator {
PARTITION_ROOT, PARTITION_ROOT,
PARTITION_ROOT_SECONDARY, /* Secondary architecture */ PARTITION_ROOT_SECONDARY, /* Secondary architecture */
PARTITION_HOME, PARTITION_HOME,
@ -41,9 +41,9 @@ enum {
PARTITION_VAR, PARTITION_VAR,
_PARTITION_DESIGNATOR_MAX, _PARTITION_DESIGNATOR_MAX,
_PARTITION_DESIGNATOR_INVALID = -1 _PARTITION_DESIGNATOR_INVALID = -1
}; } PartitionDesignator;
static inline int PARTITION_VERITY_OF(int p) { static inline PartitionDesignator PARTITION_VERITY_OF(PartitionDesignator p) {
if (p == PARTITION_ROOT) if (p == PARTITION_ROOT)
return PARTITION_ROOT_VERITY; return PARTITION_ROOT_VERITY;
if (p == PARTITION_ROOT_SECONDARY) if (p == PARTITION_ROOT_SECONDARY)
@ -87,14 +87,14 @@ struct DissectedImage {
}; };
struct MountOptions { struct MountOptions {
int partition_designator; PartitionDesignator partition_designator;
char *options; char *options;
LIST_FIELDS(MountOptions, mount_options); LIST_FIELDS(MountOptions, mount_options);
}; };
MountOptions* mount_options_free_all(MountOptions *options); MountOptions* mount_options_free_all(MountOptions *options);
DEFINE_TRIVIAL_CLEANUP_FUNC(MountOptions*, mount_options_free_all); DEFINE_TRIVIAL_CLEANUP_FUNC(MountOptions*, mount_options_free_all);
const char* mount_options_from_designator(const MountOptions *options, int designator); const char* mount_options_from_designator(const MountOptions *options, PartitionDesignator designator);
int probe_filesystem(const char *node, char **ret_fstype); int probe_filesystem(const char *node, char **ret_fstype);
int dissect_image(int fd, const void *root_hash, size_t root_hash_size, const char *verity_data, const MountOptions *mount_options, DissectImageFlags flags, DissectedImage **ret); int dissect_image(int fd, const void *root_hash, size_t root_hash_size, const char *verity_data, const MountOptions *mount_options, DissectImageFlags flags, DissectedImage **ret);
@ -114,11 +114,11 @@ DecryptedImage* decrypted_image_unref(DecryptedImage *p);
DEFINE_TRIVIAL_CLEANUP_FUNC(DecryptedImage*, decrypted_image_unref); DEFINE_TRIVIAL_CLEANUP_FUNC(DecryptedImage*, decrypted_image_unref);
int decrypted_image_relinquish(DecryptedImage *d); int decrypted_image_relinquish(DecryptedImage *d);
const char* partition_designator_to_string(int i) _const_; const char* partition_designator_to_string(PartitionDesignator d) _const_;
int partition_designator_from_string(const char *name) _pure_; PartitionDesignator partition_designator_from_string(const char *name) _pure_;
int verity_metadata_load(const char *image, const char *root_hash_path, void **ret_roothash, size_t *ret_roothash_size, char **ret_verity_data, char **ret_roothashsig); int verity_metadata_load(const char *image, const char *root_hash_path, void **ret_roothash, size_t *ret_roothash_size, char **ret_verity_data, char **ret_roothashsig);
bool dissected_image_can_do_verity(const DissectedImage *image, unsigned partition_designator); bool dissected_image_can_do_verity(const DissectedImage *image, PartitionDesignator d);
bool dissected_image_has_verity(const DissectedImage *image, unsigned partition_designator); bool dissected_image_has_verity(const DissectedImage *image, PartitionDesignator d);
int mount_image_privately_interactively(const char *path, DissectImageFlags flags, char **ret_directory, LoopDevice **ret_loop_device, DecryptedImage **ret_decrypted_image); int mount_image_privately_interactively(const char *path, DissectImageFlags flags, char **ret_directory, LoopDevice **ret_loop_device, DecryptedImage **ret_decrypted_image);