2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <media_info_private.h>
18 #include <media_util_private.h>
20 #include <tzplatform_config.h>
22 static sqlite3 *db_handle = NULL;
23 static int ref_count = 0;
24 static GMutex db_mutex;
25 static uid_t content_g_uid = 0;
27 sqlite3 * _content_get_db_handle(void)
32 uid_t _content_get_uid(void)
34 if (content_g_uid == 0)
35 return tzplatform_getuid(TZ_USER_NAME);
40 int _content_query_prepare(const char *select_query, const char *condition_query, const char *option_query, sqlite3_stmt **stmt)
42 int ret = MEDIA_CONTENT_ERROR_NONE;
45 content_retip_if_fail(select_query);
47 /* If 'option_query' is NULL, 'condition_query' is also NULL. */
49 if (STRING_VALID(condition_query))
50 query = sqlite3_mprintf("%s AND %s %s", select_query, condition_query, option_query);
52 query = sqlite3_mprintf("%s %s", select_query, option_query);
55 query = sqlite3_mprintf("%s", select_query);
58 ret = _content_get_result(query, stmt);
59 SQLITE3_SAFE_FREE(query);
64 int _content_get_result(char *query, sqlite3_stmt **stmt)
66 int err = MEDIA_CONTENT_ERROR_NONE;
68 content_retip_if_fail(_content_get_db_handle());
69 content_retip_if_fail(query);
71 content_sec_debug("Query[%s]", query);
73 err = sqlite3_prepare_v2(_content_get_db_handle(), query, strlen(query), stmt, NULL);
74 if (err != SQLITE_OK) {
75 content_error("Failed to sqlite3_prepare_v2[%s]", sqlite3_errmsg(_content_get_db_handle()));
76 if (err == SQLITE_BUSY)
77 return MEDIA_CONTENT_ERROR_DB_BUSY;
78 else if (err == SQLITE_PERM)
79 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
81 return MEDIA_CONTENT_ERROR_DB_FAILED;
84 return MEDIA_CONTENT_ERROR_NONE;
87 int _content_error_capi(int internal_error)
89 if (internal_error == MS_MEDIA_ERR_NONE)
90 return MEDIA_CONTENT_ERROR_NONE;
92 content_error("MS Error[%d]", internal_error);
94 switch (internal_error) {
95 case MS_MEDIA_ERR_INVALID_PARAMETER:
96 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
97 case MS_MEDIA_ERR_OUT_OF_MEMORY:
98 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
99 case MS_MEDIA_ERR_DB_BUSY_FAIL:
100 return MEDIA_CONTENT_ERROR_DB_BUSY;
101 case MS_MEDIA_ERR_DB_CONSTRAINT_FAIL:
102 case MS_MEDIA_ERR_DB_NO_RECORD:
103 case MS_MEDIA_ERR_DB_CORRUPT:
104 case MS_MEDIA_ERR_DB_FULL_FAIL:
105 case MS_MEDIA_ERR_DB_RESET:
106 case MS_MEDIA_ERR_DB_INTERNAL:
107 return MEDIA_CONTENT_ERROR_DB_FAILED;
108 case MS_MEDIA_ERR_IPC:
109 return MEDIA_CONTENT_ERROR_NETWORK;
110 case MS_MEDIA_ERR_PERMISSION_DENIED:
111 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
112 case MS_MEDIA_ERR_THUMB_UNSUPPORTED:
113 return MEDIA_CONTENT_ERROR_UNSUPPORTED_CONTENT;
115 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
119 int _content_query_sql(char *query_str)
121 int ret = MEDIA_CONTENT_ERROR_NONE;
123 /*DB will be updated by Media Server.*/
124 ret = media_db_request_update_db(query_str, _content_get_uid());
126 return _content_error_capi(ret);
129 int media_content_connect(void)
131 int ret = MEDIA_CONTENT_ERROR_NONE;
133 g_mutex_lock(&db_mutex);
134 content_info("ref count : %d", ref_count);
136 if (ref_count == 0) {
137 if (db_handle == NULL) {
138 ret = media_db_connect(&db_handle, _content_get_uid(), false);
139 ret = _content_error_capi(ret);
140 if (ret == MEDIA_CONTENT_ERROR_NONE)
143 content_error("Wrong DB Connection status");
144 ret = MEDIA_CONTENT_ERROR_DB_FAILED;
147 if (db_handle != NULL) {
150 content_error("Wrong DB Handle status");
151 ret = MEDIA_CONTENT_ERROR_DB_FAILED;
155 content_info("ref count changed to: %d", ref_count);
156 g_mutex_unlock(&db_mutex);
161 int media_content_connect_with_uid(uid_t uid)
163 content_sec_debug("media_content_connect_with_uid [%d]", uid);
166 return media_content_connect();
169 int media_content_disconnect(void)
171 g_mutex_lock(&db_mutex);
172 content_debug("ref count : %d", ref_count);
174 if (db_handle && ref_count > 0) {
175 if (--ref_count == 0) {
176 media_db_disconnect(db_handle);
180 content_error("Database is not connected");
181 g_mutex_unlock(&db_mutex);
182 return MEDIA_CONTENT_ERROR_DB_FAILED;
185 g_mutex_unlock(&db_mutex);
187 content_info("ref count changed to: %d", ref_count);
189 return MEDIA_CONTENT_ERROR_NONE;
192 int media_content_scan_file(const char *path)
194 int ret = MEDIA_CONTENT_ERROR_NONE;
195 char *folder_path = NULL;
196 int check_file = MEDIA_CONTENT_ERROR_NONE;
197 char storage_id[MEDIA_CONTENT_UUID_SIZE+1] = {0,};
199 content_retip_if_fail(STRING_VALID(path));
201 content_sec_debug("Path : %s", path);
203 content_retvm_if(_media_util_is_ignorable_file(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid path");
205 ret = media_svc_get_storage_id(_content_get_db_handle(), path, storage_id, _content_get_uid());
206 if (ret != MS_MEDIA_ERR_NONE) {
207 content_error("media_svc_get_storage_id failed : %d", ret);
208 return _content_error_capi(ret);
211 check_file = _media_util_check_file_exist(path);
212 if (check_file == MEDIA_CONTENT_ERROR_NONE) {
213 /* This means this path has to be inserted or refreshed */
214 folder_path = g_path_get_dirname(path);
216 if (_media_util_is_ignorable_dir(folder_path)) {
218 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
224 content_retvm_if(!_media_util_check_support_media_type(path), MEDIA_CONTENT_ERROR_NOT_SUPPORTED, "Unsupported media type");
226 ms_user_storage_type_e storage_type;
228 ret = ms_user_get_storage_type(_content_get_uid(), path, &storage_type);
229 if (ret != MS_MEDIA_ERR_NONE) {
230 content_sec_error("ms_user_get_storage_type failed : %d (%s)", ret, path);
231 return _content_error_capi(ret);
233 ret = media_svc_check_item_exist_by_path(_content_get_db_handle(), storage_id, path);
234 if (ret == MS_MEDIA_ERR_NONE) {
236 ret = media_svc_refresh_item(_content_get_db_handle(), false, storage_id, storage_type, path, _content_get_uid());
237 if (ret != MS_MEDIA_ERR_NONE) {
238 content_error("media_svc_refresh_item failed : %d", ret);
239 return _content_error_capi(ret);
242 } else if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
244 ret = media_svc_insert_item_immediately(_content_get_db_handle(), storage_id, storage_type, path, _content_get_uid());
245 if (ret != MS_MEDIA_ERR_NONE) {
246 if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
247 content_sec_error("This item is already inserted. This may be normal operation because other process already did this (%s)", path);
248 ret = MEDIA_CONTENT_ERROR_NONE;
250 content_sec_error("media_svc_insert_item_immediately failed : %d (%s)", ret, path);
253 return _content_error_capi(ret);
256 content_error("media_svc_check_item_exist_by_path failed : %d", ret);
257 return _content_error_capi(ret);
259 } else if (check_file == MEDIA_CONTENT_ERROR_PERMISSION_DENIED) {
260 content_error("You have no permission for this file %d", ret);
261 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
263 /* This means this path has to be deleted */
264 content_debug("This path doesn't exists in file system... So now start to delete it from DB");
265 ret = media_svc_delete_item_by_path(_content_get_db_handle(), storage_id, path, _content_get_uid());
266 if (ret != MS_MEDIA_ERR_NONE) {
267 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
268 content_error("Does not exist in media DB also... So, this is an invalid parameter");
269 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
272 content_error("media_svc_delete_item_by_path failed : %d", ret);
273 return _content_error_capi(ret);
277 return _content_error_capi(ret);
280 void _media_content_scan_cb(media_request_result_s* result, void *user_data)
283 media_content_scan_cb_data *cb_data = user_data;
285 err = _content_error_capi(result->result);
286 #ifdef _USE_TVPD_MODE
287 if (result->request_type != MEDIA_REQUEST_SCAN_COMPLETE &&
288 result->request_type != MEDIA_REQUEST_SCAN_PARTIAL &&
289 result->request_type != MEDIA_RECURSIVE_START) {
290 if (cb_data && cb_data->callback) {
291 content_debug("begin:User callback is being called now, result=%d", err);
292 cb_data->callback(err, cb_data->user_data);
293 content_debug("end:User callback is being called now, result=%d", err);
299 if (cb_data && cb_data->callback) {
300 content_debug("User callback is being called now");
301 cb_data->callback(err, cb_data->user_data);
310 #ifdef _USE_TVPD_MODE
311 void _media_content_scan_cb_v2(media_request_result_s* result, void *user_data)
314 media_content_scan_cb_data_v2 *cb_data = user_data;
315 media_content_complete_phase_e complete_phase = -1;
317 content_debug("cb_data is NULL");
318 err = _content_error_capi(result->result);
319 content_debug("result is %d", err);
321 if (result->request_type == MEDIA_REQUEST_SCAN_PARTIAL)
322 complete_phase = MEDIA_CONTENT_SCAN_PARTIAL_COMPLETE;
323 else if (result->request_type == MEDIA_REQUEST_SCAN_COMPLETE)
324 complete_phase = MEDIA_CONTENT_SCAN_COMPLETE;
325 else if (result->request_type == MEDIA_REQUEST_EXTRACT_COMPLETE)
326 complete_phase = MEDIA_CONTENT_EXTRACT_COMPLETE;
327 else if (result->request_type == MEDIA_RECURSIVE_START)
328 complete_phase = MEDIA_CONTENT_RECURSIVE_START;
330 if (cb_data && cb_data->callback)
331 cb_data->callback(err, complete_phase, cb_data->user_data);
333 content_debug("run error");
335 if ((result->request_type != MEDIA_REQUEST_SCAN_COMPLETE) &&
336 (result->request_type != MEDIA_REQUEST_SCAN_PARTIAL) &&
337 (result->request_type != MEDIA_RECURSIVE_START))
344 int media_content_scan_folder(const char *path, bool is_recursive, media_scan_completed_cb callback, void *user_data)
346 int ret = MEDIA_CONTENT_ERROR_NONE;
347 char storage_id[MEDIA_CONTENT_UUID_SIZE+1] = {0, };
348 ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL;
350 content_retip_if_fail(STRING_VALID(path));
351 content_retip_if_fail(callback);
353 ret = _media_content_check_dir(path);
354 content_retvm_if(ret == MEDIA_CONTENT_ERROR_PERMISSION_DENIED, ret, "Permission Denied");
356 if (ret == MEDIA_CONTENT_ERROR_NONE) {
357 /* If directory exist check that's ignore directory or not*/
358 content_retvm_if(_media_util_is_ignorable_dir(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid folder path");
360 /* This means this folder has to be deleted */
361 /* Or, it is real invalid path.. check storage type */
362 ret = ms_user_get_storage_type(_content_get_uid(), path, &storage_type);
363 if (ret != MS_MEDIA_ERR_NONE) {
364 content_sec_error("ms_user_get_storage_type failed : %d (%s)", ret, path);
365 return _content_error_capi(ret);
368 content_debug("This path doesn't exists in file system... So will be deleted it from DB");
371 ret = media_svc_get_storage_id(_content_get_db_handle(), path, storage_id, _content_get_uid());
372 if (ret != MS_MEDIA_ERR_NONE) {
373 content_error("media_svc_get_storage_id failed : %d", ret);
374 return _content_error_capi(ret);
377 media_content_scan_cb_data *cb_data = NULL;
378 cb_data = g_new0(media_content_scan_cb_data, 1);
380 cb_data->callback = callback;
381 cb_data->user_data = user_data;
383 ret = media_directory_scanning_async(path, storage_id, is_recursive, _media_content_scan_cb, cb_data, _content_get_uid());
384 if (ret != MS_MEDIA_ERR_NONE) {
385 content_error("media_directory_scanning_async failed : %d", ret);
389 return _content_error_capi(ret);
392 #ifdef _USE_TVPD_MODE
393 bool _is_mounted(const char *dir_path)
396 char mount_path[MAX_PATH_LEN] = {0,};
397 char root_path[MAX_PATH_LEN] = {0,};
399 const char *table = "/etc/mtab";
402 strcpy(root_path, tzplatform_getenv(TZ_SYS_STORAGE));
403 strcat(root_path, "/USBDrive");
405 if (!g_str_has_prefix(dir_path, root_path))
408 char *p = strstr(dir_path + strlen(root_path), "/");
409 if(p && p - dir_path < MAX_PATH_LEN)
410 strncpy(mount_path, dir_path, p - dir_path);
412 strcpy(mount_path, dir_path);
414 fp = setmntent(table, "r");
416 content_error("open /etc/mtab failed");
419 while ((mnt = getmntent(fp))) {
420 if (!strncmp(mnt->mnt_dir, mount_path, strlen(mount_path))) {
427 content_info("dir_path[%s] mount_path[%s] ret[%d]", dir_path, mount_path, ret);
431 int media_content_scan_folder_v2(const char *path, bool is_recursive, media_scan_completed_cb_v2 callback, void *user_data)
433 int ret = MEDIA_CONTENT_ERROR_NONE;
434 char storage_id[MEDIA_CONTENT_UUID_SIZE+1] = {0, };
436 content_retip_if_fail(STRING_VALID(path));
437 content_retip_if_fail(callback);
439 content_retvm_if(!_is_mounted(path), MEDIA_CONTENT_ERROR_PERMISSION_DENIED, "path is not mounted");
440 content_retvm_if(_media_util_is_ignorable_dir(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid folder path");
442 ret = _media_content_check_dir(path);
443 content_retvm_if(ret == MEDIA_CONTENT_ERROR_PERMISSION_DENIED, ret, "Permission Denied");
444 content_retvm_if(ret == MEDIA_CONTENT_ERROR_INVALID_PARAMETER, ret, "invalid path[%s]", path);
446 media_content_scan_cb_data_v2* cb_data = NULL;
447 cb_data = g_new0(media_content_scan_cb_data_v2, 1);
449 cb_data->callback = callback;
450 cb_data->user_data = user_data;
452 ret = media_svc_get_storage_id(_content_get_db_handle(), path, storage_id, _content_get_uid());
453 /*FIX ME. need to check ret value?*/
455 ret = media_directory_scanning_async(path, storage_id, is_recursive, _media_content_scan_cb_v2, cb_data, _content_get_uid());
456 if (ret != MS_MEDIA_ERR_NONE)
457 content_error("media_directory_scanning_async failed : %d", ret);
459 return _content_error_capi(ret);
463 int media_content_cancel_scan_folder(const char *path)
465 int ret = MEDIA_CONTENT_ERROR_NONE;
467 content_retip_if_fail(STRING_VALID(path));
469 ret = media_directory_scanning_cancel(path, _content_get_uid());
470 if (ret != MS_MEDIA_ERR_NONE)
471 content_error("media_directory_scanning_async failed : %d", ret);
473 return _content_error_capi(ret);
476 void _media_content_db_update_noti_cb(
478 media_item_type_e item,
479 media_item_update_type_e update_type,
482 media_type_e content_type,
486 media_noti_cb_s *_noti_info = (media_noti_cb_s *)user_data;
488 if (_noti_info != NULL && _noti_info->update_noti_cb)
489 _noti_info->update_noti_cb(
490 MEDIA_CONTENT_ERROR_NONE,
498 _noti_info->user_data);
501 int media_content_add_db_updated_cb(media_content_db_update_cb callback, void *user_data, media_content_noti_h *noti_handle)
503 int ret = MEDIA_CONTENT_ERROR_NONE;
504 media_noti_cb_s *noti_info = NULL;
506 content_retip_if_fail(callback);
507 content_retip_if_fail(noti_handle);
509 noti_info = g_new0(media_noti_cb_s, 1);
511 noti_info->update_noti_cb = callback;
512 noti_info->user_data = user_data;
514 ret = media_db_update_subscribe((MediaNotiHandle*)noti_handle, _media_content_db_update_noti_cb, (void *)noti_info);
515 if (ret != MS_MEDIA_ERR_NONE)
518 return _content_error_capi(ret);
521 int media_content_remove_db_updated_cb(media_content_noti_h noti_handle)
523 int ret = MEDIA_CONTENT_ERROR_NONE;
525 ret = media_db_update_unsubscribe((MediaNotiHandle)noti_handle);
527 return _content_error_capi(ret);
529 #ifdef _USE_TVPD_MODE
530 GMutex* _content_get_db_mutex(void)