[PATCH] depmod: simplify and cleanup depmod_modules_sort()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



In depmod_modules_sort(), there is no particular reason to read
modules.order twice. For sorting purposes, it's enough to assign
keys (sort indexes) to line number - UINT_MAX (assuming that
modules.order can't contain more than UINT_MAX lines in total).

Check for more possible errors, return convenient error codes
and handle the latter in do_depmod() as well.

Signed-off-by: Dmitry Antipov <dmantipov@xxxxxxxxx>
---
 tools/depmod.c | 42 +++++++++++++++++++++---------------------
 1 file changed, 21 insertions(+), 21 deletions(-)

diff --git a/tools/depmod.c b/tools/depmod.c
index a15126e..de6669e 100644
--- a/tools/depmod.c
+++ b/tools/depmod.c
@@ -1479,18 +1479,18 @@ static FILE *dfdopen(const char *dname, const char *filename, int flags,
 	return ret;
 }
 
-
-
-static void depmod_modules_sort(struct depmod *depmod)
+static int depmod_modules_sort(struct depmod *depmod)
 {
 	char line[PATH_MAX];
 	const char *order_file = "modules.order";
 	FILE *fp;
-	unsigned idx = 0, total = 0;
+	int ret = 0;
+	struct mod *mod;
+	unsigned idx = 0;
 
 	fp = dfdopen(depmod->cfg->dirname, order_file, O_RDONLY, "r");
 	if (fp == NULL)
-		return;
+		return -errno;
 
 	while (fgets(line, sizeof(line), fp) != NULL) {
 		size_t len = strlen(line);
@@ -1500,35 +1500,33 @@ static void depmod_modules_sort(struct depmod *depmod)
 		if (line[len - 1] != '\n') {
 			ERR("%s/%s:%u corrupted line misses '\\n'\n",
 				depmod->cfg->dirname, order_file, idx);
+			ret = -EINVAL;
 			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 = idx - UINT_MAX;
+	}
+
+	if (ferror(fp)) {
+		ERR("error reading %s/%s (%s)\n", depmod->cfg->dirname,
+		    order_file, strerror(errno));
+		ret = -errno;
+		goto corrupted;
 	}
 
 	array_sort(&depmod->modules, mod_cmp);
+
 	for (idx = 0; idx < depmod->modules.count; idx++) {
-		struct mod *m = depmod->modules.array[idx];
-		m->idx = idx;
+		mod = depmod->modules.array[idx];
+		mod->idx = idx;
 	}
 
 corrupted:
 	fclose(fp);
+	return ret;
 }
 
 static int depmod_symbol_add(struct depmod *depmod, const char *name,
@@ -3125,7 +3123,9 @@ static int do_depmod(int argc, char *argv[])
 		goto cmdline_modules_failed;
 	}
 
-	depmod_modules_sort(&depmod);
+	err = depmod_modules_sort(&depmod);
+	if (err < 0 && err != -ENOENT)
+		goto cmdline_modules_failed;
 	err = depmod_load(&depmod);
 	if (err < 0)
 		goto cmdline_modules_failed;
-- 
2.40.1




[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux