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.
23 * This file defines api utilities of contents manager engines.
25 * @file media-server-db-svc.c
26 * @author Yong Yeon Kim(yy9875.kim@samsung.com)
28 * @brief This file implements main database operation.
34 #include "media-util.h"
36 #include "media-scanner-dbg.h"
37 #include "media-scanner-utils.h"
38 #include "media-scanner-drm.h"
39 #include "media-scanner-db-svc.h"
41 #define CONFIG_PATH "/opt/usr/data/file-manager-service/plugin-config"
44 #define MSC_REGISTER_COUNT 100 /*For bundle commit*/
45 #define MSC_VALID_COUNT 100 /*For bundle commit*/
51 void **scan_func_handle = NULL; /*dlopen handel*/
66 eDELETE_INVALID_ITEMS,
76 _msc_get_mime(const char *path, char *mimetype)
81 return MS_MEDIA_ERR_INVALID_PARAMETER;
83 /*get content type and mime type from file. */
84 /*in case of drm file. */
85 if (msc_is_drm_file(path)) {
86 ret = msc_get_mime_in_drm_info(path, mimetype);
87 if (ret != MS_MEDIA_ERR_NONE) {
88 MSC_DBG_ERR("Fail to get mime");
89 return MS_MEDIA_ERR_MIME_GET_FAIL;
92 /*in case of normal files */
93 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
94 MSC_DBG_ERR("aul_get_mime_from_file fail");
95 return MS_MEDIA_ERR_MIME_GET_FAIL;
99 return MS_MEDIA_ERR_NONE;
103 _msc_check_category(const char *path, const char *mimetype, int index)
106 char *err_msg = NULL;
108 ret = ((CHECK_ITEM)func_array[index][eCHECK])(path, mimetype, &err_msg);
110 MSC_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, index), err_msg, path);
111 MS_SAFE_FREE(err_msg);
118 _msc_token_data(char *buf, char **name)
123 pos = strstr(buf, EXT);
125 MSC_DBG_ERR("This is not shared object library.");
129 len = pos - buf + EXT_LEN;
130 *name = strndup(buf, len);
131 MSC_DBG_INFO("%s", *name);
142 char *so_name = NULL;
145 fp = fopen(CONFIG_PATH, "rt");
147 MSC_DBG_ERR("fp is NULL");
148 return MS_MEDIA_ERR_FILE_OPEN_FAIL;
151 if (fgets(buf, 256, fp) == NULL)
154 ret = _msc_token_data(buf, &so_name);
157 g_array_append_val(so_array, so_name);
164 return MS_MEDIA_ERR_NONE;
168 msc_load_functions(void)
170 int lib_index = 0, func_index;
171 char func_list[eFUNC_MAX][40] = {
179 "set_all_storage_items_validity",
180 "set_item_validity_begin",
181 "set_item_validity_end",
183 "delete_all_items_in_storage",
184 "delete_all_invalid_items_in_storage",
187 "set_folder_item_validity",
188 "delete_all_invalid_items_in_folder",
190 /*init array for adding name of so*/
191 so_array = g_array_new(FALSE, FALSE, sizeof(char*));
193 /*load information of so*/
196 if(so_array->len == 0) {
197 MSC_DBG_INFO("There is no information for functions");
198 return MS_MEDIA_ERR_DYNAMIC_LINK;
201 /*the number of functions*/
202 lib_num = so_array->len;
204 MSC_DBG_INFO("The number of information of so : %d", lib_num);
205 MS_MALLOC(scan_func_handle, sizeof(void*) * lib_num);
206 if (scan_func_handle == NULL) {
207 MSC_DBG_ERR("malloc failed");
208 return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL;
211 while(lib_index < lib_num) {
213 MSC_DBG_INFO("[name of so : %s]", g_array_index(so_array, char*, lib_index));
214 scan_func_handle[lib_index] = dlopen(g_array_index(so_array, char*, lib_index), RTLD_LAZY);
215 if (!scan_func_handle[lib_index]) {
216 MSC_DBG_ERR("%s", dlerror());
217 MS_SAFE_FREE(scan_func_handle);
218 return MS_MEDIA_ERR_DYNAMIC_LINK;
223 dlerror(); /* Clear any existing error */
225 /*allocate for array of functions*/
226 MS_MALLOC(func_array, sizeof(void*) * lib_num);
227 if (func_array == NULL) {
228 MSC_DBG_ERR("malloc failed");
229 MS_SAFE_FREE(scan_func_handle);
230 return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL;
233 for(lib_index = 0 ; lib_index < lib_num; lib_index ++) {
234 MS_MALLOC(func_array[lib_index], sizeof(void*) * eFUNC_MAX);
235 if (func_array[lib_index] == NULL) {
238 for (index = 0; index < lib_index; index ++) {
239 MS_SAFE_FREE(func_array[index]);
241 MS_SAFE_FREE(func_array);
242 MS_SAFE_FREE(scan_func_handle);
244 MSC_DBG_ERR("malloc failed");
245 return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL;
249 /*add functions to array */
250 for (lib_index = 0; lib_index < lib_num; lib_index++) {
251 for (func_index = 0; func_index < eFUNC_MAX ; func_index++) {
252 func_array[lib_index][func_index] = dlsym(scan_func_handle[lib_index], func_list[func_index]);
253 if (func_array[lib_index][func_index] == NULL) {
256 for (index = 0; index < lib_index; index ++) {
257 MS_SAFE_FREE(func_array[index]);
259 MS_SAFE_FREE(func_array);
260 MS_SAFE_FREE(scan_func_handle);
262 MSC_DBG_ERR("dlsym failed");
263 return MS_MEDIA_ERR_DYNAMIC_LINK;
268 return MS_MEDIA_ERR_NONE;
272 msc_unload_functions(void)
276 for (lib_index = 0; lib_index < lib_num; lib_index ++)
277 dlclose(scan_func_handle[lib_index]);
279 for (lib_index = 0; lib_index < lib_num; lib_index++) {
280 if (func_array[lib_index]) {
281 MS_SAFE_FREE(func_array[lib_index]);
285 MS_SAFE_FREE (func_array);
286 MS_SAFE_FREE (scan_func_handle);
287 if (so_array) g_array_free(so_array, TRUE);
291 msc_connect_db(void ***handle)
295 char * err_msg = NULL;
297 /*Lock mutex for openning db*/
298 g_mutex_lock(db_mutex);
300 MS_MALLOC(*handle, sizeof (void*) * lib_num);
302 for (lib_index = 0; lib_index < lib_num; lib_index++) {
303 ret = ((CONNECT)func_array[lib_index][eCONNECT])(&((*handle)[lib_index]), &err_msg); /*dlopen*/
305 MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
306 MS_SAFE_FREE(err_msg);
307 g_mutex_unlock(db_mutex);
309 return MS_MEDIA_ERR_DB_CONNECT_FAIL;
313 MSC_DBG_INFO("connect Media DB");
315 g_mutex_unlock(db_mutex);
317 return MS_MEDIA_ERR_NONE;
321 msc_disconnect_db(void ***handle)
325 char * err_msg = NULL;
327 for (lib_index = 0; lib_index < lib_num; lib_index++) {
328 ret = ((DISCONNECT)func_array[lib_index][eDISCONNECT])((*handle)[lib_index], &err_msg); /*dlopen*/
330 MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
331 MS_SAFE_FREE(err_msg);
332 return MS_MEDIA_ERR_DB_DISCONNECT_FAIL;
336 MS_SAFE_FREE(*handle);
338 MSC_DBG_INFO("Disconnect Media DB");
340 return MS_MEDIA_ERR_NONE;
344 msc_validate_item(void **handle, char *path)
347 int res = MS_MEDIA_ERR_NONE;
349 char *err_msg = NULL;
350 char mimetype[255] = {0};
351 ms_storage_type_t storage_type;
353 ret = _msc_get_mime(path, mimetype);
354 if (ret != MS_MEDIA_ERR_NONE) {
355 MSC_DBG_ERR("err : _msc_get_mime [%d]", ret);
358 storage_type = msc_get_storage_type_by_full(path);
360 for (lib_index = 0; lib_index < lib_num; lib_index++) {
361 if (!_msc_check_category(path, mimetype, lib_index)) {
362 /*check exist in Media DB, If file is not exist, insert data in DB. */
363 ret = ((CHECK_ITEM_EXIST)func_array[lib_index][eEXIST])(handle[lib_index], path, storage_type, &err_msg); /*dlopen*/
365 MSC_DBG_ERR("not exist in %d. insert data", lib_index);
366 MS_SAFE_FREE(err_msg);
368 ret = ((INSERT_ITEM)func_array[lib_index][eINSERT_BATCH])(handle[lib_index], path, storage_type, mimetype, &err_msg); /*dlopen*/
370 MSC_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path);
371 MSC_DBG_ERR("[%s] %s", mimetype, path);
372 MS_SAFE_FREE(err_msg);
373 res = MS_MEDIA_ERR_DB_INSERT_FAIL;
376 /*if meta data of file exist, change valid field to "1" */
377 ret = ((SET_ITEM_VALIDITY)func_array[lib_index][eSET_VALIDITY])(handle[lib_index], path, true, mimetype, true, &err_msg); /*dlopen*/
379 MSC_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path);
380 MSC_DBG_ERR("[%s] %s", mimetype, path);
381 MS_SAFE_FREE(err_msg);
382 res = MS_MEDIA_ERR_DB_UPDATE_FAIL;
386 MSC_DBG_ERR("check category failed");
387 MSC_DBG_ERR("[%s] %s", mimetype, path);
391 if (msc_is_drm_file(path)) {
392 ret = msc_drm_register(path);
399 msc_invalidate_all_items(void **handle, ms_storage_type_t store_type)
402 int res = MS_MEDIA_ERR_NONE;
404 char *err_msg = NULL;
406 for (lib_index = 0; lib_index < lib_num; lib_index++) {
407 ret = ((SET_ALL_STORAGE_ITEMS_VALIDITY)func_array[lib_index][eSET_ALL_VALIDITY])(handle[lib_index], store_type, false, &err_msg); /*dlopen*/
409 MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
410 MS_SAFE_FREE(err_msg);
411 res = MS_MEDIA_ERR_DB_UPDATE_FAIL;
419 msc_insert_item_batch(void **handle, const char *path)
422 int res = MS_MEDIA_ERR_NONE;
424 char mimetype[255] = {0};
425 char *err_msg = NULL;
426 ms_storage_type_t storage_type;
428 ret = _msc_get_mime(path, mimetype);
429 if (ret != MS_MEDIA_ERR_NONE) {
430 MSC_DBG_ERR("err : _msc_get_mime [%d]", ret);
433 storage_type = msc_get_storage_type_by_full(path);
435 for (lib_index = 0; lib_index < lib_num; lib_index++) {
436 if (!_msc_check_category(path, mimetype, lib_index)) {
437 ret = ((INSERT_ITEM)func_array[lib_index][eINSERT_BATCH])(handle[lib_index], path, storage_type, mimetype, &err_msg); /*dlopen*/
439 MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
440 MSC_DBG_ERR("[%s] %s", mimetype, path);
441 MS_SAFE_FREE(err_msg);
442 res = MS_MEDIA_ERR_DB_INSERT_FAIL;
445 MSC_DBG_ERR("check category failed");
446 MSC_DBG_ERR("[%s] %s", mimetype, path);
450 if (msc_is_drm_file(path)) {
451 ret = msc_drm_register(path);
459 msc_delete_all_items(void **handle, ms_storage_type_t store_type)
463 char *err_msg = NULL;
465 /* To reset media db when differnet mmc is inserted. */
466 for (lib_index = 0; lib_index < lib_num; lib_index++) {
467 ret = ((DELETE_ALL_ITEMS_IN_STORAGE)func_array[lib_index][eDELETE_ALL])(handle[lib_index], store_type, &err_msg); /*dlopen*/
469 MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
470 MS_SAFE_FREE(err_msg);
479 msc_delete_invalid_items(void **handle, ms_storage_type_t store_type)
483 char *err_msg = NULL;
485 for (lib_index = 0; lib_index < lib_num; lib_index++) {
486 ret = ((DELETE_ALL_INVALID_ITMES_IN_STORAGE)func_array[lib_index][eDELETE_INVALID_ITEMS])(handle[lib_index], store_type, &err_msg); /*dlopen*/
488 MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
489 MS_SAFE_FREE(err_msg);
498 msc_set_folder_validity(void **handle, const char *path, int validity, int recursive)
502 char *err_msg = NULL;
504 for (lib_index = 0; lib_index < lib_num; lib_index++) {
505 ret = ((SET_FOLDER_ITEM_VALIDITY)func_array[lib_index][eSET_FOLDER_VALIDITY])(handle[lib_index], path, validity, recursive,&err_msg); /*dlopen*/
507 MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
508 MS_SAFE_FREE(err_msg);
509 return MS_MEDIA_ERR_DB_UPDATE_FAIL;
513 return MS_MEDIA_ERR_NONE;
517 msc_delete_invalid_items_in_folder(void **handle, const char*path)
521 char *err_msg = NULL;
523 for (lib_index = 0; lib_index < lib_num; lib_index++) {
524 ret = ((DELETE_ALL_INVALID_ITEMS_IN_FOLDER)func_array[lib_index][eDELETE_FOLDER])(handle[lib_index], path, &err_msg); /*dlopen*/
526 MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
527 MS_SAFE_FREE(err_msg);
528 return MS_MEDIA_ERR_DB_UPDATE_FAIL;
532 return MS_MEDIA_ERR_NONE;
535 /****************************************************************************************************
537 *****************************************************************************************************/
540 msc_register_start(void **handle)
544 char *err_msg = NULL;
546 for (lib_index = 0; lib_index < lib_num; lib_index++) {
547 ret = ((INSERT_ITEM_BEGIN)func_array[lib_index][eINSERT_BEGIN])(handle[lib_index], MSC_REGISTER_COUNT, &err_msg);/*dlopen*/
549 MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
550 MS_SAFE_FREE(err_msg);
556 msc_register_end(void **handle)
560 char *err_msg = NULL;
562 for (lib_index = 0; lib_index < lib_num; lib_index++) {
563 ret = ((INSERT_ITEM_END)func_array[lib_index][eINSERT_END])(handle[lib_index], &err_msg);/*dlopen*/
565 MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
566 MS_SAFE_FREE(err_msg);
570 for (lib_index = 0; lib_index < lib_num; lib_index++) {
571 ret = ((UPDATE_END)func_array[lib_index][eUPDATE_END])();/*dlopen*/
573 MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
574 MS_SAFE_FREE(err_msg);
580 msc_validate_start(void **handle)
584 char *err_msg = NULL;
586 for (lib_index = 0; lib_index < lib_num; lib_index++) {
587 ret = ((SET_ITEM_VALIDITY_BEGIN)func_array[lib_index][eSET_VALIDITY_BEGIN])(handle[lib_index], MSC_VALID_COUNT, &err_msg);/*dlopen*/
589 MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
590 MS_SAFE_FREE(err_msg);
596 msc_validate_end(void **handle)
600 char *err_msg = NULL;
602 for (lib_index = 0; lib_index < lib_num; lib_index++) {
603 ret = ((SET_ITEM_VALIDITY_END)func_array[lib_index][eSET_VALIDITY_END])(handle[lib_index], &err_msg);/*dlopen*/
605 MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
606 MS_SAFE_FREE(err_msg);