/* * libmedia-thumbnail * * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. * * Contact: Hyunjun Ko * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ #include "media-thumbnail.h" #include "media-thumb-ipc.h" #include "media-thumb-util.h" #include "media-thumb-db.h" #include "media-thumb-debug.h" #include #include #include #include #include #include #define THUMB_IPC_PATH tzplatform_mkpath(TZ_SYS_RUN, "media-server/media_ipc_thumbcreator.socket") static GQueue *g_manage_queue = NULL; static GQueue *g_manage_raw_queue = NULL; typedef struct { GIOChannel *channel; int msg_type; unsigned int request_id; bool isCanceled; bool isRequested; int source_id; uid_t uid; char *path; thumbUserData *userData; } thumbReq; typedef struct { GIOChannel *channel; int msg_type; bool isCanceled; bool isRequested; int request_id; int source_id; int width; int height; uid_t uid; char *path; thumbRawUserData *userData; } thumbRawReq; static int _media_thumb_send_request(); static int _media_thumb_raw_data_send_request(); int _media_thumb_get_error() { if (errno == EWOULDBLOCK) { thumb_err("Timeout. Can't try any more"); return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT; } else { thumb_stderror("recvfrom failed"); return MS_MEDIA_ERR_SOCKET_RECEIVE; } } void __media_thumb_pop() { int len = 0; if (g_manage_queue != NULL) { thumbReq *req = (thumbReq *)g_queue_pop_head(g_manage_queue); if (req != NULL) { GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id); g_io_channel_shutdown(req->channel, TRUE, NULL); g_io_channel_unref(req->channel); if (source_id != NULL) { g_source_destroy(source_id); } else { thumb_err("G_SOURCE_ID is NULL"); } SAFE_FREE(req->path); SAFE_FREE(req->userData); SAFE_FREE(req); } /* Check manage queue */ len = g_queue_get_length(g_manage_queue); if (len > 0) _media_thumb_send_request(); } } int __media_thumb_cancel(unsigned int request_id) { int len = 0, i; bool flag = false; if (g_manage_queue != NULL) { len = g_queue_get_length(g_manage_queue); for (i = 0; i < len; i++) { thumbReq *req = NULL; req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i); if (req == NULL) continue; if (req->request_id == request_id) { if (req->isRequested == true) { req->isCanceled = true; } else { g_queue_pop_nth(g_manage_queue, i); SAFE_FREE(req->path); SAFE_FREE(req->userData); SAFE_FREE(req); } flag = true; break; } } } if (flag == false) return MS_MEDIA_ERR_INTERNAL; return MS_MEDIA_ERR_NONE; } void __media_thumb_pop_raw_data() { int len = 0; if (g_manage_raw_queue != NULL) { thumbRawReq *req = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue); if (req != NULL) { GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id); g_io_channel_shutdown(req->channel, TRUE, NULL); g_io_channel_unref(req->channel); if (source_id != NULL) { g_source_destroy(source_id); } else { thumb_err("G_SOURCE_ID is NULL"); } SAFE_FREE(req->path); SAFE_FREE(req->userData); SAFE_FREE(req); } /* Check manage queue */ len = g_queue_get_length(g_manage_raw_queue); if (len > 0) _media_thumb_raw_data_send_request(); } } int __media_thumb_cancel_raw_data(int request_id) { int len = 0, i; bool flag = false; if (g_manage_raw_queue != NULL) { len = g_queue_get_length(g_manage_raw_queue); for (i = 0; i < len; i++) { thumbRawReq *req = NULL; req = (thumbRawReq *)g_queue_peek_nth(g_manage_raw_queue, i); if (req == NULL) continue; if (req->request_id == request_id) { if (req->isRequested == true) { req->isCanceled = true; } else { g_queue_pop_nth(g_manage_raw_queue, i); SAFE_FREE(req->path); SAFE_FREE(req->userData); SAFE_FREE(req); } flag = true; break; } } } if (flag == false) return MS_MEDIA_ERR_INTERNAL; return MS_MEDIA_ERR_NONE; } bool __media_thumb_check_cancel(void) { thumbReq *req = NULL; req = (thumbReq *)g_queue_peek_head(g_manage_queue); if (req == NULL) { return false; } else { if (req->isCanceled) return false; else return true; } } bool __media_thumb_check_cancel_for_raw(void) { thumbRawReq *req = NULL; req = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue); if (req == NULL) { return false; } else { if (req->isCanceled) return false; else return true; } } int _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg) { int remain_size = 0; int recv_len = 0; int recv_pos = 0; unsigned char *buf = NULL; THUMB_MALLOC(buf, header_size); if (buf == NULL) { thumb_err("memory allocation failed"); SAFE_FREE(buf); return MS_MEDIA_ERR_OUT_OF_MEMORY; } while (header_size > 0) { if ((recv_len = recv(sock, buf + recv_pos, header_size, 0)) < 0) { thumb_stderror("recv failed"); SAFE_FREE(buf); return _media_thumb_get_error(); } header_size -= recv_len; recv_pos += recv_len; } header_size = recv_pos; recv_pos = 0; memcpy(msg, buf, header_size); SAFE_FREE(buf); if (strlen(msg->org_path) == 0 || strlen(msg->org_path) >= MAX_FILEPATH_LEN) { thumb_err("org_path size is invalid %d", strlen(msg->org_path)); return MS_MEDIA_ERR_SOCKET_RECEIVE; } /* it can be empty string */ if (strlen(msg->dst_path) >= MAX_FILEPATH_LEN) { thumb_err("dst_path size is invalid %d", strlen(msg->dst_path)); return MS_MEDIA_ERR_SOCKET_RECEIVE; } if (msg->thumb_size < 0) { thumb_err("recv data is wrong"); return MS_MEDIA_ERR_SOCKET_RECEIVE; } if (msg->thumb_size > 0) { remain_size = msg->thumb_size; THUMB_MALLOC(buf, remain_size); if (buf == NULL) { return MS_MEDIA_ERR_OUT_OF_MEMORY; } while (remain_size > 0) { if ((recv_len = recv(sock, buf + recv_pos, remain_size, 0)) < 0) { thumb_stderror("recv failed"); SAFE_FREE(buf); return _media_thumb_get_error(); } fsync(sock); remain_size -= recv_len; recv_pos += recv_len; } SAFE_FREE(msg->thumb_data); THUMB_MALLOC(msg->thumb_data, msg->thumb_size); if (msg->thumb_data != NULL) { memcpy(msg->thumb_data, buf, msg->thumb_size); } else { SAFE_FREE(buf); return MS_MEDIA_ERR_OUT_OF_MEMORY; } } SAFE_FREE(buf); return MS_MEDIA_ERR_NONE; } int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size) { if (req_msg == NULL || buf == NULL) return MS_MEDIA_ERR_INVALID_PARAMETER; int thumb_data_len = 0; int size = 0; int header_size = 0; header_size = sizeof(thumbMsg) - sizeof(unsigned char *); thumb_data_len = req_msg->thumb_size; if (thumb_data_len < 0) return MS_MEDIA_ERR_INVALID_PARAMETER; //thumb_dbg("Basic Size[%d] org_path[%s] dst_path[%s] thumb_data_len[%d]", header_size, req_msg->org_path, req_msg->dst_path, thumb_data_len); size = header_size + thumb_data_len; THUMB_MALLOC(*buf, size); if (*buf == NULL) { *buf_size = 0; return 0; } memcpy(*buf, req_msg, header_size); if (thumb_data_len > 0) memcpy((*buf) + header_size, req_msg->thumb_data, thumb_data_len); *buf_size = size; return MS_MEDIA_ERR_NONE; } gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data) { thumbMsg recv_msg; int header_size = 0; int sock = 0; int err = MS_MEDIA_ERR_NONE; memset((void *)&recv_msg, 0, sizeof(thumbMsg)); sock = g_io_channel_unix_get_fd(src); header_size = sizeof(thumbMsg) - sizeof(unsigned char *); thumb_err("_media_thumb_write_socket socket : %d", sock); if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) { thumb_err("_media_thumb_recv_msg failed "); __media_thumb_pop(); return FALSE; } if (recv_msg.status != MS_MEDIA_ERR_NONE) { err = recv_msg.status; thumb_err("Failed to make thumbnail (%d)", err); } if (__media_thumb_check_cancel()) { if (data) { thumbUserData* cb = (thumbUserData*)data; if (cb->func != NULL) cb->func(err, recv_msg.dst_path, cb->user_data); } } __media_thumb_pop(); thumb_dbg("Done"); return FALSE; } gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data) { thumbMsg recv_msg; int header_size = 0; int sock = 0; int err = MS_MEDIA_ERR_NONE; memset((void *)&recv_msg, 0, sizeof(thumbMsg)); sock = g_io_channel_unix_get_fd(src); header_size = sizeof(thumbMsg) - sizeof(unsigned char *); thumb_err("_media_thumb_write_socket socket : %d", sock); if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) { thumb_err("_media_thumb_recv_msg failed "); __media_thumb_pop_raw_data(); return FALSE; } if (recv_msg.status != MS_MEDIA_ERR_NONE) { err = recv_msg.status; thumb_err("Failed to make thumbnail (%d)", err); } if (__media_thumb_check_cancel_for_raw()) { if (data) { thumbRawUserData* cb = (thumbRawUserData*)data; if (cb->func != NULL) cb->func(err, recv_msg.request_id, recv_msg.org_path, recv_msg.thumb_width, recv_msg.thumb_height, recv_msg.thumb_data, recv_msg.thumb_size, cb->user_data); } } __media_thumb_pop_raw_data(); thumb_dbg("Done"); return FALSE; } static int _media_thumb_send_request() { int err = MS_MEDIA_ERR_NONE; int sock = -1; struct sockaddr_un serv_addr; thumbReq *req_manager = NULL; int pid; err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock); if (err != MS_MEDIA_ERR_NONE) { thumb_err("ms_ipc_create_client_socket failed"); return err; } memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sun_family = AF_UNIX; SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path)); GIOChannel *channel = NULL; channel = g_io_channel_unix_new(sock); int source_id = -1; /* Connecting to the thumbnail server */ if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { thumb_stderror("connect"); if (errno == EACCES) err = MS_MEDIA_ERR_PERMISSION_DENIED; else err = MS_MEDIA_ERR_SOCKET_CONN; g_io_channel_shutdown(channel, TRUE, NULL); g_io_channel_unref(channel); close(sock); return err; } req_manager = (thumbReq *)g_queue_peek_head(g_manage_queue); if (req_manager == NULL) { thumb_err("queue peek fail"); g_io_channel_shutdown(channel, TRUE, NULL); g_io_channel_unref(channel); close(sock); return MS_MEDIA_ERR_INVALID_PARAMETER; } GSource *source = NULL; source = g_io_create_watch(channel, G_IO_IN); g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, req_manager->userData, NULL); source_id = g_source_attach(source, g_main_context_get_thread_default()); thumbMsg req_msg; memset((void *)&req_msg, 0, sizeof(thumbMsg)); pid = getpid(); req_msg.pid = pid; req_msg.msg_type = req_manager->msg_type; req_msg.request_id = 0; req_msg.uid = req_manager->uid; SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path)); req_msg.dst_path[0] = '\0'; req_msg.thumb_size = 0; unsigned char *buf = NULL; int buf_size = 0; _media_thumb_set_buffer(&req_msg, &buf, &buf_size); if (send(sock, buf, buf_size, 0) < 0) { thumb_err("send failed: %d", errno); SAFE_FREE(buf); g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id)); g_io_channel_shutdown(channel, TRUE, NULL); g_io_channel_unref(channel); close(sock); return MS_MEDIA_ERR_SOCKET_SEND; } SAFE_FREE(buf); thumb_dbg("Sending msg to thumbnail daemon is successful"); if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) { req_manager->channel = channel; req_manager->source_id = source_id; req_manager->isRequested = true; } return err; } static int _media_thumb_raw_data_send_request() { int err = MS_MEDIA_ERR_NONE; int sock = -1; struct sockaddr_un serv_addr; thumbRawReq *req_manager = NULL; int pid; err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock); if (err != MS_MEDIA_ERR_NONE) { thumb_err("ms_ipc_create_client_socket failed"); return err; } memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sun_family = AF_UNIX; SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path)); GIOChannel *channel = NULL; channel = g_io_channel_unix_new(sock); int source_id = -1; /* Connecting to the thumbnail server */ if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { thumb_stderror("connect error"); if (errno == EACCES) err = MS_MEDIA_ERR_PERMISSION_DENIED; else err = MS_MEDIA_ERR_SOCKET_CONN; g_io_channel_shutdown(channel, TRUE, NULL); g_io_channel_unref(channel); close(sock); return err; } req_manager = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue); if (req_manager == NULL) { thumb_err("queue peek fail"); g_io_channel_shutdown(channel, TRUE, NULL); g_io_channel_unref(channel); close(sock); return MS_MEDIA_ERR_INVALID_PARAMETER; } GSource *source = NULL; source = g_io_create_watch(channel, G_IO_IN); g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL); source_id = g_source_attach(source, g_main_context_get_thread_default()); thumbMsg req_msg; memset((void *)&req_msg, 0, sizeof(thumbMsg)); pid = getpid(); req_msg.pid = pid; req_msg.msg_type = req_manager->msg_type; req_msg.request_id = req_manager->request_id; req_msg.thumb_width = req_manager->width; req_msg.thumb_height = req_manager->height; req_msg.uid = req_manager->uid; SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path)); req_msg.dst_path[0] = '\0'; req_msg.thumb_size = 0; unsigned char *buf = NULL; int buf_size = 0; _media_thumb_set_buffer(&req_msg, &buf, &buf_size); if (send(sock, buf, buf_size, 0) < 0) { thumb_err("send failed: %d", errno); SAFE_FREE(buf); g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id)); g_io_channel_shutdown(channel, TRUE, NULL); g_io_channel_unref(channel); return MS_MEDIA_ERR_SOCKET_SEND; } SAFE_FREE(buf); if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) { req_manager->channel = channel; req_manager->source_id = source_id; req_manager->isRequested = true; } return MS_MEDIA_ERR_NONE; } int _media_thumb_request_async(int msg_type, unsigned int request_id, const char *origin_path, thumbUserData *userData, uid_t uid) { int err = MS_MEDIA_ERR_NONE; int len = 0; if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) return __media_thumb_cancel(request_id); if (g_manage_queue == NULL) g_manage_queue = g_queue_new(); thumbReq *thumb_req = NULL; THUMB_MALLOC(thumb_req, sizeof(thumbReq)); thumb_retvm_if(thumb_req == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Failed to create request element"); thumb_req->msg_type = msg_type; thumb_req->path = g_strdup(origin_path); thumb_req->userData = userData; thumb_req->isCanceled = false; thumb_req->isRequested = false; thumb_req->request_id = request_id; thumb_req->uid = uid; len = g_queue_get_length(g_manage_queue); g_queue_push_tail(g_manage_queue, (gpointer)thumb_req); if (len == 0) err = _media_thumb_send_request(); return err; } int _media_thumb_request_raw_data_async(int msg_type, int request_id, const char *origin_path, int width, int height, thumbRawUserData *userData, uid_t uid) { int err = MS_MEDIA_ERR_NONE; int len = 0; thumbRawReq *thumb_req = NULL; if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA) return __media_thumb_cancel_raw_data(request_id); if (g_manage_raw_queue == NULL) g_manage_raw_queue = g_queue_new(); THUMB_MALLOC(thumb_req, sizeof(thumbRawReq)); if (thumb_req == NULL) { thumb_err("Failed to create request element"); return MS_MEDIA_ERR_INVALID_PARAMETER; } thumb_req->msg_type = msg_type; thumb_req->request_id = request_id; thumb_req->path = g_strdup(origin_path); thumb_req->width = width; thumb_req->height = height; thumb_req->userData = userData; thumb_req->isCanceled = false; thumb_req->isRequested = false; thumb_req->uid = uid; len = g_queue_get_length(g_manage_raw_queue); g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req); if (len == 0) err = _media_thumb_raw_data_send_request(); return err; } int _media_thumb_request_cancel_all() { if (g_manage_raw_queue != NULL) { while (g_queue_get_length(g_manage_raw_queue) > 0) { thumbRawReq *req = (thumbRawReq *)g_queue_peek_tail(g_manage_raw_queue); if (req != NULL) { if (req->isRequested == true) { req->isCanceled = true; thumb_dbg("Last item!!"); break; } else { g_queue_pop_tail(g_manage_raw_queue); SAFE_FREE(req->path); SAFE_FREE(req->userData); SAFE_FREE(req); } } else { thumb_dbg("Queue is empty!!"); break; } } } return MS_MEDIA_ERR_NONE; }