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