Fix memory leak(additional)
[platform/core/multimedia/libmedia-thumbnail.git] / src / ipc / media-thumb-ipc.c
1 /*
2  * libmedia-thumbnail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include "media-thumbnail.h"
23 #include "media-thumb-ipc.h"
24 #include "media-thumb-util.h"
25 #include "media-thumb-db.h"
26 #include "media-thumb-debug.h"
27 #include <glib.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <grp.h>
32 #include <pwd.h>
33
34 #define GLOBAL_USER    0 //#define     tzplatform_getenv(TZ_GLOBAL) //TODO
35 #define THUMB_SOCK_BLOCK_SIZE 512
36
37 static __thread GQueue *g_request_queue = NULL;
38 typedef struct {
39         GIOChannel *channel;
40         char *path;
41         int source_id;
42         thumbUserData *userData;
43 } thumbReq;
44
45 typedef struct {
46         GIOChannel *channel;
47         int request_id;
48         int source_id;
49         thumbRawUserData *userData;
50 } thumbRawReq;
51
52 int _media_thumb_create_socket(int sock_type, int *sock)
53 {
54         int sock_fd = 0;
55
56         if ((sock_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
57                 thumb_err("socket failed: %s", strerror(errno));
58                 return MS_MEDIA_ERR_SOCKET_CONN;
59         }
60
61         if (sock_type == CLIENT_SOCKET) {
62                 struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 };
63
64                 if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) {
65                         thumb_err("setsockopt failed: %s", strerror(errno));
66                         close(sock_fd);
67                         return MS_MEDIA_ERR_SOCKET_CONN;
68                 }
69         } else if (sock_type == SERVER_SOCKET) {
70
71                 int n_reuse = 1;
72
73                 if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &n_reuse, sizeof(n_reuse)) == -1) {
74                         thumb_err("setsockopt failed: %s", strerror(errno));
75                         close(sock_fd);
76                         return MS_MEDIA_ERR_SOCKET_CONN;
77                 }
78         }
79
80         *sock = sock_fd;
81
82         return MS_MEDIA_ERR_NONE;
83 }
84
85
86 int _media_thumb_create_udp_socket(int *sock)
87 {
88         int sock_fd = 0;
89
90         if ((sock_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
91                 thumb_err("socket failed: %s", strerror(errno));
92                 return MS_MEDIA_ERR_SOCKET_CONN;
93         }
94
95         struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 };
96
97         if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) {
98                 thumb_err("setsockopt failed: %s", strerror(errno));
99                 close(sock_fd);
100                 return MS_MEDIA_ERR_SOCKET_CONN;
101         }
102
103         *sock = sock_fd;
104
105         return MS_MEDIA_ERR_NONE;
106 }
107
108 int _media_thumb_get_error()
109 {
110         if (errno == EWOULDBLOCK) {
111                 thumb_err("Timeout. Can't try any more");
112                 return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT;
113         } else {
114                 thumb_err("recvfrom failed : %s", strerror(errno));
115                 return MS_MEDIA_ERR_SOCKET_RECEIVE;
116         }
117 }
118
119 int __media_thumb_pop_req_queue(const char *path, bool shutdown_channel)
120 {
121         int req_len = 0, i;
122
123         if (g_request_queue == NULL) return MS_MEDIA_ERR_INVALID_PARAMETER;
124         req_len = g_queue_get_length(g_request_queue);
125
126         if (req_len <= 0) {
127 //              thumb_dbg("There is no request in the queue");
128         } else {
129
130                 for (i = 0; i < req_len; i++) {
131                         thumbReq *req = NULL;
132                         req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
133                         if (req == NULL) continue;
134
135                         if (strncmp(path, req->path, strlen(path)) == 0) {
136                                 if (shutdown_channel) {
137                                         GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
138                                         if (source_id != NULL) {
139                                                 g_source_destroy(source_id);
140                                         } else {
141                                                 thumb_err("G_SOURCE_ID is NULL");
142                                         }
143
144                                         g_io_channel_shutdown(req->channel, TRUE, NULL);
145                                         g_io_channel_unref(req->channel);
146                                 }
147                                 g_queue_pop_nth(g_request_queue, i);
148
149                                 SAFE_FREE(req->path);
150                                 SAFE_FREE(req->userData);
151                                 SAFE_FREE(req);
152
153                                 break;
154                         }
155                 }
156                 if (i == req_len) {
157 //                      thumb_dbg("There's no %s in the queue", path);
158                 }
159         }
160
161         return MS_MEDIA_ERR_NONE;
162 }
163
164 int __media_thumb_pop_raw_data_req_queue(int request_id, bool shutdown_channel)
165 {
166         int req_len = 0, i;
167
168         if (g_request_queue == NULL) return MS_MEDIA_ERR_INVALID_PARAMETER;
169         req_len = g_queue_get_length(g_request_queue);
170
171         if (req_len <= 0) {
172 //              thumb_dbg("There is no request in the queue");
173         } else {
174
175                 for (i = 0; i < req_len; i++) {
176                         thumbRawReq *req = NULL;
177                         req = (thumbRawReq *)g_queue_peek_nth(g_request_queue, i);
178                         if (req == NULL) continue;
179
180                         if (request_id == req->request_id) {
181                                 if (shutdown_channel) {
182                                         GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
183                                         if (source_id != NULL) {
184                                                 g_source_destroy(source_id);
185                                         } else {
186                                                 thumb_err("G_SOURCE_ID is NULL");
187                                         }
188
189                                         g_io_channel_shutdown(req->channel, TRUE, NULL);
190                                         g_io_channel_unref(req->channel);
191                                 }
192                                 g_queue_pop_nth(g_request_queue, i);
193
194                                 SAFE_FREE(req->userData);
195                                 SAFE_FREE(req);
196
197                                 break;
198                         }
199                 }
200                 if (i == req_len) {
201 //                      thumb_dbg("There's no %s in the queue", path);
202                 }
203         }
204
205         return MS_MEDIA_ERR_NONE;
206 }
207
208 int __media_thumb_check_req_queue(const char *path)
209 {
210         int req_len = 0, i;
211
212         if (g_request_queue == NULL) return MS_MEDIA_ERR_NONE;
213         req_len = g_queue_get_length(g_request_queue);
214
215 //      thumb_dbg("Queue length : %d", req_len);
216 //      thumb_dbg("Queue path : %s", path);
217
218         if (req_len <= 0) {
219 //              thumb_dbg("There is no request in the queue");
220         } else {
221
222                 for (i = 0; i < req_len; i++) {
223                         thumbReq *req = NULL;
224                         req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
225                         if (req == NULL) continue;
226
227                         if (strncmp(path, req->path, strlen(path)) == 0) {
228                                 //thumb_dbg("Same Request - %s", path);
229                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
230
231                                 break;
232                         }
233                 }
234         }
235
236         return MS_MEDIA_ERR_NONE;
237 }
238
239 int
240 _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
241 {
242         int recv_msg_len = 0;
243         int remain_size = 0;
244         int block_size = THUMB_SOCK_BLOCK_SIZE;
245         int recv_block = 0;
246         unsigned char *buf = NULL;
247         unsigned char *block_buf = NULL;
248
249         buf = (unsigned char*)malloc(header_size * sizeof(unsigned char));
250         block_buf = (unsigned char*)malloc(THUMB_SOCK_BLOCK_SIZE * sizeof(unsigned char));
251
252         if (buf == NULL || block_buf == NULL) {
253                 thumb_err("memory allocation failed");
254                 SAFE_FREE(buf);
255                 SAFE_FREE(block_buf);
256                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
257         }
258
259         if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
260                 thumb_stderror("recv failed");
261                 SAFE_FREE(buf);
262                 return _media_thumb_get_error();
263         }
264
265         memcpy(msg, buf, header_size);
266         //thumb_dbg("origin_path_size : %d, dest_path_size : %d, thumb_size : %d", msg->origin_path_size, msg->dest_path_size, msg->thumb_size);
267
268         SAFE_FREE(buf);
269
270         remain_size = msg->origin_path_size + msg->dest_path_size + msg->thumb_size + 2;
271         buf = malloc(remain_size * sizeof(unsigned char));
272         memset(buf, 0, remain_size * sizeof(unsigned char));
273
274         while(remain_size > 0) {
275                 if(remain_size < THUMB_SOCK_BLOCK_SIZE) {
276                         block_size = remain_size;
277                 }
278                 if ((recv_msg_len = recv(sock, block_buf, block_size, 0)) < 0) {
279                         thumb_err("recv failed : %s", strerror(errno));
280                         SAFE_FREE(buf);
281                         SAFE_FREE(block_buf);
282                         return _media_thumb_get_error();
283                 }
284                 memcpy(buf+recv_block, block_buf, block_size);
285                 recv_block += block_size;
286                 remain_size -= block_size;
287         }
288
289         strncpy(msg->org_path, (char *)buf, msg->origin_path_size);
290         strncpy(msg->dst_path, (char *)buf + msg->origin_path_size + 1, msg->dest_path_size);
291         SAFE_FREE(msg->thumb_data);
292         msg->thumb_data = malloc(msg->thumb_size);
293         memset(msg->thumb_data, 0, msg->thumb_size);
294         memcpy(msg->thumb_data, buf + msg->origin_path_size + msg->dest_path_size + 2, msg->thumb_size);
295
296         SAFE_FREE(buf);
297         SAFE_FREE(block_buf);
298
299         return MS_MEDIA_ERR_NONE;
300 }
301
302 int
303 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_un *from_addr, unsigned int *from_size)
304 {
305         int recv_msg_len = 0;
306         unsigned int from_addr_size = sizeof(struct sockaddr_un);
307         unsigned char *buf = NULL;
308
309         buf = (unsigned char*)malloc(sizeof(thumbMsg));
310
311         if ((recv_msg_len = recvfrom(sock, buf, sizeof(thumbMsg), 0, (struct sockaddr *)from_addr, &from_addr_size)) < 0) {
312                 thumb_stderror("recvform failed");
313                 SAFE_FREE(buf);
314                 return _media_thumb_get_error();
315         }
316
317         memcpy(msg, buf, header_size);
318
319         if (msg->origin_path_size <= 0  || msg->origin_path_size > MAX_PATH_SIZE) {
320                 SAFE_FREE(buf);
321                 thumb_err("msg->origin_path_size is invalid %d", msg->origin_path_size );
322                 return MS_MEDIA_ERR_INVALID_PARAMETER;
323         }
324
325         strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size);
326         //thumb_dbg("original path : %s", msg->org_path);
327
328         if (msg->dest_path_size <= 0  || msg->dest_path_size > MAX_PATH_SIZE) {
329                 SAFE_FREE(buf);
330                 thumb_err("msg->origin_path_size is invalid %d", msg->dest_path_size );
331                 return MS_MEDIA_ERR_INVALID_PARAMETER;
332         }
333
334         strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size);
335         //thumb_dbg("destination path : %s", msg->dst_path);
336
337         SAFE_FREE(buf);
338         *from_size = from_addr_size;
339
340         return MS_MEDIA_ERR_NONE;
341 }
342
343 int
344 _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
345 {
346         if (req_msg == NULL || buf == NULL) {
347                 return MS_MEDIA_ERR_INVALID_PARAMETER;
348         }
349
350         int org_path_len = 0;
351         int dst_path_len = 0;
352         int thumb_data_len = 0;
353         int size = 0;
354         int header_size = 0;
355
356         header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
357         org_path_len = req_msg->origin_path_size + 1;
358         dst_path_len = req_msg->dest_path_size + 1;
359         thumb_data_len = req_msg->thumb_size;
360
361         //thumb_dbg("Basic Size : %d, org_path : %s[%d], dst_path : %s[%d], thumb_data_len : %d", header_size, req_msg->org_path, org_path_len, req_msg->dst_path, dst_path_len, thumb_data_len);
362
363         size = header_size + org_path_len + dst_path_len + thumb_data_len;
364         *buf = malloc(size);
365
366         if (*buf == NULL) {
367                 *buf_size = 0;
368                 return 0;
369         }
370         memcpy(*buf, req_msg, header_size);
371         memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
372         memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
373         memcpy((*buf)+header_size + org_path_len + dst_path_len, req_msg->thumb_data, thumb_data_len);
374
375         *buf_size = size;
376
377         return MS_MEDIA_ERR_NONE;
378 }
379
380 int
381 _media_thumb_set_buffer_for_response(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
382 {
383         if (req_msg == NULL || buf == NULL) {
384                 return MS_MEDIA_ERR_INVALID_PARAMETER;
385         }
386
387         int org_path_len = 0;
388         int dst_path_len = 0;
389         int thumb_data_len = 0;
390         int size = 0;
391         int header_size = 0;
392
393         header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
394         org_path_len = req_msg->origin_path_size + 1;
395         dst_path_len = req_msg->dest_path_size + 1;
396         thumb_data_len = 1;
397
398         thumb_dbg("Basic Size : %d, org_path : %s[%d], dst_path : %s[%d], thumb_data_len : %d", header_size, req_msg->org_path, org_path_len, req_msg->dst_path, dst_path_len, thumb_data_len);
399
400         size = header_size + org_path_len + dst_path_len + thumb_data_len;
401         *buf = malloc(size);
402
403         if (*buf == NULL) {
404                 *buf_size = 0;
405                 return 0;
406         }
407         memcpy(*buf, req_msg, header_size);
408         memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
409         memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
410         memcpy((*buf)+header_size + org_path_len + dst_path_len, req_msg->thumb_data, thumb_data_len);
411
412         *buf_size = size;
413
414         return MS_MEDIA_ERR_NONE;
415 }
416
417
418 int
419 _media_thumb_set_add_raw_data_buffer(thumbRawAddMsg *req_msg, unsigned char **buf, int *buf_size)
420 {
421         if (req_msg == NULL || buf == NULL) {
422                 return MS_MEDIA_ERR_INVALID_PARAMETER;
423         }
424         int thumb_len = 0;
425         int size = 0;
426         int header_size = 0;
427
428         header_size = sizeof(thumbRawAddMsg);
429         thumb_len = req_msg->thumb_size;
430
431         size = header_size + thumb_len;
432         *buf = malloc(size);
433         memcpy(*buf, req_msg, header_size);
434         memcpy((*buf)+header_size, req_msg->thumb_data, thumb_len);
435
436         *buf_size = size;
437
438         return MS_MEDIA_ERR_NONE;
439 }
440
441 int
442 _media_thumb_request(int msg_type, media_thumb_type thumb_type, const char *origin_path, char *thumb_path, int max_length, media_thumb_info *thumb_info, uid_t uid)
443 {
444         int sock = -1;
445         struct sockaddr_un serv_addr;
446         ms_sock_info_s sock_info;
447         int recv_str_len = 0;
448         int err = MS_MEDIA_ERR_NONE;
449         int pid;
450         sock_info.port = MS_THUMB_CREATOR_PORT;
451
452         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
453         if (err != MS_MEDIA_ERR_NONE) {
454                 thumb_err("ms_ipc_create_client_socket failed");
455                 return err;
456         }
457
458         memset(&serv_addr, 0, sizeof(serv_addr));
459         sock = sock_info.sock_fd;
460         serv_addr.sun_family = AF_UNIX;
461         strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
462
463         /* Connecting to the thumbnail server */
464         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
465                 thumb_stderror("connect");
466                 ms_ipc_delete_client_socket(&sock_info);
467                 return MS_MEDIA_ERR_SOCKET_CONN;
468         }
469
470         thumbMsg req_msg;
471         thumbMsg recv_msg;
472
473         memset((void *)&req_msg, 0, sizeof(thumbMsg));
474         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
475
476         /* Get PID of client*/
477         pid = getpid();
478         req_msg.pid = pid;
479
480         /* Set requset message */
481         req_msg.msg_type = msg_type;
482         req_msg.thumb_type = thumb_type;
483         req_msg.uid = uid;
484         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
485         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
486
487         if (msg_type == THUMB_REQUEST_SAVE_FILE) {
488                 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
489                 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
490         }
491
492         req_msg.thumb_data = (unsigned char *) "\0";
493
494         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
495         req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
496         req_msg.thumb_size = 1;
497
498         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
499                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
500                 ms_ipc_delete_client_socket(&sock_info);
501                 return MS_MEDIA_ERR_INVALID_PARAMETER;
502         }
503
504         unsigned char *buf = NULL;
505         int buf_size = 0;
506         int header_size = 0;
507
508         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
509         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
510
511         if (send(sock, buf, buf_size, 0) != buf_size) {
512                 thumb_err("sendto failed: %d", errno);
513                 SAFE_FREE(buf);
514                 ms_ipc_delete_client_socket(&sock_info);
515                 return MS_MEDIA_ERR_SOCKET_SEND;
516         }
517
518         thumb_dbg("Sending msg to thumbnail daemon is successful");
519
520         SAFE_FREE(buf);
521
522         if(msg_type != THUMB_REQUEST_CANCEL_ALL_RAW_DATA && msg_type != THUMB_REQUEST_CANCEL_ALL) {             //No response..
523                 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
524                         thumb_err("_media_thumb_recv_msg failed ");
525                         ms_ipc_delete_client_socket(&sock_info);
526                         return err;
527                 }
528
529                 recv_str_len = strlen(recv_msg.org_path);
530                 thumb_dbg_slog("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
531
532                 ms_ipc_delete_client_socket(&sock_info);
533
534                 if (recv_str_len > max_length) {
535                         thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
536                         return MS_MEDIA_ERR_INVALID_PARAMETER;
537                 }
538
539                 if (recv_msg.status == THUMB_FAIL) {
540                         thumb_err("Failed to make thumbnail");
541                         return MS_MEDIA_ERR_INVALID_PARAMETER;
542                 }
543
544                 if (msg_type != THUMB_REQUEST_SAVE_FILE) {
545                         strncpy(thumb_path, recv_msg.dst_path, max_length);
546                 }
547
548                 thumb_info->origin_width = recv_msg.origin_width;
549                 thumb_info->origin_height = recv_msg.origin_height;
550         }else {
551                 thumb_dbg("No response msg_type:[%d]", msg_type);
552         }
553         return MS_MEDIA_ERR_NONE;
554 }
555
556 static int _mkdir(const char *dir, mode_t mode) {
557         char tmp[256];
558         char *p = NULL;
559         size_t len;
560
561         snprintf(tmp, sizeof(tmp),"%s",dir);
562         len = strlen(tmp);
563         if(tmp[len - 1] == '/')
564                 tmp[len - 1] = 0;
565         for(p = tmp + 1; *p; p++)
566                 if(*p == '/') {
567                         *p = 0;
568                         mkdir(tmp, mode);
569                         *p = '/';
570                 }
571         return mkdir(tmp, mode);
572 }
573
574 static char* _media_thumb_get_default_path(uid_t uid)
575 {
576         char *result_psswd = NULL;
577         struct group *grpinfo = NULL;
578         if(uid == getuid())
579         {
580                 result_psswd = strdup(THUMB_DEFAULT_PATH);
581                 grpinfo = getgrnam("users");
582                 if(grpinfo == NULL) {
583                         thumb_err("getgrnam(users) returns NULL !");
584                         if(result_psswd)
585                                 free(result_psswd);
586                         return NULL;
587                 }
588         }
589         else
590         {
591                 struct passwd *userinfo = getpwuid(uid);
592                 if(userinfo == NULL) {
593                         thumb_err("getpwuid(%d) returns NULL !", uid);
594                         return NULL;
595                 }
596                 grpinfo = getgrnam("users");
597                 if(grpinfo == NULL) {
598                         thumb_err("getgrnam(users) returns NULL !");
599                         return NULL;
600                 }
601                 // Compare git_t type and not group name
602                 if (grpinfo->gr_gid != userinfo->pw_gid) {
603                         thumb_err("UID [%d] does not belong to 'users' group!", uid);
604                         return NULL;
605                 }
606                 asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/phone", userinfo->pw_dir);
607         }
608
609         /* create dir */
610         _mkdir(result_psswd,S_IRWXU | S_IRWXG | S_IRWXO);
611
612         return result_psswd;
613 }
614
615 int _media_thumb_process(thumbMsg *req_msg, thumbMsg *res_msg, uid_t uid)
616 {
617         int err = -1;
618         GdkPixbuf *gdkdata = NULL;
619         int thumb_size = 0;
620         int thumb_w = 0;
621         int thumb_h = 0;
622         int origin_w = 0;
623         int origin_h = 0;
624         int max_length = 0;
625         char *thumb_path = NULL;
626         int need_update_db = 0;
627         int alpha = 0;
628
629         if (req_msg == NULL || res_msg == NULL) {
630                 thumb_err("Invalid msg!");
631                 return MS_MEDIA_ERR_INVALID_PARAMETER;
632         }
633
634         int msg_type = req_msg->msg_type;
635         media_thumb_type thumb_type = req_msg->thumb_type;
636         const char *origin_path = req_msg->org_path;
637
638         // Currently, The color space that is supported by the gdk-pixbuf is only RGB.
639         media_thumb_format thumb_format = MEDIA_THUMB_RGB888;
640
641         thumb_path = res_msg->dst_path;
642         thumb_path[0] = '\0';
643         max_length = sizeof(res_msg->dst_path);
644
645         err = _media_thumb_db_connect(uid);
646         if (err != MS_MEDIA_ERR_NONE) {
647                 thumb_err("_media_thumb_mb_svc_connect failed: %d", err);
648                 return MS_MEDIA_ERR_DB_CONNECT_FAIL;
649         }
650
651         if (msg_type == THUMB_REQUEST_DB_INSERT) {
652                 err = _media_thumb_get_thumb_from_db_with_size(origin_path, thumb_path, max_length, &need_update_db, &origin_w, &origin_h);
653                 if (err == MS_MEDIA_ERR_NONE) {
654                         res_msg->origin_width = origin_w;
655                         res_msg->origin_height = origin_h;
656                         _media_thumb_db_disconnect();
657                         return MS_MEDIA_ERR_NONE;
658                 } else {
659                         if (strlen(thumb_path) == 0) {
660                                 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length,uid);
661                                 if (err != MS_MEDIA_ERR_NONE) {
662                                         char *default_path = _media_thumb_get_default_path(uid);
663                                         if(default_path) {
664                                                 thumb_err("_media_thumb_get_hash_name failed - %d\n", err);
665                                                 strncpy(thumb_path, default_path, max_length);
666                                                 free(default_path);
667                                         }
668                                         _media_thumb_db_disconnect();
669                                         return err;
670                                 }
671
672                                 thumb_path[strlen(thumb_path)] = '\0';
673                         }
674                 }
675
676         } else if (msg_type == THUMB_REQUEST_SAVE_FILE) {
677                 strncpy(thumb_path, req_msg->dst_path, max_length);
678
679         } else if (msg_type == THUMB_REQUEST_ALL_MEDIA) {
680                 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length,uid);
681                 if (err < 0) {
682                         char *default_path = _media_thumb_get_default_path(uid);
683                         if(default_path) {
684                                 thumb_err("_media_thumb_get_hash_name failed - %d\n", err);
685                                 strncpy(thumb_path, default_path, max_length);
686                                 free(default_path);
687                         }
688                         _media_thumb_db_disconnect();
689                         return err;
690                 }
691
692                 thumb_path[strlen(thumb_path)] = '\0';
693         }
694
695         if (g_file_test(thumb_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
696                 thumb_warn("thumb path already exists in file system.. remove the existed file");
697                 _media_thumb_remove_file(thumb_path);
698         }
699
700         err = _thumbnail_get_data(origin_path, thumb_type, thumb_format, &gdkdata, &thumb_size, &thumb_w, &thumb_h, &origin_w, &origin_h, &alpha, uid);
701         if (err != MS_MEDIA_ERR_NONE) {
702                 char *default_path = _media_thumb_get_default_path(uid);
703                 thumb_err("_thumbnail_get_data failed - %d\n", err);
704                 if ( gdkdata != NULL ){
705                         g_object_unref(gdkdata);
706                 }
707                 if(default_path) {
708                         strncpy(thumb_path, default_path, max_length);
709                         free(default_path);
710                 }
711                 _media_thumb_db_disconnect();
712                 return err;
713         }
714
715         //thumb_dbg("Size : %d, W:%d, H:%d", thumb_size, thumb_w, thumb_h);
716         //thumb_dbg("Origin W:%d, Origin H:%d\n", origin_w, origin_h);
717         //thumb_dbg("Thumb : %s", thumb_path);
718
719         res_msg->msg_type = THUMB_RESPONSE;
720         res_msg->thumb_size = thumb_size;
721         res_msg->thumb_width = thumb_w;
722         res_msg->thumb_height = thumb_h;
723         res_msg->origin_width = origin_w;
724         res_msg->origin_height = origin_h;
725
726         /* If the image is transparent PNG format, make png file as thumbnail of this image */
727         if (alpha) {
728                 char file_ext[10];
729                 err = _media_thumb_get_file_ext(origin_path, file_ext, sizeof(file_ext));
730                 if (strncasecmp(file_ext, "png", 3) == 0) {
731                         int len = strlen(thumb_path);
732                         thumb_path[len - 3] = 'p';
733                         thumb_path[len - 2] = 'n';
734                         thumb_path[len - 1] = 'g';
735                 }
736                 thumb_dbg("Thumb path is changed : %s", thumb_path);
737         }
738
739         err = _media_thumb_save_to_file_with_gdk(gdkdata, thumb_w, thumb_h, alpha, thumb_path);
740         if (err < 0) {
741                 char *default_path = _media_thumb_get_default_path(uid);
742                 thumb_err("save_to_file_with_gdk failed - %d\n", err);
743                 if ( gdkdata != NULL ){
744                         g_object_unref(gdkdata);
745                 }
746
747                 if (msg_type == THUMB_REQUEST_DB_INSERT || msg_type == THUMB_REQUEST_ALL_MEDIA) {
748                         if(default_path) {
749                                 strncpy(thumb_path, default_path, max_length);
750                                 free(default_path);
751                         }
752                 }
753                 _media_thumb_db_disconnect();
754                 return err;
755         } else {
756                 thumb_dbg("file save success\n");
757         }
758
759         /* fsync */
760         int fd = 0;
761         fd = open(thumb_path, O_WRONLY);
762         if (fd < 0) {
763                 thumb_warn("open failed");
764         } else {
765                 err = fsync(fd);
766                 if (err == -1) {
767                         thumb_warn("fsync failed");
768                 }
769                 close(fd);
770         }
771         /* End of fsync */
772
773         g_object_unref(gdkdata);
774
775         /* DB update if needed */
776         if (need_update_db == 1) {
777                 err = _media_thumb_update_db(origin_path, thumb_path, res_msg->origin_width, res_msg->origin_height, uid);
778                 if (err != MS_MEDIA_ERR_NONE) {
779                         thumb_err("_media_thumb_update_db failed : %d", err);
780                 }
781         }
782
783         _media_thumb_db_disconnect();
784
785         return MS_MEDIA_ERR_NONE;
786 }
787
788 int
789 _media_thumb_process_raw(thumbMsg *req_msg, thumbMsg *res_msg, thumbRawAddMsg *res_raw_msg, uid_t uid)
790 {
791         int err = MS_MEDIA_ERR_NONE;
792         unsigned char *data = NULL;
793         int thumb_size = 0;
794         int thumb_w = 0;
795         int thumb_h = 0;
796
797         if (req_msg == NULL || res_msg == NULL) {
798                 thumb_err("Invalid msg!");
799                 return MS_MEDIA_ERR_INVALID_PARAMETER;
800         }
801
802         const char *origin_path = req_msg->org_path;
803
804         media_thumb_format thumb_format = MEDIA_THUMB_BGRA;
805         thumb_w = req_msg->thumb_width;
806         thumb_h = req_msg->thumb_height;
807
808         //thumb_dbg("origin_path : %s, thumb_w : %d, thumb_h : %d ", origin_path, thumb_w, thumb_h);
809
810         if (!g_file_test(origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
811                 thumb_err("origin_path does not exist in file system.");
812                 return MS_MEDIA_ERR_FILE_NOT_EXIST;
813         }
814
815         err = _thumbnail_get_raw_data(origin_path, thumb_format, &data, &thumb_size, &thumb_w, &thumb_h, uid);
816
817         if (err != MS_MEDIA_ERR_NONE) {
818                 thumb_err("_thumbnail_get_data failed - %d", err);
819                 SAFE_FREE(data);
820         }
821
822         res_msg->msg_type = THUMB_RESPONSE_RAW_DATA;
823         res_msg->thumb_width = thumb_w;
824         res_msg->thumb_height = thumb_h;
825         res_raw_msg->thumb_size = thumb_size;
826         res_raw_msg->thumb_data = malloc(thumb_size * sizeof(unsigned char));
827         memcpy(res_raw_msg->thumb_data, data, thumb_size);
828         res_msg->thumb_size = thumb_size + sizeof(thumbRawAddMsg) - sizeof(unsigned char*);
829
830         //thumb_dbg("Size : %d, W:%d, H:%d", thumb_size, thumb_w, thumb_h);
831
832         return MS_MEDIA_ERR_NONE;
833 }
834
835 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
836 {
837         thumbMsg recv_msg;
838         int header_size = 0;
839         int sock = 0;
840         int err = MS_MEDIA_ERR_NONE;
841
842         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
843         sock = g_io_channel_unix_get_fd(src);
844
845         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
846
847         thumb_err("_media_thumb_write_socket socket : %d", sock);
848
849         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
850                 thumb_err("_media_thumb_recv_msg failed ");
851                 if (recv_msg.origin_path_size > 0) {
852                         __media_thumb_pop_req_queue(recv_msg.org_path, TRUE);
853                 } else {
854                         thumb_err("origin path size is wrong.");
855                 }
856
857                 return FALSE;
858         }
859
860         g_io_channel_shutdown(src, TRUE, NULL);
861         g_io_channel_unref(src);
862         //thumb_dbg("Completed..%s", recv_msg.org_path);
863
864         if (recv_msg.status == THUMB_FAIL) {
865                 thumb_err("Failed to make thumbnail");
866                 err = MS_MEDIA_ERR_INTERNAL;
867         }
868
869         if (data) {
870                 thumbUserData* cb = (thumbUserData*)data;
871                 if (cb->func != NULL)
872                         cb->func(err, recv_msg.dst_path, cb->user_data);
873         }
874
875         __media_thumb_pop_req_queue(recv_msg.org_path, FALSE);
876
877         thumb_dbg("Done");
878         return FALSE;
879 }
880
881 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
882 {
883         thumbMsg recv_msg;
884         int header_size = 0;
885         int sock = 0;
886         int err = MS_MEDIA_ERR_NONE;
887
888         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
889         sock = g_io_channel_unix_get_fd(src);
890
891         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
892
893         thumb_err("_media_thumb_write_socket socket : %d", sock);
894
895         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
896                 thumb_err("_media_thumb_recv_msg failed ");
897                 if (recv_msg.request_id > 0) {
898                         __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, TRUE);
899                 } else {
900                         thumb_err("origin path size is wrong.");
901                 }
902
903                 return FALSE;
904         }
905
906         g_io_channel_shutdown(src, TRUE, NULL);
907         g_io_channel_unref(src);
908
909         if (recv_msg.status == THUMB_FAIL) {
910                 thumb_err("Failed to make thumbnail");
911                 err = MS_MEDIA_ERR_INTERNAL;
912         }
913
914         if (data) {
915                 thumbRawUserData* cb = (thumbRawUserData*)data;
916                 if (cb->func != NULL)
917                         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);
918         }
919
920         __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, FALSE);
921
922         thumb_dbg("Done");
923
924         SAFE_FREE(recv_msg.thumb_data);
925
926         return FALSE;
927 }
928
929 int _media_thumb_request_async(int msg_type, media_thumb_type thumb_type, const char *origin_path, thumbUserData *userData, uid_t uid)
930 {
931         int err = MS_MEDIA_ERR_NONE;
932         int sock = -1;
933         struct sockaddr_un serv_addr;
934         ms_sock_info_s sock_info;
935         int pid;
936         sock_info.port = MS_THUMB_CREATOR_PORT;
937
938         if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
939                 return MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST;
940         }
941
942         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
943         if(err != MS_MEDIA_ERR_NONE)
944         {
945                 thumb_err("ms_ipc_create_client_socket failed");
946                 return err;
947         }
948         GIOChannel *channel = NULL;
949         channel = g_io_channel_unix_new(sock);
950         int source_id = -1;
951
952
953         memset(&serv_addr, 0, sizeof(serv_addr));
954         sock = sock_info.sock_fd;
955         serv_addr.sun_family = AF_UNIX;
956
957         strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
958
959         /* Connecting to the thumbnail server */
960         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
961                 thumb_stderror("connect");
962                 g_io_channel_shutdown(channel, TRUE, NULL);
963                 g_io_channel_unref(channel);
964                 return MS_MEDIA_ERR_SOCKET_CONN;
965         }
966
967         if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
968                 //source_id = g_io_add_watch(channel, G_IO_IN, _media_thumb_write_socket, userData );
969
970                 /* Create new channel to watch udp socket */
971                 GSource *source = NULL;
972                 source = g_io_create_watch(channel, G_IO_IN);
973
974                 /* Set callback to be called when socket is readable */
975                 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, userData, NULL);
976                 source_id = g_source_attach(source, g_main_context_get_thread_default());
977         }
978
979         thumbMsg req_msg;
980         memset((void *)&req_msg, 0, sizeof(thumbMsg));
981
982         pid = getpid();
983         req_msg.pid = pid;
984         req_msg.msg_type = msg_type;
985         req_msg.thumb_type = thumb_type;
986         req_msg.uid = uid;
987
988         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
989         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
990         req_msg.dst_path[0] = '\0';
991         req_msg.thumb_data = (unsigned char *)"\0";
992
993         req_msg.origin_path_size = strlen(req_msg.org_path);
994         req_msg.dest_path_size = 1;
995         req_msg.thumb_size = 1;
996
997         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
998                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
999                 g_io_channel_shutdown(channel, TRUE, NULL);
1000                 g_io_channel_unref(channel);
1001                 ms_ipc_delete_client_socket(&sock_info);
1002                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1003         }
1004
1005         unsigned char *buf = NULL;
1006         int buf_size = 0;
1007         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
1008
1009         if (send(sock, buf, buf_size, 0) != buf_size) {
1010                 thumb_err("sendto failed: %d", errno);
1011                 SAFE_FREE(buf);
1012                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
1013                 g_io_channel_shutdown(channel, TRUE, NULL);
1014                 g_io_channel_unref(channel);
1015                 ms_ipc_delete_client_socket(&sock_info);
1016                 return MS_MEDIA_ERR_SOCKET_SEND;
1017         }
1018
1019         SAFE_FREE(buf);
1020         thumb_dbg("Sending msg to thumbnail daemon is successful");
1021
1022         if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
1023                 g_io_channel_shutdown(channel, TRUE, NULL);
1024
1025                 //thumb_dbg("Cancel : %s[%d]", origin_path, sock);
1026                 __media_thumb_pop_req_queue(origin_path, TRUE);
1027         } else if (msg_type == THUMB_REQUEST_DB_INSERT) {
1028                 if (g_request_queue == NULL) {
1029                         g_request_queue = g_queue_new();
1030                 }
1031
1032                 thumbReq *thumb_req = NULL;
1033                 thumb_req = calloc(1, sizeof(thumbReq));
1034                 if (thumb_req == NULL) {
1035                         thumb_err("Failed to create request element");
1036                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1037                 }
1038
1039                 thumb_req->channel = channel;
1040                 thumb_req->path = strdup(origin_path);
1041                 thumb_req->source_id = source_id;
1042                 thumb_req->userData = userData;
1043
1044                 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
1045         }
1046
1047         return MS_MEDIA_ERR_NONE;
1048 }
1049
1050 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)
1051 {
1052         int err = MS_MEDIA_ERR_NONE;
1053         int sock;
1054         struct sockaddr_un serv_addr;
1055         ms_sock_info_s sock_info;
1056         int pid;
1057         sock_info.port = MS_THUMB_CREATOR_PORT;
1058
1059         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
1060         if(err != MS_MEDIA_ERR_NONE)
1061         {
1062                 thumb_err("ms_ipc_create_client_socket failed");
1063                 return err;
1064         }
1065         GIOChannel *channel = NULL;
1066         channel = g_io_channel_unix_new(sock);
1067         int source_id = -1;
1068
1069
1070         memset(&serv_addr, 0, sizeof(serv_addr));
1071         sock = sock_info.sock_fd;
1072         serv_addr.sun_family = AF_UNIX;
1073
1074         strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
1075
1076         /* Connecting to the thumbnail server */
1077         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
1078                 thumb_err("connect error : %s", strerror(errno));
1079                 g_io_channel_shutdown(channel, TRUE, NULL);
1080                 g_io_channel_unref(channel);
1081                 return MS_MEDIA_ERR_SOCKET_CONN;
1082         }
1083
1084         if (msg_type != THUMB_REQUEST_CANCEL_RAW_DATA) {
1085                 /* Create new channel to watch udp socket */
1086                 GSource *source = NULL;
1087                 source = g_io_create_watch(channel, G_IO_IN);
1088
1089                 /* Set callback to be called when socket is readable */
1090                 /*NEED UPDATE SOCKET FILE DELETE*/
1091                 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, userData, NULL);
1092                 source_id = g_source_attach(source, g_main_context_get_thread_default());
1093         }
1094
1095         thumbMsg req_msg;
1096         memset((void *)&req_msg, 0, sizeof(thumbMsg));
1097
1098         pid = getpid();
1099         req_msg.pid = pid;
1100         req_msg.msg_type = msg_type;
1101         req_msg.thumb_type = MEDIA_THUMB_LARGE;
1102         req_msg.request_id = request_id;
1103         req_msg.thumb_width = width;
1104         req_msg.thumb_height = height;
1105         req_msg.uid = uid;
1106
1107         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
1108         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
1109         req_msg.dst_path[0] = '\0';
1110         req_msg.thumb_data = (unsigned char *)"\0";
1111
1112         req_msg.origin_path_size = strlen(req_msg.org_path);
1113         req_msg.dest_path_size = 1;
1114         req_msg.thumb_size = 1;
1115
1116         if (req_msg.origin_path_size > MAX_PATH_SIZE) {
1117                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
1118                 g_io_channel_shutdown(channel, TRUE, NULL);
1119                 g_io_channel_unref(channel);
1120                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1121         }
1122
1123         unsigned char *buf = NULL;
1124         int buf_size = 0;
1125         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
1126
1127         if (send(sock, buf, buf_size, 0) != buf_size) {
1128                 thumb_err("sendto failed: %d", errno);
1129                 SAFE_FREE(buf);
1130                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
1131                 g_io_channel_shutdown(channel, TRUE, NULL);
1132                 g_io_channel_unref(channel);
1133                 return MS_MEDIA_ERR_SOCKET_SEND;
1134         }
1135
1136         SAFE_FREE(buf);
1137         thumb_dbg("Sending msg to thumbnail daemon is successful");
1138
1139         if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA) {
1140                 g_io_channel_shutdown(channel, TRUE, NULL);
1141                 __media_thumb_pop_raw_data_req_queue(request_id, TRUE);
1142         } else if (msg_type == THUMB_REQUEST_RAW_DATA) {
1143                 if (g_request_queue == NULL) {
1144                         g_request_queue = g_queue_new();
1145                 }
1146                 thumbRawReq *thumb_req = NULL;
1147                 thumb_req = calloc(1, sizeof(thumbReq));
1148                 if (thumb_req == NULL) {
1149                         thumb_err("Failed to create request element");
1150                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1151                 }
1152                 thumb_req->channel = channel;
1153                 thumb_req->request_id = request_id;
1154                 thumb_req->source_id = source_id;
1155                 thumb_req->userData = userData;
1156
1157                 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
1158         }
1159         return MS_MEDIA_ERR_NONE;
1160 }