From: Michael Tremer Date: Wed, 1 Feb 2023 22:37:06 +0000 (+0000) Subject: networkd: Store the path with the configuration object X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b076fa8b6b7aa4b5746b8ef01469e074b6a228ae;p=network.git networkd: Store the path with the configuration object This makes it easier to call read and write functions without re-composing the path again and again... Signed-off-by: Michael Tremer --- diff --git a/src/networkd/config.c b/src/networkd/config.c index 0048e44d..e531ec67 100644 --- a/src/networkd/config.c +++ b/src/networkd/config.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include #include #include @@ -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; } diff --git a/src/networkd/config.h b/src/networkd/config.h index c4f91251..5b9910bd 100644 --- a/src/networkd/config.h +++ b/src/networkd/config.h @@ -28,18 +28,17 @@ 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); diff --git a/src/networkd/daemon.c b/src/networkd/daemon.c index b93877e4..5b097c29 100644 --- a/src/networkd/daemon.c +++ b/src/networkd/daemon.c @@ -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;