4e1d3058bcf9b4fb52085947de0ad59d91499abc
[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;
445         struct sockaddr_un serv_addr;
446         int recv_str_len = 0;
447         int err = MS_MEDIA_ERR_NONE;
448         int pid;
449
450         if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_PORT) < 0) {
451                 thumb_err("ms_ipc_create_client_socket failed");
452                 return MS_MEDIA_ERR_SOCKET_CONN;
453         }
454
455         memset(&serv_addr, 0, sizeof(serv_addr));
456         serv_addr.sun_family = AF_UNIX;
457         strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
458
459         /* Connecting to the thumbnail server */
460         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
461                 thumb_stderror("connect");
462                 return MS_MEDIA_ERR_SOCKET_CONN;
463         }
464
465         thumbMsg req_msg;
466         thumbMsg recv_msg;
467
468         memset((void *)&req_msg, 0, sizeof(thumbMsg));
469         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
470
471         /* Get PID of client*/
472         pid = getpid();
473         req_msg.pid = pid;
474
475         /* Set requset message */
476         req_msg.msg_type = msg_type;
477         req_msg.thumb_type = thumb_type;
478         req_msg.uid = uid;
479         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
480         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
481
482         if (msg_type == THUMB_REQUEST_SAVE_FILE) {
483                 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
484                 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
485         }
486
487         req_msg.thumb_data = (unsigned char *) "\0";
488
489         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
490         req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
491         req_msg.thumb_size = 1;
492
493         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
494                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
495                 close(sock);
496                 return MS_MEDIA_ERR_INVALID_PARAMETER;
497         }
498
499         unsigned char *buf = NULL;
500         int buf_size = 0;
501         int header_size = 0;
502
503         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
504         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
505
506         if (send(sock, buf, buf_size, 0) != buf_size) {
507                 thumb_err("sendto failed: %d", errno);
508                 SAFE_FREE(buf);
509                 close(sock);
510                 return MS_MEDIA_ERR_SOCKET_SEND;
511         }
512
513         thumb_dbg("Sending msg to thumbnail daemon is successful");
514
515         SAFE_FREE(buf);
516
517         if(msg_type != THUMB_REQUEST_CANCEL_ALL_RAW_DATA && msg_type != THUMB_REQUEST_CANCEL_ALL) {             //No response..
518                 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
519                         thumb_err("_media_thumb_recv_msg failed ");
520                         close(sock);
521                         return err;
522                 }
523
524                 recv_str_len = strlen(recv_msg.org_path);
525                 thumb_dbg_slog("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
526
527                 close(sock);
528
529                 if (recv_str_len > max_length) {
530                         thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
531                         return MS_MEDIA_ERR_INVALID_PARAMETER;
532                 }
533
534                 if (recv_msg.status == THUMB_FAIL) {
535                         thumb_err("Failed to make thumbnail");
536                         return MS_MEDIA_ERR_INVALID_PARAMETER;
537                 }
538
539                 if (msg_type != THUMB_REQUEST_SAVE_FILE) {
540                         strncpy(thumb_path, recv_msg.dst_path, max_length);
541                 }
542
543                 thumb_info->origin_width = recv_msg.origin_width;
544                 thumb_info->origin_height = recv_msg.origin_height;
545         }else {
546                 thumb_dbg("No response msg_type:[%d]", msg_type);
547         }
548         return MS_MEDIA_ERR_NONE;
549 }
550
551 static int _mkdir(const char *dir, mode_t mode) {
552         char tmp[256];
553         char *p = NULL;
554         size_t len;
555
556         snprintf(tmp, sizeof(tmp),"%s",dir);
557         len = strlen(tmp);
558         if(tmp[len - 1] == '/')
559                 tmp[len - 1] = 0;
560         for(p = tmp + 1; *p; p++)
561                 if(*p == '/') {
562                         *p = 0;
563                         mkdir(tmp, mode);
564                         *p = '/';
565                 }
566         return mkdir(tmp, mode);
567 }
568
569 static char* _media_thumb_get_default_path(uid_t uid)
570 {
571         char *result_psswd = NULL;
572         struct group *grpinfo = NULL;
573         if(uid == getuid())
574         {
575                 result_psswd = strdup(THUMB_DEFAULT_PATH);
576                 grpinfo = getgrnam("users");
577                 if(grpinfo == NULL) {
578                         thumb_err("getgrnam(users) returns NULL !");
579                         return NULL;
580                 }
581         }
582         else
583         {
584                 struct passwd *userinfo = getpwuid(uid);
585                 if(userinfo == NULL) {
586                         thumb_err("getpwuid(%d) returns NULL !", uid);
587                         return NULL;
588                 }
589                 grpinfo = getgrnam("users");
590                 if(grpinfo == NULL) {
591                         thumb_err("getgrnam(users) returns NULL !");
592                         return NULL;
593                 }
594                 // Compare git_t type and not group name
595                 if (grpinfo->gr_gid != userinfo->pw_gid) {
596                         thumb_err("UID [%d] does not belong to 'users' group!", uid);
597                         return NULL;
598                 }
599                 asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/phone", userinfo->pw_dir);
600         }
601
602         /* create dir */
603         _mkdir(result_psswd,S_IRWXU | S_IRWXG | S_IRWXO);
604
605         return result_psswd;
606 }
607
608 int _media_thumb_process(thumbMsg *req_msg, thumbMsg *res_msg, uid_t uid)
609 {
610         int err = -1;
611         GdkPixbuf *gdkdata = NULL;
612         int thumb_size = 0;
613         int thumb_w = 0;
614         int thumb_h = 0;
615         int origin_w = 0;
616         int origin_h = 0;
617         int max_length = 0;
618         char *thumb_path = NULL;
619         int need_update_db = 0;
620         int alpha = 0;
621
622         if (req_msg == NULL || res_msg == NULL) {
623                 thumb_err("Invalid msg!");
624                 return MS_MEDIA_ERR_INVALID_PARAMETER;
625         }
626
627         int msg_type = req_msg->msg_type;
628         media_thumb_type thumb_type = req_msg->thumb_type;
629         const char *origin_path = req_msg->org_path;
630
631         // Currently, The color space that is supported by the gdk-pixbuf is only RGB.
632         media_thumb_format thumb_format = MEDIA_THUMB_RGB888;
633
634         thumb_path = res_msg->dst_path;
635         thumb_path[0] = '\0';
636         max_length = sizeof(res_msg->dst_path);
637
638         err = _media_thumb_db_connect(uid);
639         if (err != MS_MEDIA_ERR_NONE) {
640                 thumb_err("_media_thumb_mb_svc_connect failed: %d", err);
641                 return MS_MEDIA_ERR_DB_CONNECT_FAIL;
642         }
643
644         if (msg_type == THUMB_REQUEST_DB_INSERT) {
645                 err = _media_thumb_get_thumb_from_db_with_size(origin_path, thumb_path, max_length, &need_update_db, &origin_w, &origin_h);
646                 if (err == MS_MEDIA_ERR_NONE) {
647                         res_msg->origin_width = origin_w;
648                         res_msg->origin_height = origin_h;
649                         _media_thumb_db_disconnect();
650                         return MS_MEDIA_ERR_NONE;
651                 } else {
652                         if (strlen(thumb_path) == 0) {
653                                 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length,uid);
654                                 if (err != MS_MEDIA_ERR_NONE) {
655                                         thumb_err("_media_thumb_get_hash_name failed - %d\n", err);
656                                         strncpy(thumb_path, _media_thumb_get_default_path(uid), max_length);
657                                         _media_thumb_db_disconnect();
658                                         return err;
659                                 }
660
661                                 thumb_path[strlen(thumb_path)] = '\0';
662                         }
663                 }
664
665         } else if (msg_type == THUMB_REQUEST_SAVE_FILE) {
666                 strncpy(thumb_path, req_msg->dst_path, max_length);
667
668         } else if (msg_type == THUMB_REQUEST_ALL_MEDIA) {
669                 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length,uid);
670                 if (err < 0) {
671                         thumb_err("_media_thumb_get_hash_name failed - %d\n", err);
672                         strncpy(thumb_path, _media_thumb_get_default_path(uid), max_length);
673                         _media_thumb_db_disconnect();
674                         return err;
675                 }
676
677                 thumb_path[strlen(thumb_path)] = '\0';
678         }
679
680         if (g_file_test(thumb_path, 
681                                         G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
682                 thumb_warn("thumb path already exists in file system.. remove the existed file");
683                 _media_thumb_remove_file(thumb_path);
684         }
685
686         err = _thumbnail_get_data(origin_path, thumb_type, thumb_format, &gdkdata, &thumb_size, &thumb_w, &thumb_h, &origin_w, &origin_h, &alpha, uid);
687         if (err != MS_MEDIA_ERR_NONE) {
688                 thumb_err("_thumbnail_get_data failed - %d\n", err);
689                 if ( gdkdata != NULL ){
690                         g_object_unref(gdkdata);
691                 }
692                 strncpy(thumb_path, _media_thumb_get_default_path(uid), max_length);
693                 _media_thumb_db_disconnect();
694                 return err;
695         }
696
697         //thumb_dbg("Size : %d, W:%d, H:%d", thumb_size, thumb_w, thumb_h);
698         //thumb_dbg("Origin W:%d, Origin H:%d\n", origin_w, origin_h);
699         //thumb_dbg("Thumb : %s", thumb_path);
700
701         res_msg->msg_type = THUMB_RESPONSE;
702         res_msg->thumb_size = thumb_size;
703         res_msg->thumb_width = thumb_w;
704         res_msg->thumb_height = thumb_h;
705         res_msg->origin_width = origin_w;
706         res_msg->origin_height = origin_h;
707
708         /* If the image is transparent PNG format, make png file as thumbnail of this image */
709         if (alpha) {
710                 char file_ext[10];
711                 err = _media_thumb_get_file_ext(origin_path, file_ext, sizeof(file_ext));
712                 if (strncasecmp(file_ext, "png", 3) == 0) {
713                         int len = strlen(thumb_path);
714                         thumb_path[len - 3] = 'p';
715                         thumb_path[len - 2] = 'n';
716                         thumb_path[len - 1] = 'g';
717                 }
718                 thumb_dbg("Thumb path is changed : %s", thumb_path);
719         }
720
721         err = _media_thumb_save_to_file_with_gdk(gdkdata, thumb_w, thumb_h, alpha, thumb_path);
722         if (err < 0) {
723                 thumb_err("save_to_file_with_gdk failed - %d\n", err);
724                 if ( gdkdata != NULL ){
725                         g_object_unref(gdkdata);
726                 }
727
728                 if (msg_type == THUMB_REQUEST_DB_INSERT || msg_type == THUMB_REQUEST_ALL_MEDIA)
729                         strncpy(thumb_path, _media_thumb_get_default_path(uid), max_length);
730
731                 _media_thumb_db_disconnect();
732                 return err;
733         } else {
734                 thumb_dbg("file save success\n");
735         }
736
737         /* fsync */
738         int fd = 0;
739         fd = open(thumb_path, O_WRONLY);
740         if (fd < 0) {
741                 thumb_warn("open failed");
742         } else {
743                 err = fsync(fd);
744                 if (err == -1) {
745                         thumb_warn("fsync failed");
746                 }
747                 close(fd);
748         }
749         /* End of fsync */
750
751         g_object_unref(gdkdata);
752
753         /* DB update if needed */
754         if (need_update_db == 1) {
755                 err = _media_thumb_update_db(origin_path, thumb_path, res_msg->origin_width, res_msg->origin_height, uid);
756                 if (err != MS_MEDIA_ERR_NONE) {
757                         thumb_err("_media_thumb_update_db failed : %d", err);
758                 }
759         }
760
761         _media_thumb_db_disconnect();
762
763         return MS_MEDIA_ERR_NONE;
764 }
765
766 int
767 _media_thumb_process_raw(thumbMsg *req_msg, thumbMsg *res_msg, thumbRawAddMsg *res_raw_msg, uid_t uid)
768 {
769         int err = MS_MEDIA_ERR_NONE;
770         unsigned char *data = NULL;
771         int thumb_size = 0;
772         int thumb_w = 0;
773         int thumb_h = 0;
774
775         if (req_msg == NULL || res_msg == NULL) {
776                 thumb_err("Invalid msg!");
777                 return MS_MEDIA_ERR_INVALID_PARAMETER;
778         }
779
780         const char *origin_path = req_msg->org_path;
781
782         media_thumb_format thumb_format = MEDIA_THUMB_BGRA;
783         thumb_w = req_msg->thumb_width;
784         thumb_h = req_msg->thumb_height;
785
786         //thumb_dbg("origin_path : %s, thumb_w : %d, thumb_h : %d ", origin_path, thumb_w, thumb_h);
787
788         if (!g_file_test(origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
789                 thumb_err("origin_path does not exist in file system.");
790                 return MS_MEDIA_ERR_FILE_NOT_EXIST;
791         }
792
793         err = _thumbnail_get_raw_data(origin_path, thumb_format, &data, &thumb_size, &thumb_w, &thumb_h, uid);
794
795         if (err != MS_MEDIA_ERR_NONE) {
796                 thumb_err("_thumbnail_get_data failed - %d", err);
797                 SAFE_FREE(data);
798         }
799
800         res_msg->msg_type = THUMB_RESPONSE_RAW_DATA;
801         res_msg->thumb_width = thumb_w;
802         res_msg->thumb_height = thumb_h;
803         res_raw_msg->thumb_size = thumb_size;
804         res_raw_msg->thumb_data = malloc(thumb_size * sizeof(unsigned char));
805         memcpy(res_raw_msg->thumb_data, data, thumb_size);
806         res_msg->thumb_size = thumb_size + sizeof(thumbRawAddMsg) - sizeof(unsigned char*);
807
808         //thumb_dbg("Size : %d, W:%d, H:%d", thumb_size, thumb_w, thumb_h);
809
810         return MS_MEDIA_ERR_NONE;
811 }
812
813 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
814 {
815         thumbMsg recv_msg;
816         int header_size = 0;
817         int sock = 0;
818         int err = MS_MEDIA_ERR_NONE;
819
820         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
821         sock = g_io_channel_unix_get_fd(src);
822
823         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
824
825         thumb_err("_media_thumb_write_socket socket : %d", sock);
826
827         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
828                 thumb_err("_media_thumb_recv_msg failed ");
829                 if (recv_msg.origin_path_size > 0) {
830                         __media_thumb_pop_req_queue(recv_msg.org_path, TRUE);
831                 } else {
832                         thumb_err("origin path size is wrong.");
833                 }
834
835                 return FALSE;
836         }
837
838         g_io_channel_shutdown(src, TRUE, NULL);
839         g_io_channel_unref(src);
840         //thumb_dbg("Completed..%s", recv_msg.org_path);
841
842         if (recv_msg.status == THUMB_FAIL) {
843                 thumb_err("Failed to make thumbnail");
844                 err = MS_MEDIA_ERR_INTERNAL;
845         }
846
847         if (data) {
848                 thumbUserData* cb = (thumbUserData*)data;
849                 if (cb->func != NULL)
850                         cb->func(err, recv_msg.dst_path, cb->user_data);
851         }
852
853         __media_thumb_pop_req_queue(recv_msg.org_path, FALSE);
854
855         thumb_dbg("Done");
856         return FALSE;
857 }
858
859 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
860 {
861         thumbMsg recv_msg;
862         int header_size = 0;
863         int sock = 0;
864         int err = MS_MEDIA_ERR_NONE;
865
866         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
867         sock = g_io_channel_unix_get_fd(src);
868
869         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
870
871         thumb_err("_media_thumb_write_socket socket : %d", sock);
872
873         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
874                 thumb_err("_media_thumb_recv_msg failed ");
875                 if (recv_msg.request_id > 0) {
876                         __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, TRUE);
877                 } else {
878                         thumb_err("origin path size is wrong.");
879                 }
880
881                 return FALSE;
882         }
883
884         g_io_channel_shutdown(src, TRUE, NULL);
885         g_io_channel_unref(src);
886
887         if (recv_msg.status == THUMB_FAIL) {
888                 thumb_err("Failed to make thumbnail");
889                 err = MS_MEDIA_ERR_INTERNAL;
890         }
891
892         if (data) {
893                 thumbRawUserData* cb = (thumbRawUserData*)data;
894                 if (cb->func != NULL)
895                         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);
896         }
897
898         __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, FALSE);
899
900         thumb_dbg("Done");
901
902         SAFE_FREE(recv_msg.thumb_data);
903
904         return FALSE;
905 }
906
907 int _media_thumb_request_async(int msg_type, media_thumb_type thumb_type, const char *origin_path, thumbUserData *userData, uid_t uid)
908 {
909         int sock;
910         struct sockaddr_un serv_addr;
911         int pid;
912
913         if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
914                 return MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST;
915         }
916
917         if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_PORT) < 0) {
918                 thumb_err("ms_ipc_create_client_socket failed");
919                 return MS_MEDIA_ERR_SOCKET_CONN;
920         }
921         GIOChannel *channel = NULL;
922         channel = g_io_channel_unix_new(sock);
923         int source_id = -1;
924
925
926         memset(&serv_addr, 0, sizeof(serv_addr));
927         serv_addr.sun_family = AF_UNIX;
928
929         strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
930
931         /* Connecting to the thumbnail server */
932         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
933                 thumb_err("connect error : %s", strerror(errno));
934                 g_io_channel_shutdown(channel, TRUE, NULL);
935                 g_io_channel_unref(channel);
936                 return MS_MEDIA_ERR_SOCKET_CONN;
937         }
938
939         if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
940                 //source_id = g_io_add_watch(channel, G_IO_IN, _media_thumb_write_socket, userData );
941
942                 /* Create new channel to watch udp socket */
943                 GSource *source = NULL;
944                 source = g_io_create_watch(channel, G_IO_IN);
945
946                 /* Set callback to be called when socket is readable */
947                 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, userData, NULL);
948                 source_id = g_source_attach(source, g_main_context_get_thread_default());
949         }
950
951         thumbMsg req_msg;
952         memset((void *)&req_msg, 0, sizeof(thumbMsg));
953
954         pid = getpid();
955         req_msg.pid = pid;
956         req_msg.msg_type = msg_type;
957         req_msg.thumb_type = thumb_type;
958         req_msg.uid = uid;
959
960         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
961         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
962         req_msg.dst_path[0] = '\0';
963         req_msg.thumb_data = (unsigned char *)"\0";
964
965         req_msg.origin_path_size = strlen(req_msg.org_path);
966         req_msg.dest_path_size = 1;
967         req_msg.thumb_size = 1;
968
969         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
970                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
971                 g_io_channel_shutdown(channel, TRUE, NULL);
972                 g_io_channel_unref(channel);
973                 return MS_MEDIA_ERR_INVALID_PARAMETER;
974         }
975
976         unsigned char *buf = NULL;
977         int buf_size = 0;
978         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
979
980         if (send(sock, buf, buf_size, 0) != buf_size) {
981                 thumb_err("sendto failed: %d", errno);
982                 SAFE_FREE(buf);
983                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
984                 g_io_channel_shutdown(channel, TRUE, NULL);
985                 g_io_channel_unref(channel);
986                 return MS_MEDIA_ERR_SOCKET_SEND;
987         }
988
989         SAFE_FREE(buf);
990         thumb_dbg("Sending msg to thumbnail daemon is successful");
991
992         if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
993                 g_io_channel_shutdown(channel, TRUE, NULL);
994
995                 //thumb_dbg("Cancel : %s[%d]", origin_path, sock);
996                 __media_thumb_pop_req_queue(origin_path, TRUE);
997         } else if (msg_type == THUMB_REQUEST_DB_INSERT) {
998                 if (g_request_queue == NULL) {
999                         g_request_queue = g_queue_new();
1000                 }
1001
1002                 thumbReq *thumb_req = NULL;
1003                 thumb_req = calloc(1, sizeof(thumbReq));
1004                 if (thumb_req == NULL) {
1005                         thumb_err("Failed to create request element");
1006                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1007                 }
1008
1009                 thumb_req->channel = channel;
1010                 thumb_req->path = strdup(origin_path);
1011                 thumb_req->source_id = source_id;
1012                 thumb_req->userData = userData;
1013
1014                 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
1015         }
1016
1017         return MS_MEDIA_ERR_NONE;
1018 }
1019
1020 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)
1021 {
1022         int sock;
1023         struct sockaddr_un serv_addr;
1024         int pid;
1025
1026         if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_PORT) < 0) {
1027                 thumb_err("ms_ipc_create_client_socket failed");
1028                 return MS_MEDIA_ERR_SOCKET_CONN;
1029         }
1030         GIOChannel *channel = NULL;
1031         channel = g_io_channel_unix_new(sock);
1032         int source_id = -1;
1033
1034
1035         memset(&serv_addr, 0, sizeof(serv_addr));
1036         serv_addr.sun_family = AF_UNIX;
1037
1038         strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
1039
1040         /* Connecting to the thumbnail server */
1041         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
1042                 thumb_err("connect error : %s", strerror(errno));
1043                 g_io_channel_shutdown(channel, TRUE, NULL);
1044                 g_io_channel_unref(channel);
1045                 return MS_MEDIA_ERR_SOCKET_CONN;
1046         }
1047
1048         if (msg_type != THUMB_REQUEST_CANCEL_RAW_DATA) {
1049                 /* Create new channel to watch udp socket */
1050                 GSource *source = NULL;
1051                 source = g_io_create_watch(channel, G_IO_IN);
1052
1053                 /* Set callback to be called when socket is readable */
1054                 /*NEED UPDATE SOCKET FILE DELETE*/
1055                 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, userData, NULL);
1056                 source_id = g_source_attach(source, g_main_context_get_thread_default());
1057         }
1058
1059         thumbMsg req_msg;
1060         memset((void *)&req_msg, 0, sizeof(thumbMsg));
1061
1062         pid = getpid();
1063         req_msg.pid = pid;
1064         req_msg.msg_type = msg_type;
1065         req_msg.thumb_type = MEDIA_THUMB_LARGE;
1066         req_msg.request_id = request_id;
1067         req_msg.thumb_width = width;
1068         req_msg.thumb_height = height;
1069         req_msg.uid = uid;
1070
1071         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
1072         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
1073         req_msg.dst_path[0] = '\0';
1074         req_msg.thumb_data = (unsigned char *)"\0";
1075
1076         req_msg.origin_path_size = strlen(req_msg.org_path);
1077         req_msg.dest_path_size = 1;
1078         req_msg.thumb_size = 1;
1079
1080         if (req_msg.origin_path_size > MAX_PATH_SIZE) {
1081                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
1082                 g_io_channel_shutdown(channel, TRUE, NULL);
1083                 g_io_channel_unref(channel);
1084                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1085         }
1086
1087         unsigned char *buf = NULL;
1088         int buf_size = 0;
1089         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
1090
1091         if (send(sock, buf, buf_size, 0) != buf_size) {
1092                 thumb_err("sendto failed: %d", errno);
1093                 SAFE_FREE(buf);
1094                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
1095                 g_io_channel_shutdown(channel, TRUE, NULL);
1096                 g_io_channel_unref(channel);
1097                 return MS_MEDIA_ERR_SOCKET_SEND;
1098         }
1099
1100         SAFE_FREE(buf);
1101         thumb_dbg("Sending msg to thumbnail daemon is successful");
1102
1103         if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA) {
1104                 g_io_channel_shutdown(channel, TRUE, NULL);
1105                 __media_thumb_pop_raw_data_req_queue(request_id, TRUE);
1106         } else if (msg_type == THUMB_REQUEST_RAW_DATA) {
1107                 if (g_request_queue == NULL) {
1108                         g_request_queue = g_queue_new();
1109                 }
1110                 thumbRawReq *thumb_req = NULL;
1111                 thumb_req = calloc(1, sizeof(thumbReq));
1112                 if (thumb_req == NULL) {
1113                         thumb_err("Failed to create request element");
1114                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1115                 }
1116                 thumb_req->channel = channel;
1117                 thumb_req->request_id = request_id;
1118                 thumb_req->source_id = source_id;
1119                 thumb_req->userData = userData;
1120
1121                 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
1122         }
1123         return MS_MEDIA_ERR_NONE;
1124 }