Remove query_do_update_list()
[platform/core/multimedia/media-server.git] / src / common / media-common-db-svc.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 #include <dlfcn.h>
22 #include <sys/stat.h>
23
24 #include "media-util.h"
25
26 #include "media-common-utils.h"
27 #include "media-common-db-svc.h"
28 #include "media-common-dbg.h"
29 #include <tzplatform_config.h>
30
31 #define CONFIG_PATH PATH_LIBDIR"/libmedia-content-plugin.so"
32
33 static GMutex db_mutex;
34 static void **func_array;
35 static void *func_handle = NULL; /*dlopen handle */
36 static int scan_other_type = -1;
37
38 enum func_list {
39         eCLEANUP_DB,
40         eINSERT_BEGIN,
41         eINSERT_END,
42         eINSERT_BATCH,
43         eINSERT_SCAN,
44         eSET_VALIDITY,
45         eDELETE_FOLDER,
46         eSEND_DIR_UPDATE_NOTI,
47         eUPDATE_FOLDER_TIME,
48         eGET_STORAGE_ID,
49         eSET_STORAGE_SCAN_STATUS,
50         eINSERT_FOLDER,
51         eSET_FOLDER_VALIDITY,
52         eGET_FOLDER_SCAN_STATUS,
53         eSET_FOLDER_SCAN_STATUS,
54         eCHECK_FOLDER_MODIFIED,
55         eGET_NULL_SCAN_FOLDER_LIST,
56         eCHANGE_VALIDITY_ITEM_BATCH,
57         eCHECK_DB,
58         eGET_UUID,
59         eCHECK_STORAGE,
60         eINSERT_STORAGE,
61         eUPDATE_STORAGE,
62         eSET_STORAGE_VALIDITY,
63         eSET_ALL_STORAGE_VALIDITY,
64         eUPDATE_ITEM_META,
65         eDELETE_INVALID_FOLDER_BY_PATH,
66         eCHECK_FOLDER_EXIST,
67         eGET_MEDIA_TYPE,
68         eGET_EXTRACT_LIST,
69         eUPDATE_ONE_EXTRACT_ITEM,
70         eREFRESH_ITEM,
71         eFUNC_MAX
72 };
73
74 int ms_load_functions(void)
75 {
76         int func_index;
77         char func_list[eFUNC_MAX][40] = {
78                 "cleanup_db",
79                 "insert_item_begin",
80                 "insert_item_end",
81                 "insert_item",
82                 "insert_item_scan",
83                 "set_item_validity",
84                 "delete_all_invalid_items_in_folder",
85                 "send_dir_update_noti",
86                 "update_folder_time",
87                 "get_storage_id",
88                 "set_storage_scan_status",
89                 "insert_folder",
90                 "set_folder_validity",
91                 "get_folder_scan_status",
92                 "set_folder_scan_status",
93                 "check_folder_modified",
94                 "get_null_scan_folder_list",
95                 "change_validity_item_batch",
96                 "check_db",
97                 "get_uuid",
98                 "check_storage",
99                 "insert_storage",
100                 "update_storage",
101                 "set_storage_validity",
102                 "set_all_storage_validity",
103                 "update_item_meta",
104                 "delete_invalid_folder_by_path",
105                 "check_folder_exist",
106                 "get_media_type",
107                 "get_extract_list",
108                 "update_one_extract_item",
109                 "refresh_item",
110                 };
111
112         /*get handle*/
113         func_handle = dlopen(CONFIG_PATH, RTLD_LAZY);
114         MS_DBG_RETVM_IF(!func_handle, MS_MEDIA_ERR_INTERNAL, "%s", dlerror());
115
116         dlerror();      /* Clear any existing error */
117
118         func_array = g_new0(gpointer, eFUNC_MAX);
119
120         /*add functions to array */
121         for (func_index = 0; func_index < eFUNC_MAX ; func_index++) {
122                 func_array[func_index] = dlsym(func_handle, func_list[func_index]);
123                 if (func_array[func_index] == NULL) {
124                         MS_DBG_ERR("dlsym failed[%s]", func_list[func_index]);
125                         g_free(func_array);
126                         dlclose(func_handle);
127
128                         return MS_MEDIA_ERR_INTERNAL;
129                 }
130         }
131
132         return MS_MEDIA_ERR_NONE;
133 }
134
135 void ms_unload_functions(void)
136 {
137         dlclose(func_handle);
138         g_free(func_array);
139 }
140
141 int ms_connect_db(sqlite3 **handle, uid_t uid)
142 {
143         int ret = MS_MEDIA_ERR_NONE;
144
145         g_mutex_lock(&db_mutex);
146
147         ret = media_db_connect(handle, uid, false);
148
149         g_mutex_unlock(&db_mutex);
150
151         return ret;
152 }
153
154 void ms_disconnect_db(sqlite3 *handle)
155 {
156         media_db_disconnect(handle);
157 }
158
159 int ms_cleanup_db(sqlite3 *handle, uid_t uid)
160 {
161         int ret = MS_MEDIA_ERR_NONE;
162
163         ret = ((CLEANUP_DB)func_array[eCLEANUP_DB])(handle, uid); /*dlopen*/
164         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CLEANUP_DB failed [%d]", ret);
165
166         MS_DBG_INFO("Cleanup Media DB");
167
168         return ret;
169 }
170
171 static int __ms_check_item_exist(sqlite3 *handle, const char *storage_id, const char *path, bool *modified)
172 {
173         int ret = MS_MEDIA_ERR_NONE;
174
175         time_t modified_time = 0;
176         unsigned long long file_size = 0;
177         struct stat st;
178         sqlite3_stmt *sql_stmt = NULL;
179         char *sql = NULL;
180
181         *modified = true;
182         MS_DBG_RETVM_IF(!MS_STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
183
184 #ifdef _USE_TVPD_MODE
185         MS_DBG_RETVM_IF(!MS_STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
186
187         sql = sqlite3_mprintf("SELECT media_modified_time, media_size, media_type, media_timeline,folder_id FROM '%q' WHERE media_path='%q'", storage_id, path);
188 #else
189         sql = sqlite3_mprintf("SELECT media_modified_time, media_size FROM media WHERE media_path='%q'", path);
190 #endif
191
192         ret = media_db_get_result(handle, sql, &sql_stmt);
193         if (ret != MS_MEDIA_ERR_NONE) {
194                 MS_DBG_ERR("Error when media_db_get_result. err = [%d]", ret);
195                 return ret;
196         }
197
198         if (sqlite3_step(sql_stmt) != SQLITE_ROW) {
199                 MS_DBG_ERR("[No-Error] Item not found. end of row [%s]", sqlite3_errmsg(handle));
200                 ret = MS_MEDIA_ERR_DB_NO_RECORD;
201         } else {
202                 modified_time = (int)sqlite3_column_int(sql_stmt, 0);
203                 file_size = (unsigned long long)sqlite3_column_int64(sql_stmt, 1);
204 #ifdef _USE_TVPD_MODE
205                 int media_type = (int)sqlite3_column_int(sql_stmt, 2);
206                 if (media_type == MS_MEDIA_TYPE_PVR)
207                         modified_time = (int)sqlite3_column_int(sql_stmt, 3);
208 #endif
209                 memset(&st, 0, sizeof(struct stat));
210                 if (stat(path, &st) == 0) {
211                         if ((st.st_mtime != modified_time) || (st.st_size != file_size)) {
212                                 *modified = true;
213                         } else {
214                                 *modified = false;
215 #ifdef _USE_TVPD_MODE
216                                 char* folder_id_media = NULL;
217                                 char* folder_id_folder = NULL;
218
219                                 folder_id_media = (char *)sqlite3_column_text(sql_stmt, 4);
220                                 char* folder_path = NULL;
221                                 folder_path = g_path_get_dirname(path);
222                                 ret = ms_get_folder_id(handle, storage_id, folder_path, &folder_id_folder);
223                                 g_free(folder_path);
224                                 if(ret == MS_MEDIA_ERR_NONE) {
225                                         if (g_strcmp0(folder_id_media, folder_id_folder) == 0)
226                                                 *modified = false;
227                                         else
228                                                 *modified = true;
229                                 } else
230                                         *modified = true;
231
232                                 g_free(folder_id_folder);
233 #endif
234                         }
235                 }
236         }
237
238         MS_SQLITE3_FINALIZE(sql_stmt);
239
240         return ret;
241 }
242
243 int ms_validate_item(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
244 {
245         int ret = MS_MEDIA_ERR_NONE;
246         bool modified = false;
247
248         /*check exist in Media DB, If file is not exist, insert data in DB. */
249         ret = __ms_check_item_exist(handle, storage_id, path, &modified);
250         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
251                 ret = ms_insert_item_batch(handle, storage_id, path, uid);
252         } else if (ret == MS_MEDIA_ERR_NONE) {
253                 if (!modified) {
254                         /*if meta data of file exist, change valid field to "1" */
255                         ret = ((SET_ITEM_VALIDITY)func_array[eSET_VALIDITY])(storage_id, path, true, true, uid); /*dlopen*/
256                         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_ITEM_VALIDITY failed [%d]", ret);
257                 } else {
258                         /* the file has same name but it is changed, so we have to update DB */
259                         ret = ((REFRESH_ITEM)func_array[eREFRESH_ITEM])(handle, storage_id, path, uid); /*dlopen*/
260                         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "REFRESH_ITEM failed [%d]", ret);
261                 }
262         } else {
263                 MS_DBG_ERR("check_item_exist failed [%d]", ret);
264         }
265
266         return ret;
267 }
268
269 int ms_scan_validate_item(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
270 {
271         int ret = MS_MEDIA_ERR_NONE;
272         bool modified = false;
273
274         /*check exist in Media DB, If file is not exist, insert data in DB. */
275         ret = __ms_check_item_exist(handle, storage_id, path, &modified);
276         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
277                 ret = ms_scan_item_batch(handle, storage_id, path, uid);
278         } else if (ret == MS_MEDIA_ERR_NONE) {
279                 if (!modified) {
280                         /*if meta data of file exist, change valid field to "1" */
281                         ret = ((SET_ITEM_VALIDITY)func_array[eSET_VALIDITY])(storage_id, path, true, true, uid); /*dlopen*/
282                         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_ITEM_VALIDITY failed [%d]", ret);
283                 } else {
284                         /* the file has same name but it is changed, so we have to update DB */
285                         ret = ((REFRESH_ITEM)func_array[eREFRESH_ITEM])(handle, storage_id, path, uid); /*dlopen*/
286                         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "REFRESH_ITEM failed [%d]", ret);
287                 }
288         } else {
289                 MS_DBG_ERR("check_item_exist failed [%d]", ret);
290         }
291
292         return ret;
293 }
294
295 int ms_validity_change_all_items(sqlite3 *handle, const char *storage_id, bool validity , uid_t uid)
296 {
297         int ret = MS_MEDIA_ERR_NONE;
298
299 #ifdef _USE_TVPD_MODE
300         MS_DBG_RETVM_IF(!MS_STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
301
302         char *sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d;", storage_id, validity);
303 #else
304         char *sql = sqlite3_mprintf("UPDATE media SET validity=%d WHERE storage_uuid=%Q;", validity, storage_id);
305 #endif
306
307         ret = media_db_update_db_direct(sql, uid);
308         MS_SQLITE3_SAFE_FREE(sql);
309
310         return ret;
311 }
312
313 int ms_insert_item_batch(sqlite3 *handle, const char* storage_id, const char *path, uid_t uid)
314 {
315         int ret = MS_MEDIA_ERR_NONE;
316         ms_user_storage_type_e storage_type = -1;
317
318         MS_DBG_FENTER();
319
320         ret = ms_user_get_storage_type(uid, path, &storage_type);
321         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get storage type");
322
323         ret = ((INSERT_ITEM)func_array[eINSERT_BATCH])(handle, storage_id, path, storage_type, uid); /*dlopen*/
324         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "INSERT_ITEM failed [%d]", ret);
325
326         return ret;
327 }
328
329 int ms_scan_item_batch(sqlite3 *handle, const char* storage_id, const char *path, uid_t uid)
330 {
331         int ret = MS_MEDIA_ERR_NONE;
332         ms_user_storage_type_e storage_type;
333
334         MS_DBG_FENTER();
335
336         ret = ms_user_get_storage_type(uid, path, &storage_type);
337         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get storage type");
338
339         ret = ((INSERT_ITEM_SCAN)func_array[eINSERT_SCAN])(handle, storage_id, path, storage_type, uid); /*dlopen*/
340         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "INSERT_ITEM_SCAN failed [%d]", ret);
341
342         return ret;
343 }
344
345 int ms_get_extract_list(sqlite3 *handle, const char* storage_id, int storage_type, int scan_type, const char* path, int is_end, uid_t uid, void* array)
346 {
347         int ret = MS_MEDIA_ERR_NONE;
348
349         ret = ((GET_EXTRACT_LIST)func_array[eGET_EXTRACT_LIST])(handle, storage_id, storage_type, scan_type, path, is_end, uid, array); /*dlopen*/
350         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_EXTRACT_LIST failed [%d]", ret);
351
352         return ret;
353 }
354
355 int ms_update_one_extract_item(sqlite3 *handle, const char* storage_id, int storage_type, void* data)
356 {
357         int ret = MS_MEDIA_ERR_NONE;
358
359         ret = ((UPDATE_ONE_EXTRACT_ITEM)func_array[eUPDATE_ONE_EXTRACT_ITEM])(handle, storage_id, storage_type, data); /*dlopen*/
360         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "UPDATE_ONE_EXTRACT_ITEM failed [%d]", ret);
361
362         return ret;
363 }
364
365 int ms_delete_invalid_items(sqlite3 *handle, const char *storage_id, uid_t uid)
366 {
367         int ret = MS_MEDIA_ERR_NONE;
368         sqlite3_stmt *sql_stmt = NULL;
369         GPtrArray *thumb_list = NULL;
370         char *path = NULL;
371         int i = 0;
372
373         thumb_list = g_ptr_array_new_with_free_func(g_free);
374 #ifdef _USE_TVPD_MODE
375         MS_DBG_RETVM_IF(!MS_STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
376
377         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);
378 #else
379         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);
380 #endif
381         MS_DBG_SLOG("[SQL query] : %s", sql);
382
383         ret = media_db_get_result(handle, sql, &sql_stmt);
384         if (ret != MS_MEDIA_ERR_NONE) {
385                 MS_DBG_ERR("Query failed. err[%d]", ret);
386                 g_ptr_array_free(thumb_list, TRUE);
387                 return ret;
388         }
389
390         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
391                 path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
392                 g_ptr_array_add(thumb_list, path);
393         }
394
395         MS_SQLITE3_FINALIZE(sql_stmt);
396
397 #ifdef _USE_TVPD_MODE
398         sql = sqlite3_mprintf("DELETE FROM '%q' WHERE validity IN (%d, %d)", storage_id, MS_INVALID, MS_SCANNING);
399 #else
400         sql = sqlite3_mprintf("DELETE FROM media WHERE validity IN (%d, %d)", MS_INVALID, MS_SCANNING);
401 #endif
402
403         ret = media_db_update_db_direct(sql, uid);
404         MS_SQLITE3_SAFE_FREE(sql);
405         if (ret != MS_MEDIA_ERR_NONE) {
406                 g_ptr_array_free(thumb_list, TRUE);
407                 return ret;
408         }
409
410         /*Delete thumbnails*/
411         for (i = 0; i < thumb_list->len; i++) {
412                 path = g_ptr_array_index(thumb_list, i);
413
414                 if (remove(path) != 0)
415                         MS_DBG_ERR("fail to remove thumbnail file.");
416         }
417
418         g_ptr_array_free(thumb_list, TRUE);
419
420         return MS_MEDIA_ERR_NONE;
421 }
422
423 int ms_set_folder_item_validity(sqlite3 *handle, const char *storage_id, const char *path, int validity, int recursive, uid_t uid)
424 {
425         int ret = MS_MEDIA_ERR_NONE;
426         char *sql = NULL;
427
428         MS_DBG_RETVM_IF(!MS_STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
429         MS_DBG_RETVM_IF(!MS_STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
430
431 #ifdef _USE_TVPD_MODE
432         if (recursive)
433                 sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d WHERE media_path LIKE '%q/%%';", storage_id, validity, path);
434         else
435                 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);
436 #else
437         if (recursive)
438                 sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE media_path LIKE '%q/%%';", INTERNAL_STORAGE_ID, validity, path);
439         else
440                 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);
441 #endif
442
443         ret = media_db_update_db_direct(sql, uid);
444         MS_SQLITE3_SAFE_FREE(sql);
445
446         return ret;
447 }
448
449 int ms_delete_invalid_items_in_folder(sqlite3 *handle, const char* storage_id, const char*path, bool is_recursive, uid_t uid)
450 {
451         int ret = MS_MEDIA_ERR_NONE;
452
453         ret = ((DELETE_ALL_INVALID_ITEMS_IN_FOLDER)func_array[eDELETE_FOLDER])(handle, storage_id, path, is_recursive, uid); /*dlopen*/
454         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "DELETE_ALL_INVALID_ITEMS_IN_FOLDER failed [%d]", ret);
455
456         return ret;
457 }
458
459 int ms_send_dir_update_noti(const char*path, const char*folder_id, ms_noti_type_e noti_type, int pid)
460 {
461         int ret = MS_MEDIA_ERR_NONE;
462
463         ret = ((SEND_DIR_UPDATE_NOTI)func_array[eSEND_DIR_UPDATE_NOTI])(path, folder_id, (int)noti_type, pid); /*dlopen*/
464         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SEND_DIR_UPDATE_NOTI failed [%d]", ret);
465
466         return ret;
467 }
468
469 int ms_update_folder_time(sqlite3 *handle, const char *storage_id, char *folder_path, uid_t uid)
470 {
471         int ret = MS_MEDIA_ERR_NONE;
472
473         ret = ((UPDATE_FOLDER_TIME)func_array[eUPDATE_FOLDER_TIME])(handle, storage_id, folder_path, uid); /*dlopen*/
474         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "UPDATE_FOLDER_TIME failed [%d]", ret);
475
476         return ret;
477 }
478
479 int ms_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
480 {
481         int ret = MS_MEDIA_ERR_NONE;
482
483         MS_DBG_FENTER();
484
485         ret = ((GET_STORAGE_ID)func_array[eGET_STORAGE_ID])(handle, path, storage_id, uid); /*dlopen*/
486         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_STORAGE_ID failed [%d]", ret);
487
488         MS_DBG("storage_id [%s]", storage_id);
489
490         return ret;
491 }
492
493 int ms_set_storage_scan_status(sqlite3 *handle, char *storage_id, media_scan_status_e scan_status, uid_t uid)
494 {
495         int ret = MS_MEDIA_ERR_NONE;
496         int status = scan_status;
497
498         MS_DBG_FENTER();
499
500         ret = ((SET_STORAGE_SCAN_STATUS)func_array[eSET_STORAGE_SCAN_STATUS])(handle, storage_id, status, uid); /*dlopen*/
501         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_STORAGE_SCAN_STATUS failed [%d]", ret);
502
503         MS_DBG("storage_id [%s], scan_status [%d]", storage_id, scan_status);
504
505         return ret;
506 }
507
508 int ms_insert_folder(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
509 {
510         int ret = MS_MEDIA_ERR_NONE;
511         ms_user_storage_type_e storage_type;
512
513         ret = ms_user_get_storage_type(uid, path, &storage_type);
514         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get storage type");
515
516         ret = ((INSERT_FOLDER)func_array[eINSERT_FOLDER])(handle, storage_id, path, storage_type, uid); /*dlopen*/
517         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "INSERT_FOLDER failed [%d]", ret);
518
519         return ret;
520 }
521
522 int ms_delete_invalid_folder(const char *storage_id, uid_t uid)
523 {
524         int ret = MS_MEDIA_ERR_NONE;
525         char *sql = NULL;
526
527 #ifdef _USE_TVPD_MODE
528         MS_DBG_RETVM_IF(!MS_STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
529
530         sql = sqlite3_mprintf("DELETE FROM folder WHERE storage_uuid='%q' AND validity IN (%d, %d)", storage_id, MS_INVALID, MS_SCANNING);
531 #else
532         sql = sqlite3_mprintf("DELETE FROM folder WHERE validity IN (%d, %d)", MS_INVALID, MS_SCANNING);
533 #endif
534         ret = media_db_update_db_direct(sql, uid);
535
536         MS_SQLITE3_SAFE_FREE(sql);
537
538         return ret;
539 }
540
541 int ms_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *path, int validity, bool is_recursive, uid_t uid)
542 {
543         int ret = MS_MEDIA_ERR_NONE;
544
545         ret = ((SET_FOLDER_VALIDITY)func_array[eSET_FOLDER_VALIDITY])(handle, storage_id, path, validity, is_recursive, uid); /*dlopen*/
546         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_FOLDER_VALIDITY failed [%d]", ret);
547
548         return ret;
549 }
550
551 int ms_check_db_upgrade(sqlite3 *handle, uid_t uid)
552 {
553         int ret = MS_MEDIA_ERR_NONE;
554
555         MS_DBG_FENTER();
556
557         ret = ((CHECK_DB)func_array[eCHECK_DB])(handle, uid);
558         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CHECK_DB failed [%d]", ret);
559
560         MS_DBG_FLEAVE();
561
562         return ret;
563 }
564
565 int ms_genarate_uuid(char **uuid)
566 {
567         int ret = MS_MEDIA_ERR_NONE;
568
569         ret = ((GET_UUID)func_array[eGET_UUID])(uuid); /*dlopen*/
570         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_UUID failed [%d]", ret);
571
572         return ret;
573 }
574
575 int ms_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid)
576 {
577         int ret = MS_MEDIA_ERR_NONE;
578
579         MS_DBG_FENTER();
580
581         ret = ((CHECK_STORAGE)func_array[eCHECK_STORAGE])(handle, storage_id, storage_path, validity, uid);
582         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CHECK_STORAGE failed [%d]", ret);
583
584         MS_DBG_FLEAVE();
585
586         return ret;
587 }
588
589 int ms_insert_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
590 {
591         int ret = MS_MEDIA_ERR_NONE;
592         ms_user_storage_type_e storage_type;
593
594         MS_DBG_FENTER();
595
596         ret = ms_user_get_storage_type(uid, storage_path, &storage_type);
597         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get storage type");
598
599         ret = ((INSERT_STORAGE)func_array[eINSERT_STORAGE])(handle, storage_id, storage_type, storage_path, uid);
600         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "INSERT_STORAGE failed [%d]", ret);
601
602         MS_DBG_FLEAVE();
603
604         return ret;
605 }
606
607 int ms_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
608 {
609         int ret = MS_MEDIA_ERR_NONE;
610
611         MS_DBG_FENTER();
612
613         ret = ((UPDATE_STORAGE)func_array[eUPDATE_STORAGE])(handle, storage_id, storage_path, uid); /*dlopen*/
614         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "UPDATE_STORAGE failed [%d]", ret);
615
616         MS_DBG_FLEAVE();
617
618         return ret;
619 }
620
621 int ms_set_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid)
622 {
623         int ret = MS_MEDIA_ERR_NONE;
624
625         ret = ((SET_STORAGE_VALIDITY)func_array[eSET_STORAGE_VALIDITY])(handle, storage_id, validity, uid); /*dlopen*/
626         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_STORAGE_VALIDITY failed [%d]", ret);
627
628         return ret;
629 }
630
631 int ms_set_all_storage_validity(sqlite3 *handle, int validity, uid_t uid)
632 {
633         int ret = MS_MEDIA_ERR_NONE;
634
635         ret = ((SET_ALL_STORAGE_VALIDITY)func_array[eSET_ALL_STORAGE_VALIDITY])(handle, validity, uid); /*dlopen*/
636         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_ALL_STORAGE_VALIDITY failed [%d]", ret);
637
638         return ret;
639 }
640
641 int ms_update_meta_batch(sqlite3 *handle, uid_t uid)
642 {
643         int ret = MS_MEDIA_ERR_NONE;
644
645         MS_DBG_FENTER();
646
647         ret = ((UPDATE_ITEM_META)func_array[eUPDATE_ITEM_META])(handle, uid); /*dlopen*/
648         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "UPDATE_ITEM_META failed [%d]", ret);
649
650         return ret;
651 }
652
653 int ms_delete_invalid_folder_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path, uid_t uid)
654 {
655         int ret = MS_MEDIA_ERR_NONE;
656
657         ret = ((DELETE_INVALID_FOLDER_BY_PATH)func_array[eDELETE_INVALID_FOLDER_BY_PATH])(handle, storage_id, folder_path, uid); /*dlopen*/
658         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "DELETE_INVALID_FOLDER_BY_PATH failed [%d]", ret);
659
660         return ret;
661 }
662
663 int ms_check_folder_exist(sqlite3 *handle, const char *storage_id, const char *folder_path)
664 {
665         int ret = MS_MEDIA_ERR_NONE;
666
667         ret = ((CHECK_FOLDER_EXIST)func_array[eCHECK_FOLDER_EXIST])(handle, storage_id, folder_path); /*dlopen*/
668         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CHECK_FOLDER_EXIST failed [%d]", ret);
669
670         return ret;
671 }
672
673 int ms_get_folder_id(sqlite3 *handle, const char *storage_id, const char *path, char **folder_id)
674 {
675         int ret = MS_MEDIA_ERR_NONE;
676         sqlite3_stmt *sql_stmt = NULL;
677         char *sql = NULL;
678
679         MS_DBG_RETVM_IF(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
680 #ifdef _USE_TVPD_MODE
681         MS_DBG_RETVM_IF(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
682
683         sql = sqlite3_mprintf("SELECT folder_id FROM folder WHERE (storage_uuid='%q' AND folder_path='%q')", storage_id, path);
684 #else
685         sql = sqlite3_mprintf("SELECT folder_id FROM folder WHERE folder_path=%Q", path);
686 #endif
687         ret = media_db_get_result(handle, sql, &sql_stmt);
688         MS_DBG_RETV_IF(ret != MS_MEDIA_ERR_NONE, ret);
689
690         if (sqlite3_step(sql_stmt) == SQLITE_ROW) {
691                 *folder_id = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
692                 MS_DBG("folder_id [%s]", *folder_id);
693         } else {
694                 *folder_id = NULL;
695                 ret = MS_MEDIA_ERR_DB_NO_RECORD;
696         }
697
698         MS_SQLITE3_FINALIZE(sql_stmt);
699
700         return ret;
701 }
702
703 /* FOR BULK COMMIT */
704 void ms_register_start(bool noti_status, int pid)
705 {
706         int ret = MS_MEDIA_ERR_NONE;
707
708         ret = ((INSERT_ITEM_BEGIN)func_array[eINSERT_BEGIN])(noti_status, pid);/*dlopen*/
709         if (ret != MS_MEDIA_ERR_NONE)
710                 MS_DBG_ERR("INSERT_ITEM_BEGIN failed [%d]", ret);
711 }
712
713 void ms_register_end(uid_t uid)
714 {
715         int ret = MS_MEDIA_ERR_NONE;
716
717         ret = ((INSERT_ITEM_END)func_array[eINSERT_END])(uid);/*dlopen*/
718         if (ret != MS_MEDIA_ERR_NONE)
719                 MS_DBG_ERR("INSERT_ITEM_END failed [%d]", ret);
720 }
721
722 int ms_get_folder_scan_status(sqlite3 *handle, const char *storage_id, const char *path, int *scan_status)
723 {
724         int ret = MS_MEDIA_ERR_NONE;
725         int status = 0;
726
727         ret = ((GET_FOLDER_SCAN_STATUS)func_array[eGET_FOLDER_SCAN_STATUS])(handle, storage_id, path, &status); /*dlopen*/
728         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_FOLDER_SCAN_STATUS failed [%d]", ret);
729         *scan_status = status;
730
731         MS_DBG_SLOG("OK path = [%s], scan_status = [%d]", path, *scan_status);
732
733         return ret;
734 }
735
736 int ms_set_folder_scan_status(sqlite3 *handle, const char *storage_id, const char *path, int scan_status, uid_t uid)
737 {
738         int ret = MS_MEDIA_ERR_NONE;
739         int status = scan_status;
740
741         ret = ((SET_FOLDER_SCAN_STATUS)func_array[eSET_FOLDER_SCAN_STATUS])(handle, storage_id, path, status, uid); /*dlopen*/
742         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_FOLDER_SCAN_STATUS failed [%d]", ret);
743
744         MS_DBG_SLOG("OK path = [%s], scan_status = [%d]", path, scan_status);
745
746         return ret;
747 }
748
749 int ms_check_folder_modified(sqlite3 *handle, const char *path, const char *storage_id, bool *modified)
750 {
751         int ret = MS_MEDIA_ERR_NONE;
752         MS_DBG("path = [%s], storage_id = [%s]", path, storage_id);
753
754         ret = ((CHECK_FOLDER_MODIFIED)func_array[eCHECK_FOLDER_MODIFIED])(handle, path, storage_id, modified); /*dlopen*/
755         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CHECK_FOLDER_MODIFIED failed [%d]", ret);
756
757         return ret;
758 }
759
760 int ms_get_null_scan_folder_list(sqlite3 *handle, const char *stroage_id, const char *path, GArray **dir_array)
761 {
762         int ret = MS_MEDIA_ERR_NONE;
763         char **folder_list = NULL;
764         int count = 0;
765         int i = 0;
766
767         ret = ((GET_NULL_SCAN_FOLDER_LIST)func_array[eGET_NULL_SCAN_FOLDER_LIST])(handle, stroage_id, path, &folder_list, &count); /*dlopen*/
768         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_NULL_SCAN_FOLDER_LIST failed [%d]", ret);
769
770         *dir_array = g_array_sized_new(FALSE, FALSE, sizeof(char*), count);
771
772         for (i = 0; i < count; i++)
773                 g_array_append_val(*dir_array, folder_list[i]);
774
775
776         MS_SAFE_FREE(folder_list);
777
778         return ret;
779 }
780
781 int ms_change_validity_item_batch(sqlite3 *handle, const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
782 {
783         int ret = MS_MEDIA_ERR_NONE;
784
785         ret = ((CHANGE_VALIDITY_ITEM_BATCH)func_array[eCHANGE_VALIDITY_ITEM_BATCH])(handle, storage_id, path, des_validity, src_validity, uid); /*dlopen*/
786         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CHANGE_VALIDITY_ITEM_BATCH failed [%d]", ret);
787
788         return ret;
789 }
790
791 int ms_get_media_type(const char *path, int *media_type)
792 {
793         int ret = MS_MEDIA_ERR_NONE;
794
795         ret = ((GET_MEDIA_TYPE)func_array[eGET_MEDIA_TYPE])(path, media_type); /*dlopen*/
796         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_MEDIA_TYPE failed [%d]", ret);
797
798         return ret;
799 }
800
801 bool ms_check_support_media_type(const char *path)
802 {
803         int ret = SYSTEM_INFO_ERROR_NONE;
804         int media_type = -1;
805         bool is_supported = false;
806
807         MS_DBG_RETVM_IF(!MS_STRING_VALID(path), false, "path is empty");
808
809         if (scan_other_type == -1) {
810                 ret = system_info_get_platform_bool("http://tizen.org/feature/content.scanning.others", &is_supported);
811                 if (ret != SYSTEM_INFO_ERROR_NONE) {
812                         MS_DBG_ERR("SYSTEM_INFO_ERROR: content.scanning.others [%d]", ret);
813                         return false;
814                 }
815
816                 scan_other_type = is_supported;
817         }
818
819         /* If not, check media type */
820         if (!scan_other_type) {
821                 ret = ms_get_media_type(path, &media_type);
822                 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, false, "Failed to get media type");
823
824                 if (media_type == MS_MEDIA_TYPE_OTHER)
825                         return false;
826         }
827
828         return true;
829 }
830
831 void ms_batch_commit_enable(bool noti_enable, int pid)
832 {
833         /*call for bundle commit*/
834         ms_register_start(noti_enable, pid);
835 }
836
837 void ms_batch_commit_disable(uid_t uid)
838 {
839         ms_register_end(uid);
840 }