]> git.ipfire.org Git - pakfire.git/commitdiff
config: Add a helper function to dump the configuration into a file handle
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 18 Oct 2024 10:44:55 +0000 (10:44 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 18 Oct 2024 10:44:55 +0000 (10:44 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/config.c
src/libpakfire/include/pakfire/config.h

index 4141b186d75e090dbea57d3f4140b3eb5da76a93..7c1b61117e1ac06c44b9940cc9686b9e6e64f91f 100644 (file)
@@ -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;
+}
index fd35545baf5959e1832f49ccc353c32f16d45f38..c3ac55b081a8d8eb27664c9d5e323d01337a3ad3 100644 (file)
@@ -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