[OpenWrt-Devel,fstools,2/2] blockd: remove symlink linkpath file if it's a dir or link
diff mbox series

Message ID 20191228210722.1407-3-zajec5@gmail.com
State Under Review
Delegated to: Rafał Miłecki
Headers show
  • block(d): remove remaining files if they exist
Related show

Commit Message

Rafał Miłecki Dec. 28, 2019, 9:07 p.m. UTC
From: Rafał Miłecki <rafal@milecki.pl>

Files like that can remain from using non-autofs mounting and can cause
mounting errors after switching to autofs:
blockd: failed to symlink /mnt/sda1->/tmp/run/blockd/sda1

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
 blockd.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff mbox series

diff --git a/blockd.c b/blockd.c
index e07530c..f977fb7 100644
--- a/blockd.c
+++ b/blockd.c
@@ -131,12 +131,19 @@  device_free(struct device *device)
 static void
 device_add(struct device *device)
+	struct stat st;
 	char path[64];
 	if (!device->autofs)
 	snprintf(path, sizeof(path), "/tmp/run/blockd/%s", device->name);
+	if (!lstat(device->target, &st)) {
+		if (S_ISLNK(st.st_mode))
+			unlink(device->target);
+		else if (S_ISDIR(st.st_mode))
+			rmdir(device->target);
+	}
 	if (symlink(path, device->target))
 		ULOG_ERR("failed to symlink %s->%s\n", device->target, path);