static GMutex scan_req_mutex;
-typedef struct media_callback_data {
- scan_complete_cb user_callback;
- void *user_data;
- char *sock_path;
-} media_callback_data;
-
typedef struct media_scan_data {
- GIOChannel *src;
- media_callback_data *cb_data;
+ GIOChannel *channel;
+ scan_complete_cb user_callback;
int pid;
guint source_id;
char *req_path;
+ void *user_data;
} media_scan_data;
-static GArray *req_list;
+static GSList *req_list;
-static int _check_dir_path(const char *dir_path, uid_t uid)
+static int __check_dir_path(const char *dir_path, uid_t uid)
{
int ret = MS_MEDIA_ERR_NONE;
ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL;
return MS_MEDIA_ERR_NONE;
}
+static gint __find_req_item(gconstpointer data, gconstpointer compare)
+{
+ return g_strcmp0(((media_scan_data *)data)->req_path, (const char *)compare);
+}
+
/* receive message from media-server[function : ms_receive_message_from_scanner] */
-gboolean _read_socket(GIOChannel *src, GIOCondition condition, gpointer data)
+static gboolean __read_socket(GIOChannel *channel, GIOCondition condition, gpointer data)
{
- scan_complete_cb user_callback;
- void *user_data = NULL;
- ms_comm_msg_s recv_msg;
- media_request_result_s req_result;
+ ms_comm_msg_s recv_msg = {0, };
+ media_request_result_s req_result = {0, };
int sockfd = -1;
- char *sock_path = NULL;
- int recv_msg_size = 0;
media_scan_data *req_data = NULL;
- bool flag = false;
- guint source_id = 0;
+ GSList *found = NULL;
- sockfd = g_io_channel_unix_get_fd(src);
+ sockfd = g_io_channel_unix_get_fd(channel);
if (sockfd < 0) {
MSAPI_DBG("sock fd is invalid!");
- return G_SOURCE_CONTINUE;
+ return G_SOURCE_REMOVE;
}
- memset(&recv_msg, 0x0, sizeof(ms_comm_msg_s));
- memset(&req_result, 0x0, sizeof(media_request_result_s));
-
- if ((recv_msg_size = read(sockfd, &recv_msg, sizeof(ms_comm_msg_s))) < 0) {
+ if (read(sockfd, &recv_msg, sizeof(ms_comm_msg_s)) < 0) {
MSAPI_DBG_STRERROR("recv failed");
return G_SOURCE_REMOVE;
}
MSAPI_RETVM_IF(!ms_ipc_is_valid_msg(recv_msg.msg), G_SOURCE_REMOVE, "Invalid msg");
- req_result.complete_path = g_strdup(recv_msg.msg);
- req_result.pid = recv_msg.pid;
- req_result.result = recv_msg.result;
-
- MSAPI_DBG("complete_path :%s", req_result.complete_path);
-
switch (recv_msg.msg_type) {
case MS_MSG_SCANNER_RESULT:
req_result.request_type = MEDIA_DIRECTORY_SCAN;
break;
default:
MSAPI_DBG("Invalid msg_type[%d]", recv_msg.msg_type);
- MS_SAFE_FREE(req_result.complete_path);
return G_SOURCE_REMOVE;
}
- MSAPI_DBG("pid[%d] result[%d] request_type[%d]", req_result.pid, req_result.result, req_result.request_type);
-
- user_callback = ((media_callback_data *)data)->user_callback;
- user_data = ((media_callback_data *)data)->user_data;
- sock_path = ((media_callback_data *)data)->sock_path;
-
- if ((recv_msg.msg_type != MS_MSG_SCANNER_COMPLETE) &&
- (recv_msg.msg_type != MS_MSG_SCANNER_PARTIAL) &&
- (recv_msg.msg_type != MS_MSG_RECURSIVE_START) &&
- req_result.complete_path != NULL) {
- /*NEED MUTEX*/
- g_mutex_lock(&scan_req_mutex);
- if (req_list != NULL) {
- int i = 0;
- int list_len = req_list->len;
-
- for (i = 0; i < list_len; i++) {
- req_data = g_array_index(req_list, media_scan_data*, i);
- if (req_data != NULL && g_strcmp0(req_data->req_path, req_result.complete_path) == 0) {
- MSAPI_DBG("FIND REQUEST [%s]", req_data->req_path);
- g_array_remove_index(req_list, i);
-
- /*close an IO channel*/
- g_io_channel_shutdown(src, FALSE, NULL);
- g_io_channel_unref(src);
-
- /* DF180202-00756 source was destroyed, this pointer url may be reused by others, cause Bus error */
- source_id = req_data->source_id;
- g_source_destroy(g_main_context_find_source_by_id(g_main_context_default(), source_id));
-
- close(sockfd);
- if (sock_path != NULL) {
- MSAPI_DBG("delete path :%s", sock_path);
- unlink(sock_path);
- MS_SAFE_FREE(sock_path);
- }
-
- MSAPI_DBG("REMOVE OK");
- flag = true;
-
- break;
- }
- }
- }
- g_mutex_unlock(&scan_req_mutex);
- MS_SAFE_FREE(data);
- } else {
- /*call user define function. Scanner V2 case only*/
- user_callback(&req_result, user_data);
- }
- /*call user define function*/
- if (flag)
- user_callback(&req_result, user_data);
+ MSAPI_DBG_SLOG("complete_path :%s", recv_msg.msg);
+ MSAPI_DBG("pid[%d] result[%d] request_type[%d]", recv_msg.pid, recv_msg.result, req_result.request_type);
- MS_SAFE_FREE(req_result.complete_path);
+ /*NEED MUTEX*/
+ g_mutex_lock(&scan_req_mutex);
- return G_SOURCE_CONTINUE;
-}
+ found = g_slist_find_custom(req_list, recv_msg.msg, __find_req_item);
+ if (!found) {
+ g_mutex_unlock(&scan_req_mutex);
+ MSAPI_DBG("Not in scan queue :%s", recv_msg.msg);
+ return G_SOURCE_REMOVE;
+ }
-static int _add_request(const char * req_path, media_callback_data *cb_data, GIOChannel *channel, guint source_id)
-{
- media_scan_data *req_data = NULL;
+ req_data = (media_scan_data *)found->data;
- /*NEED MUTEX*/
- g_mutex_lock(&scan_req_mutex);
+ req_result.complete_path = recv_msg.msg;
+ req_result.pid = recv_msg.pid;
+ req_result.result = recv_msg.result;
- if (req_list == NULL)
- req_list = g_array_new(FALSE, FALSE, sizeof(media_scan_data*));
+ if (recv_msg.msg_type != MS_MSG_SCANNER_COMPLETE &&
+ recv_msg.msg_type != MS_MSG_SCANNER_PARTIAL &&
+ recv_msg.msg_type != MS_MSG_RECURSIVE_START) {
- if (req_list == NULL) {
- MSAPI_DBG_ERR("g_array_new falied");
- g_mutex_unlock(&scan_req_mutex);
- return MS_MEDIA_ERR_OUT_OF_MEMORY;
- }
+ (req_data->user_callback)(&req_result, req_data->user_data);
+ g_free(req_data->req_path);
+ g_free(req_data);
+ req_list = g_slist_delete_link(req_list, found);
- req_data = malloc(sizeof(media_scan_data));
- if (req_data == NULL) {
- MSAPI_DBG_ERR("malloc falied");
g_mutex_unlock(&scan_req_mutex);
- return MS_MEDIA_ERR_OUT_OF_MEMORY;
- }
- req_data->cb_data = cb_data;
- req_data->req_path = strdup(req_path);
- req_data->src = channel;
- req_data->source_id = source_id;
+ MSAPI_DBG("REMOVE OK");
- g_array_append_val(req_list, req_data);
+ return G_SOURCE_REMOVE;
+ }
+ /*call user define function. Scanner V2 case only*/
+ (req_data->user_callback)(&req_result, req_data->user_data);
g_mutex_unlock(&scan_req_mutex);
- return MS_MEDIA_ERR_NONE;
+ return G_SOURCE_CONTINUE;
+}
+
+static void __add_request(media_scan_data *scan_data)
+{
+ /* NEED MUTEX */
+ g_mutex_lock(&scan_req_mutex);
+ req_list = g_slist_append(req_list, scan_data);
+
+ g_mutex_unlock(&scan_req_mutex);
}
-static int _remove_request(const char * req_path)
+static int __remove_request(const char *req_path)
{
media_scan_data *req_data = NULL;
- media_callback_data *cb_data = NULL;
- char *sock_path = NULL;
- int i = 0;
- int list_len = 0;
- bool flag = false;
- scan_complete_cb user_callback;
- void *user_data = NULL;
- GIOChannel *src = NULL;
- guint source_id = 0;
media_request_result_s req_result;
+ GSList *found = NULL;
/*NEED MUTEX*/
g_mutex_lock(&scan_req_mutex);
- if (req_list == NULL) {
- MSAPI_DBG_ERR("The request list is NULL. This is invalid operation.");
+
+ found = g_slist_find_custom(req_list, req_path, __find_req_item);
+ if (!found) {
+ MSAPI_DBG("Not in scan queue :%s", req_path);
g_mutex_unlock(&scan_req_mutex);
return MS_MEDIA_ERR_INVALID_PARAMETER;
}
- list_len = req_list->len;
+ req_data = (media_scan_data *)found->data;
- for (i = 0; i < list_len; i++) {
- req_data = g_array_index(req_list, media_scan_data*, i);
- if (strcmp(req_data->req_path, req_path) == 0) {
- flag = true;
- g_array_remove_index(req_list, i);
+ req_result.pid = -1;
+ req_result.result = MS_MEDIA_ERR_NONE;
+ req_result.complete_path = (char *)req_path;
+ req_result.request_type = MEDIA_FILES_REGISTER;
- req_result.pid = -1;
- req_result.result = MS_MEDIA_ERR_NONE;
- req_result.complete_path = strndup(req_path, strlen(req_path));
- req_result.request_type = MEDIA_FILES_REGISTER;
-
- src = req_data->src;
- source_id = req_data->source_id;
- cb_data = req_data->cb_data;
- sock_path = ((media_callback_data *)cb_data)->sock_path;
- user_callback = ((media_callback_data *)cb_data)->user_callback;
- user_data = ((media_callback_data *)cb_data)->user_data;
-
- /*call user define function*/
- MSAPI_DBG("Call Cancel Callback");
- user_callback(&req_result, user_data);
-
- /*close an IO channel*/
- g_io_channel_shutdown(src, FALSE, NULL);
- g_io_channel_unref(src);
-
- /* DF180202-00756 source was destroyed, this pointer url may be reused by others, cause Bus error */
- g_source_destroy(g_main_context_find_source_by_id(g_main_context_default(), source_id));
-
- if (sock_path != NULL) {
- MSAPI_DBG("delete path :%s", sock_path);
- unlink(sock_path);
- MS_SAFE_FREE(sock_path);
- }
-
- MS_SAFE_FREE(req_data->cb_data);
- MS_SAFE_FREE(req_data->req_path);
- MS_SAFE_FREE(req_data);
+ /*call user define function*/
+ MSAPI_DBG("Call Cancel Callback");
+ (req_data->user_callback)(&req_result, req_data->user_data);
- MSAPI_DBG("CANCEL OK");
+ g_source_remove(req_data->source_id);
- break;
- }
- }
+ g_free(req_data->req_path);
+ g_free(req_data);
+ req_list = g_slist_delete_link(req_list, found);
- if (flag == false) {
- MSAPI_DBG("Not in scan queue :%s", req_path);
- g_mutex_unlock(&scan_req_mutex);
- return MS_MEDIA_ERR_INVALID_PARAMETER;
- }
+ MSAPI_DBG("CANCEL OK");
g_mutex_unlock(&scan_req_mutex);
return MS_MEDIA_ERR_NONE;
-
}
-static int _attach_callback(const char *req_path, int *sockfd, char* sock_path, scan_complete_cb user_callback, void *user_data)
+static void __attach_callback(const char *req_path, int *sockfd, scan_complete_cb user_callback, void *user_data)
{
GIOChannel *channel = NULL;
- GMainContext *context = NULL;
- GSource *source = NULL;
- media_callback_data *cb_data;
+ media_scan_data *data = NULL;
guint source_id = 0;
- cb_data = malloc(sizeof(media_callback_data));
- MSAPI_RETVM_IF(cb_data == NULL, MS_MEDIA_ERR_OUT_OF_MEMORY, "allocate failed");
- memset(cb_data, 0, sizeof(media_callback_data));
-
- /*get the global default main context*/
- context = g_main_context_default();
-
- /* Create new channel to watch udp socket */
+ data = g_new0(media_scan_data, 1);
+ /* Create new channel to watch socket */
channel = g_io_channel_unix_new(*sockfd);
- source = g_io_create_watch(channel, G_IO_IN);
-
- cb_data->user_callback = user_callback;
- cb_data->user_data = user_data;
- cb_data->sock_path = sock_path;
+ g_io_channel_set_close_on_unref(channel, TRUE);
+ source_id = g_io_add_watch(channel, G_IO_IN, __read_socket, NULL);
+ g_io_channel_unref(channel);
- /* Set callback to be called when socket is readable */
- g_source_set_callback(source, (GSourceFunc)_read_socket, cb_data, NULL);
- source_id = g_source_attach(source, context);
- g_source_unref(source);
+ data->user_callback = user_callback;
+ data->user_data = user_data;
+ data->req_path = g_strdup(req_path);
+ data->channel = channel;
+ data->source_id = source_id;
- return _add_request(req_path, cb_data, channel, source_id);
+ __add_request(data);
}
-static int __media_db_request_update_async(ms_msg_type_e msg_type, const char *storage_id, const char *request_msg, scan_complete_cb user_callback, void *user_data, uid_t uid)
+static int __media_db_request_update_async(ms_msg_type_e msg_type,
+ const char *storage_id,
+ const char *request_msg,
+ scan_complete_cb user_callback,
+ void *user_data,
+ uid_t uid)
{
int ret = MS_MEDIA_ERR_NONE;
int request_msg_size = 0;
} else {
request_path = g_strdup(request_msg);
}
- MSAPI_RETVM_IF(request_path == NULL, MS_MEDIA_ERR_OUT_OF_MEMORY, "Out of memory");
MSAPI_DBG("querysize[%d] path[%s] query[%s]", request_msg_size, request_msg, request_path);
return ret;
}
- ret = _attach_callback(request_path, &sockfd, NULL, user_callback, user_data);
+ __attach_callback(request_path, &sockfd, user_callback, user_data);
MS_SAFE_FREE(request_path);
return ret;
} else {
request_path = g_strdup(request_msg);
}
- MSAPI_RETVM_IF(request_path == NULL, MS_MEDIA_ERR_OUT_OF_MEMORY, "Out of memory");
memset((void *)&send_msg, 0, sizeof(ms_comm_msg_s));
send_msg.msg_type = msg_type;
return ret;
}
- ret = _remove_request(request_path);
+ ret = __remove_request(request_path);
MS_SAFE_FREE(request_path);
return ret;
}
-int media_directory_scanning_async(const char *directory_path, const char *storage_id, bool recursive_on, scan_complete_cb user_callback, void *user_data, uid_t uid)
+int media_directory_scanning_async(const char *directory_path,
+ const char *storage_id,
+ bool recursive_on,
+ scan_complete_cb user_callback,
+ void *user_data,
+ uid_t uid)
{
int ret = MS_MEDIA_ERR_NONE;
- ret = _check_dir_path(directory_path, uid);
+ ret = __check_dir_path(directory_path, uid);
MSAPI_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Invalid path");
if (recursive_on)
{
int ret = MS_MEDIA_ERR_NONE;
- ret = _check_dir_path(directory_path, uid);
+ ret = __check_dir_path(directory_path, uid);
MSAPI_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Invalid path");
ret = __media_db_request_update_cancel(MS_MSG_DIRECTORY_SCANNING_CANCEL, directory_path);