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.
24 #include "media-util.h"
26 #include "media-common-utils.h"
27 #include "media-common-db-svc.h"
28 #include "media-common-dbg.h"
29 #include <tzplatform_config.h>
31 #define CONFIG_PATH PATH_LIBDIR"/libmedia-content-plugin.so"
33 static GMutex db_mutex;
34 static void **func_array;
35 static void *func_handle = NULL;
36 static int scan_other_type = -1;
38 typedef int (*CLEANUP_DB)(sqlite3*, uid_t);
39 typedef int (*INSERT_ITEM_BEGIN)(bool, int);
40 typedef int (*INSERT_ITEM_END)(uid_t);
42 typedef int (*SEND_DIR_UPDATE_NOTI)(const char *, const char *, int, int);
43 typedef int (*INSERT_ITEM)(sqlite3 *, const char *, const char *, int, uid_t);
44 typedef int (*SET_ITEM_VALIDITY)(const char *, const char *, int, int, uid_t);
45 typedef int (*DELETE_ALL_INVALID_ITEMS_IN_FOLDER)(sqlite3 *, const char *, const char *, bool, uid_t);
46 typedef int (*UPDATE_FOLDER_TIME)(sqlite3 *, const char *, const char *, uid_t);
47 typedef int (*GET_STORAGE_ID)(sqlite3 *, const char *, char *, uid_t uid);
48 typedef int (*SET_STORAGE_SCAN_STATUS)(sqlite3 *, const char *, int, uid_t);
49 typedef int (*INSERT_FOLDER)(sqlite3 *, const char *, const char *, int, uid_t);
50 typedef int (*SET_FOLDER_VALIDITY)(sqlite3 *, const char *, const char *, int, bool, uid_t);
51 typedef int (*INSERT_ITEM_SCAN)(sqlite3 *, const char *, const char *, int, uid_t);
52 typedef int (*GET_FOLDER_SCAN_STATUS)(sqlite3 *, const char *, const char *, int *);
53 typedef int (*SET_FOLDER_SCAN_STATUS)(sqlite3 *, const char *, const char *, int, uid_t);
54 typedef int (*CHECK_FOLDER_MODIFIED)(sqlite3 *, const char *, const char *, bool *);
55 typedef int (*GET_NULL_SCAN_FOLDER_LIST)(sqlite3 *, const char *, const char *, GPtrArray **);
56 typedef int (*CHANGE_VALIDITY_ITEM_BATCH)(sqlite3 *, const char *, const char *, int, int, uid_t);
58 typedef int (*CHECK_DB)(sqlite3*, uid_t);
59 typedef int (*GET_UUID)(char **);
60 typedef int (*CHECK_STORAGE)(sqlite3 *, const char *, char **, int *, uid_t);
61 typedef int (*INSERT_STORAGE)(sqlite3 *, const char *, int, const char *, uid_t);
62 typedef int (*UPDATE_STORAGE)(sqlite3 *, const char *, const char *, uid_t);
63 typedef int (*SET_STORAGE_VALIDITY)(sqlite3 *, const char *, int, uid_t uid);
64 typedef int (*SET_ALL_STORAGE_VALIDITY)(sqlite3 *, int, uid_t);
66 typedef int (*UPDATE_ITEM_META)(sqlite3 *, uid_t);
68 typedef int (*DELETE_INVALID_FOLDER_BY_PATH)(sqlite3 *, const char *, const char *, uid_t);
69 typedef int (*CHECK_FOLDER_EXIST)(sqlite3*, const char*, const char*);
70 typedef int (*GET_AND_EXTRACT_MEDIA)(sqlite3*, const char*, int, const char *);
71 typedef int (*GET_MEDIA_TYPE)(const char *, int *);
72 typedef int (*REFRESH_ITEM)(sqlite3 *, const char *, const char *, uid_t);
82 eSEND_DIR_UPDATE_NOTI,
85 eSET_STORAGE_SCAN_STATUS,
88 eGET_FOLDER_SCAN_STATUS,
89 eSET_FOLDER_SCAN_STATUS,
90 eCHECK_FOLDER_MODIFIED,
91 eGET_NULL_SCAN_FOLDER_LIST,
92 eCHANGE_VALIDITY_ITEM_BATCH,
98 eSET_STORAGE_VALIDITY,
99 eSET_ALL_STORAGE_VALIDITY,
101 eDELETE_INVALID_FOLDER_BY_PATH,
104 eGET_AND_EXTRACT_MEDIA,
109 int ms_load_functions(void)
112 char func_list[eFUNC_MAX][40] = {
119 "delete_all_invalid_items_in_folder",
120 "send_dir_update_noti",
121 "update_folder_time",
123 "set_storage_scan_status",
125 "set_folder_validity",
126 "get_folder_scan_status",
127 "set_folder_scan_status",
128 "check_folder_modified",
129 "get_null_scan_folder_list",
130 "change_validity_item_batch",
136 "set_storage_validity",
137 "set_all_storage_validity",
139 "delete_invalid_folder_by_path",
140 "check_folder_exist",
142 "get_and_extract_media",
146 func_handle = dlopen(CONFIG_PATH, RTLD_LAZY);
147 MS_DBG_RETVM_IF(!func_handle, MS_MEDIA_ERR_INTERNAL, "%s", dlerror());
151 func_array = g_new0(gpointer, eFUNC_MAX);
153 for (func_index = 0; func_index < eFUNC_MAX ; func_index++) {
154 func_array[func_index] = dlsym(func_handle, func_list[func_index]);
155 if (func_array[func_index] == NULL) {
156 MS_DBG_ERR("dlsym failed[%s]", func_list[func_index]);
158 dlclose(func_handle);
160 return MS_MEDIA_ERR_INTERNAL;
164 return MS_MEDIA_ERR_NONE;
167 void ms_unload_functions(void)
169 dlclose(func_handle);
173 int ms_connect_db(sqlite3 **handle, uid_t uid)
175 int ret = MS_MEDIA_ERR_NONE;
177 g_mutex_lock(&db_mutex);
179 ret = media_db_connect(handle, uid, false);
181 g_mutex_unlock(&db_mutex);
186 void ms_disconnect_db(sqlite3 *handle)
188 media_db_disconnect(handle);
191 int ms_cleanup_db(sqlite3 *handle, uid_t uid)
193 int ret = MS_MEDIA_ERR_NONE;
195 ret = ((CLEANUP_DB)func_array[eCLEANUP_DB])(handle, uid);
196 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CLEANUP_DB failed [%d]", ret);
198 MS_DBG_INFO("Cleanup Media DB");
203 static int __ms_check_item_exist(sqlite3 *handle, const char *storage_id, const char *path, bool *modified)
205 int ret = MS_MEDIA_ERR_NONE;
207 time_t modified_time = 0;
208 unsigned long long file_size = 0;
210 sqlite3_stmt *sql_stmt = NULL;
214 MS_DBG_RETVM_IF(!MS_STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
216 #ifdef _USE_TVPD_MODE
217 MS_DBG_RETVM_IF(!MS_STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
219 sql = sqlite3_mprintf("SELECT media_modified_time, media_size, media_type, media_timeline,folder_id FROM '%q' WHERE media_path='%q'", storage_id, path);
221 sql = sqlite3_mprintf("SELECT media_modified_time, media_size FROM media WHERE media_path='%q'", path);
224 ret = media_db_get_result(handle, sql, &sql_stmt);
225 if (ret != MS_MEDIA_ERR_NONE) {
226 MS_DBG_ERR("Error when media_db_get_result. err = [%d]", ret);
230 if (sqlite3_step(sql_stmt) != SQLITE_ROW) {
231 ret = MS_MEDIA_ERR_DB_NO_RECORD;
233 modified_time = (int)sqlite3_column_int(sql_stmt, 0);
234 file_size = (unsigned long long)sqlite3_column_int64(sql_stmt, 1);
235 #ifdef _USE_TVPD_MODE
236 int media_type = (int)sqlite3_column_int(sql_stmt, 2);
237 if (media_type == MS_MEDIA_TYPE_PVR)
238 modified_time = (int)sqlite3_column_int(sql_stmt, 3);
240 memset(&st, 0, sizeof(struct stat));
241 if (stat(path, &st) == 0) {
242 if ((st.st_mtime != modified_time) || (st.st_size != file_size)) {
246 #ifdef _USE_TVPD_MODE
247 char *folder_id_media = NULL;
248 char *folder_id_folder = NULL;
250 folder_id_media = (char *)sqlite3_column_text(sql_stmt, 4);
251 char *folder_path = NULL;
252 folder_path = g_path_get_dirname(path);
253 ret = ms_get_folder_id(handle, storage_id, folder_path, &folder_id_folder);
255 if(ret == MS_MEDIA_ERR_NONE) {
256 if (g_strcmp0(folder_id_media, folder_id_folder) == 0)
263 g_free(folder_id_folder);
269 MS_SQLITE3_FINALIZE(sql_stmt);
274 int ms_validate_item(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
276 int ret = MS_MEDIA_ERR_NONE;
277 bool modified = false;
279 ret = __ms_check_item_exist(handle, storage_id, path, &modified);
280 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
281 ret = ms_insert_item_batch(handle, storage_id, path, uid);
282 } else if (ret == MS_MEDIA_ERR_NONE) {
284 ret = ((SET_ITEM_VALIDITY)func_array[eSET_VALIDITY])(storage_id, path, true, true, uid);
285 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_ITEM_VALIDITY failed [%d]", ret);
287 ret = ((REFRESH_ITEM)func_array[eREFRESH_ITEM])(handle, storage_id, path, uid);
288 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "REFRESH_ITEM failed [%d]", ret);
291 MS_DBG_ERR("check_item_exist failed [%d]", ret);
297 #ifdef _USE_TVPD_MODE
298 int ms_scan_validate_item(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
300 int ret = MS_MEDIA_ERR_NONE;
301 bool modified = false;
303 ret = __ms_check_item_exist(handle, storage_id, path, &modified);
304 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
305 ret = ms_scan_item_batch(handle, storage_id, path, uid);
306 } else if (ret == MS_MEDIA_ERR_NONE) {
308 ret = ((SET_ITEM_VALIDITY)func_array[eSET_VALIDITY])(storage_id, path, true, true, uid);
309 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_ITEM_VALIDITY failed [%d]", ret);
311 ret = ((REFRESH_ITEM)func_array[eREFRESH_ITEM])(handle, storage_id, path, uid);
312 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "REFRESH_ITEM failed [%d]", ret);
315 MS_DBG_ERR("check_item_exist failed [%d]", ret);
322 int ms_validity_change_all_items(sqlite3 *handle, const char *storage_id, bool validity , uid_t uid)
324 int ret = MS_MEDIA_ERR_NONE;
326 #ifdef _USE_TVPD_MODE
327 MS_DBG_RETVM_IF(!MS_STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
329 char *sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d;", storage_id, validity);
331 char *sql = sqlite3_mprintf("UPDATE media SET validity=%d WHERE storage_uuid=%Q;", validity, storage_id);
334 ret = media_db_update_db_direct(sql, uid);
335 MS_SQLITE3_SAFE_FREE(sql);
340 int ms_insert_item_batch(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
342 int ret = MS_MEDIA_ERR_NONE;
343 ms_user_storage_type_e storage_type = -1;
347 ret = ms_user_get_storage_type(uid, path, &storage_type);
348 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get storage type");
350 ret = ((INSERT_ITEM)func_array[eINSERT_BATCH])(handle, storage_id, path, storage_type, uid);
351 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "INSERT_ITEM failed [%d]", ret);
356 int ms_scan_item_batch(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
358 int ret = MS_MEDIA_ERR_NONE;
359 ms_user_storage_type_e storage_type;
363 ret = ms_user_get_storage_type(uid, path, &storage_type);
364 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get storage type");
366 ret = ((INSERT_ITEM_SCAN)func_array[eINSERT_SCAN])(handle, storage_id, path, storage_type, uid);
367 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "INSERT_ITEM_SCAN failed [%d]", ret);
372 int ms_get_and_extract_media(sqlite3 *handle, const char *storage_id, int scan_type, const char *path)
374 int ret = MS_MEDIA_ERR_NONE;
376 ret = ((GET_AND_EXTRACT_MEDIA)func_array[eGET_AND_EXTRACT_MEDIA])(handle, storage_id, scan_type, path);
377 if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD)
378 MS_DBG_ERR("GET_AND_EXTRACT_MEDIA failed [%d]", ret);
383 int ms_delete_invalid_items(sqlite3 *handle, const char *storage_id, uid_t uid)
385 int ret = MS_MEDIA_ERR_NONE;
386 sqlite3_stmt *sql_stmt = NULL;
387 GPtrArray *thumb_list = NULL;
391 thumb_list = g_ptr_array_new_with_free_func(g_free);
392 #ifdef _USE_TVPD_MODE
393 MS_DBG_RETVM_IF(!MS_STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
395 char *sql = sqlite3_mprintf("SELECT media_thumbnail_path FROM '%q' WHERE validity IN (%d, %d) AND media_thumbnail_path IS NOT NULL", storage_id, MS_INVALID, MS_SCANNING);
397 char *sql = sqlite3_mprintf("SELECT media_thumbnail_path FROM media WHERE validity IN (%d, %d) AND media_thumbnail_path IS NOT NULL", MS_INVALID, MS_SCANNING);
399 MS_DBG_SLOG("[SQL query] : %s", sql);
401 ret = media_db_get_result(handle, sql, &sql_stmt);
402 if (ret != MS_MEDIA_ERR_NONE) {
403 MS_DBG_ERR("Query failed. err[%d]", ret);
404 g_ptr_array_free(thumb_list, TRUE);
408 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
409 path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
410 g_ptr_array_add(thumb_list, path);
413 MS_SQLITE3_FINALIZE(sql_stmt);
415 #ifdef _USE_TVPD_MODE
416 sql = sqlite3_mprintf("DELETE FROM '%q' WHERE validity IN (%d, %d)", storage_id, MS_INVALID, MS_SCANNING);
418 sql = sqlite3_mprintf("DELETE FROM media WHERE validity IN (%d, %d)", MS_INVALID, MS_SCANNING);
421 ret = media_db_update_db_direct(sql, uid);
422 MS_SQLITE3_SAFE_FREE(sql);
423 if (ret != MS_MEDIA_ERR_NONE) {
424 g_ptr_array_free(thumb_list, TRUE);
428 for (i = 0; i < thumb_list->len; i++) {
429 path = g_ptr_array_index(thumb_list, i);
431 if (remove(path) != 0)
432 MS_DBG_ERR("fail to remove thumbnail file.");
435 g_ptr_array_free(thumb_list, TRUE);
437 return MS_MEDIA_ERR_NONE;
440 int ms_set_folder_item_validity(sqlite3 *handle, const char *storage_id, const char *path, int validity, int recursive, uid_t uid)
442 int ret = MS_MEDIA_ERR_NONE;
445 MS_DBG_RETVM_IF(!MS_STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
446 MS_DBG_RETVM_IF(!MS_STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
448 #ifdef _USE_TVPD_MODE
450 sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d WHERE media_path LIKE '%q/%%';", storage_id, validity, path);
452 sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d WHERE folder_id IN (SELECT folder_id FROM folder WHERE folder_path='%q' AND storage_uuid='%q');", storage_id, validity, path, storage_id);
455 sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE media_path LIKE '%q/%%';", INTERNAL_STORAGE_ID, validity, path);
457 sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE folder_id=(SELECT folder_id FROM folder WHERE folder_path=%Q);", INTERNAL_STORAGE_ID, validity, path, storage_id);
460 ret = media_db_update_db_direct(sql, uid);
461 MS_SQLITE3_SAFE_FREE(sql);
466 int ms_delete_invalid_items_in_folder(sqlite3 *handle, const char *storage_id, const char *path, bool is_recursive, uid_t uid)
468 int ret = MS_MEDIA_ERR_NONE;
470 ret = ((DELETE_ALL_INVALID_ITEMS_IN_FOLDER)func_array[eDELETE_FOLDER])(handle, storage_id, path, is_recursive, uid);
471 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "DELETE_ALL_INVALID_ITEMS_IN_FOLDER failed [%d]", ret);
476 int ms_send_dir_update_noti(const char*path, const char*folder_id, ms_noti_type_e noti_type, int pid)
478 int ret = MS_MEDIA_ERR_NONE;
480 ret = ((SEND_DIR_UPDATE_NOTI)func_array[eSEND_DIR_UPDATE_NOTI])(path, folder_id, (int)noti_type, pid);
481 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SEND_DIR_UPDATE_NOTI failed [%d]", ret);
486 int ms_update_folder_time(sqlite3 *handle, const char *storage_id, char *folder_path, uid_t uid)
488 int ret = MS_MEDIA_ERR_NONE;
490 ret = ((UPDATE_FOLDER_TIME)func_array[eUPDATE_FOLDER_TIME])(handle, storage_id, folder_path, uid);
491 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "UPDATE_FOLDER_TIME failed [%d]", ret);
496 int ms_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
498 int ret = MS_MEDIA_ERR_NONE;
502 ret = ((GET_STORAGE_ID)func_array[eGET_STORAGE_ID])(handle, path, storage_id, uid);
503 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_STORAGE_ID failed [%d]", ret);
505 MS_DBG("storage_id [%s]", storage_id);
510 int ms_set_storage_scan_status(sqlite3 *handle, char *storage_id, media_scan_status_e scan_status, uid_t uid)
512 int ret = MS_MEDIA_ERR_NONE;
513 int status = scan_status;
517 ret = ((SET_STORAGE_SCAN_STATUS)func_array[eSET_STORAGE_SCAN_STATUS])(handle, storage_id, status, uid);
518 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_STORAGE_SCAN_STATUS failed [%d]", ret);
520 MS_DBG("storage_id [%s], scan_status [%d]", storage_id, scan_status);
525 int ms_insert_folder(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
527 int ret = MS_MEDIA_ERR_NONE;
528 ms_user_storage_type_e storage_type;
530 ret = ms_user_get_storage_type(uid, path, &storage_type);
531 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get storage type");
533 ret = ((INSERT_FOLDER)func_array[eINSERT_FOLDER])(handle, storage_id, path, storage_type, uid);
534 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "INSERT_FOLDER failed [%d]", ret);
539 int ms_delete_invalid_folder(const char *storage_id, uid_t uid)
541 int ret = MS_MEDIA_ERR_NONE;
544 #ifdef _USE_TVPD_MODE
545 MS_DBG_RETVM_IF(!MS_STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
547 sql = sqlite3_mprintf("DELETE FROM folder WHERE storage_uuid='%q' AND validity IN (%d, %d)", storage_id, MS_INVALID, MS_SCANNING);
549 sql = sqlite3_mprintf("DELETE FROM folder WHERE validity IN (%d, %d)", MS_INVALID, MS_SCANNING);
551 ret = media_db_update_db_direct(sql, uid);
553 MS_SQLITE3_SAFE_FREE(sql);
558 int ms_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *path, int validity, bool is_recursive, uid_t uid)
560 int ret = MS_MEDIA_ERR_NONE;
562 ret = ((SET_FOLDER_VALIDITY)func_array[eSET_FOLDER_VALIDITY])(handle, storage_id, path, validity, is_recursive, uid);
563 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_FOLDER_VALIDITY failed [%d]", ret);
568 int ms_check_db_upgrade(sqlite3 *handle, uid_t uid)
570 int ret = MS_MEDIA_ERR_NONE;
574 ret = ((CHECK_DB)func_array[eCHECK_DB])(handle, uid);
575 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CHECK_DB failed [%d]", ret);
582 int ms_genarate_uuid(char **uuid)
584 int ret = MS_MEDIA_ERR_NONE;
586 ret = ((GET_UUID)func_array[eGET_UUID])(uuid);
587 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_UUID failed [%d]", ret);
592 int ms_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid)
594 int ret = MS_MEDIA_ERR_NONE;
598 ret = ((CHECK_STORAGE)func_array[eCHECK_STORAGE])(handle, storage_id, storage_path, validity, uid);
599 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CHECK_STORAGE failed [%d]", ret);
606 int ms_insert_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
608 int ret = MS_MEDIA_ERR_NONE;
609 ms_user_storage_type_e storage_type;
613 ret = ms_user_get_storage_type(uid, storage_path, &storage_type);
614 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get storage type");
616 ret = ((INSERT_STORAGE)func_array[eINSERT_STORAGE])(handle, storage_id, storage_type, storage_path, uid);
617 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "INSERT_STORAGE failed [%d]", ret);
624 int ms_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
626 int ret = MS_MEDIA_ERR_NONE;
630 ret = ((UPDATE_STORAGE)func_array[eUPDATE_STORAGE])(handle, storage_id, storage_path, uid);
631 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "UPDATE_STORAGE failed [%d]", ret);
638 int ms_set_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid)
640 int ret = MS_MEDIA_ERR_NONE;
642 ret = ((SET_STORAGE_VALIDITY)func_array[eSET_STORAGE_VALIDITY])(handle, storage_id, validity, uid);
643 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_STORAGE_VALIDITY failed [%d]", ret);
648 int ms_set_all_storage_validity(sqlite3 *handle, int validity, uid_t uid)
650 int ret = MS_MEDIA_ERR_NONE;
652 ret = ((SET_ALL_STORAGE_VALIDITY)func_array[eSET_ALL_STORAGE_VALIDITY])(handle, validity, uid);
653 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_ALL_STORAGE_VALIDITY failed [%d]", ret);
658 int ms_update_meta_batch(sqlite3 *handle, uid_t uid)
660 int ret = MS_MEDIA_ERR_NONE;
664 ret = ((UPDATE_ITEM_META)func_array[eUPDATE_ITEM_META])(handle, uid);
665 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "UPDATE_ITEM_META failed [%d]", ret);
670 int ms_delete_invalid_folder_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path, uid_t uid)
672 int ret = MS_MEDIA_ERR_NONE;
674 ret = ((DELETE_INVALID_FOLDER_BY_PATH)func_array[eDELETE_INVALID_FOLDER_BY_PATH])(handle, storage_id, folder_path, uid);
675 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "DELETE_INVALID_FOLDER_BY_PATH failed [%d]", ret);
680 int ms_check_folder_exist(sqlite3 *handle, const char *storage_id, const char *folder_path)
682 int ret = MS_MEDIA_ERR_NONE;
684 ret = ((CHECK_FOLDER_EXIST)func_array[eCHECK_FOLDER_EXIST])(handle, storage_id, folder_path);
685 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CHECK_FOLDER_EXIST failed [%d]", ret);
690 int ms_get_folder_id(sqlite3 *handle, const char *storage_id, const char *path, char **folder_id)
692 int ret = MS_MEDIA_ERR_NONE;
693 sqlite3_stmt *sql_stmt = NULL;
696 MS_DBG_RETVM_IF(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
697 #ifdef _USE_TVPD_MODE
698 MS_DBG_RETVM_IF(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
700 sql = sqlite3_mprintf("SELECT folder_id FROM folder WHERE (storage_uuid='%q' AND folder_path='%q')", storage_id, path);
702 sql = sqlite3_mprintf("SELECT folder_id FROM folder WHERE folder_path=%Q", path);
704 ret = media_db_get_result(handle, sql, &sql_stmt);
705 MS_DBG_RETV_IF(ret != MS_MEDIA_ERR_NONE, ret);
707 if (sqlite3_step(sql_stmt) == SQLITE_ROW) {
708 *folder_id = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
709 MS_DBG("folder_id [%s]", *folder_id);
712 ret = MS_MEDIA_ERR_DB_NO_RECORD;
715 MS_SQLITE3_FINALIZE(sql_stmt);
720 void ms_register_start(bool noti_status, int pid)
722 int ret = MS_MEDIA_ERR_NONE;
724 ret = ((INSERT_ITEM_BEGIN)func_array[eINSERT_BEGIN])(noti_status, pid);
725 if (ret != MS_MEDIA_ERR_NONE)
726 MS_DBG_ERR("INSERT_ITEM_BEGIN failed [%d]", ret);
729 void ms_register_end(uid_t uid)
731 int ret = MS_MEDIA_ERR_NONE;
733 ret = ((INSERT_ITEM_END)func_array[eINSERT_END])(uid);
734 if (ret != MS_MEDIA_ERR_NONE)
735 MS_DBG_ERR("INSERT_ITEM_END failed [%d]", ret);
738 int ms_get_folder_scan_status(sqlite3 *handle, const char *storage_id, const char *path, int *scan_status)
740 int ret = MS_MEDIA_ERR_NONE;
743 ret = ((GET_FOLDER_SCAN_STATUS)func_array[eGET_FOLDER_SCAN_STATUS])(handle, storage_id, path, &status);
744 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_FOLDER_SCAN_STATUS failed [%d]", ret);
745 *scan_status = status;
747 MS_DBG_SLOG("OK path = [%s], scan_status = [%d]", path, *scan_status);
752 int ms_set_folder_scan_status(sqlite3 *handle, const char *storage_id, const char *path, int scan_status, uid_t uid)
754 int ret = MS_MEDIA_ERR_NONE;
755 int status = scan_status;
757 ret = ((SET_FOLDER_SCAN_STATUS)func_array[eSET_FOLDER_SCAN_STATUS])(handle, storage_id, path, status, uid);
758 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_FOLDER_SCAN_STATUS failed [%d]", ret);
760 MS_DBG_SLOG("OK path = [%s], scan_status = [%d]", path, scan_status);
765 int ms_check_folder_modified(sqlite3 *handle, const char *path, const char *storage_id, bool *modified)
767 int ret = MS_MEDIA_ERR_NONE;
768 MS_DBG("path = [%s], storage_id = [%s]", path, storage_id);
770 ret = ((CHECK_FOLDER_MODIFIED)func_array[eCHECK_FOLDER_MODIFIED])(handle, path, storage_id, modified);
771 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CHECK_FOLDER_MODIFIED failed [%d]", ret);
776 int ms_get_null_scan_folder_list(sqlite3 *handle, const char *stroage_id, const char *path, GPtrArray **dir_array)
778 int ret = MS_MEDIA_ERR_NONE;
780 ret = ((GET_NULL_SCAN_FOLDER_LIST)func_array[eGET_NULL_SCAN_FOLDER_LIST])(handle, stroage_id, path, dir_array);
781 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_NULL_SCAN_FOLDER_LIST failed [%d]", ret);
786 int ms_change_validity_item_batch(sqlite3 *handle, const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
788 int ret = MS_MEDIA_ERR_NONE;
790 ret = ((CHANGE_VALIDITY_ITEM_BATCH)func_array[eCHANGE_VALIDITY_ITEM_BATCH])(handle, storage_id, path, des_validity, src_validity, uid);
791 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CHANGE_VALIDITY_ITEM_BATCH failed [%d]", ret);
796 int ms_get_media_type(const char *path, int *media_type)
798 int ret = MS_MEDIA_ERR_NONE;
800 ret = ((GET_MEDIA_TYPE)func_array[eGET_MEDIA_TYPE])(path, media_type);
801 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_MEDIA_TYPE failed [%d]", ret);
806 bool ms_check_support_media_type(const char *path)
808 int ret = SYSTEM_INFO_ERROR_NONE;
810 bool is_supported = false;
812 MS_DBG_RETVM_IF(!MS_STRING_VALID(path), false, "path is empty");
814 if (scan_other_type == -1) {
815 ret = system_info_get_platform_bool("http://tizen.org/feature/content.scanning.others", &is_supported);
816 if (ret != SYSTEM_INFO_ERROR_NONE) {
817 MS_DBG_ERR("SYSTEM_INFO_ERROR: content.scanning.others [%d]", ret);
821 scan_other_type = is_supported;
824 if (!scan_other_type) {
825 ret = ms_get_media_type(path, &media_type);
826 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, false, "Failed to get media type");
828 if (media_type == MS_MEDIA_TYPE_OTHER)