From ee5e8b5702a609d847527f69a708cde0d3da54d8 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Wed, 14 Oct 2020 04:26:26 -0600 Subject: [PATCH] interface-file: implement source-directory --- libifupdown/interface-file.c | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/libifupdown/interface-file.c b/libifupdown/interface-file.c index c5a04c3..fdc19ca 100644 --- a/libifupdown/interface-file.c +++ b/libifupdown/interface-file.c @@ -18,6 +18,8 @@ #include #include #include +#include +#include #include "libifupdown/libifupdown.h" /* internally rewrite problematic ifupdown2 tokens to ifupdown-ng equivalents */ @@ -339,6 +341,47 @@ handle_source(struct lif_interface_file_parse_state *state, char *token, char *b return lif_interface_file_parse(state, source_filename); } +static bool +handle_source_directory(struct lif_interface_file_parse_state *state, char *token, char *bufp) +{ + (void) token; + + char *source_directory = lif_next_token(&bufp); + if (!*source_directory) + { + report_error(state, "missing directory to source"); + /* Broken but not fatal */ + return true; + } + + DIR *source_dir = opendir(source_directory); + if (source_dir == NULL) + { + report_error(state, "while opening directory %s: %s", source_directory, strerror(errno)); + /* Broken but not fatal */ + return true; + } + + struct dirent *dirent_p; + for (dirent_p = readdir(source_dir); dirent_p != NULL; dirent_p = readdir(source_dir)) + { + if (dirent_p->d_type != DT_REG) + continue; + + char pathbuf[4096]; + snprintf(pathbuf, sizeof pathbuf, "%s/%s", source_directory, dirent_p->d_name); + + if (!lif_interface_file_parse(state, pathbuf)) + { + closedir(source_dir); + return false; + } + } + + closedir(source_dir); + return true; +} + static bool handle_use(struct lif_interface_file_parse_state *state, char *token, char *bufp) { @@ -369,6 +412,7 @@ static const struct parser_keyword keywords[] = { {"inherit", handle_inherit}, {"interface", handle_iface}, {"source", handle_source}, + {"source-directory", handle_source_directory}, {"template", handle_iface}, {"use", handle_use}, };