]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/networkd/ports.c
networkd: Implement smarter handling of the configuration file hierarchy
[people/ms/network.git] / src / networkd / ports.c
index 87135d8cb628f19ea378c680f4dbff2c7c3c3021..95a13e37a06ba1d467c59828566d587d178419aa 100644 (file)
@@ -18,6 +18,7 @@
 #                                                                             #
 #############################################################################*/
 
+#include <dirent.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/queue.h>
@@ -129,30 +130,15 @@ static int nw_ports_add_port(nw_ports* ports, nw_port* port) {
        return 0;
 }
 
-static int __nw_ports_enumerate(const char* path, const struct stat* s, void* data) {
+static int __nw_ports_enumerate(struct dirent* entry, FILE* f, void* data) {
        nw_port* port = NULL;
        int r;
 
        nw_ports* ports = (nw_ports*)data;
 
-       // Skip anything that isn't a regular file
-       if (!S_ISREG(s->st_mode))
-               return 0;
-
-       // Find the basename of the file
-       const char* name = nw_path_basename(path);
-
-       // Break on invalid paths
-       if (!name)
-               return 0;
-
-       // Skip any hidden files
-       if (*name == '.')
-               return 0;
-
        // Create a new port
-       r = nw_port_create(&port, ports->daemon, name);
-       if (r)
+       r = nw_port_open(&port, ports->daemon, entry->d_name, f);
+       if (r < 0 || r == 1)
                goto ERROR;
 
        // Add the port to the list
@@ -168,7 +154,21 @@ ERROR:
 }
 
 int nw_ports_enumerate(nw_ports* ports) {
-       return nw_ftw(PORT_CONFIG_DIR, PORT_CONFIG_DIR "/*", __nw_ports_enumerate, ports);
+       nw_configd* configd = NULL;
+       int r;
+
+       // Fetch ports configuration directory
+       configd = nw_daemon_configd(ports->daemon, "ports");
+       if (!configd)
+               return 0;
+
+       // Walk through all files
+       r = nw_configd_walk(configd, __nw_ports_enumerate, ports);
+
+       // Cleanup
+       nw_configd_unref(configd);
+
+       return r;
 }
 
 nw_port* nw_ports_get_by_name(nw_ports* ports, const char* name) {