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