diff options
-rw-r--r-- | src/basic/env-file.c | 14 | ||||
-rw-r--r-- | src/basic/env-file.h | 1 | ||||
-rw-r--r-- | src/portable/portable.c | 8 | ||||
-rw-r--r-- | src/test/test-fileio.c | 36 |
4 files changed, 39 insertions, 20 deletions
diff --git a/src/basic/env-file.c b/src/basic/env-file.c index 34027ca2e5..01ed443d5f 100644 --- a/src/basic/env-file.c +++ b/src/basic/env-file.c @@ -478,6 +478,7 @@ int load_env_file_pairs(FILE *f, const char *fname, char ***ret) { int r; assert(f || fname); + assert(ret); r = parse_env_file_internal(f, fname, load_env_file_push_pairs, &m); if (r < 0) @@ -487,6 +488,19 @@ int load_env_file_pairs(FILE *f, const char *fname, char ***ret) { return 0; } +int load_env_file_pairs_fd(int fd, const char *fname, char ***ret) { + _cleanup_fclose_ FILE *f = NULL; + int r; + + assert(fd >= 0); + + r = fdopen_independent(fd, "re", &f); + if (r < 0) + return r; + + return load_env_file_pairs(f, fname, ret); +} + static int merge_env_file_push( const char *filename, unsigned line, const char *key, char *value, diff --git a/src/basic/env-file.h b/src/basic/env-file.h index dc38b7a5c9..fa22d2209c 100644 --- a/src/basic/env-file.h +++ b/src/basic/env-file.h @@ -14,6 +14,7 @@ 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 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); int merge_env_file(char ***env, FILE *f, const char *fname); diff --git a/src/portable/portable.c b/src/portable/portable.c index 8a3211166e..770cac9e1f 100644 --- a/src/portable/portable.c +++ b/src/portable/portable.c @@ -594,7 +594,6 @@ static int extract_image_and_extensions( _cleanup_(portable_metadata_unrefp) PortableMetadata *extension_release_meta = NULL; _cleanup_hashmap_free_ Hashmap *extra_unit_files = NULL; _cleanup_strv_free_ char **extension_release = NULL; - _cleanup_fclose_ FILE *f = NULL; const char *e; r = portable_extract_by_path(ext->path, /* path_is_extension= */ true, relax_extension_release_check, matches, &extension_release_meta, &extra_unit_files, error); @@ -608,12 +607,7 @@ static int extract_image_and_extensions( if (!validate_sysext && !ret_valid_prefixes && !ret_extension_releases) continue; - /* We need to keep the fd valid, to return the PortableMetadata to the caller. */ - r = fdopen_independent(extension_release_meta->fd, "re", &f); - if (r < 0) - return r; - - r = load_env_file_pairs(f, extension_release_meta->name, &extension_release); + r = load_env_file_pairs_fd(extension_release_meta->fd, extension_release_meta->name, &extension_release); if (r < 0) return r; diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c index 1a9a8a5ddc..d8d622e44d 100644 --- a/src/test/test-fileio.c +++ b/src/test/test-fileio.c @@ -509,6 +509,23 @@ TEST(write_string_file_verify) { assert_se(write_string_file("/proc/version", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE) == 0); } +static void check_file_pairs_one(char **l) { + assert_se(l); + assert_se(strv_length(l) == 14); + + STRV_FOREACH_PAIR(k, v, l) { + assert_se(STR_IN_SET(*k, "NAME", "ID", "PRETTY_NAME", "ANSI_COLOR", "HOME_URL", "SUPPORT_URL", "BUG_REPORT_URL")); + printf("%s=%s\n", *k, *v); + assert_se(!streq(*k, "NAME") || streq(*v, "Arch Linux")); + assert_se(!streq(*k, "ID") || streq(*v, "arch")); + assert_se(!streq(*k, "PRETTY_NAME") || streq(*v, "Arch Linux")); + assert_se(!streq(*k, "ANSI_COLOR") || streq(*v, "0;36")); + assert_se(!streq(*k, "HOME_URL") || streq(*v, "https://www.archlinux.org/")); + assert_se(!streq(*k, "SUPPORT_URL") || streq(*v, "https://bbs.archlinux.org/")); + assert_se(!streq(*k, "BUG_REPORT_URL") || streq(*v, "https://bugs.archlinux.org/")); + } +} + TEST(load_env_file_pairs) { _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-load_env_file_pairs-XXXXXX"; int fd, r; @@ -529,24 +546,17 @@ TEST(load_env_file_pairs) { WRITE_STRING_FILE_CREATE); assert_se(r == 0); + r = load_env_file_pairs_fd(fd, fn, &l); + assert_se(r >= 0); + check_file_pairs_one(l); + l = strv_free(l); + f = fdopen(fd, "r"); assert_se(f); r = load_env_file_pairs(f, fn, &l); assert_se(r >= 0); - - assert_se(strv_length(l) == 14); - STRV_FOREACH_PAIR(k, v, l) { - assert_se(STR_IN_SET(*k, "NAME", "ID", "PRETTY_NAME", "ANSI_COLOR", "HOME_URL", "SUPPORT_URL", "BUG_REPORT_URL")); - printf("%s=%s\n", *k, *v); - if (streq(*k, "NAME")) assert_se(streq(*v, "Arch Linux")); - if (streq(*k, "ID")) assert_se(streq(*v, "arch")); - if (streq(*k, "PRETTY_NAME")) assert_se(streq(*v, "Arch Linux")); - if (streq(*k, "ANSI_COLOR")) assert_se(streq(*v, "0;36")); - if (streq(*k, "HOME_URL")) assert_se(streq(*v, "https://www.archlinux.org/")); - if (streq(*k, "SUPPORT_URL")) assert_se(streq(*v, "https://bbs.archlinux.org/")); - if (streq(*k, "BUG_REPORT_URL")) assert_se(streq(*v, "https://bugs.archlinux.org/")); - } + check_file_pairs_one(l); } TEST(search_and_fopen) { |