From 5ba1550fd8a80466693275e62e4b1e152e477cf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 5 May 2022 22:44:35 +0200 Subject: [PATCH] shared/bootspec: expose more parts of the config parsing --- src/shared/bootspec.c | 55 +++++++++++++++++++++++++++---------------- src/shared/bootspec.h | 3 +++ 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c index 6647aade042..7215c9fc142 100644 --- a/src/shared/bootspec.c +++ b/src/shared/bootspec.c @@ -191,27 +191,19 @@ void boot_config_free(BootConfig *config) { set_free(config->inodes_seen); } -static int boot_loader_read_conf(const char *path, BootConfig *config) { - _cleanup_fclose_ FILE *f = NULL; +int boot_loader_read_conf(BootConfig *config, FILE *file, const char *path) { unsigned line = 1; int r; - assert(path); assert(config); - - f = fopen(path, "re"); - if (!f) { - if (errno == ENOENT) - return 0; - - return log_error_errno(errno, "Failed to open \"%s\": %m", path); - } + assert(file); + assert(path); for (;;) { _cleanup_free_ char *buf = NULL, *field = NULL; const char *p; - r = read_line(f, LONG_LINE_MAX, &buf); + r = read_line(file, LONG_LINE_MAX, &buf); if (r == 0) break; if (r == -ENOBUFS) @@ -262,6 +254,23 @@ static int boot_loader_read_conf(const char *path, BootConfig *config) { return 1; } +static int boot_loader_read_conf_path(BootConfig *config, const char *path) { + _cleanup_fclose_ FILE *f = NULL; + + assert(config); + assert(path); + + f = fopen(path, "re"); + if (!f) { + if (errno == ENOENT) + return 0; + + return log_error_errno(errno, "Failed to open \"%s\": %m", path); + } + + return boot_loader_read_conf(config, f, path); +} + static int boot_entry_compare(const BootEntry *a, const BootEntry *b) { int r; @@ -847,6 +856,18 @@ int boot_config_select_special_entries(BootConfig *config) { return 0; } +int boot_config_finalize(BootConfig *config) { + int r; + + typesafe_qsort(config->entries, config->n_entries, boot_entry_compare); + + r = boot_entries_uniquify(config->entries, config->n_entries); + if (r < 0) + return log_error_errno(r, "Failed to uniquify boot entries: %m"); + + return 0; +} + int boot_config_load( BootConfig *config, const char *esp_path, @@ -859,7 +880,7 @@ int boot_config_load( if (esp_path) { p = strjoina(esp_path, "/loader/loader.conf"); - r = boot_loader_read_conf(p, config); + r = boot_loader_read_conf_path(config, p); if (r < 0) return r; @@ -886,13 +907,7 @@ int boot_config_load( return r; } - typesafe_qsort(config->entries, config->n_entries, boot_entry_compare); - - r = boot_entries_uniquify(config->entries, config->n_entries); - if (r < 0) - return log_error_errno(r, "Failed to uniquify boot entries: %m"); - - return 0; + return boot_config_finalize(config); } int boot_config_load_auto( diff --git a/src/shared/bootspec.h b/src/shared/bootspec.h index f235830c3c0..3ff593a23f7 100644 --- a/src/shared/bootspec.h +++ b/src/shared/bootspec.h @@ -86,6 +86,9 @@ static inline const BootEntry* boot_config_default_entry(const BootConfig *confi void boot_config_free(BootConfig *config); +int boot_loader_read_conf(BootConfig *config, FILE *file, const char *path); + +int boot_config_finalize(BootConfig *config); int boot_config_load(BootConfig *config, const char *esp_path, const char *xbootldr_path); int boot_config_load_auto(BootConfig *config, const char *override_esp_path, const char *override_xbootldr_path); int boot_config_augment_from_loader(BootConfig *config, char **list, bool only_auto);