]> git.ipfire.org Git - network.git/commitdiff
networkd: Store the path with the configuration object
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 1 Feb 2023 22:37:06 +0000 (22:37 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 1 Feb 2023 22:38:27 +0000 (22:38 +0000)
This makes it easier to call read and write functions without
re-composing the path again and again...

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/config.c
src/networkd/config.h
src/networkd/daemon.c

index 0048e44dd7107f960b4c5e35cc4513a7ef3787f6..e531ec6710600c8c8cba7287d455068e57510b49 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <errno.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -38,6 +39,9 @@ struct nw_config_entry {
 struct nw_config {
        int nrefs;
 
+       // The path to the configuration file
+       char path[PATH_MAX];
+
        STAILQ_HEAD(entries, nw_config_entry) entries;
 };
 
@@ -80,7 +84,9 @@ static void nw_config_free(struct nw_config* config) {
        free(config);
 }
 
-int nw_config_create(struct nw_config** config) {
+int nw_config_create(struct nw_config** config, const char* path) {
+       int r;
+
        struct nw_config* c = calloc(1, sizeof(*c));
        if (!c)
                return 1;
@@ -91,9 +97,26 @@ int nw_config_create(struct nw_config** config) {
        // Initialise entries
        STAILQ_INIT(&c->entries);
 
+       // Store the path
+       if (path) {
+               r = nw_string_set(c->path, path);
+               if (r)
+                       goto ERROR;
+
+               // Try to read the configuration from path
+               r = nw_config_read(c);
+               if (r)
+                       goto ERROR;
+       }
+
        *config = c;
 
        return 0;
+
+ERROR:
+       nw_config_free(c);
+
+       return r;
 }
 
 struct nw_config* nw_config_ref(struct nw_config* config) {
@@ -110,6 +133,13 @@ struct nw_config* nw_config_unref(struct nw_config* config) {
        return NULL;
 }
 
+const char* nw_config_path(struct nw_config* config) {
+       if (*config->path)
+               return config->path;
+
+       return NULL;
+}
+
 int nw_config_flush(struct nw_config* config) {
        struct nw_config_entry* entry = NULL;
 
@@ -124,20 +154,30 @@ int nw_config_flush(struct nw_config* config) {
        return 0;
 }
 
-int nw_config_readf(struct nw_config* config, FILE* f) {
+static int nw_config_readf(struct nw_config* config, FILE* f) {
        // XXX TODO
 
        return 0;
 }
 
-int nw_config_read(struct nw_config* config, const char* path) {
+int nw_config_read(struct nw_config* config) {
        FILE* f = NULL;
        int r;
 
+       // We cannot read if path is not set
+       if (!*config->path) {
+               errno = ENOTSUP;
+               return 1;
+       }
+
        // Open the file
-       f = fopen(path, "r");
+       f = fopen(config->path, "r");
        if (!f) {
-               ERROR("Could not read configuration file %s: %m\n", path);
+               // Silently ignore if the file does not exist
+               if (errno == ENOENT)
+                       return 0;
+
+               ERROR("Could not read configuration file %s: %m\n", config->path);
                r = 1;
                goto ERROR;
        }
@@ -152,7 +192,7 @@ ERROR:
        return r;
 }
 
-int nw_config_writef(struct nw_config* config, FILE* f) {
+static int nw_config_writef(struct nw_config* config, FILE* f) {
        struct nw_config_entry* entry = NULL;
        int r;
 
@@ -172,12 +212,18 @@ int nw_config_writef(struct nw_config* config, FILE* f) {
        return 0;
 }
 
-int nw_config_write(struct nw_config* config, const char* path) {
+int nw_config_write(struct nw_config* config) {
        int r;
 
-       FILE* f = fopen(path, "w");
+       // We cannot write if path is not set
+       if (!*config->path) {
+               errno = ENOTSUP;
+               return 1;
+       }
+
+       FILE* f = fopen(config->path, "w");
        if (!f) {
-               ERROR("Failed to open %s for writing: %m\n", path);
+               ERROR("Failed to open %s for writing: %m\n", config->path);
                r = 1;
                goto ERROR;
        }
index c4f91251bbd4b790e877104ae00215e4211451ac..5b9910bdf2afecb5213858cee360e562090eec85 100644 (file)
 
 struct nw_config;
 
-int nw_config_create(struct nw_config** config);
+int nw_config_create(struct nw_config** config, const char* path);
 
 struct nw_config* nw_config_ref(struct nw_config* config);
 struct nw_config* nw_config_unref(struct nw_config* config);
 
-int nw_config_flush(struct nw_config* config);
+const char* nw_config_path(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_flush(struct nw_config* config);
 
-int nw_config_writef(struct nw_config* config, FILE* f);
-int nw_config_write(struct nw_config* config, const char* path);
+int nw_config_read(struct nw_config* config);
+int nw_config_write(struct nw_config* config);
 
 int nw_config_del(struct nw_config* config, const char* key);
 
index b93877e45093bb1ac14c63310193baddd43ef518..5b097c291dd28b8956936a9f68ff8ea458a34ca8 100644 (file)
@@ -110,7 +110,7 @@ static int nw_daemon_setup(struct nw_daemon* daemon) {
        int r;
 
        // Read configuration file
-       r = nw_config_read(&daemon->config, CONFIG_DIR "/settings");
+       r = nw_config_create(&daemon->config, CONFIG_DIR "/settings");
        if (r)
                return r;