From: Minje Ahn Date: Wed, 28 Aug 2019 05:51:54 +0000 (+0900) Subject: Improve msc_register_thread X-Git-Tag: accepted/tizen/unified/20190904.224442~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=26e916e54fae827e4cbff14dd43b271fcc389f60;p=platform%2Fcore%2Fmultimedia%2Fmedia-server.git Improve msc_register_thread Change-Id: I7f5938e6650b46d6afb7bac90ea84a5b651622f6 Signed-off-by: Minje Ahn --- diff --git a/src/scanner/include/media-scanner-scan.h b/src/scanner/include/media-scanner-scan.h index 24c2b97..f6e0d7c 100755 --- a/src/scanner/include/media-scanner-scan.h +++ b/src/scanner/include/media-scanner-scan.h @@ -41,7 +41,7 @@ void msc_metadata_update_thread(ms_comm_msg_s *recv_msg); int msc_init_scanner(void); int msc_deinit_scanner(void); int msc_push_scan_request(ms_scan_type_e scan_type, ms_comm_msg_s *recv_msg); -int msc_send_power_off_request(void); -int msc_remove_dir_scan_request(ms_comm_msg_s *recv_msg); +void msc_send_power_off_request(void); +void msc_remove_dir_scan_request(ms_comm_msg_s *recv_msg); #endif /*_MEDIA_SCANNER_SCAN_H_*/ diff --git a/src/scanner/media-scanner-scan.c b/src/scanner/media-scanner-scan.c index d743a79..c4759f0 100755 --- a/src/scanner/media-scanner-scan.c +++ b/src/scanner/media-scanner-scan.c @@ -36,7 +36,7 @@ GAsyncQueue *scan_queue; GAsyncQueue *reg_queue; GMutex scan_req_mutex; -static int __msc_clear_file_list(GArray *path_array); +static void __msc_clear_file_list(GArray *path_array); static bool __msc_is_stop_needed() { @@ -302,7 +302,7 @@ gboolean msc_directory_scan_thread(void *data) ms_trim_dir_path(scan_data->msg); is_recursive = (scan_data->msg_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) ? false : true; - if (!g_file_test(scan_data->msg, G_FILE_TEST_IS_DIR)) { + if (g_file_test(scan_data->msg, G_FILE_TEST_IS_DIR)) { if (ms_check_folder_exist(handle, scan_data->storage_id, scan_data->msg) == MS_MEDIA_ERR_NONE) { MS_DBG_WARN("[%.*s] already exist", MAX_MSG_SIZE, scan_data->msg); ret = __msc_directory_scan_update_and_delete(handle, scan_data, is_recursive, MS_ITEM_UPDATE); @@ -448,30 +448,13 @@ _POWEROFF: return FALSE; } -static void __msc_insert_register_request(GArray *register_array, ms_comm_msg_s *insert_data) +static void __msc_clear_file_list(GArray *path_array) { - MS_DBG_SLOG("path : %.*s", MAX_MSG_SIZE, insert_data->msg); + if (!path_array) + return; - if (insert_data->pid == POWEROFF) - g_array_prepend_val(register_array, insert_data); - else - g_array_append_val(register_array, insert_data); -} - -static int __msc_clear_file_list(GArray *path_array) -{ - if (path_array) { - while (path_array->len != 0) { - char *data = NULL; - data = g_array_index(path_array , char*, 0); - g_array_remove_index(path_array, 0); - MS_SAFE_FREE(data); - } - g_array_free(path_array, FALSE); - path_array = NULL; - } - - return MS_MEDIA_ERR_NONE; + g_array_set_clear_func(path_array, g_free); + g_array_free(path_array, TRUE); } static int __msc_make_file_list(char *file_path, GArray **path_array, uid_t uid) @@ -525,7 +508,6 @@ static int __msc_make_file_list(char *file_path, GArray **path_array, uid_t uid) return MS_MEDIA_ERR_NONE; FREE_RESOURCE: - __msc_clear_file_list(*path_array); if (fp) fclose(fp); @@ -536,174 +518,81 @@ FREE_RESOURCE: static int __msc_batch_insert(int pid, GArray *path_array, uid_t uid) { - int err; - unsigned int i; + int ret = MS_MEDIA_ERR_NONE; sqlite3 *handle = NULL; char *insert_path = NULL; char storage_id[MS_UUID_SIZE] = {0,}; + int i = 0; - /* connect to media db, if conneting is failed, db updating is stopped */ - err = ms_connect_db(&handle, uid); - if (err != MS_MEDIA_ERR_NONE) - return MS_MEDIA_ERR_DB_CONNECT_FAIL; + ret = ms_connect_db(&handle, uid); + MS_DBG_RETV_IF(ret != MS_MEDIA_ERR_NONE, MS_MEDIA_ERR_DB_CONNECT_FAIL); - /*start db updating */ - /*call for bundle commit*/ ms_batch_commit_enable(true, false, true, pid); - MS_DBG_WARN("BULK REGISTER START[%d]", pid); - - /* get the inserting file path from array and insert to db */ for (i = 0; i < path_array->len; i++) { insert_path = g_array_index(path_array, char*, i); - /* get storage list */ memset(storage_id, 0x0, MS_UUID_SIZE); - if (ms_get_storage_id(handle, insert_path, storage_id, uid) < 0) { + if (ms_get_storage_id(handle, insert_path, storage_id, uid) != MS_MEDIA_ERR_NONE) { MS_DBG_ERR("There is no storage id in media db"); continue; } - /* insert to db */ - err = ms_insert_item_batch(handle, storage_id, insert_path, uid); + ret = ms_insert_item_batch(handle, storage_id, insert_path, uid); if (__msc_is_stop_needed()) { - /*call for bundle commit*/ - ms_register_end(uid); + ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP; break; } } /*call for bundle commit*/ ms_batch_commit_disable(true, false, uid); + ms_disconnect_db(handle); - /*disconnect form media db*/ - if (handle) ms_disconnect_db(handle); - - return MS_MEDIA_ERR_NONE; -} - -static int __msc_pop_register_request(GArray *register_array, ms_comm_msg_s **register_data) -{ - int remain_request; - ms_comm_msg_s *insert_data = NULL; - - while (1) { - remain_request = g_async_queue_length(reg_queue); - - /*updating requests remain*/ - if (register_array->len != 0 && remain_request == 0) { - *register_data = g_array_index(register_array, ms_comm_msg_s*, 0); - g_array_remove_index(register_array, 0); - if (*register_data == NULL) - continue; - - break; - } else if (remain_request != 0) { - insert_data = g_async_queue_pop(reg_queue); - __msc_insert_register_request(register_array, insert_data); - continue; - } else if (register_array->len == 0 && remain_request == 0) { - /*Threre is no request, Wait until pushung new request*/ - insert_data = g_async_queue_pop(reg_queue); - __msc_insert_register_request(register_array, insert_data); - continue; - } - } - - if ((strlen((*register_data)->msg) <= 0) || (strlen((*register_data)->msg) >= MS_FILE_PATH_LEN_MAX)) { - MS_DBG_ERR("message size[%zu] is wrong", strlen((*register_data)->msg)); - return MS_MEDIA_ERR_INVALID_IPC_MESSAGE; - } - - return MS_MEDIA_ERR_NONE; - + return ret; } gboolean msc_register_thread(void *data) { + int ret = MS_MEDIA_ERR_NONE; ms_comm_msg_s *register_data = NULL; - GArray *register_array = NULL; GArray *path_array = NULL; - char *file_path = NULL; - int ret = MS_MEDIA_ERR_NONE; - int pid = 0; - ms_comm_msg_s *msg_data = NULL; - - /*create array for processing overlay data*/ - register_array = g_array_new(FALSE, FALSE, sizeof(ms_comm_msg_s *)); - if (register_array == NULL) { - MS_DBG_ERR("g_array_new error"); - return false; - } while (1) { - ret = __msc_pop_register_request(register_array, ®ister_data); - if (register_data == NULL) { - MS_DBG_ERR("register_data is NULL"); - continue; - } + register_data = g_async_queue_pop(reg_queue); if (register_data->pid == POWEROFF) { - MS_DBG_ERR("power off"); + MS_DBG_WARN("power off"); goto _POWEROFF; } - if (ret != MS_MEDIA_ERR_NONE) { - MS_DBG_ERR("__msc_pop_register_request failed [%d]", ret); - goto FREE_RESOURCE; - } - - /* check current request */ - pid = register_data->pid; - if (register_data->msg_type != MS_MSG_BULK_INSERT) { MS_DBG_ERR("wrong message type"); - goto FREE_RESOURCE; - } - - file_path = g_strdup(register_data->msg); - if (file_path == NULL) { - MS_DBG_ERR("file_path is NULL"); - goto FREE_RESOURCE; + ret = MS_MEDIA_ERR_INVALID_PARAMETER; + goto NEXT; } - ret = __msc_make_file_list(file_path, &path_array, register_data->uid); + ret = __msc_make_file_list(register_data->msg, &path_array, register_data->uid); if (ret != MS_MEDIA_ERR_NONE) { MS_DBG_ERR("__msc_make_file_list failed [%d]", ret); - goto FREE_RESOURCE; + goto NEXT; } - ret = __msc_batch_insert(pid, path_array, register_data->uid); + MS_DBG_SLOG("BULK REGISTER START [%.*s]", MAX_MSG_SIZE, register_data->msg); + ret = __msc_batch_insert(register_data->pid, path_array, register_data->uid); + __msc_clear_file_list(path_array); -FREE_RESOURCE: +NEXT: /*Active flush */ malloc_trim(0); - /* If register_files operation is stopped, there is no necessrty for sending result. */ msc_send_result(ret, register_data); - - MS_DBG_WARN("BULK REGISTER END [%d |%.*s]", ret, MAX_MSG_SIZE, register_data->msg); - - __msc_clear_file_list(path_array); - - MS_SAFE_FREE(file_path); MS_SAFE_FREE(register_data); + MS_DBG_WARN("BULK REGISTER END [%d]", ret); } /*thread while*/ _POWEROFF: - MS_SAFE_FREE(file_path); MS_SAFE_FREE(register_data); - if (register_array) { - while (register_array->len != 0) { - msg_data = g_array_index(register_array , ms_comm_msg_s*, 0); - g_array_remove_index(register_array, 0); - MS_SAFE_FREE(msg_data); - } - g_array_free(register_array, FALSE); - register_array = NULL; - } - - __msc_clear_file_list(path_array); return false; } @@ -785,7 +674,7 @@ int msc_push_scan_request(ms_scan_type_e scan_type, ms_comm_msg_s *recv_msg) return ret; } -int msc_send_power_off_request(void) +void msc_send_power_off_request(void) { ms_comm_msg_s *data = NULL; @@ -823,33 +712,29 @@ int msc_send_power_off_request(void) MS_DBG_ERR("memory allocation failed"); } } - - return MS_MEDIA_ERR_NONE; } -int msc_remove_dir_scan_request(ms_comm_msg_s *recv_msg) +void msc_remove_dir_scan_request(ms_comm_msg_s *recv_msg) { - char *cancel_path = recv_msg->msg; - int pid = recv_msg->pid; int i = 0; - int len = g_async_queue_length(scan_queue); ms_comm_msg_s *msg = NULL; GAsyncQueue *temp_scan_queue = NULL; + int queue_len = g_async_queue_length(scan_queue); - MS_DBG_WARN("scan_req_mutex is LOCKED"); - g_mutex_lock(&scan_req_mutex); - - if (len == 0) { + if (queue_len == 0) { MS_DBG_ERR("Request is not stacked"); - goto END_REMOVE_REQUEST; + return; } + MS_DBG_WARN("scan_req_mutex is LOCKED"); + g_mutex_lock(&scan_req_mutex); + temp_scan_queue = g_async_queue_new(); - for (i = 0; i < len; i++) { + for (i = 0; i < queue_len; i++) { /*create new queue to compare request*/ msg = g_async_queue_pop(scan_queue); - if ((strcmp(msg->msg, cancel_path) == 0) && (pid == msg->pid)) { + if ((g_strcmp0(msg->msg, recv_msg->msg) == 0) && (recv_msg->pid == msg->pid)) { MS_DBG("Find request. Remove it"); MS_SAFE_FREE(msg); } else { @@ -859,9 +744,6 @@ int msc_remove_dir_scan_request(ms_comm_msg_s *recv_msg) g_async_queue_unref(scan_queue); scan_queue = temp_scan_queue; -END_REMOVE_REQUEST: g_mutex_unlock(&scan_req_mutex); MS_DBG_WARN("scan_req_mutex is UNLOCKED"); - - return MS_MEDIA_ERR_NONE; }