env-file: add parse_env_data() helper

This commit is contained in:
Lennart Poettering
2026-03-20 16:34:58 +01:00
committed by Lennart Poettering
parent 226525b432
commit 7876bd5cfc
5 changed files with 74 additions and 57 deletions
+34
View File
@@ -420,6 +420,40 @@ int parse_env_file_fd_sentinel(
return r;
}
int parse_env_datav(
const char *data,
size_t size,
const char *fname, /* only used for logging */
va_list ap) {
assert(data);
if (size == SIZE_MAX)
size = strlen_ptr(data);
_cleanup_fclose_ FILE *f = fmemopen_unlocked((void*) data, size, "r");
if (!f)
return -ENOMEM;
return parse_env_filev(f, fname, ap);
}
int parse_env_data_sentinel(
const char *data,
size_t size,
const char *fname, /* only used for logging */
...) {
va_list ap;
int r;
va_start(ap, fname);
r = parse_env_datav(data, size, fname, ap);
va_end(ap);
return r;
}
static int load_env_file_push(
const char *filename, unsigned line,
const char *key, char *value,
+5
View File
@@ -9,6 +9,11 @@ int parse_env_file_sentinel(FILE *f, const char *fname, ...) _sentinel_;
#define parse_env_file(f, fname, ...) parse_env_file_sentinel(f, fname, __VA_ARGS__, NULL)
int parse_env_file_fd_sentinel(int fd, const char *fname, ...) _sentinel_;
#define parse_env_file_fd(fd, fname, ...) parse_env_file_fd_sentinel(fd, fname, __VA_ARGS__, NULL)
int parse_env_datav(const char *data, size_t size, const char *fname, va_list ap);
int parse_env_data_sentinel(const char *data, size_t size, const char *fname, ...) _sentinel_;
#define parse_env_data(text, size, fname, ...) parse_env_data_sentinel(text, size, fname, __VA_ARGS__, NULL)
int load_env_file(FILE *f, const char *fname, char ***ret);
int load_env_file_pairs(FILE *f, const char *fname, char ***ret);
int load_env_file_pairs_fd(int fd, const char *fname, char ***ret);
+28 -36
View File
@@ -13,8 +13,6 @@
#include "bus-wait-for-jobs.h"
#include "chase.h"
#include "env-file.h"
#include "fd-util.h"
#include "fileio.h"
#include "format-table.h"
#include "fs-util.h"
#include "install.h"
@@ -332,15 +330,12 @@ static int verb_inspect_image(int argc, char *argv[], uintptr_t _data, void *use
nl = true;
} else {
_cleanup_free_ char *pretty_portable = NULL, *pretty_os = NULL;
_cleanup_fclose_ FILE *f = NULL;
f = fmemopen_unlocked((void*) data, sz, "r");
if (!f)
return log_error_errno(errno, "Failed to open /etc/os-release buffer: %m");
r = parse_env_file(f, "/etc/os-release",
"PORTABLE_PRETTY_NAME", &pretty_portable,
"PRETTY_NAME", &pretty_os);
r = parse_env_data(
data, sz,
"/etc/os-release",
"PORTABLE_PRETTY_NAME", &pretty_portable,
"PRETTY_NAME", &pretty_os);
if (r < 0)
return log_error_errno(r, "Failed to parse /etc/os-release: %m");
@@ -396,33 +391,30 @@ static int verb_inspect_image(int argc, char *argv[], uintptr_t _data, void *use
*confext_version_id = NULL, *confext_scope = NULL,
*confext_image_id = NULL, *confext_image_version = NULL,
*confext_build_id = NULL;
_cleanup_fclose_ FILE *f = NULL;
f = fmemopen_unlocked((void*) data, sz, "r");
if (!f)
return log_error_errno(errno, "Failed to open extension-release buffer: %m");
r = parse_env_file(f, name,
"SYSEXT_ID", &sysext_id,
"SYSEXT_VERSION_ID", &sysext_version_id,
"SYSEXT_BUILD_ID", &sysext_build_id,
"SYSEXT_IMAGE_ID", &sysext_image_id,
"SYSEXT_IMAGE_VERSION", &sysext_image_version,
"SYSEXT_SCOPE", &sysext_scope,
"SYSEXT_LEVEL", &sysext_level,
"SYSEXT_PRETTY_NAME", &sysext_pretty_os,
"CONFEXT_ID", &confext_id,
"CONFEXT_VERSION_ID", &confext_version_id,
"CONFEXT_BUILD_ID", &confext_build_id,
"CONFEXT_IMAGE_ID", &confext_image_id,
"CONFEXT_IMAGE_VERSION", &confext_image_version,
"CONFEXT_SCOPE", &confext_scope,
"CONFEXT_LEVEL", &confext_level,
"CONFEXT_PRETTY_NAME", &confext_pretty_os,
"ID", &id,
"VERSION_ID", &version_id,
"PORTABLE_PRETTY_NAME", &pretty_portable,
"PORTABLE_PREFIXES", &portable_prefixes);
r = parse_env_data(
data, sz,
name,
"SYSEXT_ID", &sysext_id,
"SYSEXT_VERSION_ID", &sysext_version_id,
"SYSEXT_BUILD_ID", &sysext_build_id,
"SYSEXT_IMAGE_ID", &sysext_image_id,
"SYSEXT_IMAGE_VERSION", &sysext_image_version,
"SYSEXT_SCOPE", &sysext_scope,
"SYSEXT_LEVEL", &sysext_level,
"SYSEXT_PRETTY_NAME", &sysext_pretty_os,
"CONFEXT_ID", &confext_id,
"CONFEXT_VERSION_ID", &confext_version_id,
"CONFEXT_BUILD_ID", &confext_build_id,
"CONFEXT_IMAGE_ID", &confext_image_id,
"CONFEXT_IMAGE_VERSION", &confext_image_version,
"CONFEXT_SCOPE", &confext_scope,
"CONFEXT_LEVEL", &confext_level,
"CONFEXT_PRETTY_NAME", &confext_pretty_os,
"ID", &id,
"VERSION_ID", &version_id,
"PORTABLE_PRETTY_NAME", &pretty_portable,
"PORTABLE_PREFIXES", &portable_prefixes);
if (r < 0)
return log_error_errno(r, "Failed to parse extension release from '%s': %m", name);
+5 -14
View File
@@ -711,7 +711,6 @@ static int boot_entry_load_unified(
_cleanup_free_ char *fname = NULL, *os_pretty_name = NULL, *os_image_id = NULL, *os_name = NULL, *os_id = NULL,
*os_image_version = NULL, *os_version = NULL, *os_version_id = NULL, *os_build_id = NULL;
const char *k, *good_name, *good_version, *good_sort_key;
_cleanup_fclose_ FILE *f = NULL;
int r;
assert(root);
@@ -723,11 +722,8 @@ static int boot_entry_load_unified(
if (!k)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Path is not below root: %s", path);
f = fmemopen_unlocked((void*) osrelease_text, strlen(osrelease_text), "r");
if (!f)
return log_oom();
r = parse_env_file(f, "os-release",
r = parse_env_data(osrelease_text, /* size= */ SIZE_MAX,
".osrel",
"PRETTY_NAME", &os_pretty_name,
"IMAGE_ID", &os_image_id,
"NAME", &os_name,
@@ -755,14 +751,9 @@ static int boot_entry_load_unified(
_cleanup_free_ char *profile_id = NULL, *profile_title = NULL;
if (profile_text) {
fclose(f);
f = fmemopen_unlocked((void*) profile_text, strlen(profile_text), "r");
if (!f)
return log_oom();
r = parse_env_file(
f, "profile",
r = parse_env_data(
profile_text, /* size= */ SIZE_MAX,
".profile",
"ID", &profile_id,
"TITLE", &profile_title);
if (r < 0)
+2 -7
View File
@@ -28,7 +28,6 @@ static int uki_read_pretty_name(
char **ret) {
_cleanup_free_ char *pname = NULL, *name = NULL;
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ void *osrel = NULL;
size_t osrel_size;
int r;
@@ -51,12 +50,8 @@ static int uki_read_pretty_name(
return 0;
}
f = fmemopen(osrel, osrel_size, "r");
if (!f)
return log_error_errno(errno, "Failed to open embedded os-release file: %m");
r = parse_env_file(
f, NULL,
r = parse_env_data(
osrel, osrel_size, ".osrel",
"PRETTY_NAME", &pname,
"NAME", &name);
if (r < 0)