diff mbox series

[OpenWrt-Devel,fstools,4/4] block: don't duplicate mounting code in the mount_device()

Message ID 20181130130923.4678-4-zajec5@gmail.com
State Accepted
Delegated to: Rafał Miłecki
Headers show
Series [OpenWrt-Devel,fstools,1/4] block: fix formatting & indent in the mount_device() | expand

Commit Message

Rafał Miłecki Nov. 30, 2018, 1:09 p.m. UTC
From: Rafał Miłecki <rafal@milecki.pl>

Once target directory gets specified mounting code is identical for
devices having and not having UCI config entry. Share it.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
---
 block.c | 49 ++++++++++++++++++++-----------------------------
 1 file changed, 20 insertions(+), 29 deletions(-)
diff mbox series

Patch

diff --git a/block.c b/block.c
index 0671aca..a356315 100644
--- a/block.c
+++ b/block.c
@@ -992,8 +992,11 @@  static void blockd_notify(char *device, struct mount *m, struct probe_info *pr)
 static int mount_device(struct probe_info *pr, int type)
 {
 	struct mount *m;
+	char _target[32];
+	char *target;
 	char *device;
 	char *mp;
+	int err;
 
 	if (!pr)
 		return -1;
@@ -1024,11 +1027,8 @@  static int mount_device(struct probe_info *pr, int type)
 	if (type == TYPE_HOTPLUG)
 		blockd_notify(device, m, pr);
 
+	/* Check if device should be mounted & set the target directory */
 	if (m) {
-		char _target[32];
-		char *target;
-		int err = 0;
-
 		switch (type) {
 		case TYPE_HOTPLUG:
 			if (m->autofs)
@@ -1055,39 +1055,30 @@  static int mount_device(struct probe_info *pr, int type)
 			snprintf(_target, sizeof(_target), "/mnt/%s", device);
 			target = _target;
 		}
-		mkdir_p(target);
-
-		if (check_fs)
-			check_filesystem(pr);
-
-		err = handle_mount(pr->dev, target, pr->type, m);
-		if (err)
-			ULOG_ERR("mounting %s (%s) as %s failed (%d) - %m\n",
-			         pr->dev, pr->type, target, errno);
-		else
-			handle_swapfiles(true);
-		return err;
+	} else if (anon_mount) {
+		snprintf(_target, sizeof(_target), "/mnt/%s", device);
+		target = _target;
+	} else {
+		/* No reason to mount this device */
+		return 0;
 	}
 
-	if (anon_mount) {
-		char target[32];
-		int err = 0;
+	/* Mount the device */
 
-		snprintf(target, sizeof(target), "/mnt/%s", device);
-		mkdir_p(target);
+	if (check_fs)
+		check_filesystem(pr);
 
-		if (check_fs)
-			check_filesystem(pr);
+	mkdir_p(target);
 
-		err = handle_mount(pr->dev, target, pr->type, NULL);
-		if (err)
-			ULOG_ERR("mounting %s (%s) as %s failed (%d) - %m\n",
-			         pr->dev, pr->type, target, errno);
-		else
-			handle_swapfiles(true);
+	err = handle_mount(pr->dev, target, pr->type, m);
+	if (err) {
+		ULOG_ERR("mounting %s (%s) as %s failed (%d) - %m\n",
+				pr->dev, pr->type, target, errno);
 		return err;
 	}
 
+	handle_swapfiles(true);
+
 	return 0;
 }