Fix the problem of not getting the dev_list 25/306325/3
authorJiyong <jiyong.min@samsung.com>
Tue, 20 Feb 2024 06:43:42 +0000 (15:43 +0900)
committerJiyong <jiyong.min@samsung.com>
Wed, 21 Feb 2024 01:57:27 +0000 (10:57 +0900)
- The format of 'reply_var' is tuple, "(a(issssssisibii))".
  The 'iter' is initialized for tuple by g_variant_iter_init(&iter, reply_var).
  Thus, the 'iter' could not iterate the array.
  So g_variant_get_child_value() is added to get devices(array) from tuple.

Change-Id: Iae074445f4105342ec1b9347e2d4b240cd6b39ab

src/common/media-common-system.c

index 626a7bd..840c844 100644 (file)
@@ -218,9 +218,10 @@ void ms_sys_unset_device_block_event_cb(void)
 
 int ms_sys_get_device_list(GSList **dev_list)
 {
-       GDBusConnection *g_bus = NULL;
-       GError *error = NULL;
-       GVariant *reply_var = NULL;
+       g_autoptr(GDBusConnection) g_bus = NULL;
+       g_autoptr(GError) error = NULL;
+       g_autoptr(GVariant) reply_var = NULL;
+       g_autoptr(GVariant) devices_var = NULL;
        GVariantIter iter;
        GVariant *block_device = NULL;
        int state = 0;
@@ -231,7 +232,6 @@ int ms_sys_get_device_list(GSList **dev_list)
        g_bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
        if (!g_bus) {
                MS_DBG_ERR("Failed to connect to the g D-BUS daemon: %s", error->message);
-               g_error_free(error);
                return MS_MEDIA_ERR_INTERNAL;
        }
 
@@ -242,28 +242,25 @@ int ms_sys_get_device_list(GSList **dev_list)
                                                                                        BLOCK_DEVICE_METHOD,
                                                                                        g_variant_new("(s)", BLOCK_DEVICE_ALL),
                                                                                        NULL,
-                                                                                       G_DBUS_SEND_MESSAGE_FLAGS_NONE,
+                                                                                       G_DBUS_CALL_FLAGS_NONE,
                                                                                        -1,
                                                                                        NULL,
                                                                                        &error);
        if (!reply_var) {
                MS_DBG_ERR("Failed to get GVariant[%s]", error->message);
-               g_error_free(error);
-               g_object_unref(g_bus);
                return MS_MEDIA_ERR_INTERNAL;
        }
 
-       g_variant_iter_init(&iter, reply_var);
+       devices_var = g_variant_get_child_value(reply_var, 0);
+       if (g_variant_iter_init(&iter, devices_var) == 0) {
+               MS_DBG_INFO("There is no external storage mounted..");
+               return MS_MEDIA_ERR_NONE;
+       }
 
        while ((block_device = g_variant_iter_next_value(&iter))) {
-               if (g_variant_n_children(block_device) == 0) {
-                       MS_DBG_INFO("There is no external storage mounted..");
-                       g_variant_unref(block_device);
-                       break;
-               }
-
                g_variant_get_child(block_device, 9, "i", &state);
                if (state == 0) {
+                       MS_DBG_INFO("invalid state");
                        g_variant_unref(block_device);
                        continue;
                }
@@ -280,9 +277,6 @@ int ms_sys_get_device_list(GSList **dev_list)
                g_variant_unref(block_device);
        }
 
-       g_variant_unref(reply_var);
-       g_object_unref(g_bus);
-
        MS_DBG_FLEAVE();
 
        return MS_MEDIA_ERR_NONE;