Refactor file download mode 93/201093/1 tizen_5.0 accepted/tizen/unified/20190315.061232 submit/tizen/20190314.014942 submit/tizen_5.0/20190319.011148
authorDongwoo Lee <dwoo08.lee@samsung.com>
Fri, 8 Mar 2019 07:16:39 +0000 (16:16 +0900)
committerDongwoo Lee <dwoo08.lee@samsung.com>
Fri, 8 Mar 2019 07:22:56 +0000 (16:22 +0900)
To fix the problem of file download mode, and to improve error
handling of mount/unmount operation, this patch refactors file
download mode.

Change-Id: I2df4378bc00181632f1399833221a182a6c4407b
Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
src/dfu.c

index f761572..0131e42 100644 (file)
--- a/src/dfu.c
+++ b/src/dfu.c
@@ -97,22 +97,73 @@ int dfu_request_io(struct tfm_context *ctx, unsigned long len)
        return 0;
 }
 
-static void mount_dev(const char *dev, const char *fstype)
+static char *get_partition_fstype(const char *devname)
 {
+       blkid_tag_iterate tag_iter;
+       blkid_dev dev;
+       blkid_cache cache = NULL;
+       const char *type, *value;
        int ret;
 
+       ret = blkid_get_cache(&cache, NULL);
+       if (ret < 0)
+               return NULL;
+
+       blkid_probe_all(cache);
+
+       dev = blkid_get_dev(cache, devname, 0);
+       if (!dev)
+               return NULL;
+
+       dev = blkid_verify(cache, dev);
+       if (!dev)
+               return NULL;
+
+       tag_iter = blkid_tag_iterate_begin(dev);
+       while (blkid_tag_next(tag_iter, &type, &value) == 0) {
+               if (!strncmp(type, "TYPE", 4)) {
+                       char *fstype = strdup(value);
+
+                       if (!fstype)
+                               return NULL;
+
+                       blkid_tag_iterate_end(tag_iter);
+                       blkid_put_cache(cache);
+                       return fstype;
+               }
+       }
+       blkid_tag_iterate_end(tag_iter);
+       blkid_put_cache(cache);
+
+       return NULL;
+}
+
+static int mount_dev(const char *dev)
+{
+       int ret;
+       char *fstype;
+
        ret = mkdir(DFU_MOUNT_PATH,  0600);
        if (ret < 0) {
                fprintf(stderr, "Failed to create target directory\n");
-               exit(-1);
+               return ret;
+       }
+
+       fstype = get_partition_fstype(dev);
+       if (!fstype) {
+               fprintf(stderr, "failed to get partition filesystem type: %s\n", dev);
+               return -EINVAL;
        }
 
        ret = mount(dev, DFU_MOUNT_PATH, fstype, 0, NULL);
        if (ret < 0) {
                fprintf(stderr, "Failed to mount target partition\n");
                rmdir(DFU_MOUNT_PATH);
-               exit(-1);
        }
+
+       free(fstype);
+
+       return ret;
 }
 
 static void umount_dev(void)
@@ -120,14 +171,10 @@ static void umount_dev(void)
        int ret;
 
        ret = umount(DFU_MOUNT_PATH);
-       if (ret < 0) {
-               fprintf(stderr, "Failed to mount target partition\n");
-
-               /* umount is failed, but anyway try to delete mount point */
-               rmdir(DFU_MOUNT_PATH);
-               exit(-1);
-       }
+       if (ret < 0)
+               fprintf(stderr, "Failed to unmount target partition\n");
 
+       /* whether umount is succeed or not, continue to try deleting mount point */
        rmdir(DFU_MOUNT_PATH);
 }
 
@@ -158,44 +205,6 @@ void dfu_sync(struct tfm_context *ctx)
        fprintf(stdout, "finished\n");
 }
 
-static char *get_partition_fstype(const char *devname)
-{
-       blkid_tag_iterate tag_iter;
-       blkid_dev dev;
-       blkid_cache cache = NULL;
-       const char *type, *value;
-       int ret;
-
-       ret = blkid_get_cache(&cache, NULL);
-       if (ret < 0)
-               return NULL;
-
-       blkid_probe_all(cache);
-
-       dev = blkid_get_dev(cache, devname, 0);
-       if (!dev)
-               return NULL;
-
-       dev = blkid_verify(cache, dev);
-       if (!dev)
-               return NULL;
-
-       tag_iter = blkid_tag_iterate_begin(dev);
-       while (blkid_tag_next(tag_iter, &type, &value) == 0) {
-               if (!strncmp(type, "TYPE", 4)) {
-                       char *fstype = strdup(value);
-
-                       blkid_tag_iterate_end(tag_iter);
-                       blkid_put_cache(cache);
-                       return fstype;
-               }
-       }
-       blkid_tag_iterate_end(tag_iter);
-       blkid_put_cache(cache);
-
-       return NULL;
-}
-
 static int dfu_start_entity(struct tfm_context *ctx, int idx, unsigned long size)
 {
        char **info = dfu_info[idx];
@@ -210,23 +219,20 @@ static int dfu_start_entity(struct tfm_context *ctx, int idx, unsigned long size
                break;
        case DFU_INFO_MODE_FILE:
        {
+               int ret;
                int path_prefix = strlen(DFU_MOUNT_PATH);
                int path_suffix = strlen(info[DFU_INFO_PATH]);
                int path_name = strlen(info[DFU_INFO_NAME]);
-               char fstype;
 
-               fstype = get_partition_fstype(info[DFU_INFO_PARTITION]);
-               if (!fstype) {
-                       fprintf(stderr, "failed to get partition filesystem type: %s", info[DFU_INFO_PARTITION]);
+               ret = mount_dev(info[DFU_INFO_PARTITION]);
+               if (ret < 0)
                        return -EINVAL;
-               }
-
-               mount_dev(info[DFU_INFO_PARTITION], fstype);
-               free(fstype);
 
                file = malloc(path_prefix + path_suffix + path_name + 1);
-               if (!file)
+               if (!file) {
+                       umount_dev();
                        return -ENOMEM;
+               }
 
                strncpy(file, DFU_MOUNT_PATH, path_prefix + 1);
                strncat(file, info[DFU_INFO_PATH], path_suffix);