]> git.ipfire.org Git - people/ms/network.git/commitdiff
networkd: Implement writing configuration files
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 30 Jan 2023 03:17:30 +0000 (03:17 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 30 Jan 2023 03:17:30 +0000 (03:17 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/config.c
src/networkd/config.h

index a3db7c65f81541212b2061ca7ef731f589319c6e..b2a23f003d43f9bf6863650f9a9195d56c503d11 100644 (file)
@@ -165,6 +165,46 @@ ERROR:
        return r;
 }
 
+int nw_config_writef(struct nw_config* config, FILE* f) {
+       struct nw_config_entry* entry = NULL;
+       int r;
+
+       STAILQ_FOREACH(entry, &config->entries, nodes) {
+               // Skip if value is NULL
+               if (!*entry->value)
+                       continue;
+
+               // Write the entry
+               r = fprintf(f, "%s=\"%s\"\n", entry->key, entry->value);
+               if (r < 0) {
+                       ERROR("Failed to write configuration: %m\n");
+                       return r;
+               }
+       }
+
+       return 0;
+}
+
+int nw_config_write(struct nw_config* config, const char* path) {
+       int r;
+
+       FILE* f = fopen(path, "w");
+       if (!f) {
+               ERROR("Failed to open %s for writing: %m\n", path);
+               r = 1;
+               goto ERROR;
+       }
+
+       // Write configuration
+       r = nw_config_writef(config, f);
+
+ERROR:
+       if (f)
+               fclose(f);
+
+       return r;
+}
+
 static struct nw_config_entry* nw_config_find(struct nw_config* config, const char* key) {
        struct nw_config_entry* entry = NULL;
 
index c5a2f7f2ae4a7b1494afed1343ad675ae9486f43..90e73f4b9ceb2333cc123451091e91288bb008d0 100644 (file)
@@ -34,6 +34,9 @@ struct nw_config* nw_config_unref(struct nw_config* config);
 int nw_config_readf(struct nw_config** config, FILE* f);
 int nw_config_read(struct nw_config** config, const char* path);
 
+int nw_config_writef(struct nw_config* config, FILE* f);
+int nw_config_write(struct nw_config* config, const char* path);
+
 int nw_config_del(struct nw_config* config, const char* key);
 
 int nw_config_set(struct nw_config* config, const char* key, const char* value);