]> git.ipfire.org Git - people/ms/network.git/commitdiff
networkd: Refactor enumerating zones
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Feb 2023 18:00:26 +0000 (18:00 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Feb 2023 18:00:26 +0000 (18:00 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/daemon.c
src/networkd/zone.h
src/networkd/zones.c
src/networkd/zones.h

index 8122f581fc32331813d92c4a9cccd054aae4e764..6a6def362f141a5150987eca5ee13696e9643382 100644 (file)
@@ -140,11 +140,6 @@ static int nw_daemon_load_config(nw_daemon* daemon) {
        if (r)
                return r;
 
-       // Load zones
-       r = nw_zones_load(&daemon->zones, daemon);
-       if (r)
-               return r;
-
        return r;
 }
 
@@ -264,6 +259,17 @@ static int nw_daemon_enumerate_ports(nw_daemon* daemon) {
        return nw_ports_enumerate(daemon->ports);
 }
 
+static int nw_daemon_enumerate_zones(nw_daemon* daemon) {
+       int r;
+
+       // Create a new zones container
+       r = nw_zones_create(&daemon->zones, daemon);
+       if (r)
+               return r;
+
+       return nw_zones_enumerate(daemon->zones);
+}
+
 static int nw_daemon_enumerate(nw_daemon* daemon) {
        int r;
 
@@ -277,6 +283,11 @@ static int nw_daemon_enumerate(nw_daemon* daemon) {
        if (r)
                return r;
 
+       // Zones
+       r = nw_daemon_enumerate_zones(daemon);
+       if (r)
+               return r;
+
        return 0;
 }
 
index 9df30ec9ecbe81680d2a806280b9af10ac095162..2748e6d13b6e57ca81e5c40958d1852aa53d090e 100644 (file)
@@ -21,6 +21,8 @@
 #ifndef NETWORKD_ZONE_H
 #define NETWORKD_ZONE_H
 
+#define ZONE_CONFIG_DIR                        CONFIG_DIR "/zones"
+
 #define NETWORK_ZONE_NAME_MAX_LENGTH           16
 #define NETWORK_ZONE_DEFAULT_MTU                       1500
 
index fd43f1c7f3b728c8524d42a815bdc0cd3dc51dd1..1b0ffdc84cabb640aee452eab97f620a7836bb2f 100644 (file)
@@ -25,6 +25,8 @@
 
 #include "daemon.h"
 #include "logging.h"
+#include "string.h"
+#include "util.h"
 #include "zone.h"
 #include "zones.h"
 
@@ -46,7 +48,7 @@ struct nw_zones {
        unsigned int num;
 };
 
-static int nw_zones_create(nw_zones** zones, nw_daemon* daemon) {
+int nw_zones_create(nw_zones** zones, nw_daemon* daemon) {
        nw_zones* z = calloc(1, sizeof(*z));
        if (!z)
                return 1;
@@ -120,89 +122,48 @@ static int nw_zones_add_zone(nw_zones* zones, nw_zone* zone) {
        return 0;
 }
 
-static int nw_zones_load_filter(const struct dirent* path) {
-       const char* fn = path->d_name;
-
-       // Ignore everything starting with '.'
-       if (*fn == '.')
-               return 0;
-
-       // Ignore anything that isn't a directory
-       if (path->d_type != DT_DIR)
-               return 0;
-
-       return 1;
-}
-
-static int __nw_zones_load(nw_zones* zones) {
-       struct dirent** paths = NULL;
-       int n;
-       int r = 0;
-
+static int __nw_zones_enumerate(const char* path, const struct stat* s, void* data) {
        nw_zone* zone = NULL;
+       int r;
 
-       // Scan the zones directory
-       n = scandir(CONFIG_DIR "/zones", &paths, nw_zones_load_filter, alphasort);
-       if (n < 0) {
-               ERROR("Could not load zones: %m\n");
-               return 1;
-       }
-
-       DEBUG("Found %d zone(s)\n", n);
-
-       // Load all zones
-       for (int i = 0; i < n; i++) {
-               const char* name = paths[i]->d_name;
-
-               DEBUG("Loading zone '%s'...\n", name);
-
-               // Create a new zone object
-               r = nw_zone_create(&zone, zones->daemon, name);
-               if (r)
-                       goto ERROR;
-
-               // Store the zone
-               r = nw_zones_add_zone(zones, zone);
-               if (r) {
-                       nw_zone_unref(zone);
-                       goto ERROR;
-               }
+       nw_zones* zones = (nw_zones*)data;
 
-               nw_zone_unref(zone);
-       }
+       // Skip anything that isn't a directory
+       if (!S_ISDIR(s->st_mode))
+               return 0;
 
-ERROR:
-       // Free paths
-       if (paths) {
-               for (int i = 0; i < n; i++) {
-                       free(paths[i]);
-               }
-               free(paths);
-       }
+       // Find the basename of the file
+       const char* name = nw_path_basename(path);
 
-       return r;
-}
+       // Break on invalid paths
+       if (!name)
+               return 0;
 
-int nw_zones_load(nw_zones** zones, nw_daemon* daemon) {
-       int r;
+       // Skip any hidden files
+       if (*name == '.')
+               return 0;
 
-       // Create a new zones object
-       r = nw_zones_create(zones, daemon);
+       // Create a new zone
+       r = nw_zone_create(&zone, zones->daemon, name);
        if (r)
-               return r;
+               goto ERROR;
 
-       // Load all zones
-       r = __nw_zones_load(*zones);
+       // Add the zone to the list
+       r = nw_zones_add_zone(zones, zone);
        if (r)
                goto ERROR;
 
-       return 0;
-
 ERROR:
-       nw_zones_unref(*zones);
+       if (zone)
+               nw_zone_unref(zone);
+
        return r;
 }
 
+int nw_zones_enumerate(nw_zones* zones) {
+       return nw_ftw(ZONE_CONFIG_DIR, ZONE_CONFIG_DIR "/*", __nw_zones_enumerate, zones);
+}
+
 size_t nw_zones_num(nw_zones* zones) {
        struct nw_zones_entry* entry = NULL;
        size_t length = 0;
index 330e5249adf37c7be4dfcd811ec62cec250e3e98..dbf7ccdac3809ce884d26f598679f632fc3d33eb 100644 (file)
@@ -25,10 +25,12 @@ typedef struct nw_zones nw_zones;
 
 #include "daemon.h"
 
+int nw_zones_create(nw_zones** zones, nw_daemon* daemon);
+
 nw_zones* nw_zones_ref(nw_zones* zones);
 nw_zones* nw_zones_unref(nw_zones* zones);
 
-int nw_zones_load(nw_zones** zones, nw_daemon* daemon);
+int nw_zones_enumerate(nw_zones* zones);
 
 size_t nw_zones_num(nw_zones* zones);