diff mbox series

depmod: Add support for excluding a directory

Message ID 20220324170354.67733-1-saul.wold@windriver.com (mailing list archive)
State New
Headers show
Series depmod: Add support for excluding a directory | expand

Commit Message

Saul Wold March 24, 2022, 5:03 p.m. UTC
This adds support to depmod to enable a new exclude directive in
the depmod.d/exclude.conf configuration file. Currently depmod
already excludes directories named source or build. This change
will allow additional directories like .debug to be excluded also
via a new exclude directive.

depmod.d/exclude.conf example:
exclude	.debug

Signed-off-by: Saul Wold <saul.wold@windriver.com>
---
 man/depmod.d.xml | 14 +++++++++++++
 tools/depmod.c   | 53 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+)

Comments

Lucas De Marchi March 30, 2022, 9:18 p.m. UTC | #1
Hi Saul,

On Thu, Mar 24, 2022 at 10:03:54AM -0700, Saul Wold wrote:
>This adds support to depmod to enable a new exclude directive in
>the depmod.d/exclude.conf configuration file. Currently depmod
>already excludes directories named source or build. This change
>will allow additional directories like .debug to be excluded also
>via a new exclude directive.
>
>depmod.d/exclude.conf example:
>exclude	.debug
>
>Signed-off-by: Saul Wold <saul.wold@windriver.com>

Overall it looks good to me.

I'd not mention exclude.conf name and rather say depmod.d/*.conf like in
the man page. It's up to distros/users to use whatever name they wish.

>---
> man/depmod.d.xml | 14 +++++++++++++
> tools/depmod.c   | 53 ++++++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 67 insertions(+)
>
>diff --git a/man/depmod.d.xml b/man/depmod.d.xml
>index b315e93..9ab790a 100644
>--- a/man/depmod.d.xml
>+++ b/man/depmod.d.xml
>@@ -131,6 +131,20 @@
>           </para>
>         </listitem>
>       </varlistentry>
>+      <varlistentry>
>+        <term>external <replaceable>excludedir</replaceable>
>+        </term>

wrong copy and paste?
>+        <listitem>
>+          <para>
>+            This specifies the trailing directories that will be excluded
>+            during the search for kernel modules.
>+          </para>
>+          <para>
>+	    The <replaceable>excludedir</replaceable> the trailing directory

s/the/is the/ ?

>+	    to exclude
>+          </para>
>+        </listitem>
>+      </varlistentry>
>     </variablelist>
>   </refsect1>
>
>diff --git a/tools/depmod.c b/tools/depmod.c
>index eb810b8..4ac758c 100644
>--- a/tools/depmod.c
>+++ b/tools/depmod.c
>@@ -458,6 +458,12 @@ struct cfg_external {
> 	char path[];
> };
>
>+struct cfg_exclude {
>+	struct cfg_exclude *next;
>+	size_t len;
>+	char exclude_dir[];
>+};
>+
> struct cfg {
> 	const char *kversion;
> 	char dirname[PATH_MAX];
>@@ -469,6 +475,7 @@ struct cfg {
> 	struct cfg_override *overrides;
> 	struct cfg_search *searches;
> 	struct cfg_external *externals;
>+	struct cfg_exclude *excludes;
> };
>
> static enum search_type cfg_define_search_type(const char *path)
>@@ -580,6 +587,31 @@ static void cfg_external_free(struct cfg_external *ext)
> 	free(ext);
> }
>
>+static int cfg_exclude_add(struct cfg *cfg, const char *path)
>+{
>+	struct cfg_exclude *exc;
>+	size_t len = strlen(path);
>+
>+	exc = malloc(sizeof(struct cfg_exclude) + len);

+ len + 1 as we want to store the \0 too.

>+	if (exc == NULL) {
>+		ERR("exclude add: out of memory\n");
>+		return -ENOMEM;
>+	}
>+	exc->len = len;
>+	memcpy(exc->exclude_dir, path, len);

len + 1

>+
>+	DBG("exclude add: %s\n", path);
>+
>+	exc->next = cfg->excludes;
>+	cfg->excludes = exc;
>+	return 0;
>+}
>+
>+static void cfg_exclude_free(struct cfg_exclude *exc)
>+{
>+	free(exc);
>+}
>+
> static int cfg_kernel_matches(const struct cfg *cfg, const char *pattern)
> {
> 	regex_t re;
>@@ -657,6 +689,11 @@ static int cfg_file_parse(struct cfg *cfg, const char *filename)
> 			}
>
> 			cfg_external_add(cfg, dir);
>+		} else if (streq(cmd, "exclude")) {
>+			const char *sp;
>+			while ((sp = strtok_r(NULL, "\t ", &saveptr)) != NULL) {
>+				cfg_exclude_add(cfg, sp);
>+			}
> 		} else if (streq(cmd, "include")
> 				|| streq(cmd, "make_map_files")) {
> 			INF("%s:%u: command %s not implemented yet\n",
>@@ -857,6 +894,12 @@ static void cfg_free(struct cfg *cfg)
> 		cfg->externals = cfg->externals->next;
> 		cfg_external_free(tmp);
> 	}
>+
>+	while (cfg->excludes) {
>+		struct cfg_exclude *tmp = cfg->excludes;
>+		cfg->excludes = cfg->excludes->next;
>+		cfg_exclude_free(tmp);
>+	}
> }
>
>
>@@ -1239,12 +1282,22 @@ static int depmod_modules_search_dir(struct depmod *depmod, DIR *d, size_t basel
> 		const char *name = de->d_name;
> 		size_t namelen;
> 		uint8_t is_dir;
>+	        struct cfg_exclude *exc;
>+		int exclude = 0;
>
> 		if (name[0] == '.' && (name[1] == '\0' ||
> 				       (name[1] == '.' && name[2] == '\0')))
> 			continue;
> 		if (streq(name, "build") || streq(name, "source"))
> 			continue;
>+
>+	        for (exc = depmod->cfg->excludes; exc != NULL; exc = exc->next) {
>+			if (streq(name, exc->exclude_dir))
>+				exclude = 1;

I'd suggest to add a `break;` here, but even better would be to move
this part to a helper should_skip_dir() so you can simply return
and here we do

		if (should_skip_dir(depmod->cfg, name))
			continue;

Lucas De Marchi

>+		}
>+		if (exclude)
>+			continue;
>+
> 		namelen = strlen(name);
> 		if (scratchbuf_alloc(s_path, baselen + namelen + 2) < 0) {
> 			err = -ENOMEM;
>-- 
>2.31.1
>
diff mbox series

Patch

diff --git a/man/depmod.d.xml b/man/depmod.d.xml
index b315e93..9ab790a 100644
--- a/man/depmod.d.xml
+++ b/man/depmod.d.xml
@@ -131,6 +131,20 @@ 
           </para>
         </listitem>
       </varlistentry>
+      <varlistentry>
+        <term>external <replaceable>excludedir</replaceable>
+        </term>
+        <listitem>
+          <para>
+            This specifies the trailing directories that will be excluded
+            during the search for kernel modules.
+          </para>
+          <para>
+	    The <replaceable>excludedir</replaceable> the trailing directory
+	    to exclude
+          </para>
+        </listitem>
+      </varlistentry>
     </variablelist>
   </refsect1>
 
diff --git a/tools/depmod.c b/tools/depmod.c
index eb810b8..4ac758c 100644
--- a/tools/depmod.c
+++ b/tools/depmod.c
@@ -458,6 +458,12 @@  struct cfg_external {
 	char path[];
 };
 
+struct cfg_exclude {
+	struct cfg_exclude *next;
+	size_t len;
+	char exclude_dir[];
+};
+
 struct cfg {
 	const char *kversion;
 	char dirname[PATH_MAX];
@@ -469,6 +475,7 @@  struct cfg {
 	struct cfg_override *overrides;
 	struct cfg_search *searches;
 	struct cfg_external *externals;
+	struct cfg_exclude *excludes;
 };
 
 static enum search_type cfg_define_search_type(const char *path)
@@ -580,6 +587,31 @@  static void cfg_external_free(struct cfg_external *ext)
 	free(ext);
 }
 
+static int cfg_exclude_add(struct cfg *cfg, const char *path)
+{
+	struct cfg_exclude *exc;
+	size_t len = strlen(path);
+
+	exc = malloc(sizeof(struct cfg_exclude) + len);
+	if (exc == NULL) {
+		ERR("exclude add: out of memory\n");
+		return -ENOMEM;
+	}
+	exc->len = len;
+	memcpy(exc->exclude_dir, path, len);
+
+	DBG("exclude add: %s\n", path);
+
+	exc->next = cfg->excludes;
+	cfg->excludes = exc;
+	return 0;
+}
+
+static void cfg_exclude_free(struct cfg_exclude *exc)
+{
+	free(exc);
+}
+
 static int cfg_kernel_matches(const struct cfg *cfg, const char *pattern)
 {
 	regex_t re;
@@ -657,6 +689,11 @@  static int cfg_file_parse(struct cfg *cfg, const char *filename)
 			}
 
 			cfg_external_add(cfg, dir);
+		} else if (streq(cmd, "exclude")) {
+			const char *sp;
+			while ((sp = strtok_r(NULL, "\t ", &saveptr)) != NULL) {
+				cfg_exclude_add(cfg, sp);
+			}
 		} else if (streq(cmd, "include")
 				|| streq(cmd, "make_map_files")) {
 			INF("%s:%u: command %s not implemented yet\n",
@@ -857,6 +894,12 @@  static void cfg_free(struct cfg *cfg)
 		cfg->externals = cfg->externals->next;
 		cfg_external_free(tmp);
 	}
+
+	while (cfg->excludes) {
+		struct cfg_exclude *tmp = cfg->excludes;
+		cfg->excludes = cfg->excludes->next;
+		cfg_exclude_free(tmp);
+	}
 }
 
 
@@ -1239,12 +1282,22 @@  static int depmod_modules_search_dir(struct depmod *depmod, DIR *d, size_t basel
 		const char *name = de->d_name;
 		size_t namelen;
 		uint8_t is_dir;
+	        struct cfg_exclude *exc;
+		int exclude = 0;
 
 		if (name[0] == '.' && (name[1] == '\0' ||
 				       (name[1] == '.' && name[2] == '\0')))
 			continue;
 		if (streq(name, "build") || streq(name, "source"))
 			continue;
+
+	        for (exc = depmod->cfg->excludes; exc != NULL; exc = exc->next) {
+			if (streq(name, exc->exclude_dir))
+				exclude = 1;
+		}
+		if (exclude)
+			continue;
+
 		namelen = strlen(name);
 		if (scratchbuf_alloc(s_path, baselen + namelen + 2) < 0) {
 			err = -ENOMEM;