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