summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMinje Ahn <minje.ahn@samsung.com>2019-09-26 06:18:02 (GMT)
committerMinje Ahn <minje.ahn@samsung.com>2019-09-27 01:00:56 (GMT)
commit1dbcc2241014a5090093bbccce4878010763d2ea (patch)
tree87dd64706f6b5f396c88646f5c4be402d017c703 /src
parenta7dfb761065e2b20829560d7aeeca5c2043a2bed (diff)
downloadlibmedia-thumbnail-1dbcc2241014a5090093bbccce4878010763d2ea.zip
libmedia-thumbnail-1dbcc2241014a5090093bbccce4878010763d2ea.tar.gz
libmedia-thumbnail-1dbcc2241014a5090093bbccce4878010763d2ea.tar.bz2
Change-Id: I827538cf8d3c634878d4d2da3fab71c8c89d0316 Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
Diffstat (limited to 'src')
-rwxr-xr-xsrc/include/ipc/media-thumb-ipc.h1
-rwxr-xr-xsrc/ipc/media-thumb-ipc.c317
2 files changed, 134 insertions, 184 deletions
diff --git a/src/include/ipc/media-thumb-ipc.h b/src/include/ipc/media-thumb-ipc.h
index 7d0357d..b242b8e 100755
--- a/src/include/ipc/media-thumb-ipc.h
+++ b/src/include/ipc/media-thumb-ipc.h
@@ -38,7 +38,6 @@
#define MAX_PATH_SIZE 4096
-int _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg);
int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size);
int _media_thumb_request_async(int msg_type, unsigned int request_id, const char *origin_path, thumbUserData *userData, uid_t uid);
diff --git a/src/ipc/media-thumb-ipc.c b/src/ipc/media-thumb-ipc.c
index 64afe5a..dfbab42 100755
--- a/src/ipc/media-thumb-ipc.c
+++ b/src/ipc/media-thumb-ipc.c
@@ -66,64 +66,19 @@ typedef struct {
static int __media_thumb_send_request(void);
static int __media_thumb_raw_data_send_request(void);
-
-static int __media_thumb_get_error(void)
-{
- 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;
- }
-}
-
-static void __media_thumb_pop(void)
-{
- int len = 0;
- int sock = 0;
- thumbReq *req = NULL;
-
- if (!g_manage_queue)
- return;
-
- 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);
- sock = g_io_channel_unix_get_fd(req->channel);
-
- g_io_channel_shutdown(req->channel, TRUE, NULL);
- g_io_channel_unref(req->channel);
- close(sock);
- if (source_id) {
- 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();
-}
-
static int __media_thumb_cancel(unsigned int request_id)
{
int len = 0, i;
+ thumbReq *req = NULL;
thumb_retv_if(!g_manage_queue, MS_MEDIA_ERR_NONE);
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)
+ continue;
if (req->request_id == request_id) {
if (req->isRequested) {
@@ -143,52 +98,19 @@ static int __media_thumb_cancel(unsigned int request_id)
return MS_MEDIA_ERR_INTERNAL;
}
-static void __media_thumb_pop_raw_data(void)
-{
- int len = 0;
- int sock = 0;
- thumbRawReq *req = NULL;
-
- if (!g_manage_raw_queue)
- return;
-
- 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);
- sock = g_io_channel_unix_get_fd(req->channel);
-
- g_io_channel_shutdown(req->channel, TRUE, NULL);
- g_io_channel_unref(req->channel);
- close(sock);
- if (source_id) {
- 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();
-}
-
static int __media_thumb_cancel_raw_data(int request_id)
{
int len = 0, i;
+ thumbRawReq *req = NULL;
thumb_retv_if(!g_manage_raw_queue, MS_MEDIA_ERR_NONE);
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)
+ continue;
if (req->request_id == request_id) {
if (req->isRequested) {
@@ -208,35 +130,27 @@ static int __media_thumb_cancel_raw_data(int request_id)
return MS_MEDIA_ERR_INTERNAL;
}
-static bool __media_thumb_check_cancel(void)
+static bool __media_thumb_is_canceled(void)
{
- thumbReq *req = NULL;
- req = (thumbReq *)g_queue_peek_head(g_manage_queue);
+ thumbReq *req = (thumbReq *)g_queue_peek_head(g_manage_queue);
if (!req)
- return false;
-
- if (req->isCanceled)
- return false;
- else
return true;
+
+ return req->isCanceled;
}
-static bool __media_thumb_check_cancel_for_raw(void)
+static bool __media_thumb_is_canceled_for_raw(void)
{
- thumbRawReq *req = NULL;
- req = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
+ thumbRawReq *req = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
if (!req)
- return false;
-
- if (req->isCanceled)
- return false;
- else
return true;
+
+ return req->isCanceled;
}
-int _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
+static int __media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
{
int remain_size = 0;
int recv_len = 0;
@@ -244,17 +158,13 @@ int _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
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;
- }
+ thumb_retv_if(!buf, 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();
+ return MS_MEDIA_ERR_IPC;
}
header_size -= recv_len;
recv_pos += recv_len;
@@ -266,48 +176,34 @@ int _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
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 %zu", 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 %zu", strlen(msg->dst_path));
- return MS_MEDIA_ERR_SOCKET_RECEIVE;
- }
+ thumb_retvm_if(strlen(msg->org_path) == 0 || strlen(msg->org_path) >= MAX_FILEPATH_LEN, MS_MEDIA_ERR_IPC, "Invalid org_path");
+ thumb_retvm_if(strlen(msg->dst_path) >= MAX_FILEPATH_LEN, MS_MEDIA_ERR_IPC, "Invalid dst_path");
+ thumb_retvm_if(msg->thumb_size < 0, MS_MEDIA_ERR_IPC, "Invalid thumb_size");
+ thumb_retv_if(msg->thumb_size == 0, MS_MEDIA_ERR_NONE);
- if (msg->thumb_size < 0) {
- thumb_err("recv data is wrong");
- return MS_MEDIA_ERR_SOCKET_RECEIVE;
- }
+ remain_size = msg->thumb_size;
+ THUMB_MALLOC(buf, remain_size);
+ thumb_retv_if(!buf, MS_MEDIA_ERR_OUT_OF_MEMORY);
- if (msg->thumb_size > 0) {
- remain_size = msg->thumb_size;
- THUMB_MALLOC(buf, remain_size);
- thumb_retv_if(!buf, 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;
+ while (remain_size > 0) {
+ if ((recv_len = recv(sock, buf + recv_pos, remain_size, 0)) < 0) {
+ thumb_stderror("recv failed");
+ SAFE_FREE(buf);
+ return MS_MEDIA_ERR_IPC;
}
- 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);
+ fsync(sock);
+ remain_size -= recv_len;
+ recv_pos += recv_len;
+ }
- return MS_MEDIA_ERR_OUT_OF_MEMORY;
- }
+ SAFE_FREE(msg->thumb_data);
+ THUMB_MALLOC(msg->thumb_data, msg->thumb_size);
+ if (msg->thumb_data) {
+ memcpy(msg->thumb_data, buf, msg->thumb_size);
+ } else {
+ SAFE_FREE(buf);
+ return MS_MEDIA_ERR_OUT_OF_MEMORY;
}
SAFE_FREE(buf);
@@ -329,8 +225,6 @@ int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_siz
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) {
@@ -346,25 +240,86 @@ int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_siz
return MS_MEDIA_ERR_NONE;
}
-gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
+static void __media_thumb_pop(void)
+{
+ int len = 0;
+ int sock = 0;
+ thumbReq *req = NULL;
+ GSource *source_id = NULL;
+
+ if (!g_manage_queue)
+ return;
+
+ req = (thumbReq *)g_queue_pop_head(g_manage_queue);
+ if (req) {
+ source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
+ sock = g_io_channel_unix_get_fd(req->channel);
+
+ g_io_channel_shutdown(req->channel, TRUE, NULL);
+ g_io_channel_unref(req->channel);
+ close(sock);
+ if (source_id)
+ g_source_destroy(source_id);
+
+ 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();
+}
+
+static void __media_thumb_pop_raw_data(void)
+{
+ int len = 0;
+ int sock = 0;
+ thumbRawReq *req = NULL;
+ GSource *source_id = NULL;
+
+ if (!g_manage_raw_queue)
+ return;
+
+ req = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue);
+ if (req) {
+ source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
+ sock = g_io_channel_unix_get_fd(req->channel);
+
+ g_io_channel_shutdown(req->channel, TRUE, NULL);
+ g_io_channel_unref(req->channel);
+ close(sock);
+ if (source_id)
+ g_source_destroy(source_id);
+
+ 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();
+}
+
+static 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;
+ thumbUserData *userdata = NULL;
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) {
+ if ((err = __media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
thumb_err("_media_thumb_recv_msg failed ");
- __media_thumb_pop();
-
- return G_SOURCE_REMOVE;
+ goto NEXT;
}
if (recv_msg.status != MS_MEDIA_ERR_NONE) {
@@ -372,27 +327,27 @@ gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpoi
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);
- }
- }
+ if (__media_thumb_is_canceled() || !data)
+ goto NEXT;
- __media_thumb_pop();
+ userdata = (thumbUserData*)data;
+ if (userdata->func)
+ userdata->func(err, recv_msg.dst_path, userdata->user_data);
thumb_dbg("Done");
+NEXT:
+ __media_thumb_pop();
return G_SOURCE_REMOVE;
}
-gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
+static 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;
+ thumbRawUserData *userdata = NULL;
memset((void *)&recv_msg, 0, sizeof(thumbMsg));
sock = g_io_channel_unix_get_fd(src);
@@ -401,11 +356,9 @@ gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condit
thumb_err("_media_thumb_write_socket socket : %d", sock);
- if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
+ 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 G_SOURCE_REMOVE;
+ goto NEXT;
}
if (recv_msg.status != MS_MEDIA_ERR_NONE) {
@@ -413,17 +366,16 @@ gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condit
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);
- }
- }
+ if (__media_thumb_is_canceled_for_raw() || !data)
+ goto NEXT;
- __media_thumb_pop_raw_data();
+ userdata = (thumbRawUserData*)data;
+ if (userdata->func)
+ userdata->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, userdata->user_data);
thumb_dbg("Done");
+NEXT:
+ __media_thumb_pop_raw_data();
return G_SOURCE_REMOVE;
}
@@ -453,7 +405,7 @@ static int __media_thumb_send_request(void)
if (errno == EACCES)
err = MS_MEDIA_ERR_PERMISSION_DENIED;
else
- err = MS_MEDIA_ERR_SOCKET_CONN;
+ err = MS_MEDIA_ERR_IPC;
g_io_channel_shutdown(channel, TRUE, NULL);
g_io_channel_unref(channel);
@@ -463,8 +415,7 @@ static int __media_thumb_send_request(void)
}
req_manager = (thumbReq *)g_queue_peek_head(g_manage_queue);
-
- if (req_manager == NULL) {
+ if (!req_manager) {
thumb_err("queue peek fail");
g_io_channel_shutdown(channel, TRUE, NULL);
g_io_channel_unref(channel);
@@ -474,7 +425,7 @@ static int __media_thumb_send_request(void)
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);
+ 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;
@@ -500,7 +451,7 @@ static int __media_thumb_send_request(void)
g_io_channel_shutdown(channel, TRUE, NULL);
g_io_channel_unref(channel);
close(sock);
- return MS_MEDIA_ERR_SOCKET_SEND;
+ return MS_MEDIA_ERR_IPC;
}
SAFE_FREE(buf);
@@ -540,7 +491,7 @@ static int __media_thumb_raw_data_send_request(void)
if (errno == EACCES)
err = MS_MEDIA_ERR_PERMISSION_DENIED;
else
- err = MS_MEDIA_ERR_SOCKET_CONN;
+ err = MS_MEDIA_ERR_IPC;
g_io_channel_shutdown(channel, TRUE, NULL);
g_io_channel_unref(channel);
@@ -550,7 +501,7 @@ static int __media_thumb_raw_data_send_request(void)
req_manager = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
- if (req_manager == NULL) {
+ if (!req_manager) {
thumb_err("queue peek fail");
g_io_channel_shutdown(channel, TRUE, NULL);
g_io_channel_unref(channel);
@@ -560,7 +511,7 @@ static int __media_thumb_raw_data_send_request(void)
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);
+ 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;
@@ -588,7 +539,7 @@ static int __media_thumb_raw_data_send_request(void)
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;
+ return MS_MEDIA_ERR_IPC;
}
SAFE_FREE(buf);