Remove Ecore evas dependency
[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 <glib.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <grp.h>
31 #include <pwd.h>
32
33 #define GLOBAL_USER    0 //#define     tzplatform_getenv(TZ_GLOBAL) //TODO
34
35 static __thread GQueue *g_request_queue = NULL;
36 typedef struct {
37         GIOChannel *channel;
38         char *path;
39         int source_id;
40 } thumbReq;
41
42 int
43 _media_thumb_create_socket(int sock_type, int *sock)
44 {
45         int sock_fd = 0;
46
47         if ((sock_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
48                 thumb_err("socket failed: %s", strerror(errno));
49                 return MEDIA_THUMB_ERROR_NETWORK;
50         }
51
52         if (sock_type == CLIENT_SOCKET) {
53
54 #ifdef _USE_MEDIA_UTIL_
55                 struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 };
56 #else
57                 struct timeval tv_timeout = { TIMEOUT_SEC, 0 };
58 #endif
59
60                 if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) {
61                         thumb_err("setsockopt failed: %s", strerror(errno));
62                         close(sock_fd);
63                         return MEDIA_THUMB_ERROR_NETWORK;
64                 }
65         } else if (sock_type == SERVER_SOCKET) {
66
67                 int n_reuse = 1;
68
69                 if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &n_reuse, sizeof(n_reuse)) == -1) {
70                         thumb_err("setsockopt failed: %s", strerror(errno));
71                         close(sock_fd);
72                         return MEDIA_THUMB_ERROR_NETWORK;
73                 }
74         }
75
76         *sock = sock_fd;
77
78         return MEDIA_THUMB_ERROR_NONE;
79 }
80
81
82 int
83 _media_thumb_create_udp_socket(int *sock)
84 {
85         int sock_fd = 0;
86
87         if ((sock_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
88                 thumb_err("socket failed: %s", strerror(errno));
89                 return MEDIA_THUMB_ERROR_NETWORK;
90         }
91
92 #ifdef _USE_MEDIA_UTIL_
93         struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 };
94 #else
95         struct timeval tv_timeout = { TIMEOUT_SEC, 0 };
96 #endif
97
98         if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) {
99                 thumb_err("setsockopt failed: %s", strerror(errno));
100                 close(sock_fd);
101                 return MEDIA_THUMB_ERROR_NETWORK;
102         }
103
104         *sock = sock_fd;
105
106         return MEDIA_THUMB_ERROR_NONE;
107 }
108
109 int _media_thumb_get_error()
110 {
111         if (errno == EWOULDBLOCK) {
112                 thumb_err("Timeout. Can't try any more");
113                 return MEDIA_THUMB_ERROR_TIMEOUT;
114         } else {
115                 thumb_err("recvfrom failed : %s", strerror(errno));
116                 return MEDIA_THUMB_ERROR_NETWORK;
117         }
118 }
119
120 int __media_thumb_pop_req_queue(const char *path, bool shutdown_channel)
121 {
122         int req_len = 0, i;
123
124         if (g_request_queue == NULL) return -1;
125         req_len = g_queue_get_length(g_request_queue);
126
127 //      thumb_dbg("Queue length : %d", req_len);
128 //      thumb_dbg("Queue path : %s", path);
129
130         if (req_len <= 0) {
131 //              thumb_dbg("There is no request in the queue");
132         } else {
133
134                 for (i = 0; i < req_len; i++) {
135                         thumbReq *req = NULL;
136                         req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
137                         if (req == NULL) continue;
138
139                         if (strncmp(path, req->path, strlen(path)) == 0) {
140                                 //thumb_dbg("Popped %s", path);
141                                 if (shutdown_channel) {
142                                         g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id));
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);
151
152                                 break;
153                         }
154                 }
155                 if (i == req_len) {
156 //                      thumb_dbg("There's no %s in the queue", path);
157                 }
158         }
159
160         return MEDIA_THUMB_ERROR_NONE;
161 }
162
163 int __media_thumb_check_req_queue(const char *path)
164 {
165         int req_len = 0, i;
166
167         req_len = g_queue_get_length(g_request_queue);
168
169 //      thumb_dbg("Queue length : %d", req_len);
170 //      thumb_dbg("Queue path : %s", path);
171
172         if (req_len <= 0) {
173 //              thumb_dbg("There is no request in the queue");
174         } else {
175
176                 for (i = 0; i < req_len; i++) {
177                         thumbReq *req = NULL;
178                         req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
179                         if (req == NULL) continue;
180
181                         if (strncmp(path, req->path, strlen(path)) == 0) {
182                                 //thumb_dbg("Same Request - %s", path);
183                                 return -1;
184
185                                 break;
186                         }
187                 }
188         }
189
190         return MEDIA_THUMB_ERROR_NONE;
191 }
192
193 int
194 _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
195 {
196         int recv_msg_len = 0;
197         unsigned char *buf = NULL;
198
199         buf = (unsigned char*)malloc(header_size);
200
201         if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
202                 thumb_err("recvfrom failed : %s", strerror(errno));
203                 SAFE_FREE(buf);
204                 return _media_thumb_get_error();
205         }
206
207         memcpy(msg, buf, header_size);
208         //thumb_dbg("origin_path_size : %d, dest_path_size : %d", msg->origin_path_size, msg->dest_path_size);
209
210         SAFE_FREE(buf);
211
212         buf = (unsigned char*)malloc(msg->origin_path_size);
213
214         if ((recv_msg_len = recv(sock, buf, msg->origin_path_size, 0)) < 0) {
215                 thumb_err("recvfrom failed : %s", strerror(errno));
216                 SAFE_FREE(buf);
217                 return _media_thumb_get_error();
218         }
219
220         strncpy(msg->org_path, (char*)buf, msg->origin_path_size);
221         //thumb_dbg("original path : %s", msg->org_path);
222
223         SAFE_FREE(buf);
224         buf = (unsigned char*)malloc(msg->dest_path_size);
225
226         if ((recv_msg_len = recv(sock, buf, msg->dest_path_size, 0)) < 0) {
227                 thumb_err("recvfrom failed : %s", strerror(errno));
228                 SAFE_FREE(buf);
229                 return _media_thumb_get_error();
230         }
231
232         strncpy(msg->dst_path, (char*)buf, msg->dest_path_size);
233         //thumb_dbg("destination path : %s", msg->dst_path);
234
235         SAFE_FREE(buf);
236         return MEDIA_THUMB_ERROR_NONE;
237 }
238
239 #ifdef _USE_UDS_SOCKET_
240 int
241 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_un *from_addr, unsigned int *from_size)
242 #else
243 int
244 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_in *from_addr, unsigned int *from_size)
245 #endif
246 {
247         int recv_msg_len = 0;
248 #ifdef _USE_UDS_SOCKET_
249         unsigned int from_addr_size = sizeof(struct sockaddr_un);
250 #else
251         unsigned int from_addr_size = sizeof(struct sockaddr_in);
252 #endif
253         unsigned char *buf = NULL;
254
255         buf = (unsigned char*)malloc(sizeof(thumbMsg));
256
257         if ((recv_msg_len = recvfrom(sock, buf, sizeof(thumbMsg), 0, (struct sockaddr *)from_addr, &from_addr_size)) < 0) {
258                 thumb_err("recvfrom failed : %s", strerror(errno));
259                 SAFE_FREE(buf);
260                 return _media_thumb_get_error();
261         }
262
263         memcpy(msg, buf, header_size);
264
265         if (msg->origin_path_size <= 0  || msg->origin_path_size > MAX_PATH_SIZE) {
266                 SAFE_FREE(buf);
267                 thumb_err("msg->origin_path_size is invalid %d", msg->origin_path_size );
268                 return MEDIA_THUMB_ERROR_NETWORK;
269         }
270
271         strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size);
272         //thumb_dbg("original path : %s", msg->org_path);
273
274         if (msg->dest_path_size <= 0  || msg->dest_path_size > MAX_PATH_SIZE) {
275                 SAFE_FREE(buf);
276                 thumb_err("msg->origin_path_size is invalid %d", msg->dest_path_size );
277                 return MEDIA_THUMB_ERROR_NETWORK;
278         }
279
280         strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size);
281         //thumb_dbg("destination path : %s", msg->dst_path);
282
283         SAFE_FREE(buf);
284         *from_size = from_addr_size;
285
286         return MEDIA_THUMB_ERROR_NONE;
287 }
288
289 int
290 _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
291 {
292         if (req_msg == NULL || buf == NULL) {
293                 return -1;
294         }
295
296         int org_path_len = 0;
297         int dst_path_len = 0;
298         int size = 0;
299         int header_size = 0;
300
301         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
302         org_path_len = strlen(req_msg->org_path) + 1;
303         dst_path_len = strlen(req_msg->dst_path) + 1;
304
305         //thumb_dbg("Basic Size : %d, org_path : %s[%d], dst_path : %s[%d]", header_size, req_msg->org_path, org_path_len, req_msg->dst_path, dst_path_len);
306
307         size = header_size + org_path_len + dst_path_len;
308         *buf = malloc(size);
309         memcpy(*buf, req_msg, header_size);
310         memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
311         memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
312
313         *buf_size = size;
314
315         return 0;
316 }
317
318 int
319 _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)
320 {
321         int sock;
322 #ifdef _USE_UDS_SOCKET_
323         struct sockaddr_un serv_addr;
324 #elif defined(_USE_UDS_SOCKET_TCP_)
325         struct sockaddr_un serv_addr;
326 #else
327         const char *serv_ip = "127.0.0.1";
328         struct sockaddr_in serv_addr;
329 #endif
330
331         int recv_str_len = 0;
332         int err;
333         int pid;
334
335
336 #ifdef _USE_MEDIA_UTIL_
337 #ifdef _USE_UDS_SOCKET_
338         if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_PORT) < 0) {
339 #elif defined(_USE_UDS_SOCKET_TCP_)
340         if (ms_ipc_create_client_tcp_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_TCP_PORT) < 0) {
341 #else
342         if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock) < 0) {
343 #endif
344                 thumb_err("ms_ipc_create_client_socket failed");
345                 return MEDIA_THUMB_ERROR_NETWORK;
346         }
347 #else
348         /* Creaete a TCP socket */
349         if (_media_thumb_create_socket(CLIENT_SOCKET, &sock) < 0) {
350                 thumb_err("_media_thumb_create_socket failed");
351                 return MEDIA_THUMB_ERROR_NETWORK;
352         }
353 #endif
354
355         memset(&serv_addr, 0, sizeof(serv_addr));
356 #ifdef _USE_UDS_SOCKET_
357         serv_addr.sun_family = AF_UNIX;
358 #elif defined(_USE_UDS_SOCKET_TCP_)
359         serv_addr.sun_family = AF_UNIX;
360 #else
361         serv_addr.sin_family = AF_INET;
362         serv_addr.sin_addr.s_addr = inet_addr(serv_ip);
363 #endif
364
365 #ifdef _USE_MEDIA_UTIL_
366 #ifdef _USE_UDS_SOCKET_
367         strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
368 #elif defined(_USE_UDS_SOCKET_TCP_)
369         thumb_dbg("");
370         strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
371 #else
372         serv_addr.sin_port = htons(MS_THUMB_CREATOR_PORT);
373 #endif
374 #else
375         serv_addr.sin_port = htons(THUMB_DAEMON_PORT);
376 #endif
377
378         /* Connecting to the thumbnail server */
379         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
380                 thumb_err("connect error : %s", strerror(errno));
381                 return MEDIA_THUMB_ERROR_NETWORK;
382         }
383
384         thumbMsg req_msg;
385         thumbMsg recv_msg;
386
387         memset((void *)&req_msg, 0, sizeof(thumbMsg));
388         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
389
390         /* Get PID of client*/
391         pid = getpid();
392         req_msg.pid = pid;
393
394         /* Set requset message */
395         req_msg.msg_type = msg_type;
396         req_msg.thumb_type = thumb_type;
397         req_msg.uid = uid;
398         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
399         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
400
401         if (msg_type == THUMB_REQUEST_SAVE_FILE) {
402                 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
403                 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
404         }
405
406         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
407         req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
408
409         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
410                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
411                 close(sock);
412                 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
413         }
414
415         unsigned char *buf = NULL;
416         int buf_size = 0;
417         int header_size = 0;
418
419         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
420         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
421
422         if (send(sock, buf, buf_size, 0) != buf_size) {
423                 thumb_err("sendto failed: %d\n", errno);
424                 SAFE_FREE(buf);
425                 close(sock);
426                 return MEDIA_THUMB_ERROR_NETWORK;
427         }
428
429         thumb_dbg("Sending msg to thumbnail daemon is successful");
430
431         SAFE_FREE(buf);
432
433         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
434                 thumb_err("_media_thumb_recv_msg failed ");
435                 close(sock);
436                 return err;
437         }
438
439         recv_str_len = strlen(recv_msg.org_path);
440         thumb_dbg("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
441
442         close(sock);
443
444         if (recv_str_len > max_length) {
445                 thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
446                 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
447         }
448
449         if (recv_msg.status == THUMB_FAIL) {
450                 thumb_err("Failed to make thumbnail");
451                 return -1;
452         }
453
454         if (msg_type != THUMB_REQUEST_SAVE_FILE) {
455                 strncpy(thumb_path, recv_msg.dst_path, max_length);
456         }
457
458         thumb_info->origin_width = recv_msg.origin_width;
459         thumb_info->origin_height = recv_msg.origin_height;
460
461         return 0;
462 }
463
464 static int _mkdir(const char *dir, mode_t mode) {
465         char tmp[256];
466         char *p = NULL;
467         size_t len;
468
469         snprintf(tmp, sizeof(tmp),"%s",dir);
470         len = strlen(tmp);
471         if(tmp[len - 1] == '/')
472                 tmp[len - 1] = 0;
473         for(p = tmp + 1; *p; p++)
474                 if(*p == '/') {
475                         *p = 0;
476                         mkdir(tmp, mode);
477                         *p = '/';
478                 }
479         return mkdir(tmp, mode);
480 }
481
482 static char* _media_thumb_get_default_path(uid_t uid)
483 {
484         char *result_psswd = NULL;
485         struct group *grpinfo = NULL;
486         if(uid == getuid())
487         {
488                 result_psswd = strdup(THUMB_DEFAULT_PATH);
489                 grpinfo = getgrnam("users");
490                 if(grpinfo == NULL) {
491                         thumb_err("getgrnam(users) returns NULL !");
492                         return NULL;
493                 }
494         }
495         else
496         {
497                 struct passwd *userinfo = getpwuid(uid);
498                 if(userinfo == NULL) {
499                         thumb_err("getpwuid(%d) returns NULL !", uid);
500                         return NULL;
501                 }
502                 grpinfo = getgrnam("users");
503                 if(grpinfo == NULL) {
504                         thumb_err("getgrnam(users) returns NULL !");
505                         return NULL;
506                 }
507                 // Compare git_t type and not group name
508                 if (grpinfo->gr_gid != userinfo->pw_gid) {
509                         thumb_err("UID [%d] does not belong to 'users' group!", uid);
510                         return NULL;
511                 }
512                 asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/phone", userinfo->pw_dir);
513         }
514
515         /* create dir */
516         _mkdir(result_psswd,S_IRWXU | S_IRWXG | S_IRWXO);
517
518         return result_psswd;
519 }
520
521 int
522 _media_thumb_process(thumbMsg *req_msg, thumbMsg *res_msg, uid_t uid)
523 {
524         int err = -1;
525         GdkPixbuf *data = NULL;
526         int thumb_size = 0;
527         int thumb_w = 0;
528         int thumb_h = 0;
529         int origin_w = 0;
530         int origin_h = 0;
531         int max_length = 0;
532         char *thumb_path = NULL;
533         int need_update_db = 0;
534         int alpha = 0;
535
536         if (req_msg == NULL || res_msg == NULL) {
537                 thumb_err("Invalid msg!");
538                 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
539         }
540
541         int msg_type = req_msg->msg_type;
542         media_thumb_type thumb_type = req_msg->thumb_type;
543         const char *origin_path = req_msg->org_path;
544
545         media_thumb_format thumb_format = MEDIA_THUMB_BGRA;
546
547         thumb_path = res_msg->dst_path;
548         thumb_path[0] = '\0';
549         max_length = sizeof(res_msg->dst_path);
550
551         err = _media_thumb_db_connect(uid);
552         if (err < 0) {
553                 thumb_err("_media_thumb_mb_svc_connect failed: %d", err);
554                 return MEDIA_THUMB_ERROR_DB;
555         }
556
557         if (msg_type == THUMB_REQUEST_DB_INSERT) {
558                 err = _media_thumb_get_thumb_from_db_with_size(origin_path, thumb_path, max_length, &need_update_db, &origin_w, &origin_h);
559                 if (err == 0) {
560                         res_msg->origin_width = origin_w;
561                         res_msg->origin_height = origin_h;
562                         _media_thumb_db_disconnect();
563                         return MEDIA_THUMB_ERROR_NONE;
564                 } else {
565                         if (strlen(thumb_path) == 0) {
566                                 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length,uid);
567                                 if (err < 0) {
568                                         thumb_err("_media_thumb_get_hash_name failed - %d\n", err);
569                                         strncpy(thumb_path, _media_thumb_get_default_path(uid), max_length);
570                                         _media_thumb_db_disconnect();
571                                         return err;
572                                 }
573
574                                 thumb_path[strlen(thumb_path)] = '\0';
575                         }
576                 }
577
578         } else if (msg_type == THUMB_REQUEST_SAVE_FILE) {
579                 strncpy(thumb_path, req_msg->dst_path, max_length);
580
581         } else if (msg_type == THUMB_REQUEST_ALL_MEDIA) {
582                 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length,uid);
583                 if (err < 0) {
584                         thumb_err("_media_thumb_get_hash_name failed - %d\n", err);
585                         strncpy(thumb_path, _media_thumb_get_default_path(uid), max_length);
586                         _media_thumb_db_disconnect();
587                         return err;
588                 }
589
590                 thumb_path[strlen(thumb_path)] = '\0';
591         }
592
593         if (g_file_test(thumb_path, 
594                                         G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
595                 thumb_warn("thumb path already exists in file system.. remove the existed file");
596                 _media_thumb_remove_file(thumb_path);
597         }
598
599         err = _thumbnail_get_data(origin_path, thumb_type, thumb_format, &data, &thumb_size, &thumb_w, &thumb_h, &origin_w, &origin_h, &alpha, uid);
600         if (err < 0) {
601                 thumb_err("_thumbnail_get_data failed - %d\n", err);
602                 g_object_unref(data);
603
604                 strncpy(thumb_path, _media_thumb_get_default_path(uid), max_length);
605                 _media_thumb_db_disconnect();
606                 return err;
607         }
608
609         //thumb_dbg("Size : %d, W:%d, H:%d", thumb_size, thumb_w, thumb_h);
610         //thumb_dbg("Origin W:%d, Origin H:%d\n", origin_w, origin_h);
611         //thumb_dbg("Thumb : %s", thumb_path);
612
613         res_msg->msg_type = THUMB_RESPONSE;
614         res_msg->thumb_size = thumb_size;
615         res_msg->thumb_width = thumb_w;
616         res_msg->thumb_height = thumb_h;
617         res_msg->origin_width = origin_w;
618         res_msg->origin_height = origin_h;
619
620         /* If the image is transparent PNG format, make png file as thumbnail of this image */
621         if (alpha) {
622                 char file_ext[10];
623                 err = _media_thumb_get_file_ext(origin_path, file_ext, sizeof(file_ext));
624                 if (strncasecmp(file_ext, "png", 3) == 0) {
625                         int len = strlen(thumb_path);
626                         thumb_path[len - 3] = 'p';
627                         thumb_path[len - 2] = 'n';
628                         thumb_path[len - 1] = 'g';
629                 }
630                 thumb_dbg("Thumb path is changed : %s", thumb_path);
631         }
632
633         err = _media_thumb_save_to_file_with_gdk(data, thumb_w, thumb_h, alpha, thumb_path);
634         if (err < 0) {
635                 thumb_err("save_to_file_with_gdk failed - %d\n", err);
636                 g_object_unref(data);
637
638                 if (msg_type == THUMB_REQUEST_DB_INSERT || msg_type == THUMB_REQUEST_ALL_MEDIA)
639                         strncpy(thumb_path, _media_thumb_get_default_path(uid), max_length);
640
641                 _media_thumb_db_disconnect();
642                 return err;
643         } else {
644                 thumb_dbg("file save success\n");
645         }
646
647         /* fsync */
648         int fd = 0;
649         fd = open(thumb_path, O_WRONLY);
650         if (fd < 0) {
651                 thumb_warn("open failed");
652         } else {
653                 err = fsync(fd);
654                 if (err == -1) {
655                         thumb_warn("fsync failed");
656                 }
657
658                 close(fd);
659         }
660         /* End of fsync */
661
662         g_object_unref(data);
663
664         /* DB update if needed */
665         if (need_update_db == 1) {
666                 err = _media_thumb_update_db(origin_path, thumb_path, res_msg->origin_width, res_msg->origin_height, uid);
667                 if (err < 0) {
668                         thumb_err("_media_thumb_update_db failed : %d", err);
669                 }
670         }
671
672         _media_thumb_db_disconnect();
673
674         return 0;
675 }
676
677 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
678 {
679         thumbMsg recv_msg;
680         int header_size = 0;
681         int recv_str_len = 0;
682         int sock = 0;
683         int err = MEDIA_THUMB_ERROR_NONE;
684
685         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
686         sock = g_io_channel_unix_get_fd(src);
687
688         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
689
690         thumb_err("_media_thumb_write_socket socket : %d", sock);
691
692         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
693                 thumb_err("_media_thumb_recv_msg failed ");
694                 g_io_channel_shutdown(src, TRUE, NULL);
695                 return FALSE;
696         }
697
698         recv_str_len = strlen(recv_msg.dst_path);
699         thumb_dbg("recv %s(%d) in  [ %s ] from thumb daemon is successful", recv_msg.dst_path, recv_str_len, recv_msg.org_path);
700
701         g_io_channel_shutdown(src, TRUE, NULL);
702
703         //thumb_dbg("Completed..%s", recv_msg.org_path);
704         __media_thumb_pop_req_queue(recv_msg.org_path, FALSE);
705
706         if (recv_msg.status == THUMB_FAIL) {
707                 thumb_err("Failed to make thumbnail");
708                 err = MEDIA_THUMB_ERROR_UNSUPPORTED;
709                 goto callback;
710         }
711
712 callback:
713         if (data) {
714                 thumbUserData* cb = (thumbUserData*)data;
715                 cb->func(err, recv_msg.dst_path, cb->user_data);
716                 free(cb);
717                 cb = NULL;
718         }
719
720         thumb_dbg("Done");
721         return FALSE;
722 }
723
724 int
725 _media_thumb_request_async(int msg_type, media_thumb_type thumb_type, const char *origin_path, thumbUserData *userData, uid_t uid)
726 {
727         int sock;
728 #ifdef _USE_UDS_SOCKET_
729         struct sockaddr_un serv_addr;
730 #elif defined(_USE_UDS_SOCKET_TCP_)
731         struct sockaddr_un serv_addr;
732 #else
733         const char *serv_ip = "127.0.0.1";
734         struct sockaddr_in serv_addr;
735 #endif
736
737         int pid;
738
739         if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
740                 return MEDIA_THUMB_ERROR_DUPLICATED_REQUEST;
741         }
742
743 #ifdef _USE_MEDIA_UTIL_
744 #ifdef _USE_UDS_SOCKET_
745         if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_PORT) < 0) {
746 #elif defined(_USE_UDS_SOCKET_TCP_)
747         if (ms_ipc_create_client_tcp_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_TCP_PORT) < 0) {
748 #else
749         if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock) < 0) {
750 #endif
751                 thumb_err("ms_ipc_create_client_socket failed");
752                 return MEDIA_THUMB_ERROR_NETWORK;
753         }
754 #else
755         /* Creaete a TCP socket */
756         if (_media_thumb_create_socket(CLIENT_SOCKET, &sock) < 0) {
757                 thumb_err("_media_thumb_create_socket failed");
758                 return MEDIA_THUMB_ERROR_NETWORK;
759         }
760 #endif
761
762         GIOChannel *channel = NULL;
763         channel = g_io_channel_unix_new(sock);
764         int source_id = -1;
765
766
767         memset(&serv_addr, 0, sizeof(serv_addr));
768 #ifdef _USE_UDS_SOCKET_
769         serv_addr.sun_family = AF_UNIX;
770 #elif defined(_USE_UDS_SOCKET_TCP_)
771         serv_addr.sun_family = AF_UNIX;
772 #else
773         serv_addr.sin_family = AF_INET;
774         serv_addr.sin_addr.s_addr = inet_addr(serv_ip);
775 #endif
776
777 #ifdef _USE_MEDIA_UTIL_
778 #ifdef _USE_UDS_SOCKET_
779         strcpy(serv_addr.sun_path, "/tmp/media_ipc_thumbcreator.dat");
780 #elif defined(_USE_UDS_SOCKET_TCP_)
781         strcpy(serv_addr.sun_path, "/tmp/media_ipc_thumbcreator.dat");
782 #else
783         serv_addr.sin_port = htons(MS_THUMB_CREATOR_PORT);
784 #endif
785 #else
786         serv_addr.sin_port = htons(THUMB_DAEMON_PORT);
787 #endif
788
789         /* Connecting to the thumbnail server */
790         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
791                 thumb_err("connect error : %s", strerror(errno));
792                 g_io_channel_shutdown(channel, TRUE, NULL);
793                 return MEDIA_THUMB_ERROR_NETWORK;
794         }
795
796         if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
797                 //source_id = g_io_add_watch(channel, G_IO_IN, _media_thumb_write_socket, userData );
798
799                 /* Create new channel to watch udp socket */
800                 GSource *source = NULL;
801                 source = g_io_create_watch(channel, G_IO_IN);
802
803                 /* Set callback to be called when socket is readable */
804                 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, userData, NULL);
805                 source_id = g_source_attach(source, g_main_context_get_thread_default());
806         }
807
808         thumbMsg req_msg;
809         memset((void *)&req_msg, 0, sizeof(thumbMsg));
810
811         pid = getpid();
812         req_msg.pid = pid;
813         req_msg.msg_type = msg_type;
814         req_msg.thumb_type = thumb_type;
815         req_msg.uid = uid;      
816         
817         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
818         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
819
820         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
821         req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
822
823         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
824                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
825                 g_io_channel_shutdown(channel, TRUE, NULL);
826                 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
827         }
828
829         unsigned char *buf = NULL;
830         int buf_size = 0;
831         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
832
833         //thumb_dbg("buffer size : %d", buf_size);
834
835         if (send(sock, buf, buf_size, 0) != buf_size) {
836                 thumb_err("sendto failed: %d\n", errno);
837                 SAFE_FREE(buf);
838                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
839                 g_io_channel_shutdown(channel, TRUE, NULL);
840                 return MEDIA_THUMB_ERROR_NETWORK;
841         }
842
843         SAFE_FREE(buf);
844         thumb_dbg("Sending msg to thumbnail daemon is successful");
845
846 #if 0
847         if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
848                 g_io_channel_shutdown(channel, TRUE, NULL);
849         }
850 #else
851         if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
852                 g_io_channel_shutdown(channel, TRUE, NULL);
853
854                 //thumb_dbg("Cancel : %s[%d]", origin_path, sock);
855                 __media_thumb_pop_req_queue(origin_path, TRUE);
856         } else if (msg_type == THUMB_REQUEST_DB_INSERT) {
857                 if (g_request_queue == NULL) {
858                         g_request_queue = g_queue_new();
859                 }
860
861                 thumbReq *thumb_req = NULL;
862                 thumb_req = calloc(1, sizeof(thumbReq));
863                 if (thumb_req == NULL) {
864                         thumb_err("Failed to create request element");
865                         return 0;
866                 }
867
868                 thumb_req->channel = channel;
869                 thumb_req->path = strdup(origin_path);
870                 thumb_req->source_id = source_id;
871
872                 //thumb_dbg("Push : %s [%d]", origin_path, sock);
873                 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
874         }
875 #endif
876
877         return 0;
878 }
879