File: 0003-Support-including-source-files-by-pattern.patch

package info (click to toggle)
ifupdown-ng 0.12.1-7
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 964 kB
  • sloc: ansic: 3,572; sh: 980; makefile: 233
file content (68 lines) | stat: -rw-r--r-- 1,963 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
From: =?utf-8?q?Daniel_Gr=C3=B6ber?= <dxld@darkboxed.org>
Date: Fri, 2 Dec 2022 20:04:25 +0100
Subject: Support including 'source' files by pattern

Forwarded: https://github.com/ifupdown-ng/ifupdown-ng/pull/225
---
 doc/interfaces.scd           |  3 ++-
 libifupdown/interface-file.c | 26 +++++++++++++++++++++++++-
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/doc/interfaces.scd b/doc/interfaces.scd
index f4a6473..0388888 100644
--- a/doc/interfaces.scd
+++ b/doc/interfaces.scd
@@ -50,7 +50,8 @@ with an address of *203.0.113.2* and gateway of *203.0.113.1*.
 	_object_.
 
 *source* _filename_
-	Includes the file _filename_ as configuration data.
+	Includes the file _filename_ as configuration data. Shell
+	wildcards  can  be used. See wordexp(3).
 
 *source-directory* _directory_
 	Includes the files in _directory_ as configuration data.
diff --git a/libifupdown/interface-file.c b/libifupdown/interface-file.c
index d0fa233..35f89ec 100644
--- a/libifupdown/interface-file.c
+++ b/libifupdown/interface-file.c
@@ -20,6 +20,10 @@
 #include <string.h>
 #include <dirent.h>
 #include <errno.h>
+#include <wordexp.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
 #include "libifupdown/libifupdown.h"
 
 /* internally rewrite problematic ifupdown2 tokens to ifupdown-ng equivalents */
@@ -380,7 +384,27 @@ handle_source(struct lif_interface_file_parse_state *state, char *token, char *b
 		return true;
 	}
 
-	return lif_interface_file_parse(state, source_filename);
+	bool ok = true;
+	wordexp_t p;
+	if (wordexp(source_filename, &p, WRDE_NOCMD)) {
+		report_error(state, "matching pattern failed");
+		ok = false;
+		goto out;
+	}
+
+	for (size_t i = 0; i < p.we_wordc; i++) {
+		char *m = p.we_wordv[i];
+		struct stat sb;
+		int rv = stat(m, &sb);
+		if (i == 0 && rv == -1)
+			goto out; // no matches
+		ok &= lif_interface_file_parse(state, m);
+	}
+
+out:
+	wordfree(&p);
+
+	return ok;
 }
 
 static bool