libfstools: make sure file is closed on error
[project/fstools.git] / block.c
diff --git a/block.c b/block.c
index b0ac52463c8fee4be17399bda8049781238d28b5..9f5cebf28f5adddbf42791fa9b6b00bff11b567b 100644 (file)
--- a/block.c
+++ b/block.c
@@ -483,19 +483,27 @@ static int config_load(char *cfg)
 
 static struct probe_info* _probe_path(char *path)
 {
-       struct probe_info *pr;
+       struct probe_info *pr, *epr;
        char tmppath[64];
 
-       /* skip ubi device if ubiblock device is present */
+       pr = probe_path(path);
+       if (!pr)
+               return NULL;
+
        if (path[5] == 'u' && path[6] == 'b' && path[7] == 'i' &&
            path[8] >= '0' && path[8] <= '9' ) {
+               /* skip ubi device if not UBIFS (as it requires ubiblock) */
+               if (strcmp("ubifs", pr->type))
+                       return NULL;
+
+               /* skip ubi device if ubiblock device is present */
                snprintf(tmppath, sizeof(tmppath), "/dev/ubiblock%s", path + 8);
-               list_for_each_entry(pr, &devices, list)
-                       if (!strcasecmp(pr->dev, tmppath))
+               list_for_each_entry(epr, &devices, list)
+                       if (!strcmp(epr->dev, tmppath))
                                return NULL;
        }
 
-       return probe_path(path);
+       return pr;
 }
 
 static int _cache_load(const char *path)
@@ -923,6 +931,8 @@ static int handle_mount(const char *source, const char *target,
                err = exec_mount(source, target, fstype, mount_opts);
        }
 
+       free(mount_opts);
+
        return err;
 }
 
@@ -1108,15 +1118,25 @@ static int mount_device(struct probe_info *pr, int type)
 
 static int umount_device(char *path, int type, bool all)
 {
-       char *mp;
+       char *mp, *devpath;
        int err;
 
-       mp = find_mount_point(path);
+       if (strlen(path) > 5 && !strncmp("/dev/", path, 5)) {
+               mp = find_mount_point(path);
+       } else {
+               devpath = malloc(strlen(path) + 6);
+               strcpy(devpath, "/dev/");
+               strcat(devpath, path);
+               mp = find_mount_point(devpath);
+               free(devpath);
+       }
+
        if (!mp)
                return -1;
-       if (!strcmp(mp, "/") && !all)
+       if (!strcmp(mp, "/") && !all) {
+               free(mp);
                return 0;
-
+       }
        if (type != TYPE_AUTOFS)
                blockd_notify("umount", basename(path), NULL, NULL);
 
@@ -1141,23 +1161,11 @@ static int mount_action(char *action, char *device, int type)
        if (!action || !device)
                return -1;
 
-       if (config_load(NULL))
-               return -1;
-
-       cache_load(1);
-
-       list_for_each_entry(pr, &devices, list)
-               if (!strcmp(basename(pr->dev), device))
-                       path = pr->dev;
-
-       if (!path)
-               return -1;
-
        if (!strcmp(action, "remove")) {
                if (type == TYPE_HOTPLUG)
                        blockd_notify("hotplug", device, NULL, NULL);
 
-               umount_device(path, type, true);
+               umount_device(device, type, true);
 
                return 0;
        } else if (strcmp(action, "add")) {
@@ -1166,6 +1174,18 @@ static int mount_action(char *action, char *device, int type)
                return -1;
        }
 
+       if (config_load(NULL))
+               return -1;
+
+       cache_load(1);
+
+       list_for_each_entry(pr, &devices, list)
+               if (!strcmp(basename(pr->dev), device))
+                       path = pr->dev;
+
+       if (!path)
+               return -1;
+
        return mount_device(find_block_info(NULL, NULL, path), type);
 }