[LEDE-DEV] [PATCH mountd 3/5] mountd: Factor code getting block devices attributes

Florian Fainelli f.fainelli at gmail.com
Sun Mar 5 16:50:14 PST 2017


Factor the code obtaining a bunch of a block devices attributes: vendor, rev,
model, size since this is a repetitive and identical procedure for each of these.

This also fixes unused fgets() return values for these parts of the code.

Signed-off-by: Florian Fainelli <f.fainelli at gmail.com>
---
 mount.c | 89 ++++++++++++++++++++++++-----------------------------------------
 1 file changed, 33 insertions(+), 56 deletions(-)

diff --git a/mount.c b/mount.c
index a7f1862f2747..fa01eeba7402 100644
--- a/mount.c
+++ b/mount.c
@@ -432,6 +432,33 @@ static char* mount_get_serial(char *dev)
 	return disc_id;
 }
 
+static void mount_dev_get_property(char *name, char *prop_val,
+				   const char *prop_name)
+{
+	char tmp[64];
+	FILE *fp;
+
+	snprintf(tmp, 64, "/sys/class/block/%s/device/%s", name, prop_name);
+	fp = fopen(tmp, "r");
+	if(!fp)
+	{
+		snprintf(tmp, 64, "/sys/block/%s/device/%s", name, prop_name);
+		fp = fopen(tmp, "r");
+	}
+	if(!fp)
+		goto unknown;
+	else {
+		if (!fgets(prop_val, 64, fp))
+			goto unknown;
+
+		prop_val[strlen(prop_val) - 1] = '\0';
+		fclose(fp);
+		return;
+	}
+unknown:
+	snprintf(prop_val, 64, "unknown");
+}
+
 static void mount_dev_add(char *dev)
 {
 	struct mount *mount = mount_find(0, dev);
@@ -489,62 +516,12 @@ static void mount_dev_add(char *dev)
 		}
 		strcpy(name, dev);
 		name[3] = '\0';
-		snprintf(tmp, 64, "/sys/class/block/%s/device/model", name);
-		fp = fopen(tmp, "r");
-		if(!fp)
-		{
-			snprintf(tmp, 64, "/sys/block/%s/device/model", name);
-			fp = fopen(tmp, "r");
-		}
-		if(!fp)
-			snprintf(model, 64, "unknown");
-		else {
-			fgets(model, 64, fp);
-			model[strlen(model) - 1] = '\0';;
-			fclose(fp);
-		}
-		snprintf(tmp, 64, "/sys/class/block/%s/device/vendor", name);
-		fp = fopen(tmp, "r");
-		if(!fp)
-		{
-			snprintf(tmp, 64, "/sys/block/%s/device/vendor", name);
-			fp = fopen(tmp, "r");
-		}
-		if(!fp)
-			snprintf(vendor, 64, "unknown");
-		else {
-			fgets(vendor, 64, fp);
-			vendor[strlen(vendor) - 1] = '\0';
-			fclose(fp);
-		}
-		snprintf(tmp, 64, "/sys/class/block/%s/device/rev", name);
-		fp = fopen(tmp, "r");
-		if(!fp)
-		{
-			snprintf(tmp, 64, "/sys/block/%s/device/rev", name);
-			fp = fopen(tmp, "r");
-		}
-		if(!fp)
-			snprintf(rev, 64, "unknown");
-		else {
-			fgets(rev, 64, fp);
-			rev[strlen(rev) - 1] = '\0';
-			fclose(fp);
-		}
-		snprintf(tmp, 64, "/sys/class/block/%s/size", dev);
-		fp = fopen(tmp, "r");
-		if(!fp)
-		{
-			snprintf(tmp, 64, "/sys/block/%s/%s/size", name, dev);
-			fp = fopen(tmp, "r");
-		}
-		if(!fp)
-			snprintf(size, 64, "unknown");
-		else {
-			fgets(size, 64, fp);
-			size[strlen(size) - 1] = '\0';
-			fclose(fp);
-		}
+
+		mount_dev_get_property(name, model, "model");
+		mount_dev_get_property(name, vendor, "vendor");
+		mount_dev_get_property(name, rev, "rev");
+		mount_dev_get_property(name, size, "size");
+
 		strcpy(tmp2, dev);
 		tmp2[3] = '\0';
 		snprintf(tmp, 64, "/sys/block/%s/queue/hw_sector_size", tmp2);
-- 
2.9.3




More information about the Lede-dev mailing list