From: Tobias Stoeckmann Date: Fri, 27 Sep 2024 20:17:36 +0000 (+0200) Subject: depmod: Read modules.order only once X-Git-Tag: v34~277 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=19c0df1b75bf7e9e3b733ea9a4850b867c34cb87;p=thirdparty%2Fkmod.git depmod: Read modules.order only once The current code iterates through modules.order twice. First, it figures out how many lines exist. Then it iterates again to make sure that the first line has the lowest sort index, then ascending. Negative values make sure that the previously assigned positive values will be larger, i.e. modules in modules.order take precedence, then modules found in file system which were not listed in modules.order. This can be simplified by setting the initial sort index value to the lowest possible value needed, i.e. -depmod->modules.count. With this value, it is possible to iterate only once. Signed-off-by: Tobias Stoeckmann Reviewed-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/158 Signed-off-by: Lucas De Marchi --- diff --git a/tools/depmod.c b/tools/depmod.c index 7d1f3feb..02a89046 100644 --- a/tools/depmod.c +++ b/tools/depmod.c @@ -1463,39 +1463,30 @@ static void depmod_modules_sort(struct depmod *depmod) char line[PATH_MAX]; const char *order_file = "modules.order"; FILE *fp; - unsigned idx = 0, total = 0; + size_t idx = 0; + // all sorted modules shall have precedence + int i = -(int)depmod->modules.count; fp = dfdopen(depmod->cfg->dirname, order_file, O_RDONLY, "r"); if (fp == NULL) return; while (fgets(line, sizeof(line), fp) != NULL) { + struct mod *mod; size_t len = strlen(line); idx++; if (len == 0) continue; if (line[len - 1] != '\n') { - ERR("%s/%s:%u corrupted line misses '\\n'\n", + ERR("%s/%s:%zu corrupted line misses '\\n'\n", depmod->cfg->dirname, order_file, idx); goto corrupted; } - } - total = idx + 1; - idx = 0; - fseek(fp, 0, SEEK_SET); - while (fgets(line, sizeof(line), fp) != NULL) { - size_t len = strlen(line); - struct mod *mod; - - idx++; - if (len == 0) - continue; line[len - 1] = '\0'; - mod = hash_find(depmod->modules_by_uncrelpath, line); if (mod == NULL) continue; - mod->sort_idx = idx - total; + mod->sort_idx = i++; } array_sort(&depmod->modules, mod_cmp);