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