4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 #include <tzplatform_config.h>
30 #include "media-util.h"
32 #define PATH_PLUGIN_LIB PATH_LIBDIR"/libmedia-content-plugin.so"
34 static GMainLoop * mainloop = NULL;
35 static struct timeval start_time;
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);
40 static void callback(media_request_result_s * result, void *user_data)
42 long long ms_time = 0;
43 struct timeval end_time;
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);
49 printf("db updating done. Time [%lld]\n", ms_time);
50 g_main_loop_quit(mainloop);
54 static void __print_help(void)
56 printf("=======================================================================================\n");
58 printf("db-update [option] <directory path> \n");
61 printf(" -r : [only directory] update all directory recursivly under <directory path>\n");
63 printf("db-update --help for check this messages.\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"));
68 printf("=======================================================================================\n");
71 static void __check_media_db(void)
73 void *funcHandle = NULL;
74 sqlite3 *db_handle = NULL;
77 funcHandle = dlopen(PATH_PLUGIN_LIB, RTLD_LAZY);
78 if (funcHandle == NULL) {
79 printf("Error when open plug-in\n");
83 svc_check_db = dlsym(funcHandle, "check_db");
84 if (svc_check_db == NULL) {
85 printf("find check_db failed\n");
90 ret = media_db_connect(&db_handle, tzplatform_getuid(TZ_USER_NAME), false);
92 printf("Error svc_connect\n");
94 ret = svc_check_db(db_handle, tzplatform_getuid(TZ_USER_NAME));
96 printf("Error svc_check_db\n");
98 media_db_disconnect(db_handle);
100 printf("Check media db done\n");
105 static int __get_storage_id(const char *path, char *storage_id, uid_t uid)
107 void *funcHandle = NULL;
108 sqlite3 *db_handle = NULL;
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);
116 funcHandle = dlopen(PATH_PLUGIN_LIB, RTLD_LAZY);
117 if (funcHandle == NULL) {
118 printf("Error when open plug-in\n");
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");
129 ret = media_db_connect(&db_handle, uid, false);
131 printf("Error svc_connect\n");
136 ret = svc_get_storage_id(db_handle, path, storage_id, tzplatform_getuid(TZ_USER_NAME));
138 printf("Error svc_get_storage_id\n");
142 media_db_disconnect(db_handle);
143 printf("Start Scanning for [%s][%s]\n", path, storage_id);
150 int dir_scan(const char *path, bool is_recursive)
153 char storage_id[36+1] = {0,};
155 ret = __get_storage_id(path, storage_id, tzplatform_getuid(TZ_USER_NAME));
159 gettimeofday(&start_time, NULL);
160 return media_directory_scanning_async(path, storage_id, is_recursive, callback, NULL, tzplatform_getuid(TZ_USER_NAME));
163 int main(int argc, char **argv)
169 char *req_path = NULL;
171 if (argc > 3 || argc < 2) {
178 mainloop = g_main_loop_new(NULL, FALSE);
181 if (strcmp(argv1 , "--help") == 0) {
186 if (strcmp(argv1 , "check_db") == 0) {
191 if (g_file_test(argv1, G_FILE_TEST_IS_DIR)) {
193 if (argv1[len - 1] == '/')
194 req_path = g_strndup(argv1, len - 1);
196 req_path = g_strdup(argv1);
198 ret = dir_scan(req_path, false);
202 printf("error : %d\n", ret);
206 printf("[%d]invalid path\n", __LINE__);
210 } else if (argc == 3) {
212 if (strcmp(argv1, "-r") == 0) {
213 if ((argv2 != NULL) && (g_file_test(argv2, G_FILE_TEST_IS_DIR))) {
215 if (argv2[len - 1] == '/')
216 req_path = g_strndup(argv2, len - 1);
218 req_path = g_strdup(argv2);
220 ret = dir_scan(req_path, true);
224 printf("error : %d\n", ret);
228 printf("[%d]invalid path\n", __LINE__);
233 printf("[%d] invalid option\n", __LINE__);
239 g_main_loop_run(mainloop);