Improve msc_storage_scan_thread() 32/212832/6
authorMinje Ahn <minje.ahn@samsung.com>
Tue, 27 Aug 2019 06:42:36 +0000 (15:42 +0900)
committerhj kim <backto.kim@samsung.com>
Fri, 30 Aug 2019 00:34:54 +0000 (00:34 +0000)
Change-Id: I3a50e558b598b9e6a7230b8a5b3896a29e7c1ca0
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
src/scanner/media-scanner-scan.c

index a60a88c..ea30c9a 100755 (executable)
@@ -337,16 +337,63 @@ _POWEROFF:
        return false;
 }
 
-gboolean msc_storage_scan_thread(void *data)
+static int __msc_storage_scan_partial(ms_comm_msg_s *scan_data, ms_user_storage_type_e storage_type)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       sqlite3 *handle = NULL;
+
+       ret = ms_connect_db(&handle, scan_data->uid);
+       MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "ms_connect_db failed");
+
+       ms_set_db_status(MS_DB_UPDATING, storage_type);
+
+       ret = ms_validity_change_all_items(handle, scan_data->storage_id, false, scan_data->uid);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_validity_change_all_items failed");
+               ms_disconnect_db(handle);
+
+               return ret;
+       }
+
+       ms_batch_commit_enable(true, true, false, 0);
+       ret = __msc_db_update(handle, scan_data->storage_id, scan_data);
+       ms_batch_commit_disable(true, true, scan_data->uid);
+
+       if (ms_delete_invalid_items(handle, scan_data->storage_id, scan_data->uid) != MS_MEDIA_ERR_NONE)
+               MS_DBG_ERR("deleting invalid items in storage failed");
+
+       if (ms_delete_invalid_folder(scan_data->storage_id, scan_data->uid) != MS_MEDIA_ERR_NONE)
+               MS_DBG_ERR("deleting invalid folders in storage failed");
+
+       ms_disconnect_db(handle);
+
+       return ret;
+}
+
+static int __msc_storage_scan_all(ms_comm_msg_s *scan_data, ms_user_storage_type_e storage_type)
 {
-       ms_comm_msg_s *scan_data = NULL;
        int ret = MS_MEDIA_ERR_NONE;
        sqlite3 *handle = NULL;
+
+       ret = ms_connect_db(&handle, scan_data->uid);
+       MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "ms_connect_db failed");
+
+       ms_set_db_status(MS_DB_UPDATING, storage_type);
+
+       ms_batch_commit_enable(true, false, false, 0);
+       ret = __msc_db_update(handle, scan_data->storage_id, scan_data);
+       ms_batch_commit_disable(true, false, scan_data->uid);
+
+       ms_disconnect_db(handle);
+
+       return ret;
+}
+
+gboolean msc_storage_scan_thread(void *data)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       ms_comm_msg_s *scan_data = NULL;
        ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL;
