1From f50e2d67575ac5f256fb853ca9d29aeac92d9a57 Mon Sep 17 00:00:00 2001
2From: Saul Wold <saul.wold@windriver.com>
3Date: Thu, 31 Mar 2022 14:56:28 -0700
4Subject: [PATCH] depmod: Add support for excluding a directory
5
6This adds support to depmod to enable a new exclude directive in
7the depmod.d/*.conf configuration file. Currently depmod
8already excludes directories named source or build. This change
9will allow additional directories like .debug to be excluded also
10via a new exclude directive.
11
12depmod.d/exclude.conf example:
13exclude	.debug
14
15Upstream-Status: Accepted
16
17Signed-off-by: Saul Wold <saul.wold@windriver.com>
18[ Fix warnings and make should_exclude_dir() return bool ]
19Signed-off-by: Lucas De Marchi <lucas.demarchi@intel.com>
20---
21 man/depmod.d.xml | 14 ++++++++++
22 tools/depmod.c   | 66 +++++++++++++++++++++++++++++++++++++++++++++---
23 2 files changed, 76 insertions(+), 4 deletions(-)
24
25diff --git a/man/depmod.d.xml b/man/depmod.d.xml
26index b315e93..76548e9 100644
27--- a/man/depmod.d.xml
28+++ b/man/depmod.d.xml
29@@ -131,6 +131,20 @@
30           </para>
31         </listitem>
32       </varlistentry>
33+      <varlistentry>
34+        <term>exclude <replaceable>excludedir</replaceable>
35+        </term>
36+        <listitem>
37+          <para>
38+            This specifies the trailing directories that will be excluded
39+            during the search for kernel modules.
40+          </para>
41+          <para>
42+	    The <replaceable>excludedir</replaceable> is the trailing directory
43+	    to exclude
44+          </para>
45+        </listitem>
46+      </varlistentry>
47     </variablelist>
48   </refsect1>
49
50diff --git a/tools/depmod.c b/tools/depmod.c
51index 07a35ba..4117dd1 100644
52--- a/tools/depmod.c
53+++ b/tools/depmod.c
54@@ -458,6 +458,11 @@ struct cfg_external {
55 	char path[];
56 };
57
58+struct cfg_exclude {
59+	struct cfg_exclude *next;
60+	char exclude_dir[];
61+};
62+
63 struct cfg {
64 	const char *kversion;
65 	char dirname[PATH_MAX];
66@@ -469,6 +474,7 @@ struct cfg {
67 	struct cfg_override *overrides;
68 	struct cfg_search *searches;
69 	struct cfg_external *externals;
70+	struct cfg_exclude *excludes;
71 };
72
73 static enum search_type cfg_define_search_type(const char *path)
74@@ -580,6 +586,30 @@ static void cfg_external_free(struct cfg_external *ext)
75 	free(ext);
76 }
77
78+static int cfg_exclude_add(struct cfg *cfg, const char *path)
79+{
80+	struct cfg_exclude *exc;
81+	size_t len = strlen(path);
82+
83+	exc = malloc(sizeof(struct cfg_exclude) + len + 1);
84+	if (exc == NULL) {
85+		ERR("exclude add: out of memory\n");
86+		return -ENOMEM;
87+	}
88+	memcpy(exc->exclude_dir, path, len + 1);
89+
90+	DBG("exclude add: %s\n", path);
91+
92+	exc->next = cfg->excludes;
93+	cfg->excludes = exc;
94+	return 0;
95+}
96+
97+static void cfg_exclude_free(struct cfg_exclude *exc)
98+{
99+	free(exc);
100+}
101+
102 static int cfg_kernel_matches(const struct cfg *cfg, const char *pattern)
103 {
104 	regex_t re;
105@@ -657,6 +687,11 @@ static int cfg_file_parse(struct cfg *cfg, const char *filename)
106 			}
107
108 			cfg_external_add(cfg, dir);
109+		} else if (streq(cmd, "exclude")) {
110+			const char *sp;
111+			while ((sp = strtok_r(NULL, "\t ", &saveptr)) != NULL) {
112+				cfg_exclude_add(cfg, sp);
113+			}
114 		} else if (streq(cmd, "include")
115 				|| streq(cmd, "make_map_files")) {
116 			INF("%s:%u: command %s not implemented yet\n",
117@@ -857,6 +892,12 @@ static void cfg_free(struct cfg *cfg)
118 		cfg->externals = cfg->externals->next;
119 		cfg_external_free(tmp);
120 	}
121+
122+	while (cfg->excludes) {
123+		struct cfg_exclude *tmp = cfg->excludes;
124+		cfg->excludes = cfg->excludes->next;
125+		cfg_exclude_free(tmp);
126+	}
127 }
128
129
130@@ -1229,6 +1270,25 @@ add:
131 	return 0;
132 }
133
134+static bool should_exclude_dir(const struct cfg *cfg, const char *name)
135+{
136+	struct cfg_exclude *exc;
137+
138+	if (name[0] == '.' && (name[1] == '\0' ||
139+			(name[1] == '.' && name[2] == '\0')))
140+		return true;
141+
142+	if (streq(name, "build") || streq(name, "source"))
143+		return true;
144+
145+	for (exc = cfg->excludes; exc != NULL; exc = exc->next) {
146+		if (streq(name, exc->exclude_dir))
147+			return true;
148+	}
149+
150+	return false;
151+}
152+
153 static int depmod_modules_search_dir(struct depmod *depmod, DIR *d, size_t baselen, struct scratchbuf *s_path)
154 {
155 	struct dirent *de;
156@@ -1240,11 +1300,9 @@ static int depmod_modules_search_dir(struct depmod *depmod, DIR *d, size_t basel
157 		size_t namelen;
158 		uint8_t is_dir;
159
160-		if (name[0] == '.' && (name[1] == '\0' ||
161-				       (name[1] == '.' && name[2] == '\0')))
162-			continue;
163-		if (streq(name, "build") || streq(name, "source"))
164+		if (should_exclude_dir(depmod->cfg, name))
165 			continue;
166+
167 		namelen = strlen(name);
168 		if (scratchbuf_alloc(s_path, baselen + namelen + 2) < 0) {
169 			err = -ENOMEM;
170--
1712.31.1
172
173