blockd: create mountpoint parent folder if needed
authorDaniel Golle <daniel@makrotopia.org>
Sat, 17 Jul 2021 22:13:27 +0000 (23:13 +0100)
committerDaniel Golle <daniel@makrotopia.org>
Sun, 18 Jul 2021 02:38:26 +0000 (03:38 +0100)
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
blockd.c

index 58b33e327e6e91147c1631718b538236b80992e7..9bb8ab25c47e80605ce7648c95a0919a4ee7faf3 100644 (file)
--- a/blockd.c
+++ b/blockd.c
@@ -209,7 +209,7 @@ static void device_mount_remove(struct ubus_context *ctx, struct device *device)
 static void device_mount_add(struct ubus_context *ctx, struct device *device)
 {
        struct stat st;
-       char *path;
+       char *path, *tmp;
 
        if (asprintf(&path, "/tmp/run/blockd/%s", device->name) == -1)
                exit(ENOMEM);
@@ -220,6 +220,14 @@ static void device_mount_add(struct ubus_context *ctx, struct device *device)
                else if (S_ISDIR(st.st_mode))
                        rmdir(device->target);
        }
+
+       tmp = strrchr(device->target, '/');
+       if (tmp && tmp != device->target && tmp != &device->target[strlen(path)-1]) {
+               *tmp = '\0';
+               mkdir_p(device->target, 0755);
+               *tmp = '/';
+       }
+
        if (symlink(path, device->target))
                ULOG_ERR("failed to symlink %s->%s (%d) - %m\n", device->target, path, errno);
        else