check the return value of function 'dlsym'
[platform/core/multimedia/media-server.git] / src / mediadb-update.c
1 /*
2  *  Media Server
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Yong Yeon Kim <yy9875.kim@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 <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <dlfcn.h>
26 #include <glib.h>
27 #include <sys/time.h>
28 #include <tzplatform_config.h>
29
30 #include "media-util.h"
31
32 #define PATH_PLUGIN_LIB                         PATH_LIBDIR"/libmedia-content-plugin.so"
33
34 static GMainLoop * mainloop = NULL;
35 static struct timeval start_time;
36
37 static int (*svc_check_db)                      (sqlite3 * handle, uid_t uid);
38 static int (*svc_get_storage_id)                (sqlite3 * handle, const char *path, char *storage_id, uid_t uid);
39
40 static void callback(media_request_result_s * result, void *user_data)
41 {
42         long long ms_time = 0;
43         struct timeval end_time;
44
45         if (result->result != MEDIA_REQUEST_SCAN_PARTIAL) {
46                 gettimeofday(&end_time, NULL);
47                 ms_time = (end_time.tv_sec * 1000LL + end_time.tv_usec / 1000) - (start_time.tv_sec * 1000LL + start_time.tv_usec/ 1000);
48
49                 printf("db updating done. Time [%lld]\n", ms_time);
50                 g_main_loop_quit(mainloop);
51         }
52 }
53
54 static void __print_help(void)
55 {
56         printf("=======================================================================================\n");
57         printf("\n");
58         printf("db-update [option] <directory path> \n");
59         printf("\n");
60         printf("[option]\n");
61         printf("        -r : [only directory] update all directory recursivly under <directory path>\n");
62         printf("\n");
63         printf("db-update --help for check this messages.\n");
64         printf("\n");
65         printf("A file or directory must exists under %s or %s.\n", tzplatform_getenv(TZ_USER_CONTENT), tzplatform_mkpath(TZ_SYS_STORAGE, "sdcard"));
66         printf("Using %s is allowed SD card is mounted.\n", tzplatform_mkpath(TZ_SYS_STORAGE, "sdcard"));
67         printf("\n");
68         printf("=======================================================================================\n");
69 }
70
71 static void __check_media_db(void)
72 {
73         void *funcHandle = NULL;
74         sqlite3 *db_handle = NULL;
75         int ret = 0;
76
77         funcHandle = dlopen(PATH_PLUGIN_LIB, RTLD_LAZY);
78         if (funcHandle == NULL) {
79                 printf("Error when open plug-in\n");
80                 return;
81         }
82
83         svc_check_db = dlsym(funcHandle, "check_db");
84         if (svc_check_db == NULL) {
85                 printf("find check_db failed\n");
86                 dlclose(funcHandle);
87                 return;
88         }
89
90         ret = media_db_connect(&db_handle, tzplatform_getuid(TZ_USER_NAME), false);
91         if (ret < 0)
92                 printf("Error svc_connect\n");
93
94         ret = svc_check_db(db_handle, tzplatform_getuid(TZ_USER_NAME));
95         if (ret < 0)
96                 printf("Error svc_check_db\n");
97
98         media_db_disconnect(db_handle);
99
100         printf("Check media db done\n");
101
102         dlclose(funcHandle);
103 }
104
105 static int __get_storage_id(const char *path, char *storage_id, uid_t uid)
106 {
107         void *funcHandle = NULL;
108         sqlite3 *db_handle = NULL;
109         int ret = 0;
110
111         if (strncmp(tzplatform_getenv(TZ_USER_CONTENT), path, strlen(tzplatform_getenv(TZ_USER_CONTENT))) != 0 && strncmp(tzplatform_getenv(TZ_SYS_STORAGE), path, strlen(tzplatform_getenv(TZ_SYS_STORAGE))) != 0) {
112                 printf("Not support path[%s]\n", path);
113                 return -1;
114         }
115
116         funcHandle = dlopen(PATH_PLUGIN_LIB, RTLD_LAZY);
117         if (funcHandle == NULL) {
118                 printf("Error when open plug-in\n");
119                 return -1;
120         }
121
122         svc_get_storage_id = dlsym(funcHandle, "get_storage_id");
123         if (svc_get_storage_id == NULL) {
124                 printf("find get_storage_id failed\n");
125                 dlclose(funcHandle);
126                 return -1;
127         }
128
129         ret = media_db_connect(&db_handle, uid, false);
130         if (ret < 0) {
131                 printf("Error svc_connect\n");
132                 dlclose(funcHandle);
133                 return -1;
134         }
135
136         ret = svc_get_storage_id(db_handle, path, storage_id, tzplatform_getuid(TZ_USER_NAME));
137         if (ret < 0) {
138                 printf("Error svc_get_storage_id\n");
139                 dlclose(funcHandle);
140                 return -1;
141         }
142         media_db_disconnect(db_handle);
143         printf("Start Scanning for [%s][%s]\n", path, storage_id);
144
145         dlclose(funcHandle);
146
147         return 0;
148 }
149
150 int dir_scan(const char *path, bool is_recursive)
151 {
152         int ret = 0;
153         char storage_id[36+1] = {0,};
154
155         ret = __get_storage_id(path, storage_id, tzplatform_getuid(TZ_USER_NAME));
156         if (ret < 0)
157                 return -1;
158
159         gettimeofday(&start_time, NULL);
160         return media_directory_scanning_async(path, storage_id, is_recursive, callback, NULL, tzplatform_getuid(TZ_USER_NAME));
161 }
162
163 int main(int argc, char **argv)
164 {
165         int ret;
166         int len;
167         char *argv1 = NULL;
168         char *argv2 = NULL;
169         char *req_path = NULL;
170
171         if (argc > 3 || argc < 2) {
172                 __print_help();
173                 exit(0);
174         }
175
176         argv1 = argv[1];
177
178         mainloop = g_main_loop_new(NULL, FALSE);
179
180         if (argc == 2) {
181                 if (strcmp(argv1 , "--help") == 0) {
182                         __print_help();
183                         exit(0);
184                 }
185
186                 if (strcmp(argv1 , "check_db") == 0) {
187                         __check_media_db();
188                         exit(0);
189                 }
190
191                 if (g_file_test(argv1, G_FILE_TEST_IS_DIR)) {
192                         len = strlen(argv1);
193                         if (argv1[len - 1] == '/')
194                                 req_path = g_strndup(argv1, len - 1);
195                         else
196                                 req_path = g_strdup(argv1);
197
198                         ret = dir_scan(req_path, false);
199                         g_free(req_path);
200
201                         if (ret != 0) {
202                                 printf("error : %d\n", ret);
203                                 exit(0);
204                         }
205                 } else {
206                         printf("[%d]invalid path\n", __LINE__);
207                         __print_help();
208                         exit(0);
209                 }
210         } else if (argc == 3) {
211                 argv2 = argv[2];
212                 if (strcmp(argv1, "-r") == 0) {
213                         if ((argv2 != NULL) && (g_file_test(argv2, G_FILE_TEST_IS_DIR))) {
214                                 len = strlen(argv2);
215                                 if (argv2[len - 1] == '/')
216                                         req_path = g_strndup(argv2, len - 1);
217                                 else
218                                         req_path = g_strdup(argv2);
219
220                                 ret = dir_scan(req_path, true);
221                                 g_free(req_path);
222
223                                 if (ret != 0) {
224                                         printf("error : %d\n", ret);
225                                         exit(0);
226                                 }
227                         } else {
228                                 printf("[%d]invalid path\n", __LINE__);
229                                 __print_help();
230                                 exit(0);
231                         }
232                 } else {
233                         printf("[%d] invalid option\n", __LINE__);
234                         __print_help();
235                         exit(0);
236                 }
237         }
238
239         g_main_loop_run(mainloop);
240
241         exit(0);
242 }