block: remove mount target file if it's a link
[project/fstools.git] / block.c
diff --git a/block.c b/block.c
index 15caaba14549413accccd4ec005b9b9e2c9ef902..9e530f1a93b8e729cf9aeadfd508bd261928070a 100644 (file)
--- a/block.c
+++ b/block.c
@@ -575,23 +575,6 @@ static void cache_load(int mtd)
 }
 
 
-static int print_block_uci(struct probe_info *pr)
-{
-       if (!strcmp(pr->type, "swap")) {
-               printf("config 'swap'\n");
-       } else {
-               printf("config 'mount'\n");
-               printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr->dev));
-       }
-       if (pr->uuid)
-               printf("\toption\tuuid\t'%s'\n", pr->uuid);
-       else
-               printf("\toption\tdevice\t'%s'\n", pr->dev);
-       printf("\toption\tenabled\t'0'\n\n");
-
-       return 0;
-}
-
 static struct device* find_block_device(char *uuid, char *label, char *path)
 {
        struct device *dev;
@@ -704,6 +687,30 @@ static char* find_mount_point(char *block)
        return point;
 }
 
+static int print_block_uci(struct probe_info *pr)
+{
+       if (!strcmp(pr->type, "swap")) {
+               printf("config 'swap'\n");
+       } else {
+               char *mp = find_mount_point(pr->dev);
+
+               printf("config 'mount'\n");
+               if (mp) {
+                       printf("\toption\ttarget\t'%s'\n", mp);
+                       free(mp);
+               } else {
+                       printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr->dev));
+               }
+       }
+       if (pr->uuid)
+               printf("\toption\tuuid\t'%s'\n", pr->uuid);
+       else
+               printf("\toption\tdevice\t'%s'\n", pr->dev);
+       printf("\toption\tenabled\t'0'\n\n");
+
+       return 0;
+}
+
 static int print_block_info(struct probe_info *pr)
 {
        static char *mp;
@@ -1065,6 +1072,7 @@ static int mount_device(struct device *dev, int type)
 {
        struct mount *m;
        struct probe_info *pr;
+       struct stat st;
        char _target[32];
        char *target;
        char *device;
@@ -1087,17 +1095,21 @@ static int mount_device(struct device *dev, int type)
                return 0;
        }
 
-       mp = find_mount_point(pr->dev);
-       if (mp && (type != TYPE_HOTPLUG)) {
-               ULOG_ERR("%s is already mounted on %s\n", pr->dev, mp);
-               free(mp);
-               return -1;
-       }
-
        m = dev->m;
        if (m && m->extroot)
                return -1;
 
+       mp = find_mount_point(pr->dev);
+       if (mp) {
+               if (m && m->type == TYPE_MOUNT && strcmp(m->target, mp)) {
+                       ULOG_ERR("%s is already mounted on %s\n", pr->dev, mp);
+                       err = -1;
+               } else
+                       err = 0;
+               free(mp);
+               return err;
+       }
+
        if (type == TYPE_HOTPLUG)
                blockd_notify(device, m, pr);
 
@@ -1143,6 +1155,8 @@ static int mount_device(struct device *dev, int type)
                check_filesystem(pr);
 
        mkdir_p(target);
+       if (!lstat(target, &st) && S_ISLNK(st.st_mode))
+               unlink(target);
 
        err = handle_mount(pr->dev, target, pr->type, m);
        if (err) {
@@ -1532,7 +1546,7 @@ static int mount_extroot(char *cfg)
                ULOG_INFO("extroot: device not present, retrying in %u seconds\n", delay_root);
                sleep(delay_root);
                make_devs();
-               cache_load(0);
+               cache_load(1);
                dev = find_block_device(m->uuid, m->label, m->device);
        }
        if (dev) {