Improve ms_storage_mount_status() 29/241029/4
authorMinje Ahn <minje.ahn@samsung.com>
Fri, 14 Aug 2020 02:20:53 +0000 (11:20 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Tue, 18 Aug 2020 04:02:27 +0000 (13:02 +0900)
Change-Id: Icaada27e87364d3a9f61f64ee831643193fb4b92
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
src/common/media-common-utils.c

index cb69953..6aded3e 100644 (file)
@@ -307,71 +307,46 @@ int ms_check_scan_ignore(char * path, uid_t uid)
        return ret;
 }
 
-bool ms_storage_mount_status(const char* start_path)
+typedef struct storage_result {
+       char *storage_path;
+       bool result;
+} storage_result_s;
+
+static void __ms_check_mount_status(usb_device_h usb_device, void *user_data)
 {
-       bool ret = false;
-#ifndef _USE_DEVICED_DBUS
-       int count = 0;
-       int err = 0;
-       usb_device_list_h list;
-       usb_device_h device;
+       storage_result_s *data = (storage_result_s *)user_data;
        char *mount_path = NULL;
 
-       char *storage_path = NULL;
+       mount_path = usb_device_get_mountpath(usb_device);
+       if (!mount_path)
+               return;
+
+       MS_DBG_SWARN("mount_path [%s]", mount_path);
+       data->result = (g_strcmp0(mount_path, data->storage_path) == 0);
+}
+
+bool ms_storage_mount_status(const char *start_path)
+{
+       bool ret = false;
+#ifndef _USE_DEVICED_DBUS
+       storage_result_s res = {0, };
        char *remain_path = NULL;
        int remain_len = 0;
 
-       remain_path = strstr(start_path+strlen(MEDIA_ROOT_PATH_USB) +1, "/");
+       remain_path = strstr(start_path + strlen(MEDIA_ROOT_PATH_USB) + 1, "/");
        if (remain_path != NULL)
                remain_len = strlen(remain_path);
 
-       storage_path = g_strndup(start_path, strlen(start_path) - remain_len);
-
-       MS_DBG_SWARN("storage_path [%s]", storage_path);
-
-       err = usb_device_get_device_list(USB_MASS_STORAGE, &list);
-       if (err == 0) {
-               count = usb_device_list_get_count(list);
-               if (count > 0) {
-                       err = usb_device_list_get_first(list, &device);
-                       if (err != USB_ERROR_LIST_FAILED_TO_GET && device != NULL) {
-                               mount_path = usb_device_get_mountpath(device);
-                               if (mount_path != NULL) {
-                                       MS_DBG_SWARN("mount_path [%s]", mount_path);
-                                       if (strlen(mount_path) == strlen(storage_path)) {
-                                               if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
-                                                       MS_DBG_SWARN("start path is mounted [%s]", start_path);
-                                                       ret = true;
-                                               }
-                                       }
-                               }
-                       }
+       res.storage_path = g_strndup(start_path, strlen(start_path) - remain_len);
 
-                       if (ret != true) {
-                               while (usb_device_list_get_next(list, &device) == 0) {
-                                       if (device != NULL) {
-                                               mount_path = usb_device_get_mountpath(device);
-                                               if (mount_path != NULL) {
-                                                       MS_DBG_SWARN("mount_path [%s]", mount_path);
-                                                       if (strlen(mount_path) == strlen(storage_path)) {
-                                                               if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
-                                                                       MS_DBG_SWARN("start path is mounted [%s]", start_path);
-                                                                       ret = true;
-                                                                       break;
-                                                               }
-                                                       }
-                                               }
-                                       }
-                               }
-                       }
-               }
+       MS_DBG_SWARN("storage_path [%s]", res.storage_path);
 
-               usb_device_free_device_list(list);
-       } else {
-               MS_DBG_ERR("usb_device_get_device_list falied [%d]", err);
-       }
+       usb_mass_storage_foreach(__ms_check_mount_status, &res);
+       g_free(res.storage_path);
+       ret = res.result;
 
-       g_free(storage_path);
+       if (ret)
+               MS_DBG_SWARN("start path is mounted [%s]", start_path);
 #endif
        return ret;
 }