Remove unused code
[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 THUMB_SOCK_BLOCK_SIZE 512
35 #define THUMB_IPC_PATH tzplatform_mkpath(TZ_SYS_RUN, "media-server/media_ipc_thumbcreator.socket")
36
37 static GQueue *g_request_queue = NULL;
38 static GQueue *g_manage_queue = NULL;
39 static GQueue *g_request_raw_queue = NULL;
40 static GQueue *g_manage_raw_queue = NULL;
41
42
43 typedef struct {
44         GIOChannel *channel;
45         int msg_type;
46         unsigned int request_id;
47         bool isCanceled;
48         int source_id;
49         uid_t uid;
50         char *path;
51         thumbUserData *userData;
52 } thumbReq;
53
54 typedef struct {
55         GIOChannel *channel;
56         int msg_type;
57         bool isCanceled;
58         int request_id;
59         int source_id;
60         int width;
61         int height;
62         uid_t uid;
63         char *path;
64         thumbRawUserData *userData;
65 } thumbRawReq;
66
67 int _media_thumb_send_request();
68 int _media_thumb_raw_data_send_request();
69
70
71 int _media_thumb_get_error()
72 {
73         if (errno == EWOULDBLOCK) {
74                 thumb_err("Timeout. Can't try any more");
75                 return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT;
76         } else {
77                 thumb_stderror("recvfrom failed");
78                 return MS_MEDIA_ERR_SOCKET_RECEIVE;
79         }
80 }
81
82 void __media_thumb_shutdown_channel(bool only_shutdown)
83 {
84         int len = -1;
85         thumbReq *req = (thumbReq *)g_queue_peek_head(g_request_queue);
86
87         len = g_queue_get_length(g_manage_queue);
88
89         if (req != NULL) {
90                 if (!only_shutdown) {
91                         if (len == 0 && req->isCanceled == true) {
92                                 thumb_dbg("The manage queue will be released");
93                         } else {
94                                 thumb_dbg("There is remain item in the manage queue");
95                                 return;
96                         }
97                 }
98                 GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
99                 if (source_id != NULL) {
100                         g_source_destroy(source_id);
101                 } else {
102                         thumb_err("G_SOURCE_ID is NULL");
103                 }
104
105                 g_io_channel_shutdown(req->channel, TRUE, NULL);
106                 g_io_channel_unref(req->channel);
107                 g_queue_pop_head(g_request_queue);
108
109                 SAFE_FREE(req->path);
110                 SAFE_FREE(req->userData);
111                 SAFE_FREE(req);
112
113                 if (g_manage_queue && len == 0) {
114                         g_queue_free(g_manage_queue);
115                         g_manage_queue = NULL;
116                 }
117         }
118 }
119
120 int __media_thumb_pop_req_queue()
121 {
122         int req_len = 0;
123
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                 __media_thumb_shutdown_channel(true);
130         }
131
132         /* Check manage queue */
133         if (g_manage_queue) {
134                 req_len = g_queue_get_length(g_manage_queue);
135
136                 if (req_len > 0)
137                         _media_thumb_send_request();
138         }
139
140         return MS_MEDIA_ERR_NONE;
141 }
142
143 int __media_thumb_check_req_queue_for_cancel(unsigned int request_id, const char *path)
144 {
145         int req_len = 0;
146
147         req_len = g_queue_get_length(g_request_queue);
148
149         if (req_len <= 0) {
150 //              thumb_dbg("There is no request in the queue");
151         } else {
152                 thumbReq *req = NULL;
153                 req = (thumbReq *)g_queue_peek_head(g_request_queue);
154
155                 if (req != NULL && strncmp(path, req->path, strlen(path)) == 0 && request_id == req->request_id) {
156                         req->isCanceled = true;
157                         __media_thumb_shutdown_channel(false);
158
159                         return MS_MEDIA_ERR_NONE;
160                 }
161         }
162
163         return MS_MEDIA_ERR_INTERNAL;
164 }
165
166 int __media_thumb_pop_manage_queue(unsigned int request_id, const char *path)
167 {
168         int req_len = 0, i;
169         bool flag = false;
170
171         req_len = g_queue_get_length(g_manage_queue);
172
173         if (req_len < 0) {
174 //              thumb_dbg("There is no request in the queue");
175         } else {
176                 for (i = 0; i < req_len; i++) {
177                         thumbReq *req = NULL;
178                         req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i);
179                         if (req == NULL) continue;
180
181                         if (strncmp(path, req->path, strlen(path)) == 0 && request_id == req->request_id) {
182                                 g_queue_pop_nth(g_manage_queue, i);
183
184                                 SAFE_FREE(req->path);
185                                 SAFE_FREE(req->userData);
186                                 SAFE_FREE(req);
187                                 flag = true;
188                                 break;
189                         }
190                 }
191                 if (!flag) {
192                         return __media_thumb_check_req_queue_for_cancel(request_id, path);
193                 } else {
194                         __media_thumb_shutdown_channel(false);
195
196                         return MS_MEDIA_ERR_NONE;
197                 }
198         }
199
200         return MS_MEDIA_ERR_NONE;
201 }
202
203
204 int __media_thumb_pop_raw_data_req_queue(int request_id, bool shutdown_channel)
205 {
206         int req_len = 0, i;
207
208         req_len = g_queue_get_length(g_request_raw_queue);
209
210         if (req_len <= 0) {
211 //              thumb_dbg("There is no request in the queue");
212         } else {
213
214                 for (i = 0; i < req_len; i++) {
215                         thumbRawReq *req = NULL;
216                         req = (thumbRawReq *)g_queue_peek_nth(g_request_raw_queue, i);
217                         if (req == NULL) continue;
218
219                         if (request_id == req->request_id) {
220                                 if (shutdown_channel) {
221                                         GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
222                                         if (source_id != NULL) {
223                                                 g_source_destroy(source_id);
224                                         } else {
225                                                 thumb_err("G_SOURCE_ID is NULL");
226                                         }
227
228                                         g_io_channel_shutdown(req->channel, TRUE, NULL);
229                                         g_io_channel_unref(req->channel);
230                                 }
231                                 g_queue_pop_nth(g_request_raw_queue, i);
232
233                                 SAFE_FREE(req->userData);
234                                 SAFE_FREE(req);
235
236                                 break;
237                         }
238                 }
239                 if (i == req_len) {
240 //                      thumb_dbg("There's no %s in the queue", path);
241                 }
242         }
243
244         return MS_MEDIA_ERR_NONE;
245 }
246
247 int __media_thumb_check_raw_data_req_queue_for_cancel(int request_id)
248 {
249         int req_len = 0;
250
251         req_len = g_queue_get_length(g_request_raw_queue);
252
253         if (req_len <= 0) {
254 //              thumb_dbg("There is no request in the queue");
255         } else {
256                 thumbRawReq *req = NULL;
257                 req = (thumbRawReq *)g_queue_peek_head(g_request_raw_queue);
258
259                 if (req != NULL && request_id == req->request_id) {
260                         req->isCanceled = true;
261                         return MS_MEDIA_ERR_NONE;
262                 }
263         }
264
265         return MS_MEDIA_ERR_INTERNAL;
266 }
267
268 int __media_thumb_pop_raw_data_manage_queue(int request_id)
269 {
270         int req_len = 0, i;
271         int flag = false;
272
273         req_len = g_queue_get_length(g_manage_raw_queue);
274
275         if (req_len < 0) {
276 //              thumb_dbg("There is no request in the queue");
277         } else {
278
279                 for (i = 0; i < req_len; i++) {
280                         thumbRawReq *req = NULL;
281                         req = (thumbRawReq *)g_queue_peek_nth(g_manage_raw_queue, i);
282                         if (req == NULL) continue;
283
284                         if (request_id == req->request_id) {
285                                 g_queue_pop_nth(g_manage_raw_queue, i);
286
287                                 SAFE_FREE(req->userData);
288                                 SAFE_FREE(req);
289                                 flag = true;
290                                 break;
291                         }
292                 }
293                 if (!flag) {
294                         return __media_thumb_check_raw_data_req_queue_for_cancel(request_id);
295                 }
296         }
297
298         return MS_MEDIA_ERR_NONE;
299 }
300 #if 0
301 int __media_thumb_check_req_queue(const char *path)
302 {
303         int req_len = 0, i;
304
305         req_len = g_queue_get_length(g_request_queue);
306
307 //      thumb_dbg("Queue length : %d", req_len);
308 //      thumb_dbg("Queue path : %s", path);
309
310         if (req_len <= 0) {
311 //              thumb_dbg("There is no request in the queue");
312         } else {
313
314                 for (i = 0; i < req_len; i++) {
315                         thumbReq *req = NULL;
316                         req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
317                         if (req == NULL) continue;
318
319                         if (strncmp(path, req->path, strlen(path)) == 0) {
320                                 //thumb_dbg("Same Request - %s", path);
321                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
322
323                                 break;
324                         }
325                 }
326         }
327
328         if (g_manage_queue != NULL) {
329                 req_len = g_queue_get_length(g_manage_queue);
330
331                 if (req_len <= 0) {
332 //              thumb_dbg("There is no request in the queue");
333                 } else {
334                         for (i = 0; i < req_len; i++) {
335                                 thumbReq *req = NULL;
336                                 req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i);
337                                 if (req == NULL) continue;
338
339                                 if (strncmp(path, req->path, strlen(path)) == 0) {
340                                         //thumb_dbg("Same Request - %s", path);
341                                         return MS_MEDIA_ERR_INVALID_PARAMETER;
342
343                                         break;
344                                 }
345                         }
346                 }
347         }
348
349
350         return MS_MEDIA_ERR_NONE;
351 }
352 #endif
353 bool __media_thumb_check_cancel(void)
354 {
355         thumbReq *req = NULL;
356         req = (thumbReq *)g_queue_peek_head(g_request_queue);
357
358         if (req == NULL) {
359                 return false;
360         } else {
361                 if (req->isCanceled)
362                         return false;
363                 else
364                         return true;
365         }
366 }
367
368 bool __media_thumb_check_cancel_for_raw(void)
369 {
370         thumbRawReq *req = NULL;
371         req = (thumbRawReq *)g_queue_peek_head(g_request_raw_queue);
372
373         if (req == NULL) {
374                 return false;
375         } else {
376                 if (req->isCanceled)
377                         return false;
378                 else
379                         return true;
380         }
381 }
382
383 int _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
384 {
385         int recv_msg_len = 0;
386         int remain_size = 0;
387         int block_size = THUMB_SOCK_BLOCK_SIZE;
388         int recv_block = 0;
389         unsigned char *buf = NULL;
390         unsigned char *block_buf = NULL;
391
392         THUMB_MALLOC(buf, header_size);
393         THUMB_MALLOC(block_buf, THUMB_SOCK_BLOCK_SIZE);
394         if (buf == NULL || block_buf == NULL) {
395                 thumb_err("memory allocation failed");
396                 SAFE_FREE(buf);
397                 SAFE_FREE(block_buf);
398                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
399         }
400
401         if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
402                 thumb_stderror("recv failed");
403                 SAFE_FREE(buf);
404                 SAFE_FREE(block_buf);
405                 return _media_thumb_get_error();
406         }
407
408         memcpy(msg, buf, header_size);
409         //thumb_dbg("origin_path_size : %d, dest_path_size : %d, thumb_size : %d", msg->origin_path_size, msg->dest_path_size, msg->thumb_size);
410
411         SAFE_FREE(buf);
412         if (msg->origin_path_size < 0 || msg->dest_path_size < 0 || msg->thumb_size < 0) {
413                 thumb_err("recv data is wrong");
414                 SAFE_FREE(block_buf);
415                 return MS_MEDIA_ERR_SOCKET_RECEIVE;
416         }
417
418         remain_size = msg->origin_path_size + msg->dest_path_size + msg->thumb_size;
419         THUMB_MALLOC(buf, remain_size);
420         if (buf == NULL) {
421                 SAFE_FREE(block_buf);
422                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
423         }
424
425         while (remain_size > 0) {
426                 if (remain_size < THUMB_SOCK_BLOCK_SIZE) {
427                         block_size = remain_size;
428                 }
429                 if ((recv_msg_len = recv(sock, block_buf, block_size, 0)) < 0) {
430                         thumb_stderror("recv failed");
431                         SAFE_FREE(buf);
432                         SAFE_FREE(block_buf);
433                         return _media_thumb_get_error();
434                 }
435                 memcpy(buf+recv_block, block_buf, block_size);
436                 recv_block += block_size;
437                 remain_size -= block_size;
438         }
439
440         SAFE_STRLCPY(msg->org_path, (char *)buf, sizeof(msg->org_path));
441         SAFE_STRLCPY(msg->dst_path, (char *)buf + msg->origin_path_size, sizeof(msg->dst_path));
442
443         SAFE_FREE(msg->thumb_data);
444         if (msg->thumb_size > 0) {
445                 THUMB_MALLOC(msg->thumb_data, msg->thumb_size);
446                 if (msg->thumb_data != NULL) {
447                         memcpy(msg->thumb_data, buf + msg->origin_path_size + msg->dest_path_size, msg->thumb_size);
448                 } else {
449                         SAFE_FREE(buf);
450                         SAFE_FREE(block_buf);
451
452                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
453                 }
454         }
455
456         SAFE_FREE(buf);
457         SAFE_FREE(block_buf);
458
459         return MS_MEDIA_ERR_NONE;
460 }
461
462 int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
463 {
464         if (req_msg == NULL || buf == NULL) {
465                 return MS_MEDIA_ERR_INVALID_PARAMETER;
466         }
467
468         int org_path_len = 0;
469         int dst_path_len = 0;
470         int thumb_data_len = 0;
471         int size = 0;
472         int header_size = 0;
473
474         header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
475         org_path_len = req_msg->origin_path_size;
476         dst_path_len = req_msg->dest_path_size;
477         thumb_data_len = req_msg->thumb_size;
478
479         //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);
480
481         size = header_size + org_path_len + dst_path_len + thumb_data_len;
482         THUMB_MALLOC(*buf, size);
483         if (*buf == NULL) {
484                 *buf_size = 0;
485                 return 0;
486         }
487         memcpy(*buf, req_msg, header_size);
488         memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
489         memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
490         if (thumb_data_len > 0)
491                 memcpy((*buf)+header_size + org_path_len + dst_path_len, req_msg->thumb_data, thumb_data_len);
492
493         *buf_size = size;
494
495         return MS_MEDIA_ERR_NONE;
496 }
497
498 int _media_thumb_request(int msg_type, const char *origin_path, char *thumb_path, int max_length, uid_t uid)
499 {
500         int sock = -1;
501         struct sockaddr_un serv_addr;
502         int recv_str_len = 0;
503         int err = MS_MEDIA_ERR_NONE;
504         int pid;
505
506         err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
507         if (err != MS_MEDIA_ERR_NONE) {
508                 thumb_err("ms_ipc_create_client_socket failed");
509                 return err;
510         }
511
512         memset(&serv_addr, 0, sizeof(serv_addr));
513         serv_addr.sun_family = AF_UNIX;
514         SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
515
516         /* Connecting to the thumbnail server */
517         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
518                 thumb_stderror("connect");
519                 close(sock);
520                 return MS_MEDIA_ERR_SOCKET_CONN;
521         }
522
523         thumbMsg req_msg;
524         thumbMsg recv_msg;
525
526         memset((void *)&req_msg, 0, sizeof(thumbMsg));
527         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
528
529         /* Get PID of client*/
530         pid = getpid();
531         req_msg.pid = pid;
532
533         /* Set requset message */
534         req_msg.msg_type = msg_type;
535         req_msg.uid = uid;
536         SAFE_STRLCPY(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
537         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
538
539         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
540         req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
541         req_msg.thumb_size = 0;
542
543         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
544                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
545                 close(sock);
546                 return MS_MEDIA_ERR_INVALID_PARAMETER;
547         }
548
549         unsigned char *buf = NULL;
550         int buf_size = 0;
551         int header_size = 0;
552
553         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
554         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
555
556         if (send(sock, buf, buf_size, 0) != buf_size) {
557                 thumb_err("sendto failed: %d", errno);
558                 SAFE_FREE(buf);
559                 close(sock);
560                 return MS_MEDIA_ERR_SOCKET_SEND;
561         }
562
563         thumb_dbg("Sending msg to thumbnail daemon is successful");
564
565         SAFE_FREE(buf);
566
567         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
568                 thumb_err("_media_thumb_recv_msg failed ");
569                 close(sock);
570                 return err;
571         }
572
573         recv_str_len = strlen(recv_msg.org_path);
574         thumb_dbg_slog("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
575
576         close(sock);
577         SAFE_FREE(recv_msg.thumb_data);
578
579         if (recv_str_len > max_length) {
580                 thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
581                 return MS_MEDIA_ERR_INVALID_PARAMETER;
582         }
583
584         if (recv_msg.status != MS_MEDIA_ERR_NONE) {
585                 thumb_err("Failed to make thumbnail");
586                 return recv_msg.status;
587         }
588
589         strncpy(thumb_path, recv_msg.dst_path, max_length);
590
591         return MS_MEDIA_ERR_NONE;
592 }
593
594 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
595 {
596         thumbMsg recv_msg;
597         int header_size = 0;
598         int sock = 0;
599         int err = MS_MEDIA_ERR_NONE;
600
601         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
602         sock = g_io_channel_unix_get_fd(src);
603
604         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
605
606         thumb_err("_media_thumb_write_socket socket : %d", sock);
607
608         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
609                 thumb_err("_media_thumb_recv_msg failed ");
610                 __media_thumb_pop_req_queue();
611
612                 return FALSE;
613         }
614
615         if (recv_msg.status != MS_MEDIA_ERR_NONE) {
616                 err = recv_msg.status;
617                 thumb_err("Failed to make thumbnail (%d)", err);
618         }
619
620         if (__media_thumb_check_cancel()) {
621                 if (data) {
622                         thumbUserData* cb = (thumbUserData*)data;
623                         if (cb->func != NULL)
624                                 cb->func(err, recv_msg.dst_path, cb->user_data);
625                 }
626         }
627
628         __media_thumb_pop_req_queue();
629
630         thumb_dbg("Done");
631
632         return FALSE;
633 }
634
635 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
636 {
637         thumbMsg recv_msg;
638         int header_size = 0;
639         int sock = 0;
640         int len = -1;
641         int err = MS_MEDIA_ERR_NONE;
642
643         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
644         sock = g_io_channel_unix_get_fd(src);
645
646         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
647
648         thumb_err("_media_thumb_write_socket socket : %d", sock);
649
650         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
651                 thumb_err("_media_thumb_recv_msg failed ");
652                 if (recv_msg.request_id > 0) {
653                         __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, TRUE);
654                 } else {
655                         thumb_err("origin path size is wrong.");
656                 }
657
658                 return FALSE;
659         }
660
661         g_io_channel_shutdown(src, TRUE, NULL);
662         g_io_channel_unref(src);
663
664         if (recv_msg.status != MS_MEDIA_ERR_NONE) {
665                 err = recv_msg.status;
666                 thumb_err("Failed to make thumbnail (%d)", err);
667         }
668
669         if (__media_thumb_check_cancel_for_raw()) {
670                 if (data) {
671                         thumbRawUserData* cb = (thumbRawUserData*)data;
672                         if (cb->func != NULL)
673                                 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);
674                 }
675         }
676
677         __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, FALSE);
678
679         thumb_dbg("Done");
680
681         /* Check manage queue */
682         if (g_manage_raw_queue) {
683                 len = g_queue_get_length(g_manage_raw_queue);
684
685                 if (len > 0) {
686                         _media_thumb_raw_data_send_request();
687                 } else {
688                         g_queue_free(g_manage_raw_queue);
689                         g_manage_raw_queue = NULL;
690                 }
691         }
692
693         return FALSE;
694 }
695
696 int _media_thumb_send_request()
697 {
698         int err = MS_MEDIA_ERR_NONE;
699         int sock = -1;
700         struct sockaddr_un serv_addr;
701         thumbReq *req_manager = NULL;
702         int pid;
703
704         err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
705         if (err != MS_MEDIA_ERR_NONE) {
706                 thumb_err("ms_ipc_create_client_socket failed");
707                 return err;
708         }
709
710         memset(&serv_addr, 0, sizeof(serv_addr));
711         serv_addr.sun_family = AF_UNIX;
712         SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
713
714         GIOChannel *channel = NULL;
715         channel = g_io_channel_unix_new(sock);
716         int source_id = -1;
717
718         /* Connecting to the thumbnail server */
719         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
720                 thumb_stderror("connect");
721                 if (errno == EACCES)
722                         err = MS_MEDIA_ERR_PERMISSION_DENIED;
723                 else
724                         err = MS_MEDIA_ERR_SOCKET_CONN;
725
726                 g_io_channel_shutdown(channel, TRUE, NULL);
727                 g_io_channel_unref(channel);
728                 close(sock);
729
730                 return err;
731         }
732
733         req_manager = (thumbReq *)g_queue_pop_head(g_manage_queue);
734
735         if (req_manager == NULL) {
736                 thumb_err("queue pop fail");
737                 g_io_channel_shutdown(channel, TRUE, NULL);
738                 g_io_channel_unref(channel);
739                 close(sock);
740                 return MS_MEDIA_ERR_INVALID_PARAMETER;
741         }
742
743         GSource *source = NULL;
744         source = g_io_create_watch(channel, G_IO_IN);
745         g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, req_manager->userData, NULL);
746         source_id = g_source_attach(source, g_main_context_get_thread_default());
747
748         thumbMsg req_msg;
749
750         memset((void *)&req_msg, 0, sizeof(thumbMsg));
751
752         pid = getpid();
753         req_msg.pid = pid;
754         req_msg.msg_type = req_manager->msg_type;
755         req_msg.request_id = 0;
756         req_msg.uid = req_manager->uid;
757         SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
758         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
759         req_msg.dst_path[0] = '\0';
760         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
761         req_msg.dest_path_size = 1;
762         req_msg.thumb_size = 0;
763
764         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
765                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
766                 g_io_channel_shutdown(channel, TRUE, NULL);
767                 g_io_channel_unref(channel);
768                 close(sock);
769                 return MS_MEDIA_ERR_INVALID_PARAMETER;
770         }
771
772         unsigned char *buf = NULL;
773         int buf_size = 0;
774         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
775
776         if (send(sock, buf, buf_size, 0) != buf_size) {
777                 thumb_err("sendto failed: %d", errno);
778                 SAFE_FREE(buf);
779                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
780                 g_io_channel_shutdown(channel, TRUE, NULL);
781                 g_io_channel_unref(channel);
782                 close(sock);
783                 return MS_MEDIA_ERR_SOCKET_SEND;
784         }
785
786         SAFE_FREE(buf);
787         thumb_dbg("Sending msg to thumbnail daemon is successful");
788
789
790         if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
791                 if (g_request_queue == NULL) {
792                         g_request_queue = g_queue_new();
793                 }
794
795                 thumbReq *thumb_req = NULL;
796                 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
797                 if (thumb_req == NULL) {
798                         thumb_err("Failed to create request element");
799                         return MS_MEDIA_ERR_INVALID_PARAMETER;
800                 }
801
802                 thumb_req->channel = channel;
803                 thumb_req->path = strdup(req_manager->path);
804                 thumb_req->source_id = source_id;
805                 thumb_req->userData = req_manager->userData;
806
807                 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
808         }
809
810         return err;
811 }
812
813 int _media_thumb_raw_data_send_request()
814 {
815         int err = MS_MEDIA_ERR_NONE;
816         int sock = -1;
817         struct sockaddr_un serv_addr;
818         thumbRawReq *req_manager = NULL;
819         int pid;
820
821         err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
822         if (err != MS_MEDIA_ERR_NONE) {
823                 thumb_err("ms_ipc_create_client_socket failed");
824                 return err;
825         }
826
827         memset(&serv_addr, 0, sizeof(serv_addr));
828         serv_addr.sun_family = AF_UNIX;
829         SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
830
831         GIOChannel *channel = NULL;
832         channel = g_io_channel_unix_new(sock);
833         int source_id = -1;
834
835         /* Connecting to the thumbnail server */
836         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
837                 thumb_stderror("connect error");
838                 if (errno == EACCES)
839                         err = MS_MEDIA_ERR_PERMISSION_DENIED;
840                 else
841                         err = MS_MEDIA_ERR_SOCKET_CONN;
842
843                 g_io_channel_shutdown(channel, TRUE, NULL);
844                 g_io_channel_unref(channel);
845                 close(sock);
846                 return err;
847         }
848
849         req_manager = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue);
850
851         if (req_manager == NULL) {
852                 thumb_err("queue pop fail");
853                 g_io_channel_shutdown(channel, TRUE, NULL);
854                 g_io_channel_unref(channel);
855                 close(sock);
856                 return MS_MEDIA_ERR_INVALID_PARAMETER;
857         }
858
859         GSource *source = NULL;
860         source = g_io_create_watch(channel, G_IO_IN);
861         g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL);
862         source_id = g_source_attach(source, g_main_context_get_thread_default());
863
864         thumbMsg req_msg;
865         memset((void *)&req_msg, 0, sizeof(thumbMsg));
866
867         pid = getpid();
868         req_msg.pid = pid;
869         req_msg.msg_type = req_manager->msg_type;
870         req_msg.request_id = req_manager->request_id;
871         req_msg.thumb_width = req_manager->width;
872         req_msg.thumb_height = req_manager->height;
873         req_msg.uid = req_manager->uid;
874
875         SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
876         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
877         req_msg.dst_path[0] = '\0';
878
879         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
880         req_msg.dest_path_size = 1;
881         req_msg.thumb_size = 0;
882
883         if (req_msg.origin_path_size > MAX_PATH_SIZE) {
884                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
885                 g_io_channel_shutdown(channel, TRUE, NULL);
886                 g_io_channel_unref(channel);
887                 return MS_MEDIA_ERR_INVALID_PARAMETER;
888         }
889
890         unsigned char *buf = NULL;
891         int buf_size = 0;
892         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
893
894         if (send(sock, buf, buf_size, 0) != buf_size) {
895                 thumb_err("sendto failed: %d", errno);
896                 SAFE_FREE(buf);
897                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
898                 g_io_channel_shutdown(channel, TRUE, NULL);
899                 g_io_channel_unref(channel);
900                 return MS_MEDIA_ERR_SOCKET_SEND;
901         }
902
903         SAFE_FREE(buf);
904
905         if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
906                 if (g_request_raw_queue == NULL) {
907                         g_request_raw_queue = g_queue_new();
908                 }
909                 thumbRawReq *thumb_req = NULL;
910                 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
911                 if (thumb_req == NULL) {
912                         thumb_err("Failed to create request element");
913                         return MS_MEDIA_ERR_INVALID_PARAMETER;
914                 }
915                 thumb_req->channel = channel;
916                 thumb_req->request_id = req_manager->request_id;
917                 thumb_req->source_id = source_id;
918                 thumb_req->userData = req_manager->userData;
919
920                 g_queue_push_tail(g_request_raw_queue, (gpointer)thumb_req);
921         }
922         return MS_MEDIA_ERR_NONE;
923 }
924
925 int _media_thumb_request_async(int msg_type, unsigned int request_id, const char *origin_path, thumbUserData *userData, uid_t uid)
926 {
927         int err = MS_MEDIA_ERR_NONE;
928
929         if (g_manage_queue == NULL) {
930                 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA)
931                         return MS_MEDIA_ERR_INTERNAL;
932
933                 g_manage_queue = g_queue_new();
934
935                 thumbReq *thumb_req = NULL;
936                 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
937                 if (thumb_req == NULL) {
938                         thumb_err("Failed to create request element");
939                         return MS_MEDIA_ERR_INVALID_PARAMETER;
940                 }
941
942                 thumb_req->msg_type = msg_type;
943                 thumb_req->path = strdup(origin_path);
944                 thumb_req->userData = userData;
945                 thumb_req->isCanceled = false;
946                 thumb_req->request_id = request_id;
947                 thumb_req->uid = uid;
948
949                 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
950
951                 /* directly request at first time */
952                 err = _media_thumb_send_request();
953
954         } else {
955                 if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
956                         /* Enqueue */
957 #if 0
958                         if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
959                                 thumb_err("duplicated request");
960                                 return MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST;
961                         }
962 #endif
963                         thumbReq *thumb_req = NULL;
964                         THUMB_MALLOC(thumb_req, sizeof(thumbReq));
965                         if (thumb_req == NULL) {
966                                 thumb_err("Failed to create request element");
967                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
968                         }
969
970                         thumb_req->msg_type = msg_type;
971                         thumb_req->path = strdup(origin_path);
972                         thumb_req->userData = userData;
973                         thumb_req->isCanceled = false;
974                         thumb_req->request_id = request_id;
975                         thumb_req->uid = uid;
976
977                         g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
978                 } else {
979                         /* Dequeue */
980                         err = __media_thumb_pop_manage_queue(request_id, origin_path);
981                 }
982         }
983
984         return err;
985 }
986
987 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)
988 {
989         int err = MS_MEDIA_ERR_NONE;
990
991         if (g_manage_raw_queue == NULL) {
992                 if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
993                         return MS_MEDIA_ERR_INTERNAL;
994
995                 g_manage_raw_queue = g_queue_new();
996
997                 thumbRawReq *thumb_req = NULL;
998                 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
999                 if (thumb_req == NULL) {
1000                         thumb_err("Failed to create request element");
1001                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1002                 }
1003
1004                 thumb_req->msg_type = msg_type;
1005                 thumb_req->request_id = request_id;
1006                 thumb_req->path = strdup(origin_path);
1007                 thumb_req->width = width;
1008                 thumb_req->height = height;
1009                 thumb_req->userData = userData;
1010                 thumb_req->isCanceled = false;
1011                 thumb_req->uid = uid;
1012
1013                 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
1014
1015                 /* directly request at first time */
1016                 err = _media_thumb_raw_data_send_request();
1017
1018         } else {
1019                 if (msg_type != THUMB_REQUEST_CANCEL_RAW_DATA) {
1020                         /* Enqueue */
1021                         thumbRawReq *thumb_req = NULL;
1022                         THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
1023                         if (thumb_req == NULL) {
1024                                 thumb_err("Failed to create request element");
1025                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1026                         }
1027
1028                         thumb_req->msg_type = msg_type;
1029                         thumb_req->request_id = request_id;
1030                         thumb_req->path = strdup(origin_path);
1031                         thumb_req->width = width;
1032                         thumb_req->height = height;
1033                         thumb_req->userData = userData;
1034                         thumb_req->isCanceled = false;
1035                         thumb_req->uid = uid;
1036
1037                         g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
1038
1039                 } else {
1040                         /* Dequeue */
1041                         err = __media_thumb_pop_raw_data_manage_queue(request_id);
1042                 }
1043         }
1044
1045         return err;
1046 }
1047
1048 int _media_thumb_request_cancel_all(bool isRaw)
1049 {
1050         int i;
1051         int req_len = -1;
1052         int len = 0;
1053
1054         if (isRaw) {
1055                 thumbRawReq *tmp_raw_req = NULL;
1056                 if (g_manage_raw_queue == NULL) {
1057                         thumb_err("manage_queue is NULL");
1058                         if (g_request_raw_queue != NULL) {
1059                                 /* Check request queue */
1060                                 len = g_queue_get_length(g_request_raw_queue);
1061                                 if (len > 0) {
1062                                         tmp_raw_req = g_queue_peek_head(g_request_raw_queue);
1063                                         if (tmp_raw_req != NULL)
1064                                                 tmp_raw_req->isCanceled = true;
1065                                         return MS_MEDIA_ERR_NONE;
1066                                 }
1067                         }
1068                         thumb_err("request_queue is NULL");
1069                         return MS_MEDIA_ERR_INTERNAL;
1070                 }
1071                 req_len = g_queue_get_length(g_manage_raw_queue);
1072                 for (i = 0; i < req_len; i++) {
1073                         tmp_raw_req = g_queue_pop_tail(g_manage_raw_queue);
1074                         if (tmp_raw_req) {
1075                                 SAFE_FREE(tmp_raw_req->path);
1076                                 SAFE_FREE(tmp_raw_req->userData);
1077                                 SAFE_FREE(tmp_raw_req);
1078                                 tmp_raw_req = NULL;
1079                         }
1080                 }
1081                 g_queue_free(g_manage_raw_queue);
1082                 g_manage_raw_queue = NULL;
1083                 if (g_request_raw_queue != NULL) {
1084                         /* Check request queue */
1085                         len = g_queue_get_length(g_request_raw_queue);
1086                         if (len > 0) {
1087                                 tmp_raw_req = g_queue_peek_head(g_request_raw_queue);
1088                                 if (tmp_raw_req != NULL)
1089                                         tmp_raw_req->isCanceled = true;
1090                         }
1091                 }
1092         } else {
1093                 thumbReq *tmp_req = NULL;
1094                 if (g_manage_queue == NULL) {
1095                         thumb_err("manage_queue is NULL");
1096                         if (g_request_queue != NULL) {
1097                                 /* Check request queue */
1098                                 len = g_queue_get_length(g_request_queue);
1099                                 if (len > 0) {
1100                                         tmp_req = g_queue_peek_head(g_request_queue);
1101                                         if (tmp_req != NULL)
1102                                                 tmp_req->isCanceled = true;
1103                                         return MS_MEDIA_ERR_NONE;
1104                                 }
1105                         }
1106                         thumb_err("request_queue is NULL");
1107                         return MS_MEDIA_ERR_INTERNAL;
1108                 }
1109                 req_len = g_queue_get_length(g_manage_queue);
1110                 for (i = 0; i < req_len; i++) {
1111                         tmp_req = g_queue_pop_tail(g_manage_queue);
1112                         if (tmp_req) {
1113                                 SAFE_FREE(tmp_req->path);
1114                                 SAFE_FREE(tmp_req->userData);
1115                                 SAFE_FREE(tmp_req);
1116                                 tmp_req = NULL;
1117                         }
1118                 }
1119                 g_queue_free(g_manage_queue);
1120                 g_manage_queue = NULL;
1121                 if (g_request_queue != NULL) {
1122                         /* Check request queue */
1123                         len = g_queue_get_length(g_request_queue);
1124                         if (len > 0) {
1125                                 tmp_req = g_queue_peek_head(g_request_queue);
1126                                 if (tmp_req != NULL)
1127                                         tmp_req->isCanceled = true;
1128                         }
1129                 }
1130         }
1131
1132         return MS_MEDIA_ERR_NONE;
1133 }