Change strncpy to g_strlcpy
[platform/core/multimedia/libmedia-thumbnail.git] / server / thumb-server-internal.c
1 /*
2  * media-thumbnail-server
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 "thumb-server-internal.h"
23 #include "media-thumb-util.h"
24 #include "media-thumb-debug.h"
25
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <dirent.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <Ecore_Evas.h>
32 #include <vconf.h>
33
34 #ifdef LOG_TAG
35 #undef LOG_TAG
36 #endif
37
38 #define LOG_TAG "MEDIA_THUMBNAIL_SERVER"
39 #define THUMB_DEFAULT_WIDTH 320
40 #define THUMB_DEFAULT_HEIGHT 240
41 #define THUMB_BLOCK_SIZE 512
42 #define THUMB_COMM_SOCK_PATH tzplatform_mkpath(TZ_SYS_RUN, "media-server/media_ipc_thumbcomm.socket")
43 #define THUMB_EMPTY_STR ""
44
45 static __thread char **arr_path;
46 static __thread uid_t *arr_uid;
47 static __thread int g_idx = 0;
48 static __thread int g_cur_idx = 0;
49
50 GMainLoop *g_thumb_server_mainloop; // defined in thumb-server.c as extern
51
52 static gboolean __thumb_server_send_msg_to_agent(int msg_type);
53 static void __thumb_daemon_stop_job(void);
54 static int __thumb_daemon_all_extract(uid_t uid);
55 int _thumb_daemon_process_queue_jobs(gpointer data);
56 static gboolean _thumb_server_send_deny_message(int sockfd);
57
58 gboolean _thumb_daemon_start_jobs(gpointer data)
59 {
60         thumb_dbg("");
61         /* Initialize ecore-evas to use evas library */
62         ecore_evas_init();
63
64         __thumb_server_send_msg_to_agent(MS_MSG_THUMB_SERVER_READY);
65
66         return FALSE;
67 }
68
69 void _thumb_daemon_finish_jobs(void)
70 {
71         sqlite3 *sqlite_db_handle = _media_thumb_db_get_handle();
72
73         if (sqlite_db_handle != NULL) {
74                 _media_thumb_db_disconnect();
75                 thumb_dbg("sqlite3 handle is alive. So disconnect to sqlite3");
76         }
77
78         /* Shutdown ecore-evas */
79         ecore_evas_shutdown();
80         g_main_loop_quit(g_thumb_server_mainloop);
81
82         return;
83 }
84
85 int _thumb_daemon_mmc_status(void)
86 {
87         int err = -1;
88         int status = -1;
89
90         err = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &status);
91         if (err == 0) {
92                 return status;
93         } else if (err == -1) {
94                 thumb_err("vconf_get_int failed : %d", err);
95         } else {
96                 thumb_err("vconf_get_int Unexpected error code: %d", err);
97         }
98
99         return status;
100 }
101
102 void _thumb_daemon_mmc_eject_vconf_cb(void *data)
103 {
104         int err = -1;
105         int status = 0;
106
107         thumb_warn("_thumb_daemon_vconf_cb called");
108
109         err = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &status);
110         if (err == 0) {
111                 if (status == VCONFKEY_SYSMAN_MMC_REMOVED || status == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED) {
112                         thumb_warn("SD card is ejected or not mounted. So media-thumbnail-server stops jobs to extract all thumbnails");
113
114                         __thumb_daemon_stop_job();
115                 }
116         } else if (err == -1) {
117                 thumb_err("vconf_get_int failed : %d", err);
118         } else {
119                 thumb_err("vconf_get_int Unexpected error code: %d", err);
120         }
121
122         return;
123 }
124
125 void _thumb_daemon_vconf_cb(void *data)
126 {
127         int err = -1;
128         int status = 0;
129
130         thumb_warn("_thumb_daemon_vconf_cb called");
131
132         err = vconf_get_int(VCONFKEY_SYSMAN_MMC_FORMAT, &status);
133         if (err == 0) {
134                 if (status == VCONFKEY_SYSMAN_MMC_FORMAT_COMPLETED) {
135                         thumb_warn("SD card format is completed. So media-thumbnail-server stops jobs to extract all thumbnails");
136
137                         __thumb_daemon_stop_job();
138                 } else {
139                         thumb_dbg("not completed");
140                 }
141         } else if (err == -1) {
142                 thumb_err("vconf_get_int failed : %d", err);
143         } else {
144                 thumb_err("vconf_get_int Unexpected error code: %d", err);
145         }
146
147         return;
148 }
149
150 static void __thumb_daemon_stop_job()
151 {
152         int i = 0;
153         char *path = NULL;
154
155         thumb_warn("There are %d jobs in the queue. But all jobs will be stopped", g_idx - g_cur_idx);
156
157         for (i = g_cur_idx; i < g_idx; i++) {
158                 path = arr_path[g_cur_idx++];
159                 SAFE_FREE(path);
160         }
161
162         return;
163 }
164
165 int _thumb_daemon_process_job(thumbMsg *req_msg, thumbMsg *res_msg)
166 {
167         int err = MS_MEDIA_ERR_NONE;
168
169         err = _media_thumb_process(req_msg, res_msg);
170         if (err != MS_MEDIA_ERR_NONE)
171                 thumb_warn("_media_thumb_process is failed: %d", err);
172
173         return err;
174 }
175
176 static int __thumb_daemon_process_job_raw(thumbMsg *req_msg, thumbMsg *res_msg)
177 {
178         int err = MS_MEDIA_ERR_NONE;
179
180         err = _media_thumb_process_raw(req_msg, res_msg);
181         if (err != MS_MEDIA_ERR_NONE)
182                 thumb_warn("_media_thumb_process_raw is failed: %d", err);
183
184         return err;
185 }
186
187 static int __thumb_daemon_all_extract(uid_t uid)
188 {
189         int err = MS_MEDIA_ERR_NONE;
190         char query_string[MAX_PATH_SIZE + 1] = { 0, };
191         char path[MAX_PATH_SIZE + 1] = { 0, };
192         sqlite3 *sqlite_db_handle = NULL;
193         sqlite3_stmt *sqlite_stmt = NULL;
194
195         err = _media_thumb_db_connect(uid);
196         if (err != MS_MEDIA_ERR_NONE) {
197                 thumb_err("_media_thumb_db_connect failed: %d", err);
198                 return err;
199         }
200
201         sqlite_db_handle = _media_thumb_db_get_handle();
202         if (sqlite_db_handle == NULL) {
203                 thumb_err("sqlite handle is NULL");
204                 return MS_MEDIA_ERR_INTERNAL;
205         }
206
207         if (_thumb_daemon_mmc_status() == VCONFKEY_SYSMAN_MMC_MOUNTED) {
208                 snprintf(query_string, sizeof(query_string), SELECT_PATH_FROM_UNEXTRACTED_THUMB_MEDIA);
209         } else {
210                 snprintf(query_string, sizeof(query_string), SELECT_PATH_FROM_UNEXTRACTED_THUMB_INTERNAL_MEDIA);
211         }
212
213         thumb_warn("Query: %s", query_string);
214
215         err = sqlite3_prepare_v2(sqlite_db_handle, query_string, strlen(query_string), &sqlite_stmt, NULL);
216         if (SQLITE_OK != err) {
217                 thumb_err("prepare error [%s]", sqlite3_errmsg(sqlite_db_handle));
218                 _media_thumb_db_disconnect();
219                 return MS_MEDIA_ERR_INTERNAL;
220         }
221
222         while (1) {
223                 err = sqlite3_step(sqlite_stmt);
224                 if (err != SQLITE_ROW) {
225                         thumb_dbg("end of row [%s]", sqlite3_errmsg(sqlite_db_handle));
226                         break;
227                 }
228
229                 SAFE_STRLCPY(path, (const char *)sqlite3_column_text(sqlite_stmt, 0), sizeof(path));
230                 path[sizeof(path) - 1] = '\0';
231
232                 thumb_dbg_slog("Path : %s", path);
233
234                 if (g_idx == 0) {
235                         arr_path = (char**)malloc(sizeof(char*));
236                         arr_uid = (uid_t*)malloc(sizeof(uid_t));
237                 } else {
238                         arr_path = (char**)realloc(arr_path, (g_idx + 1) * sizeof(char*));
239                         arr_uid = (uid_t*)realloc(arr_uid, (g_idx + 1) * sizeof(uid_t));
240                 }
241                 arr_uid[g_idx] = uid;
242                 arr_path[g_idx++] = strdup(path);
243         }
244
245         sqlite3_finalize(sqlite_stmt);
246         _media_thumb_db_disconnect();
247
248         return MS_MEDIA_ERR_NONE;
249 }
250
251 int _thumb_daemon_process_queue_jobs(gpointer data)
252 {
253         int err = MS_MEDIA_ERR_NONE;
254         char *path = NULL;
255         uid_t uid = NULL;
256
257         if (g_cur_idx < g_idx) {
258                 thumb_warn("There are %d jobs in the queue", g_idx - g_cur_idx);
259                 thumb_dbg("Current idx : [%d]", g_cur_idx);
260                 uid = arr_uid[g_cur_idx];
261                 path = arr_path[g_cur_idx++];
262
263                 thumbMsg recv_msg, res_msg;
264                 memset(&recv_msg, 0x00, sizeof(thumbMsg));
265                 memset(&res_msg, 0x00, sizeof(thumbMsg));
266
267                 recv_msg.msg_type = THUMB_REQUEST_DB_INSERT;
268                 recv_msg.uid = uid;
269                 SAFE_STRLCPY(recv_msg.org_path, path, sizeof(recv_msg.org_path));
270                 recv_msg.org_path[sizeof(recv_msg.org_path) - 1] = '\0';
271
272                 err = _thumb_daemon_process_job(&recv_msg, &res_msg);
273                 if (err != MS_MEDIA_ERR_NONE)
274                         thumb_err("Thumbnail processing is failed : %d", err);
275
276                 SAFE_FREE(path);
277         } else {
278                 g_cur_idx = 0;
279                 g_idx = 0;
280                 thumb_warn("Deleting array");
281                 SAFE_FREE(arr_path);
282                 SAFE_FREE(arr_uid);
283                 //_media_thumb_db_disconnect();
284
285                 __thumb_server_send_msg_to_agent(MS_MSG_THUMB_EXTRACT_ALL_DONE); // MS_MSG_THUMB_EXTRACT_ALL_DONE
286
287                 return FALSE;
288         }
289
290         return TRUE;
291 }
292
293 gboolean _thumb_server_read_socket(GIOChannel *src,
294                                                                         GIOCondition condition,
295                                                                         gpointer data)
296 {
297         struct sockaddr_un client_addr;
298         unsigned int client_addr_len;
299         thumbMsg recv_msg;
300         thumbMsg res_msg;
301         ms_peer_credentials credentials;
302
303         int sock = -1;
304         int client_sock = -1;
305
306         memset((void *)&recv_msg, 0, sizeof(recv_msg));
307         memset((void *)&res_msg, 0, sizeof(res_msg));
308         memset((void *)&credentials, 0, sizeof(credentials));
309
310         sock = g_io_channel_unix_get_fd(src);
311         if (sock < 0) {
312                 thumb_err("sock fd is invalid!");
313                 return TRUE;
314         }
315
316         client_addr_len = sizeof(client_addr);
317
318         if ((client_sock = accept(sock, (struct sockaddr*)&client_addr, &client_addr_len)) < 0) {
319                 thumb_stderror("accept failed : %s");
320                 return TRUE;
321         }
322
323         if (ms_cynara_receive_untrusted_message_thumb(client_sock, &recv_msg, &credentials) != MS_MEDIA_ERR_NONE) {
324                 thumb_err("ms_cynara_receive_untrusted_message_thumb failed");
325                 close(client_sock);
326                 return FALSE;
327         }
328
329         if (recv_msg.msg_type != THUMB_REQUEST_KILL_SERVER) {
330                 if (ms_cynara_check(&credentials, MEDIA_STORAGE_PRIVILEGE) != MS_MEDIA_ERR_NONE) {
331                         thumb_err("Cynara denied access to process request");
332                         _thumb_server_send_deny_message(client_sock);
333                         close(client_sock);
334                         return TRUE;
335                 }
336         }
337
338         SAFE_FREE(credentials.smack);
339         SAFE_FREE(credentials.uid);
340
341         thumb_warn_slog("Received [%d] %s(%d) from PID(%d)", recv_msg.msg_type, recv_msg.org_path, strlen(recv_msg.org_path), recv_msg.pid);
342
343         if (recv_msg.msg_type == THUMB_REQUEST_ALL_MEDIA) {
344                 thumb_dbg("All thumbnails are being extracted now");
345                 __thumb_daemon_all_extract(recv_msg.uid);
346                 g_idle_add(_thumb_daemon_process_queue_jobs, NULL);
347         } else if (recv_msg.msg_type == THUMB_REQUEST_RAW_DATA) {
348                 __thumb_daemon_process_job_raw(&recv_msg, &res_msg);
349         } else if (recv_msg.msg_type == THUMB_REQUEST_KILL_SERVER) {
350                 thumb_warn("received KILL msg from thumbnail agent.");
351         } else {
352                 _thumb_daemon_process_job(&recv_msg, &res_msg);
353         }
354
355         if (res_msg.msg_type == 0)
356                 res_msg.msg_type = recv_msg.msg_type;
357         res_msg.request_id = recv_msg.request_id;
358         SAFE_STRLCPY(res_msg.org_path, recv_msg.org_path, sizeof(res_msg.org_path));
359         res_msg.origin_path_size = recv_msg.origin_path_size;
360         if (res_msg.msg_type != THUMB_RESPONSE_RAW_DATA) {
361                 res_msg.dest_path_size = strlen(res_msg.dst_path)+1;
362                 res_msg.thumb_size = 0;
363         } else {
364                 res_msg.dest_path_size = 1;
365                 res_msg.dst_path[0] = '\0';
366         }
367
368         int buf_size = 0;
369         int sending_block = 0;
370         int block_size = sizeof(res_msg) - MAX_FILEPATH_LEN*2 - sizeof(unsigned char *);
371         unsigned char *buf = NULL;
372         _media_thumb_set_buffer(&res_msg, &buf, &buf_size);
373
374         while (buf_size > 0) {
375                 if (buf_size < THUMB_BLOCK_SIZE) {
376                         block_size = buf_size;
377                 }
378                 if (send(client_sock, buf+sending_block, block_size, 0) != block_size) {
379                         thumb_stderror("sendto failed : %s");
380                 }
381                 sending_block += block_size;
382                 buf_size -= block_size;
383                 if (block_size < THUMB_BLOCK_SIZE) {
384                         block_size = THUMB_BLOCK_SIZE;
385                 }
386         }
387         if (buf_size == 0) {
388                 thumb_dbg_slog("Sent data(%d) from %s", res_msg.thumb_size, res_msg.org_path);
389         }
390         SAFE_FREE(buf);
391         SAFE_FREE(res_msg.thumb_data);
392
393         if (recv_msg.msg_type == THUMB_REQUEST_KILL_SERVER) {
394                 thumb_warn("Shutting down...");
395                 g_main_loop_quit(g_thumb_server_mainloop);
396         }
397
398         close(client_sock);
399
400         return TRUE;
401 }
402
403 static gboolean __thumb_server_send_msg_to_agent(int msg_type)
404 {
405         int sock;
406         ms_sock_info_s sock_info;
407         struct sockaddr_un serv_addr;
408         ms_thumb_server_msg send_msg;
409         sock_info.port = MS_THUMB_COMM_PORT;
410
411         if (ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock_info) < 0) {
412                 thumb_err("ms_ipc_create_server_socket failed");
413                 return FALSE;
414         }
415
416         memset(&serv_addr, 0, sizeof(serv_addr));
417
418         sock = sock_info.sock_fd;
419         serv_addr.sun_family = AF_UNIX;
420         SAFE_STRLCPY(serv_addr.sun_path, THUMB_COMM_SOCK_PATH, sizeof(serv_addr.sun_path));
421
422
423         /* Connecting to the thumbnail server */
424         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
425                 thumb_stderror("connect");
426                 ms_ipc_delete_client_socket(&sock_info);
427                 return MS_MEDIA_ERR_SOCKET_CONN;
428         }
429
430         send_msg.msg_type = msg_type;
431
432         if (send(sock, &send_msg, sizeof(ms_thumb_server_msg), 0) != sizeof(ms_thumb_server_msg)) {
433                 thumb_stderror("sendto failed");
434                 ms_ipc_delete_client_socket(&sock_info);
435                 return FALSE;
436         }
437
438         thumb_dbg("Sending msg to thumbnail agent[%d] is successful", send_msg.msg_type);
439
440         ms_ipc_delete_client_socket(&sock_info);
441
442         return TRUE;
443 }
444
445 static gboolean _thumb_server_send_deny_message(int sockfd)
446 {
447         thumbMsg msg = {0};
448         int bytes_to_send = sizeof(msg) - sizeof(msg.org_path) - sizeof(msg.dst_path);
449
450         msg.msg_type = THUMB_RESPONSE;
451         msg.status = MS_MEDIA_ERR_PERMISSION_DENIED;
452
453         if (send(sockfd, &msg, bytes_to_send, 0) != bytes_to_send) {
454                 thumb_stderror("send failed");
455                 return FALSE;
456         }
457
458         return TRUE;
459 }
460
461 gboolean _thumb_server_prepare_socket(int *sock_fd)
462 {
463         int sock;
464         unsigned short serv_port;
465
466         thumbMsg recv_msg;
467         thumbMsg res_msg;
468
469         memset((void *)&recv_msg, 0, sizeof(recv_msg));
470         memset((void *)&res_msg, 0, sizeof(res_msg));
471         serv_port = MS_THUMB_DAEMON_PORT;
472
473         if (ms_ipc_create_server_socket(serv_port, &sock) < 0) {
474                 thumb_err("ms_ipc_create_server_socket failed");
475                 return FALSE;
476         }
477
478         if (ms_cynara_enable_credentials_passing(sock) != MS_MEDIA_ERR_NONE) {
479                 thumb_err("ms_cynara_enable_credentials_passing failed");
480                 close(sock);
481                 return FALSE;
482         }
483
484         *sock_fd = sock;
485
486         return TRUE;
487 }
488
489 int _thumbnail_get_data(const char *origin_path,
490                                                 media_thumb_format format,
491                                                 char *thumb_path,
492                                                 unsigned char **data,
493                                                 int *size,
494                                                 int *width,
495                                                 int *height,
496                                                 int *origin_width,
497                                                 int *origin_height,
498                                                 int *alpha,
499                                                 bool *is_saved)
500 {
501         int err = MS_MEDIA_ERR_NONE;
502         int thumb_width = -1;
503         int thumb_height = -1;
504
505         if (origin_path == NULL || size == NULL
506                         || width == NULL || height == NULL) {
507                 thumb_err("Invalid parameter");
508                 return MS_MEDIA_ERR_INVALID_PARAMETER;
509         }
510
511         if (format < MEDIA_THUMB_BGRA || format > MEDIA_THUMB_RGB888) {
512                 thumb_err("parameter format is invalid");
513                 return MS_MEDIA_ERR_INVALID_PARAMETER;
514         }
515
516         if (!g_file_test
517                 (origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
518                         thumb_err("Original path (%s) does not exist", origin_path);
519                         return MS_MEDIA_ERR_INVALID_PARAMETER;
520         }
521
522         thumb_dbg("Origin path : %s", origin_path);
523
524         int file_type = THUMB_NONE_TYPE;
525         media_thumb_info thumb_info = {0,};
526         file_type = _media_thumb_get_file_type(origin_path);
527         thumb_width = *width;
528         thumb_height = *height;
529         if (thumb_width == 0 || thumb_height == 0) {
530                 thumb_width = THUMB_DEFAULT_WIDTH;
531                 thumb_height = THUMB_DEFAULT_HEIGHT;
532         }
533
534         thumb_info.is_raw = FALSE;
535
536         if (file_type == THUMB_IMAGE_TYPE) {
537                 err = _media_thumb_image(origin_path, thumb_path, thumb_width, thumb_height, format, &thumb_info);
538                 if (err != MS_MEDIA_ERR_NONE) {
539                         thumb_err("_media_thumb_image failed");
540                         return err;
541                 }
542         } else if (file_type == THUMB_VIDEO_TYPE) {
543                 err = _media_thumb_video(origin_path, thumb_width, thumb_height, format, &thumb_info);
544                 if (err != MS_MEDIA_ERR_NONE) {
545                         thumb_err("_media_thumb_image failed");
546                         return err;
547                 }
548         } else {
549                 thumb_err("invalid file type");
550                 return MS_MEDIA_ERR_THUMB_UNSUPPORTED;
551         }
552
553         if (size) *size = thumb_info.size;
554         if (width) *width = thumb_info.width;
555         if (height) *height = thumb_info.height;
556         *data = thumb_info.data;
557         if (origin_width) *origin_width = thumb_info.origin_width;
558         if (origin_height) *origin_height = thumb_info.origin_height;
559         if (alpha) *alpha = thumb_info.alpha;
560         if (is_saved) *is_saved = thumb_info.is_saved;
561
562         thumb_dbg("Thumb data is generated successfully (Size:%d, W:%d, H:%d) 0x%x", *size, *width, *height, *data);
563
564         return MS_MEDIA_ERR_NONE;
565 }
566
567 int _thumbnail_get_raw_data(const char *origin_path,
568                                                 media_thumb_format format,
569                                                 int *width,
570                                                 int *height,
571                                                 unsigned char **data,
572                                                 int *size)
573 {
574         int err = MS_MEDIA_ERR_NONE;
575         int thumb_width = -1;
576         int thumb_height = -1;
577         const char * thumb_path = NULL;
578
579         if (origin_path == NULL || *width <= 0 || *height <= 0) {
580                 thumb_err("Invalid parameter");
581                 return MS_MEDIA_ERR_INVALID_PARAMETER;
582         }
583
584         if (format < MEDIA_THUMB_BGRA || format > MEDIA_THUMB_RGB888) {
585                 thumb_err("parameter format is invalid");
586                 return MS_MEDIA_ERR_INVALID_PARAMETER;
587         }
588
589         if (!g_file_test(origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
590                 thumb_err("Original path (%s) does not exist", origin_path);
591                 return MS_MEDIA_ERR_INVALID_PARAMETER;
592         }
593
594         int file_type = THUMB_NONE_TYPE;
595         media_thumb_info thumb_info = {0,};
596         file_type = _media_thumb_get_file_type(origin_path);
597         thumb_width = *width;
598         thumb_height = *height;
599         thumb_info.is_raw = TRUE;
600
601         if (file_type == THUMB_IMAGE_TYPE) {
602                 err = _media_thumb_image(origin_path, thumb_path, thumb_width, thumb_height, format, &thumb_info);
603                 if (err != MS_MEDIA_ERR_NONE) {
604                         thumb_err("_media_thumb_image failed");
605                         return err;
606                 }
607         } else if (file_type == THUMB_VIDEO_TYPE) {
608                 err = _media_thumb_video(origin_path, thumb_width, thumb_height, format, &thumb_info);
609                 if (err != MS_MEDIA_ERR_NONE) {
610                         thumb_err("_media_thumb_image failed");
611                         return err;
612                 }
613         } else {
614                 thumb_err("invalid file type");
615                 return MS_MEDIA_ERR_INVALID_PARAMETER;
616         }
617
618         if (size) *size = thumb_info.size;
619         *data = thumb_info.data;
620         *width = thumb_info.width;
621         *height = thumb_info.height;
622
623         return MS_MEDIA_ERR_NONE;
624 }
625
626 int _media_thumb_process(thumbMsg *req_msg, thumbMsg *res_msg)
627 {
628         int err = MS_MEDIA_ERR_NONE;
629         unsigned char *data = NULL;
630         int thumb_size = 0;
631         int thumb_w = 0;
632         int thumb_h = 0;
633         int origin_w = 0;
634         int origin_h = 0;
635         int max_length = 0;
636         char *thumb_path = NULL;
637         int need_update_db = 0;
638         int alpha = 0;
639         bool is_saved = FALSE;
640
641         if (req_msg == NULL || res_msg == NULL) {
642                 thumb_err("Invalid msg!");
643                 return MS_MEDIA_ERR_INVALID_PARAMETER;
644         }
645
646         int msg_type = req_msg->msg_type;
647         const char *origin_path = req_msg->org_path;
648
649         media_thumb_format thumb_format = MEDIA_THUMB_BGRA;
650         thumb_w = req_msg->thumb_width;
651         thumb_h = req_msg->thumb_height;
652         thumb_path = res_msg->dst_path;
653         thumb_path[0] = '\0';
654         max_length = sizeof(res_msg->dst_path) -1;
655         res_msg->status = MS_MEDIA_ERR_NONE;
656
657         if (!g_file_test(origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
658                 thumb_err("origin_path does not exist in file system.");
659                 res_msg->status = MS_MEDIA_ERR_FILE_NOT_EXIST;
660                 return MS_MEDIA_ERR_FILE_NOT_EXIST;
661         }
662
663         err = _media_thumb_db_connect(req_msg->uid);
664         if (err != MS_MEDIA_ERR_NONE) {
665                 thumb_err("_media_thumb_mb_svc_connect failed: %d", err);
666                 res_msg->status = MS_MEDIA_ERR_DB_CONNECT_FAIL;
667                 return err;
668         }
669
670         if (msg_type == THUMB_REQUEST_DB_INSERT) {
671                 err = _media_thumb_get_thumb_from_db_with_size(origin_path, thumb_path, max_length, &need_update_db, &origin_w, &origin_h);
672                 if (err == MS_MEDIA_ERR_NONE) {
673                         res_msg->origin_width = origin_w;
674                         res_msg->origin_height = origin_h;
675                         _media_thumb_db_disconnect();
676                         return MS_MEDIA_ERR_NONE;
677                 } else {
678                         if (strlen(thumb_path) == 0) {
679                                 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length, req_msg->uid);
680                                 if (err != MS_MEDIA_ERR_NONE) {
681                                         thumb_err("_media_thumb_get_hash_name failed - %d", err);
682                                         strncpy(thumb_path, THUMB_EMPTY_STR, max_length);
683                                         _media_thumb_db_disconnect();
684                                         res_msg->status = err;
685                                         return err;
686                                 }
687
688                                 thumb_path[strlen(thumb_path)] = '\0';
689                         }
690                 }
691
692         } else if (msg_type == THUMB_REQUEST_ALL_MEDIA) {
693                 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length, req_msg->uid);
694                 if (err != MS_MEDIA_ERR_NONE) {
695                         thumb_err("_media_thumb_get_hash_name failed - %d", err);
696                         strncpy(thumb_path, THUMB_EMPTY_STR, max_length);
697                         _media_thumb_db_disconnect();
698                         res_msg->status = err;
699                         return err;
700                 }
701
702                 thumb_path[strlen(thumb_path)] = '\0';
703         }
704
705         thumb_dbg_slog("Thumb path : %s", thumb_path);
706
707         if (g_file_test(thumb_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
708                 thumb_warn("thumb path already exists in file system.. remove the existed file");
709                 _media_thumb_remove_file(thumb_path);
710         }
711
712         err = _thumbnail_get_data(origin_path, thumb_format, thumb_path, &data, &thumb_size, &thumb_w, &thumb_h, &origin_w, &origin_h, &alpha, &is_saved);
713         if (err != MS_MEDIA_ERR_NONE) {
714                 thumb_err("_thumbnail_get_data failed - %d", err);
715                 SAFE_FREE(data);
716                 strncpy(thumb_path, THUMB_EMPTY_STR, max_length);
717                 res_msg->status = err;
718
719                 goto DB_UPDATE;
720         }
721
722         res_msg->msg_type = THUMB_RESPONSE;
723         res_msg->thumb_size = thumb_size;
724         res_msg->thumb_width = thumb_w;
725         res_msg->thumb_height = thumb_h;
726         res_msg->origin_width = origin_w;
727         res_msg->origin_height = origin_h;
728
729         /* If the image is transparent PNG format, make png file as thumbnail of this image */
730         if (alpha) {
731                 char file_ext[10];
732                 err = _media_thumb_get_file_ext(origin_path, file_ext, sizeof(file_ext));
733                 if (strncasecmp(file_ext, "png", 3) == 0) {
734                         int len = strlen(thumb_path);
735                         thumb_path[len - 3] = 'p';
736                         thumb_path[len - 2] = 'n';
737                         thumb_path[len - 1] = 'g';
738                 }
739                 thumb_dbg_slog("Thumb path is changed : %s", thumb_path);
740         }
741
742         if (is_saved == FALSE && data != NULL) {
743                 err = _media_thumb_save_to_file_with_evas(data, thumb_w, thumb_h, alpha, thumb_path);
744                 if (err != MS_MEDIA_ERR_NONE) {
745                         thumb_err("save_to_file_with_evas failed - %d", err);
746                         SAFE_FREE(data);
747
748                         if (msg_type == THUMB_REQUEST_DB_INSERT || msg_type == THUMB_REQUEST_ALL_MEDIA) {
749                                 strncpy(thumb_path, THUMB_EMPTY_STR, max_length);
750                         }
751                         _media_thumb_db_disconnect();
752                         res_msg->status = err;
753                         return err;
754                 } else {
755                         thumb_dbg("file save success");
756                 }
757         } else {
758                 thumb_dbg("file is already saved");
759         }
760
761         /* fsync */
762         int fd = 0;
763         fd = open(thumb_path, O_WRONLY);
764         if (fd < 0) {
765                 thumb_warn("open failed");
766         } else {
767                 err = fsync(fd);
768                 if (err == -1) {
769                         thumb_warn("fsync failed");
770                         res_msg->status = MS_MEDIA_ERR_INTERNAL;
771                 }
772
773                 close(fd);
774         }
775         /* End of fsync */
776
777         SAFE_FREE(data);
778 DB_UPDATE:
779         /* DB update if needed */
780         if (need_update_db == 1) {
781                 err = _media_thumb_update_db(origin_path, thumb_path, res_msg->origin_width, res_msg->origin_height, req_msg->uid);
782                 if (err != MS_MEDIA_ERR_NONE) {
783                         thumb_err("_media_thumb_update_db failed : %d", err);
784                         res_msg->status = err;
785                 }
786         }
787
788         _media_thumb_db_disconnect();
789
790         return err;
791 }
792
793 int
794 _media_thumb_process_raw(thumbMsg *req_msg, thumbMsg *res_msg)
795 {
796         int err = MS_MEDIA_ERR_NONE;
797         unsigned char *data = NULL;
798         int thumb_size = 0;
799         int thumb_w = 0;
800         int thumb_h = 0;
801
802         if (req_msg == NULL || res_msg == NULL) {
803                 thumb_err("Invalid msg!");
804                 return MS_MEDIA_ERR_INVALID_PARAMETER;
805         }
806
807         const char *origin_path = req_msg->org_path;
808
809         media_thumb_format thumb_format = MEDIA_THUMB_BGRA;
810         thumb_w = req_msg->thumb_width;
811         thumb_h = req_msg->thumb_height;
812         res_msg->status = MS_MEDIA_ERR_NONE;
813
814         err = _thumbnail_get_raw_data(origin_path, thumb_format, &thumb_w, &thumb_h, &data, &thumb_size);
815
816         if (err != MS_MEDIA_ERR_NONE) {
817                 thumb_err("_thumbnail_get_data failed - %d", err);
818                 res_msg->status = err;
819                 SAFE_FREE(data);
820         }
821
822         res_msg->msg_type = THUMB_RESPONSE_RAW_DATA;
823         res_msg->thumb_width = thumb_w;
824         res_msg->thumb_height = thumb_h;
825         res_msg->thumb_size = thumb_size;
826         res_msg->thumb_data = malloc(thumb_size * sizeof(unsigned char));
827         memcpy(res_msg->thumb_data, data, thumb_size);
828
829         SAFE_FREE(data);
830
831         return err;
832 }