From: Michael Tremer Date: Fri, 18 Oct 2024 10:44:55 +0000 (+0000) Subject: config: Add a helper function to dump the configuration into a file handle X-Git-Tag: 0.9.30~1019 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=00d64ab710a9c4f8267167ff6812ef8b86303535;p=pakfire.git config: Add a helper function to dump the configuration into a file handle Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/config.c b/src/libpakfire/config.c index 4141b186d..7c1b61117 100644 --- a/src/libpakfire/config.c +++ b/src/libpakfire/config.c @@ -608,3 +608,37 @@ ERROR: int pakfire_config_dump(struct pakfire_config* config, FILE* f) { return pakfire_config_walk_sections(config, pakfire_config_dump_section, f); } + +FILE* pakfire_config_fdump(struct pakfire_config* config) { + FILE* f = NULL; + int r; + + char* buffer = NULL; + size_t length = 0; + + // Open a new memory stream + f = open_memstream(&buffer, &length); + if (!f) + goto ERROR; + + // Dump the configuration + r = pakfire_config_dump(config, f); + if (r < 0) + goto ERROR; + + // Close the memory stream + fclose(f); + + // Re-open as a new file handle + f = fmemopen(buffer, length, "r"); + if (!f) + goto ERROR; + + return f; + +ERROR: + if (buffer) + free(buffer); + + return NULL; +} diff --git a/src/libpakfire/include/pakfire/config.h b/src/libpakfire/include/pakfire/config.h index fd35545ba..c3ac55b08 100644 --- a/src/libpakfire/include/pakfire/config.h +++ b/src/libpakfire/include/pakfire/config.h @@ -59,6 +59,7 @@ int pakfire_config_has_section(struct pakfire_config* config, const char* sectio int pakfire_config_read(struct pakfire_config* config, FILE* f); int pakfire_config_dump(struct pakfire_config* config, FILE* f); +FILE* pakfire_config_fdump(struct pakfire_config* config); #endif