block: add type converting function for enum block_device_type 24/206124/2
authorsanghyeok.oh <sanghyeok.oh@samsung.com>
Tue, 14 May 2019 11:08:05 +0000 (20:08 +0900)
committersanghyeok.oh <sanghyeok.oh@samsung.com>
Tue, 21 May 2019 10:31:39 +0000 (19:31 +0900)
Change-Id: I04d09f8639b1604d543f9746cfc926a87dd084e0
Signed-off-by: sanghyeok.oh <sanghyeok.oh@samsung.com>
src/block/block.c
src/block/block.h

index cd0d250..f179834 100644 (file)
@@ -3241,6 +3241,22 @@ static GVariant *request_show_device_list(GDBusConnection *conn,
        show_block_device_list();
        return dbus_handle_new_g_variant_tuple();
 }
+
+static enum block_device_type get_bdev_type_from_type_string(const char *type_str)
+{
+       if (!type_str)
+               return BLOCK_UNKNOWN_DEV;
+
+       if (strcmp(type_str, BLOCK_TYPE_SCSI) == 0)
+               return BLOCK_SCSI_DEV;
+       if (strcmp(type_str, BLOCK_TYPE_MMC) == 0)
+               return BLOCK_MMC_DEV;
+       if (strcmp(type_str, BLOCK_TYPE_ALL) == 0)
+               return BLOCK_ALL_DEV;
+
+       return BLOCK_UNKNOWN_DEV;
+}
+
 // Called by MainThread
 static GVariant *request_get_device_list(GDBusConnection *conn,
                const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
@@ -3251,7 +3267,7 @@ static GVariant *request_get_device_list(GDBusConnection *conn,
        struct block_data *data;
        dd_list *elem;
        char *type = NULL;
-       int block_type;
+       enum block_device_type block_type;
        int i;
        int item_cnt = 0;
        GVariantBuilder *builder = NULL;
@@ -3266,13 +3282,8 @@ static GVariant *request_get_device_list(GDBusConnection *conn,
 
        _D("Block (%s) device list is requested.", type);
 
-       if (!strncmp(type, BLOCK_TYPE_SCSI, sizeof(BLOCK_TYPE_SCSI)))
-               block_type = BLOCK_SCSI_DEV;
-       else if (!strncmp(type, BLOCK_TYPE_MMC, sizeof(BLOCK_TYPE_MMC)))
-               block_type = BLOCK_MMC_DEV;
-       else if (!strncmp(type, BLOCK_TYPE_ALL, sizeof(BLOCK_TYPE_ALL)))
-               block_type = -1;
-       else {
+       block_type = get_bdev_type_from_type_string(type);
+       if (block_type == BLOCK_UNKNOWN_DEV) {
                _E("Invalid type (%s) is requested.", type);
                error = "Invalid type is requested";
                goto out;
@@ -3284,20 +3295,14 @@ static GVariant *request_get_device_list(GDBusConnection *conn,
        for (i = 0; i < THREAD_MAX; i++) {
                pthread_mutex_lock(&(th_manager[i].mutex));
                DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
-                       data = bdev->data;
-                       if (!data)
-                               continue;
-                       if (bdev->removed)
+                       if (!bdev->data || bdev->removed)
                                continue;
 
-                       switch (block_type) {
-                       case BLOCK_SCSI_DEV:
-                       case BLOCK_MMC_DEV:
+                       data = bdev->data;
+
+                       if (block_type != BLOCK_ALL_DEV) {
                                if (data->block_type != block_type)
                                        continue;
-                               break;
-                       default:
-                               break;
                        }
 
                        g_variant_builder_add(builder, "(issssssisibii)",
@@ -3350,14 +3355,13 @@ static GVariant *request_get_device_list_2(GDBusConnection *conn,
        struct block_data *data;
        dd_list *elem;
        char *type = NULL;
-       int block_type;
+       enum block_device_type block_type;
        int i;
        int item_cnt;
        GVariantBuilder *builder = NULL;
        const char *error = NULL;
 
        g_variant_get(param, "(s)", &type);
-
        if (!type) {
                _E("Delivered type is NULL.");
                error = "Delivered type is NULL";
@@ -3366,13 +3370,8 @@ static GVariant *request_get_device_list_2(GDBusConnection *conn,
 
        _D("Block (%s) device list is requested.", type);
 
-       if (!strncmp(type, BLOCK_TYPE_SCSI, sizeof(BLOCK_TYPE_SCSI)))
-               block_type = BLOCK_SCSI_DEV;
-       else if (!strncmp(type, BLOCK_TYPE_MMC, sizeof(BLOCK_TYPE_MMC)))
-               block_type = BLOCK_MMC_DEV;
-       else if (!strncmp(type, BLOCK_TYPE_ALL, sizeof(BLOCK_TYPE_ALL)))
-               block_type = -1;
-       else {
+       block_type = get_bdev_type_from_type_string(type);
+       if (block_type == BLOCK_UNKNOWN_DEV) {
                _E("Invalid type (%s) is requested.", type);
                error = "Invalid type is requested";
                goto out;
@@ -3384,20 +3383,14 @@ static GVariant *request_get_device_list_2(GDBusConnection *conn,
        for (i = 0; i < THREAD_MAX; i++) {
                pthread_mutex_lock(&(th_manager[i].mutex));
                DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
-                       data = bdev->data;
-                       if (!data)
-                               continue;
-                       if (bdev->removed)
+                       if (!bdev->data || bdev->removed)
                                continue;
 
-                       switch (block_type) {
-                       case BLOCK_SCSI_DEV:
-                       case BLOCK_MMC_DEV:
+                       data = bdev->data;
+
+                       if (block_type != BLOCK_ALL_DEV) {
                                if (data->block_type != block_type)
                                        continue;
-                               break;
-                       default:
-                               break;
                        }
 
                        g_variant_builder_add(builder, "(issssssisibi)",
index 658bf89..3736819 100644 (file)
@@ -53,9 +53,11 @@ void add_fs(const struct block_fs_ops *fs);
 void remove_fs(const struct block_fs_ops *fs);
 
 enum block_device_type {
-       BLOCK_SCSI_DEV,
+       BLOCK_UNKNOWN_DEV = -1,
+       BLOCK_SCSI_DEV = 0, /* Should be zero. used as index of array */
        BLOCK_MMC_DEV,
        BLOCK_EXTENDEDSD_DEV,
+       BLOCK_ALL_DEV,
 };
 
 enum mount_state {