-       int scan_type;
-       bool valid_status = true;
-       char *update_path = NULL;
-       char *storage_id = NULL;
 
        while (1) {
                scan_data = g_async_queue_pop(storage_queue);
@@ -357,101 +404,51 @@ gboolean msc_storage_scan_thread(void *data)
 
                MS_DBG_WARN("STORAGE SCAN START [%.*s][%s]", MAX_MSG_SIZE, scan_data->msg, scan_data->storage_id);
 
-               scan_type = scan_data->msg_type;
-               if (scan_type != MS_MSG_STORAGE_ALL
-                       && scan_type != MS_MSG_STORAGE_PARTIAL
-                       && scan_type != MS_MSG_STORAGE_INVALID) {
-                       MS_DBG_ERR("Invalid request[%d]", scan_type);
+               if (strlen(scan_data->storage_id) == 0) {
+                       MS_DBG_ERR("storage_id length is 0");
                        ret = MS_MEDIA_ERR_INVALID_PARAMETER;
                        goto NEXT;
                }
 
-               ret = ms_connect_db(&handle, scan_data->uid);
-               if (ret != MS_MEDIA_ERR_NONE) {
-                       MS_DBG_ERR("ms_connect_db failed");
-                       goto NEXT;
-               }
-
                ret = ms_user_get_storage_type(scan_data->uid, scan_data->msg, &storage_type);
                if (ret != MS_MEDIA_ERR_NONE) {
                        MS_DBG_ERR("ms_user_get_storage_type failed");
                        goto NEXT;
                }
-               update_path = g_strdup(scan_data->msg);
 
-               if (strlen(scan_data->storage_id) > 0) {
-                       storage_id = g_strdup(scan_data->storage_id);
-                       if (storage_id == NULL) {
-                               MS_DBG_ERR("storage_id NULL");
-                               ret = MS_MEDIA_ERR_INVALID_PARAMETER;
-                               goto NEXT;
-                       }
+               if (scan_data->msg_type == MS_MSG_STORAGE_PARTIAL) {
+                       ret = __msc_storage_scan_partial(scan_data, storage_type);
+               } else if (scan_data->msg_type == MS_MSG_STORAGE_ALL || scan_data->msg_type == MS_MSG_STORAGE_INVALID) {
+                       ret = __msc_storage_scan_all(scan_data, storage_type);
                } else {
-                       MS_DBG_ERR("storage_id length is 0. There is no information of your request [%.*s]", MAX_MSG_SIZE, scan_data->msg);
+                       MS_DBG_ERR("Invalid request[%d]", scan_data->msg_type);
                        ret = MS_MEDIA_ERR_INVALID_PARAMETER;
                        goto NEXT;
                }
 
-               /*start db updating */
-               ms_set_db_status(MS_DB_UPDATING, storage_type);
-
-               valid_status = (scan_type == MS_MSG_STORAGE_PARTIAL) ? true : false;
-               ms_batch_commit_enable(true, valid_status, false, 0);
-
-               if (scan_type == MS_MSG_STORAGE_PARTIAL) {
-                       ret = ms_validity_change_all_items(handle, storage_id, false, scan_data->uid);
-                       if (ret != MS_MEDIA_ERR_NONE) {
-                               MS_DBG_ERR("ms_validity_change_all_items failed");
-                               goto NEXT;
-                       }
-               }
-
-               ret = __msc_db_update(handle, storage_id, scan_data);
-
-               /*call for bundle commit*/
-               ms_batch_commit_disable(true, valid_status, scan_data->uid);
-
-               if (scan_type == MS_MSG_STORAGE_PARTIAL && ret == MS_MEDIA_ERR_NONE) {
-                       if (ms_delete_invalid_items(handle, storage_id, scan_data->uid) != MS_MEDIA_ERR_NONE)
-                               MS_DBG_ERR("deleting invalid items in storage failed");
-
-                       if (ms_delete_invalid_folder(storage_id, scan_data->uid) != MS_MEDIA_ERR_NONE)
-                               MS_DBG_ERR("deleting invalid folders in storage failed");
-               }
-
                /* send notification */
-               ms_send_dir_update_noti(update_path, NULL, MS_ITEM_UPDATE, scan_data->pid);
+               ms_send_dir_update_noti(scan_data->msg, NULL, MS_ITEM_UPDATE, scan_data->pid);
 
                if (ret == MS_MEDIA_ERR_SCANNER_FORCE_STOP)
                        ms_set_db_status(MS_DB_STOPPED, storage_type);
                else
                        ms_set_db_status(MS_DB_UPDATED, storage_type);
-
 NEXT:
-               MS_SAFE_FREE(update_path);
-               MS_SAFE_FREE(storage_id);
-
                if (__msc_is_stop_needed())
                        goto _POWEROFF;
 
-               /*disconnect from media db*/
-               if (handle) ms_disconnect_db(handle);
-
                /*Active flush */
                malloc_trim(0);
 
                msc_send_result(ret, scan_data);
-
                MS_SAFE_FREE(scan_data);
 
                MS_DBG_WARN("STORAGE SCAN END[%d]", ret);
        }                       /*thread while*/
-
 _POWEROFF:
        MS_SAFE_FREE(scan_data);
-       if (handle) ms_disconnect_db(handle);
 
-       return false;
+       return FALSE;
 }
 
 static void __msc_insert_register_request(GArray *register_array, ms_comm_msg_s *insert_data)