Improve ms_check_mounted_storage() 30/241030/9
authorMinje Ahn <minje.ahn@samsung.com>
Fri, 14 Aug 2020 02:22:06 +0000 (11:22 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Tue, 18 Aug 2020 04:23:11 +0000 (13:23 +0900)
Change-Id: I83612e75ed089bcf00e6ca2c185c731f7e5973fe
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
src/common/include/media-common-external-storage.h
src/common/media-common-external-storage.c
src/common/media-common-utils.c
src/server/media-server-device-block.c

index af3121368569aed5811f65fc073284332b5f9d74..c56b1205e32f49f6ca20c9094a0596c83dec5a48 100755 (executable)
 
 #include "media-common-types.h"
 
-int ms_get_stg_changed_event(void);
-
+#ifndef _USE_DEVICED_DBUS
 int ms_read_device_info(const char *root_path, char **device_uuid);
 int ms_write_device_info(const char *root_path, char *device_uuid);
-
 int ms_get_added_storage_path(const char *add_path, char **device_id);
+#endif
 
 #endif /*_MEDIA_SERVER_EXTERNAL_STORAGE_H_*/
index 0e36ca775417905e4c2443ea34bf11fa8c5bb607..a881ee8d9ec37cf77811ae965de36069298a72ce 100755 (executable)
  *
  */
 
-#include <stdio.h>
-#include <glib.h>
-
 #include "media-util.h"
-#include "media-server-ipc.h"
 #include "media-common-dbg.h"
-#include "media-common-utils.h"
 #include "media-common-db-svc.h"
 #include "media-common-external-storage.h"
 
-#include <tzplatform_config.h>
-
 #define DEVICE_INFO_FILE ".device_info_"
 
+#ifndef _USE_DEVICED_DBUS
 int ms_read_device_info(const char *root_path, char **device_uuid)
 {
        int ret = MS_MEDIA_ERR_NONE;
@@ -130,7 +124,6 @@ int ms_write_device_info(const char *root_path, char *device_uuid)
        return MS_MEDIA_ERR_NONE;
 }
 
-#ifndef _USE_DEVICED_DBUS
 int ms_get_added_storage_path(const char *add_path, char **device_id)
 {
        int ret = MS_MEDIA_ERR_NONE;
index 6aded3efa9492f28093a810ec1ee3cdf3686ecb1..d37b7bef908011349f2b5fb0e810ae33aad32ab2 100644 (file)
@@ -307,6 +307,8 @@ int ms_check_scan_ignore(char * path, uid_t uid)
        return ret;
 }
 
+#ifndef _USE_DEVICED_DBUS
+
 typedef struct storage_result {
        char *storage_path;
        bool result;
@@ -324,6 +326,7 @@ static void __ms_check_mount_status(usb_device_h usb_device, void *user_data)
        MS_DBG_SWARN("mount_path [%s]", mount_path);
        data->result = (g_strcmp0(mount_path, data->storage_path) == 0);
 }
+#endif
 
 bool ms_storage_mount_status(const char *start_path)
 {
index 1df9f8d0632c4bc7d2958d17ebc2e90a1a8a0443..b7cc7acd7d2a988e2941a7c81c84d0c354bc7ada 100644 (file)
@@ -209,39 +209,39 @@ static void __ms_usb_remove_event(const char *mount_path)
        char device_id[MS_UUID_SIZE] = {0,};
        uid_t uid = MEDIA_DEFAULT_UID;
 
-       if (mount_path != NULL) {
+       if (!mount_path)
+               return;
 
-               ms_connect_db(&handle, uid);
+       ms_connect_db(&handle, uid);
 
-               while (1) {
-                       memset(device_id, 0x0, sizeof(device_id));
-                       ret = ms_get_storage_id(handle, mount_path, device_id, uid);
+       while (1) {
+               memset(device_id, 0x0, sizeof(device_id));
+               ret = ms_get_storage_id(handle, mount_path, device_id, uid);
 
-                       MS_DBG_SWARN("removed path [%s %s]", mount_path, device_id);
+               MS_DBG_SWARN("removed path [%s %s]", mount_path, device_id);
 
-                       if (ret == MS_MEDIA_ERR_NONE) {
-                               ms_set_storage_validity(handle, device_id, 0, uid);
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("Device ID is INVALID");
+                       break;
+               }
 
-                               /* update storage_id table set validity=0 first */
-                               if (ms_validity_change_all_items(handle, device_id, false, uid) != MS_MEDIA_ERR_NONE)
-                                       MS_DBG_ERR("ms_validity_change_all_items failed");
-                               /* folder validity set 0 under the start_path in folder table*/
-                               if (ms_set_folder_validity(handle, device_id, mount_path, MS_INVALID, true, uid) != MS_MEDIA_ERR_NONE)
-                                       MS_DBG_ERR("set_folder_validity failed");
+               ms_set_storage_validity(handle, device_id, 0, uid);
 
-                               if (ms_get_scanner_status()) {
-                                       /*If the media scanner is running, the removed USB can be queueing in the job list.*/
-                                       /*So try to remove it.*/
-                                       ms_send_storage_otg_scan_request(mount_path, device_id, MS_SCAN_INVALID, uid);
-                               }
-                       } else {
-                               MS_DBG_ERR("Device ID is INVALID");
-                               break;
-                       }
+               /* update storage_id table set validity=0 first */
+               if (ms_validity_change_all_items(handle, device_id, false, uid) != MS_MEDIA_ERR_NONE)
+                       MS_DBG_ERR("ms_validity_change_all_items failed");
+               /* folder validity set 0 under the start_path in folder table*/
+               if (ms_set_folder_validity(handle, device_id, mount_path, MS_INVALID, true, uid) != MS_MEDIA_ERR_NONE)
+                       MS_DBG_ERR("set_folder_validity failed");
+
+               if (ms_get_scanner_status()) {
+                       /*If the media scanner is running, the removed USB can be queueing in the job list.*/
+                       /*So try to remove it.*/
+                       ms_send_storage_otg_scan_request(mount_path, device_id, MS_SCAN_INVALID, uid);
                }
-
-               ms_disconnect_db(handle);
        }
+
+       ms_disconnect_db(handle);
 }
 
 void ms_device_block_changed_cb(usb_device_h usb_device, char *action, void *user_data)
@@ -285,58 +285,16 @@ void ms_device_block_changed_cb(usb_device_h usb_device, char *action, void *use
        MS_DBG_WARN("usb_event_callback END[%s]", action);
 }
 
-static int __ms_check_mounted_storage_list(GArray **added_list)
+static void __msc_get_storage_path(usb_device_h usb_device, void *user_data)
 {
-       int ret = MS_MEDIA_ERR_NONE;
-       int count = 0;
-       int err = 0;
-       usb_device_list_h list;
-       usb_device_h device;
+       GPtrArray **res_list = (GPtrArray **)user_data;
        char *mount_path = NULL;
-       char *storage_path = NULL;
-
-       err = usb_device_get_device_list(USB_MASS_STORAGE, &list);
-       if (err == 0) {
-               if (*added_list == NULL)
-                       *added_list = g_array_new(FALSE, FALSE, sizeof(char*));
-
-               count = usb_device_list_get_count(list);
-               if (count > 0) {
-                       int i = 0;
-                       for (i = 0; i < count; i++) {
-                               if (i == 0) {
-                                       if (0 == usb_device_list_get_first(list, &device) && device != NULL) {
-                                               mount_path = usb_device_get_mountpath(device);
-                                               if (mount_path != NULL) {
-                                                       MS_DBG_SWARN("mount_path [%s]", mount_path);
-                                                       storage_path = g_strdup(mount_path);
-                                                       g_array_append_val(*added_list, storage_path);
-                                               }
-                                       } else {
-                                               MS_DBG_ERR(" FAILED TO usb_device_list_get_first");
-                                       }
-                               } else {
-                                       if (0 == usb_device_list_get_next(list, &device) && device != NULL) {
-                                               mount_path = usb_device_get_mountpath(device);
-                                               if (mount_path != NULL) {
-                                                       MS_DBG_SWARN("mount_path [%s]", mount_path);
-                                                       storage_path = g_strdup(mount_path);
-                                                       g_array_append_val(*added_list, storage_path);
-                                               }
-                                       } else {
-                                               MS_DBG_ERR(" FAILED TO usb_device_list_get_next");
-                                       }
-                               }
-                       }
-               }
 
-               usb_device_free_device_list(list);
-       } else {
-               MS_DBG_ERR("usb_device_get_device_list falied [%d]", err);
-               ret = MS_MEDIA_ERR_INTERNAL;
+       mount_path = usb_device_get_mountpath(usb_device);
+       if (mount_path) {
+               MS_DBG_SWARN("mount_path [%s]", mount_path);
+               g_ptr_array_add(*res_list, g_strdup(mount_path));
        }
-
-       return ret;
 }
 
 int ms_check_mounted_storage(uid_t uid)
@@ -344,7 +302,7 @@ int ms_check_mounted_storage(uid_t uid)
        int ret = MS_MEDIA_ERR_NONE;
        int count  = 0;
        int i = 0;
-       GArray *added_list = NULL;
+       GPtrArray *added_list = NULL;
        char *mounted_path = NULL;
        char *storage_id = NULL;
        ms_dir_scan_type_t scan_type = MS_SCAN_ALL;
@@ -352,100 +310,95 @@ int ms_check_mounted_storage(uid_t uid)
 
        ms_connect_db(&handle, uid);
 
-       ret = __ms_check_mounted_storage_list(&added_list);
-       if (ret != MS_MEDIA_ERR_NONE)
-               MS_DBG_ERR("__ms_check_mounted_storage_list failed");
-
-       if (added_list != NULL) {
-               count = added_list->len;
-
-               for (i = 0; i < count; i++) {
-                       mounted_path = g_array_index(added_list, char *, i);
-                       /* read deive information */
-                       ret = ms_read_device_info(mounted_path, &storage_id);
-
-                       if (storage_id == NULL) {
-                               /* in this case, all new storage */
-                               char *device_uuid = NULL;
-                               ret = ms_genarate_uuid(&device_uuid);
-                               ret = ms_write_device_info(mounted_path, device_uuid);
-                               if (ret != MS_MEDIA_ERR_NONE) {
-                                       MS_DBG_ERR("ms_write_device_info failed");
-                                       MS_SAFE_FREE(device_uuid);
-                                       continue;
-                               }
-                               ret = ms_insert_storage(handle, device_uuid, mounted_path, uid);
+       added_list = g_ptr_array_new_with_free_func(g_free);
 
-                               scan_type = MS_SCAN_ALL;
-                               storage_id = g_strdup(device_uuid);
+       usb_mass_storage_foreach(__msc_get_storage_path, &added_list);
+
+       count = added_list->len;
 
+       for (i = 0; i < count; i++) {
+               mounted_path = g_ptr_array_index(added_list, i);
+               /* read deive information */
+               ret = ms_read_device_info(mounted_path, &storage_id);
+
+               if (!storage_id) {
+                       /* in this case, all new storage */
+                       char *device_uuid = NULL;
+                       ret = ms_genarate_uuid(&device_uuid);
+                       ret = ms_write_device_info(mounted_path, device_uuid);
+                       if (ret != MS_MEDIA_ERR_NONE) {
+                               MS_DBG_ERR("ms_write_device_info failed");
                                MS_SAFE_FREE(device_uuid);
-                       } else {
-                               /* update storage information into media DB */
-                               int validity = 0;
-                               char *db_storage_path = NULL;
-
-                               ret = ms_check_storage(handle, storage_id, &db_storage_path, &validity, uid);
-                               if (ret == 0) {
-                                       if (validity == 1) {
-                                               MS_DBG_ERR("This storage is already updated.");
-                                               g_free(db_storage_path);
-                                               continue;
-                                       }
+                               continue;
+                       }
+                       ret = ms_insert_storage(handle, device_uuid, mounted_path, uid);
+
+                       scan_type = MS_SCAN_ALL;
+                       storage_id = g_strdup(device_uuid);
+
+                       MS_SAFE_FREE(device_uuid);
+               } else {
+                       /* update storage information into media DB */
+                       int validity = 0;
+                       char *db_storage_path = NULL;
+
+                       ret = ms_check_storage(handle, storage_id, &db_storage_path, &validity, uid);
+                       if (ret == 0) {
+                               if (validity == 1) {
+                                       MS_DBG_ERR("This storage is already updated.");
+                                       g_free(db_storage_path);
+                                       g_free(storage_id);
+                                       storage_id = NULL;
+                                       continue;
+                               }
 
-                                       if (db_storage_path != NULL) {
-                                               MS_DBG_SWARN("%s %s", mounted_path, db_storage_path);
-                                               if (strcmp(mounted_path, db_storage_path)) {
-                                                       /* update storage path */
-                                                       MS_DBG_SWARN("update storage from %s to %s", db_storage_path, mounted_path);
-                                                       ret = ms_update_storage(handle, storage_id, mounted_path, uid);
-                                               }
-
-                                               ms_set_storage_validity(handle, storage_id, 1, uid);
-                                               /* update storage_id table set validity=0 first */
-                                               ms_validity_change_all_items(handle, storage_id, false, uid);
-                                               ms_set_folder_validity(handle, storage_id, mounted_path, MS_INVALID, true, uid);
-                                               /* update folder table set scan_status=0 first */
-                                               ms_set_folder_scan_status(handle, storage_id, NULL, MS_DIR_SCAN_NONE, uid);
-
-                                               scan_type = MS_SCAN_PART;
-                                               g_free(db_storage_path);
-                                       } else {
-                                               /* there is no information of this storage in Media DB */
-                                               MS_DBG_SWARN("insert storage %s", mounted_path);
-                                               ret = ms_insert_storage(handle, storage_id, mounted_path, uid);
-                                               scan_type = MS_SCAN_ALL;
+                               if (db_storage_path) {
+                                       MS_DBG_SWARN("%s %s", mounted_path, db_storage_path);
+                                       if (strcmp(mounted_path, db_storage_path)) {
+                                               /* update storage path */
+                                               MS_DBG_SWARN("update storage from %s to %s", db_storage_path, mounted_path);
+                                               ret = ms_update_storage(handle, storage_id, mounted_path, uid);
                                        }
+
+                                       ms_set_storage_validity(handle, storage_id, 1, uid);
+                                       /* update storage_id table set validity=0 first */
+                                       ms_validity_change_all_items(handle, storage_id, false, uid);
+                                       ms_set_folder_validity(handle, storage_id, mounted_path, MS_INVALID, true, uid);
+                                       /* update folder table set scan_status=0 first */
+                                       ms_set_folder_scan_status(handle, storage_id, NULL, MS_DIR_SCAN_NONE, uid);
+
+                                       scan_type = MS_SCAN_PART;
+                                       g_free(db_storage_path);
                                } else {
                                        /* there is no information of this storage in Media DB */
                                        MS_DBG_SWARN("insert storage %s", mounted_path);
                                        ret = ms_insert_storage(handle, storage_id, mounted_path, uid);
                                        scan_type = MS_SCAN_ALL;
                                }
+                       } else {
+                               /* there is no information of this storage in Media DB */
+                               MS_DBG_SWARN("insert storage %s", mounted_path);
+                               ret = ms_insert_storage(handle, storage_id, mounted_path, uid);
+                               scan_type = MS_SCAN_ALL;
                        }
-
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               MS_DBG_ERR("error : ms_update_storage or ms_insert_storage failed");
-
-                       /* request to update media db */
-                       ret = ms_insert_folder(handle, storage_id, mounted_path, uid);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               MS_DBG_ERR("error : ms_insert_folder failed");
-
-                       ms_send_storage_otg_scan_request(mounted_path, storage_id, scan_type, uid);
-                       g_free(storage_id);
                }
 
-               for (i = 0; i < count; i++) {
-                       mounted_path = g_array_index(added_list, char*, 0);
-                       g_free(mounted_path);
-                       g_array_remove_index(added_list, 0);
-               }
+               if (ret != MS_MEDIA_ERR_NONE)
+                       MS_DBG_ERR("error : ms_update_storage or ms_insert_storage failed");
 
-               g_array_free(added_list, FALSE);
-               added_list = NULL;
+               /* request to update media db */
+               ret = ms_insert_folder(handle, storage_id, mounted_path, uid);
+               if (ret != MS_MEDIA_ERR_NONE)
+                       MS_DBG_ERR("error : ms_insert_folder failed");
+
+               ms_send_storage_otg_scan_request(mounted_path, storage_id, scan_type, uid);
+               g_free(storage_id);
+               storage_id = NULL;
        }
 
+       g_ptr_array_free(added_list, TRUE);
+       added_list = NULL;
+
        /*disconnect form media db*/
        ms_disconnect_db(handle);